1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Strategy.js
10 * Class: OpenLayers.Strategy.Save
11 * A strategy that commits newly created or modified features. By default
12 * the strategy waits for a call to <save> before persisting changes. By
13 * configuring the strategy with the <auto> option, changes can be saved
17 * - <OpenLayers.Strategy>
19 OpenLayers.Strategy.Save = OpenLayers.Class(OpenLayers.Strategy, {
23 * {Boolean | Number} Auto-save. Default is false. If true, features will be
24 * saved immediately after being added to the layer and with each
25 * modification or deletion. If auto is a number, features will be
26 * saved on an interval provided by the value (in seconds).
32 * {Number} The id of the timer.
37 * Constructor: OpenLayers.Strategy.Save
38 * Create a new Save strategy.
41 * options - {Object} Optional object whose properties will be set on the
44 initialize: function(options) {
45 OpenLayers.Strategy.prototype.initialize.apply(this, [options]);
50 * Activate the strategy. Register any listeners, do appropriate setup.
53 * {Boolean} The strategy was successfully activated.
55 activate: function() {
56 var activated = OpenLayers.Strategy.prototype.activate.call(this);
59 if(typeof this.auto === "number") {
60 this.timer = window.setInterval(
61 OpenLayers.Function.bind(this.save, this),
65 this.layer.events.on({
66 "featureadded": this.triggerSave,
67 "afterfeaturemodified": this.triggerSave,
77 * APIMethod: deactivate
78 * Deactivate the strategy. Unregister any listeners, do appropriate
82 * {Boolean} The strategy was successfully deactivated.
84 deactivate: function() {
85 var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
88 if(typeof this.auto === "number") {
89 window.clearInterval(this.timer);
91 this.layer.events.un({
92 "featureadded": this.triggerSave,
93 "afterfeaturemodified": this.triggerSave,
103 * Method: triggerSave
104 * Registered as a listener. Calls save if a feature has insert, update,
108 * event - {Object} The event this function is listening for.
110 triggerSave: function(event) {
111 var feature = event.feature;
112 if(feature.state === OpenLayers.State.INSERT ||
113 feature.state === OpenLayers.State.UPDATE ||
114 feature.state === OpenLayers.State.DELETE) {
115 this.save([event.feature]);
121 * Tell the layer protocol to commit unsaved features. If the layer
122 * projection differs from the map projection, features will be
123 * transformed into the layer projection before being committed.
126 * features - {Array} Features to be saved. If null, then default is all
127 * features in the layer. Features are assumed to be in the map
130 save: function(features) {
132 features = this.layer.features;
134 var remote = this.layer.projection;
135 var local = this.layer.map.getProjectionObject();
136 if(!local.equals(remote)) {
137 var len = features.length;
138 var clones = new Array(len);
140 for(var i=0; i<len; ++i) {
142 clone = orig.clone();
143 clone.fid = orig.fid;
144 clone.state = orig.state;
145 clone._original = orig;
146 clone.geometry.transform(local, remote);
151 this.layer.protocol.commit(features, {
152 callback: this.onCommit,
159 * Called after protocol commit.
162 * response - {<OpenLayers.Protocol.Response>} A response object.
164 onCommit: function(response) {
165 if(response.success()) {
166 var features = response.reqFeatures;
167 // deal with inserts, updates, and deletes
170 var insertIds = response.insertIds || [];
172 for(var i=0, len=features.length; i<len; ++i) {
173 feature = features[i];
174 // if projection was different, we may be dealing with clones
175 feature = feature._original || feature;
176 state = feature.state;
178 if(state == OpenLayers.State.DELETE) {
179 destroys.push(feature);
180 } else if(state == OpenLayers.State.INSERT) {
181 feature.fid = insertIds[j];
184 feature.state = null;
187 if(destroys.length > 0) {
188 this.layer.destroyFeatures(destroys);
193 CLASS_NAME: "OpenLayers.Strategy.Save"