1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Strategy.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Strategy.Save |
---|
12 | * A strategy that commits newly created or modified features. By default |
---|
13 | * the strategy waits for a call to <save> before persisting changes. By |
---|
14 | * configuring the strategy with the <auto> option, changes can be saved |
---|
15 | * automatically. |
---|
16 | * |
---|
17 | * Inherits from: |
---|
18 | * - <OpenLayers.Strategy> |
---|
19 | */ |
---|
20 | OpenLayers.Strategy.Save = OpenLayers.Class(OpenLayers.Strategy, { |
---|
21 | |
---|
22 | /** |
---|
23 | * Constant: EVENT_TYPES |
---|
24 | * {Array(String)} Supported application event types. Register a listener |
---|
25 | * for a particular event with the following syntax: |
---|
26 | * (code) |
---|
27 | * strategy.events.register(type, obj, listener); |
---|
28 | * (end) |
---|
29 | * |
---|
30 | * - *start* Triggered before saving |
---|
31 | * - *success* Triggered after a successful transaction |
---|
32 | * - *fail* Triggered after a failed transaction |
---|
33 | * |
---|
34 | */ |
---|
35 | EVENT_TYPES: ["start", "success", "fail"], |
---|
36 | |
---|
37 | /** |
---|
38 | * Property: events |
---|
39 | * {<OpenLayers.Events>} Events instance for triggering this protocol |
---|
40 | * events. |
---|
41 | */ |
---|
42 | events: null, |
---|
43 | |
---|
44 | /** |
---|
45 | * APIProperty: auto |
---|
46 | * {Boolean | Number} Auto-save. Default is false. If true, features will be |
---|
47 | * saved immediately after being added to the layer and with each |
---|
48 | * modification or deletion. If auto is a number, features will be |
---|
49 | * saved on an interval provided by the value (in seconds). |
---|
50 | */ |
---|
51 | auto: false, |
---|
52 | |
---|
53 | /** |
---|
54 | * Property: timer |
---|
55 | * {Number} The id of the timer. |
---|
56 | */ |
---|
57 | timer: null, |
---|
58 | |
---|
59 | /** |
---|
60 | * Constructor: OpenLayers.Strategy.Save |
---|
61 | * Create a new Save strategy. |
---|
62 | * |
---|
63 | * Parameters: |
---|
64 | * options - {Object} Optional object whose properties will be set on the |
---|
65 | * instance. |
---|
66 | */ |
---|
67 | initialize: function(options) { |
---|
68 | OpenLayers.Strategy.prototype.initialize.apply(this, [options]); |
---|
69 | this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES); |
---|
70 | }, |
---|
71 | |
---|
72 | /** |
---|
73 | * APIMethod: activate |
---|
74 | * Activate the strategy. Register any listeners, do appropriate setup. |
---|
75 | * |
---|
76 | * Returns: |
---|
77 | * {Boolean} The strategy was successfully activated. |
---|
78 | */ |
---|
79 | activate: function() { |
---|
80 | var activated = OpenLayers.Strategy.prototype.activate.call(this); |
---|
81 | if(activated) { |
---|
82 | if(this.auto) { |
---|
83 | if(typeof this.auto === "number") { |
---|
84 | this.timer = window.setInterval( |
---|
85 | OpenLayers.Function.bind(this.save, this), |
---|
86 | this.auto * 1000 |
---|
87 | ); |
---|
88 | } else { |
---|
89 | this.layer.events.on({ |
---|
90 | "featureadded": this.triggerSave, |
---|
91 | "afterfeaturemodified": this.triggerSave, |
---|
92 | scope: this |
---|
93 | }); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | return activated; |
---|
98 | }, |
---|
99 | |
---|
100 | /** |
---|
101 | * APIMethod: deactivate |
---|
102 | * Deactivate the strategy. Unregister any listeners, do appropriate |
---|
103 | * tear-down. |
---|
104 | * |
---|
105 | * Returns: |
---|
106 | * {Boolean} The strategy was successfully deactivated. |
---|
107 | */ |
---|
108 | deactivate: function() { |
---|
109 | var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); |
---|
110 | if(deactivated) { |
---|
111 | if(this.auto) { |
---|
112 | if(typeof this.auto === "number") { |
---|
113 | window.clearInterval(this.timer); |
---|
114 | } else { |
---|
115 | this.layer.events.un({ |
---|
116 | "featureadded": this.triggerSave, |
---|
117 | "afterfeaturemodified": this.triggerSave, |
---|
118 | scope: this |
---|
119 | }); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | return deactivated; |
---|
124 | }, |
---|
125 | |
---|
126 | /** |
---|
127 | * Method: triggerSave |
---|
128 | * Registered as a listener. Calls save if a feature has insert, update, |
---|
129 | * or delete state. |
---|
130 | * |
---|
131 | * Parameters: |
---|
132 | * event - {Object} The event this function is listening for. |
---|
133 | */ |
---|
134 | triggerSave: function(event) { |
---|
135 | var feature = event.feature; |
---|
136 | if(feature.state === OpenLayers.State.INSERT || |
---|
137 | feature.state === OpenLayers.State.UPDATE || |
---|
138 | feature.state === OpenLayers.State.DELETE) { |
---|
139 | this.save([event.feature]); |
---|
140 | } |
---|
141 | }, |
---|
142 | |
---|
143 | /** |
---|
144 | * APIMethod: save |
---|
145 | * Tell the layer protocol to commit unsaved features. If the layer |
---|
146 | * projection differs from the map projection, features will be |
---|
147 | * transformed into the layer projection before being committed. |
---|
148 | * |
---|
149 | * Parameters: |
---|
150 | * features - {Array} Features to be saved. If null, then default is all |
---|
151 | * features in the layer. Features are assumed to be in the map |
---|
152 | * projection. |
---|
153 | */ |
---|
154 | save: function(features) { |
---|
155 | if(!features) { |
---|
156 | features = this.layer.features; |
---|
157 | } |
---|
158 | this.events.triggerEvent("start", {features:features}); |
---|
159 | var remote = this.layer.projection; |
---|
160 | var local = this.layer.map.getProjectionObject(); |
---|
161 | if(!local.equals(remote)) { |
---|
162 | var len = features.length; |
---|
163 | var clones = new Array(len); |
---|
164 | var orig, clone; |
---|
165 | for(var i=0; i<len; ++i) { |
---|
166 | orig = features[i]; |
---|
167 | clone = orig.clone(); |
---|
168 | clone.fid = orig.fid; |
---|
169 | clone.state = orig.state; |
---|
170 | if(orig.url) { |
---|
171 | clone.url = orig.url; |
---|
172 | } |
---|
173 | clone._original = orig; |
---|
174 | clone.geometry.transform(local, remote); |
---|
175 | clones[i] = clone; |
---|
176 | } |
---|
177 | features = clones; |
---|
178 | } |
---|
179 | this.layer.protocol.commit(features, { |
---|
180 | callback: this.onCommit, |
---|
181 | scope: this |
---|
182 | }); |
---|
183 | }, |
---|
184 | |
---|
185 | /** |
---|
186 | * Method: onCommit |
---|
187 | * Called after protocol commit. |
---|
188 | * |
---|
189 | * Parameters: |
---|
190 | * response - {<OpenLayers.Protocol.Response>} A response object. |
---|
191 | */ |
---|
192 | onCommit: function(response) { |
---|
193 | var evt = {"response": response}; |
---|
194 | if(response.success()) { |
---|
195 | var features = response.reqFeatures; |
---|
196 | // deal with inserts, updates, and deletes |
---|
197 | var state, feature; |
---|
198 | var destroys = []; |
---|
199 | var insertIds = response.insertIds || []; |
---|
200 | var j = 0; |
---|
201 | for(var i=0, len=features.length; i<len; ++i) { |
---|
202 | feature = features[i]; |
---|
203 | // if projection was different, we may be dealing with clones |
---|
204 | feature = feature._original || feature; |
---|
205 | state = feature.state; |
---|
206 | if(state) { |
---|
207 | if(state == OpenLayers.State.DELETE) { |
---|
208 | destroys.push(feature); |
---|
209 | } else if(state == OpenLayers.State.INSERT) { |
---|
210 | feature.fid = insertIds[j]; |
---|
211 | ++j; |
---|
212 | } |
---|
213 | feature.state = null; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | if(destroys.length > 0) { |
---|
218 | this.layer.destroyFeatures(destroys); |
---|
219 | } |
---|
220 | |
---|
221 | this.events.triggerEvent("success", evt); |
---|
222 | |
---|
223 | } else { |
---|
224 | this.events.triggerEvent("fail", evt); |
---|
225 | } |
---|
226 | }, |
---|
227 | |
---|
228 | CLASS_NAME: "OpenLayers.Strategy.Save" |
---|
229 | }); |
---|