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

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/core/Protocol/TriggerEventDecorator.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/Util.js
 
22
 * @requires OpenLayers/Protocol.js
 
23
 */
 
24
 
 
25
/**
 
26
 * Class: mapfish.Protocol.TriggerEventDecorator
 
27
 * Decorator class responsible to trigger events when requests are
 
28
 * sent and received.
 
29
 *
 
30
 * Inherits from:
 
31
 * - <OpenLayers.Protocol>
 
32
 */
 
33
 
 
34
mapfish.Protocol.TriggerEventDecorator = OpenLayers.Class(OpenLayers.Protocol, {
 
35
 
 
36
    /**
 
37
     * APIProperty: protocol
 
38
     * {<OpenLayers.Protocol>} The decorated protocol.
 
39
     */
 
40
    protocol: null,
 
41
 
 
42
    /**
 
43
     * Property: events
 
44
     * {<OpenLayers.Events>}
 
45
     */
 
46
    events: null,
 
47
 
 
48
    /**
 
49
     * Constant: EVENT_TYPES
 
50
     * {Array(String)} Supported event types. Register a listener
 
51
     *     for a particular event with the following syntax:
 
52
     *
 
53
     * (start code)
 
54
     * protocol.events.register(type, obj, listener);
 
55
     * (end)
 
56
     *
 
57
     * Listeners will be called with a reference to an event object.
 
58
     *
 
59
     * Supported events:
 
60
     * - *crudtriggered* Triggered when either "create", "read", "update"
 
61
     *     or "delete" is called, listeners will receive an object with
 
62
     *     a type property referencing either the string "create",
 
63
     *     "read", "update", or "delete" depending on what type of
 
64
     *     CRUD operation is actually triggered.
 
65
     * - *crudfinished* Triggered when either "create", "read", "update"
 
66
     *     or "delete" is finished, listeners will receive an
 
67
     *     <OpenLayers.Protocol.Response> object.
 
68
     * - *committriggered* Triggered when a commit operation is triggered.
 
69
     * - *commitfinished* Triggered when a commit operation is finished.
 
70
     * - *clear* Triggered when the clear API method is called.
 
71
     */
 
72
    EVENT_TYPES: ["crudtriggered", "crudfinished",
 
73
                  "committriggered", "commitfinished",
 
74
                  "clear"],
 
75
 
 
76
    /**
 
77
     * Constructor: mapfish.Protocol.TriggerEventDecorator
 
78
     *
 
79
     * Parameters:
 
80
     * options - {Object} Optional parameters to set in the protocol.
 
81
     *
 
82
     * Returns:
 
83
     * {<mapfish.Protocol.TriggerEventDecorator>}
 
84
     */
 
85
    initialize: function(options) {
 
86
        OpenLayers.Protocol.prototype.initialize.call(this, options);
 
87
        this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES);
 
88
        if (this.eventListeners instanceof Object) {
 
89
            this.events.on(this.eventListeners);
 
90
        }
 
91
    },
 
92
 
 
93
    /**
 
94
     * APIMethod: read
 
95
     * Construct a request for reading new features.
 
96
     *
 
97
     * Parameters:
 
98
     * options - {Object} Optional object for configuring the request.
 
99
     *
 
100
     * Returns:
 
101
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
102
     * object, the same object will be passed to the callback function passed
 
103
     * if one exists in the options object.
 
104
     */
 
105
    read: function(options) {
 
106
        var newOptions = OpenLayers.Util.applyDefaults({
 
107
            callback: this.createCallback(this.handleCRUD, options),
 
108
            scope: null
 
109
        }, options);
 
110
        this.events.triggerEvent("crudtriggered", {type: "read"});
 
111
        return this.protocol.read(newOptions);
 
112
    },
 
113
    
 
114
    
 
115
    /**
 
116
     * APIMethod: create
 
117
     * Construct a request for writing newly created features.
 
118
     *
 
119
     * Parameters:
 
120
     * features - {Array({<OpenLayers.Feature.Vector>})} or
 
121
     *            {<OpenLayers.Feature.Vector>}
 
122
     * options - {Object} Optional object for configuring the request.
 
123
     *
 
124
     * Returns:
 
125
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
126
     * object, the same object will be passed to the callback function passed
 
127
     * if one exists in the options object.
 
128
     */
 
129
    create: function(features, options) {
 
130
        var newOptions = OpenLayers.Util.applyDefaults({
 
131
            callback: this.createCallback(this.handleCRUD, options),
 
132
            scope: null
 
133
        }, options);
 
134
        this.events.triggerEvent("crudtriggered", {type: "create"});
 
135
        return this.protocol.create(features, newOptions);
 
136
    },
 
137
    
 
138
    /**
 
139
     * APIMethod: update
 
140
     * Construct a request updating modified features.
 
141
     *
 
142
     * Parameters:
 
143
     * features - {Array({<OpenLayers.Feature.Vector>})} or
 
144
     *            {<OpenLayers.Feature.Vector>}
 
145
     * options - {Object} Optional object for configuring the request.
 
146
     *
 
147
     * Returns:
 
148
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
149
     * object, the same object will be passed to the callback function passed
 
150
     * if one exists in the options object.
 
151
     */
 
152
    update: function(features, options) {
 
153
        var newOptions = OpenLayers.Util.applyDefaults({
 
154
            callback: this.createCallback(this.handleCRUD, options),
 
155
            scope: null
 
156
        }, options);
 
157
        this.events.triggerEvent("crudtriggered", {type: "update"});
 
158
        return this.protocol.update(features, newOptions);
 
159
    },
 
160
    
 
161
    /**
 
162
     * APIMethod: delete
 
163
     * Construct a request deleting a removed feature.
 
164
     *
 
165
     * Parameters:
 
166
     * feature - {<OpenLayers.Feature.Vector>}
 
167
     * options - {Object} Optional object for configuring the request.
 
168
     *
 
169
     * Returns:
 
170
     * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
 
171
     * object, the same object will be passed to the callback function passed
 
172
     * if one exists in the options object.
 
173
     */
 
174
    "delete": function(feature, options) {
 
175
        var newOptions = OpenLayers.Util.applyDefaults({
 
176
            callback: this.createCallback(this.handleCRUD, options),
 
177
            scope: null
 
178
        }, options);
 
179
        this.events.triggerEvent("crudtriggered", {type: "delete"});
 
180
        return this.protocol["delete"](feature, newOptions);
 
181
    },
 
182
 
 
183
    /**
 
184
     * APIMethod: commit
 
185
     * Go over the features and for each take action
 
186
     * based on the feature state. Possible actions are create,
 
187
     * update and delete.
 
188
     *
 
189
     * Parameters:
 
190
     * features - {Array({<OpenLayers.Feature.Vector>})}
 
191
     * options - {Object} Object whose possible keys are "create", "update",
 
192
     *      "delete", "callback" and "scope", the values referenced by the
 
193
     *      first three are objects as passed to the "create", "update", and
 
194
     *      "delete" methods, the value referenced by the "callback" key is
 
195
     *      a function which is called when the commit operation is complete
 
196
     *      using the scope referenced by the "scope" key.
 
197
     *
 
198
     * Returns:
 
199
     * {Array({<OpenLayers.Protocol.Response>})} An array of
 
200
     * <OpenLayers.Protocol.Response> objects.
 
201
     */
 
202
    commit: function(features, options) {
 
203
        var newOptions = OpenLayers.Util.applyDefaults({
 
204
            callback: this.createCallback(this.handleCommit, options),
 
205
            scope: null
 
206
        }, options);
 
207
        this.events.triggerEvent("committriggered");
 
208
        return this.protocol.commit(features, newOptions);
 
209
    },
 
210
 
 
211
    /**
 
212
     * Method: createCallback
 
213
     * Returns a function that applies the given public method with the
 
214
     * options argument.
 
215
     *
 
216
     * Parameters:
 
217
     * method - {Function} The method to be applied by the callback.
 
218
     * options - {Object} Options sent to the protocol method (read, create,
 
219
     *     update, or delete).
 
220
     */
 
221
    createCallback: function(method, options) {
 
222
        return OpenLayers.Function.bind(method, this, options);
 
223
    },
 
224
 
 
225
    /**
 
226
     * Method: handleCRUD
 
227
     * Method used to handle "create", "read", "update", and "delete"
 
228
     * operations.
 
229
     *
 
230
     * Parameters:
 
231
     * options - {Object} Options sent to the protocol method (read, create,
 
232
     *     update, or delete).
 
233
     * response - {<OpenLayers.Protocol.Response>} Response object received
 
234
     *     from the underlying protocol.
 
235
     */
 
236
    handleCRUD: function(options, response) {
 
237
        if (options && options.callback) {
 
238
            options.callback.call(options.scope, response);
 
239
        }
 
240
        this.events.triggerEvent("crudfinished", response);
 
241
    },
 
242
 
 
243
    /**
 
244
     * Method: handleCommit
 
245
     * Method used to handle "commit" operations.
 
246
     *
 
247
     * Parameters:
 
248
     * options -  {Object} Options sent to the protocol commit method.
 
249
     */
 
250
    handleCommit: function(options) {
 
251
        if (options.callback) {
 
252
            options.callback.call(options.scope);
 
253
        }
 
254
        this.events.triggerEvent("commitfinished");
 
255
    },
 
256
 
 
257
    /**
 
258
     * APIMethod: clear
 
259
     *      Clear all the previous results.
 
260
     */
 
261
    clear: function() {
 
262
        this.events.triggerEvent("clear");
 
263
    },
 
264
 
 
265
    CLASS_NAME: "mapfish.Protocol.TriggerEventDecorator"
 
266
});