~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/core/Strategy/ProtocolListener.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007  Camptocamp
 
3
 *
 
4
 * This file is part of MapFish Client
 
5
 *
 
6
 * MapFish Client is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * MapFish Client is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
/*
 
21
 * @requires OpenLayers/Strategy.js
 
22
 * @requires core/Strategy.js
 
23
 */
 
24
 
 
25
/**
 
26
 * Class: mapfish.Strategy.ProtocolListener
 
27
 * A strategy that listens to "crudfinished" and "clear" events triggered
 
28
 * by a <mapfish.Protocol.TriggerEventDecorator> protocol, upon receiving
 
29
 * a "crudfinished" event and if the request is of type "read", the
 
30
 * strategy adds the received features to the layer, upon receiving a
 
31
 * "clear" event, the strategy destroys the features in the layer. A
 
32
 * <mapfish.Protocol.TriggerEventDecorator> protocol must be
 
33
 * configured in the layer for this strategy to work as expected.
 
34
 *
 
35
 * Example usage:
 
36
 * (start code)
 
37
 * var layer = new OpenLayers.Layer.Vector(
 
38
 *     "some layer name", {
 
39
 *         protocol: new mapfish.Protocol.TriggerEventDecorator(someProtocol),
 
40
 *         strategies: [new mapfish.Strategy.ProtocolListener({append: true})]
 
41
 *     }
 
42
 * );
 
43
 * (end)
 
44
 *
 
45
 * Inherits from:
 
46
 *  - <OpenLayers.Strategy>
 
47
 */
 
48
mapfish.Strategy.ProtocolListener = OpenLayers.Class(OpenLayers.Strategy, {
 
49
 
 
50
    /**
 
51
     * APIProperty: append
 
52
     * {Boolean} If false, existing layer features are destroyed
 
53
     *     before adding newly read features. Defaults to false.
 
54
     */
 
55
    append: false,
 
56
 
 
57
    /**
 
58
     * APIProperty: recenter
 
59
     * {Boolean} If true, map is recentered to features extent.
 
60
     *     Defaults to false.
 
61
     */
 
62
    recenter: false,
 
63
 
 
64
    /**
 
65
     * Constructor: mapfish.Strategy.ProtocolListener
 
66
     * Create a new ProtocolListener strategy.
 
67
     *
 
68
     * Parameters:
 
69
     * options - {Object} Optional object whose properties will be set on the
 
70
     *     instance.
 
71
     */
 
72
    initialize: function(options) {
 
73
        OpenLayers.Strategy.prototype.initialize.apply(this, [options]);
 
74
    },
 
75
 
 
76
    /**
 
77
     * Method: activate
 
78
     * Set up strategy with regard to adding features to the layer when
 
79
     * receiving crudfinished events and destroying the layer features
 
80
     * when receiving clear events.
 
81
     *
 
82
     * Returns:
 
83
     * {Boolean} The strategy was successfully activated.
 
84
     */
 
85
    activate: function() {
 
86
        if (this.layer.protocol.CLASS_NAME != "mapfish.Protocol.TriggerEventDecorator") {
 
87
            OpenLayers.Console.error([
 
88
                "This strategy is to be used with a layer whose protocol ",
 
89
                "is an instance of mapfish.Protocol.TriggerEventDecorator"].join('')
 
90
            );
 
91
            return false;
 
92
        }
 
93
        var activated = OpenLayers.Strategy.prototype.activate.call(this);
 
94
        if (activated) {
 
95
            this.layer.protocol.events.on({
 
96
                "crudfinished": this.onCrudfinished,
 
97
                "clear": this.onClear,
 
98
                scope: this
 
99
            });
 
100
        }
 
101
        return activated;
 
102
    },
 
103
 
 
104
    /**
 
105
     * Method: deactivate
 
106
     * Tear down strategy.
 
107
     *
 
108
     * Returns:
 
109
     * {Boolean} The strategy was successfully deactivated.
 
110
     */
 
111
    deactivate: function() {
 
112
        var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
 
113
        if (deactivated) {
 
114
            this.layer.protocol.events.un({
 
115
                "crudfinished": this.onCrudfinished,
 
116
                "clear": this.onClear,
 
117
                scope: this
 
118
            });
 
119
        }
 
120
        return deactivated;
 
121
    },
 
122
 
 
123
    /**
 
124
     * Method: onCrudfinished
 
125
     * Callback function called on protocol crudfinished event.
 
126
     *
 
127
     * Parameters:
 
128
     * options - {<OpenLayers.Response>} Protocol response
 
129
     */
 
130
    onCrudfinished: function(response) {
 
131
        if (response.requestType == "read") {
 
132
            this.addFeatures(response.features);
 
133
        }
 
134
    },
 
135
 
 
136
    /**
 
137
     * Method: addFeatures
 
138
     * Adds the read features to the layer
 
139
     *
 
140
     * Parameters:
 
141
     * options - {<OpenLayers.Response>} Protocol response
 
142
     */
 
143
    addFeatures: function(features) {
 
144
        if (!this.append) {
 
145
            this.layer.destroyFeatures();
 
146
        }
 
147
        if (features && features.length > 0) {
 
148
            this.layer.addFeatures(features);
 
149
            if (this.recenter) {
 
150
                this.layer.map.zoomToExtent(this.layer.getDataExtent());
 
151
            }
 
152
        }
 
153
    },
 
154
 
 
155
    /**
 
156
     * Method: onClear
 
157
     * Callback function called on protocol clear event.
 
158
     */
 
159
    onClear: function() {
 
160
        this.layer.destroyFeatures();
 
161
    },
 
162
 
 
163
    CLASS_NAME: "mapfish.Strategy.ProtocolListener"
 
164
});
 
 
b'\\ No newline at end of file'