2
* Copyright (C) 2007 Camptocamp
4
* This file is part of MapFish Client
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.
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.
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/>.
21
* @requires OpenLayers/Util.js
22
* @requires OpenLayers/Protocol.js
26
* Class: mapfish.Protocol.TriggerEventDecorator
27
* Decorator class responsible to trigger events when requests are
31
* - <OpenLayers.Protocol>
34
mapfish.Protocol.TriggerEventDecorator = OpenLayers.Class(OpenLayers.Protocol, {
37
* APIProperty: protocol
38
* {<OpenLayers.Protocol>} The decorated protocol.
44
* {<OpenLayers.Events>}
49
* Constant: EVENT_TYPES
50
* {Array(String)} Supported event types. Register a listener
51
* for a particular event with the following syntax:
54
* protocol.events.register(type, obj, listener);
57
* Listeners will be called with a reference to an event object.
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.
72
EVENT_TYPES: ["crudtriggered", "crudfinished",
73
"committriggered", "commitfinished",
77
* Constructor: mapfish.Protocol.TriggerEventDecorator
80
* options - {Object} Optional parameters to set in the protocol.
83
* {<mapfish.Protocol.TriggerEventDecorator>}
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);
95
* Construct a request for reading new features.
98
* options - {Object} Optional object for configuring the request.
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.
105
read: function(options) {
106
var newOptions = OpenLayers.Util.applyDefaults({
107
callback: this.createCallback(this.handleCRUD, options),
110
this.events.triggerEvent("crudtriggered", {type: "read"});
111
return this.protocol.read(newOptions);
117
* Construct a request for writing newly created features.
120
* features - {Array({<OpenLayers.Feature.Vector>})} or
121
* {<OpenLayers.Feature.Vector>}
122
* options - {Object} Optional object for configuring the request.
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.
129
create: function(features, options) {
130
var newOptions = OpenLayers.Util.applyDefaults({
131
callback: this.createCallback(this.handleCRUD, options),
134
this.events.triggerEvent("crudtriggered", {type: "create"});
135
return this.protocol.create(features, newOptions);
140
* Construct a request updating modified features.
143
* features - {Array({<OpenLayers.Feature.Vector>})} or
144
* {<OpenLayers.Feature.Vector>}
145
* options - {Object} Optional object for configuring the request.
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.
152
update: function(features, options) {
153
var newOptions = OpenLayers.Util.applyDefaults({
154
callback: this.createCallback(this.handleCRUD, options),
157
this.events.triggerEvent("crudtriggered", {type: "update"});
158
return this.protocol.update(features, newOptions);
163
* Construct a request deleting a removed feature.
166
* feature - {<OpenLayers.Feature.Vector>}
167
* options - {Object} Optional object for configuring the request.
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.
174
"delete": function(feature, options) {
175
var newOptions = OpenLayers.Util.applyDefaults({
176
callback: this.createCallback(this.handleCRUD, options),
179
this.events.triggerEvent("crudtriggered", {type: "delete"});
180
return this.protocol["delete"](feature, newOptions);
185
* Go over the features and for each take action
186
* based on the feature state. Possible actions are create,
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.
199
* {Array({<OpenLayers.Protocol.Response>})} An array of
200
* <OpenLayers.Protocol.Response> objects.
202
commit: function(features, options) {
203
var newOptions = OpenLayers.Util.applyDefaults({
204
callback: this.createCallback(this.handleCommit, options),
207
this.events.triggerEvent("committriggered");
208
return this.protocol.commit(features, newOptions);
212
* Method: createCallback
213
* Returns a function that applies the given public method with the
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).
221
createCallback: function(method, options) {
222
return OpenLayers.Function.bind(method, this, options);
227
* Method used to handle "create", "read", "update", and "delete"
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.
236
handleCRUD: function(options, response) {
237
if (options && options.callback) {
238
options.callback.call(options.scope, response);
240
this.events.triggerEvent("crudfinished", response);
244
* Method: handleCommit
245
* Method used to handle "commit" operations.
248
* options - {Object} Options sent to the protocol commit method.
250
handleCommit: function(options) {
251
if (options.callback) {
252
options.callback.call(options.scope);
254
this.events.triggerEvent("commitfinished");
259
* Clear all the previous results.
262
this.events.triggerEvent("clear");
265
CLASS_NAME: "mapfish.Protocol.TriggerEventDecorator"