diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 93a5e8c4d3..e3803fdbda 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -188,6 +188,7 @@ "OpenLayers/Strategy/Cluster.js", "OpenLayers/Strategy/Paging.js", "OpenLayers/Strategy/BBOX.js", + "OpenLayers/Strategy/Save.js", "OpenLayers/Protocol.js", "OpenLayers/Protocol/HTTP.js", "OpenLayers/Protocol/SQL.js", diff --git a/lib/OpenLayers/Strategy/Save.js b/lib/OpenLayers/Strategy/Save.js new file mode 100644 index 0000000000..b4b8f3b150 --- /dev/null +++ b/lib/OpenLayers/Strategy/Save.js @@ -0,0 +1,194 @@ +/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD + * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the + * full text of the license. */ + +/** + * @requires OpenLayers/Strategy.js + */ + +/** + * Class: OpenLayers.Strategy.Save + * A strategy that commits newly created or modified features. By default + * the strategy waits for a call to before persisting changes. By + * configuring the strategy with the option, changes can be saved + * automatically. + * + * Inherits from: + * - + */ +OpenLayers.Strategy.Save = OpenLayers.Class(OpenLayers.Strategy, { + + /** + * APIProperty: auto + * {Boolean | Number} Auto-save. Default is false. If true, features will be + * saved immediately after being added to the layer and with each + * modification or deletion. If auto is a number, features will be + * saved on an interval provided by the value (in seconds). + */ + auto: false, + + /** + * Property: timer + * {Number} The id of the timer. + */ + timer: null, + + /** + * Constructor: OpenLayers.Strategy.Save + * Create a new Save strategy. + * + * Parameters: + * options - {Object} Optional object whose properties will be set on the + * instance. + */ + initialize: function(options) { + OpenLayers.Strategy.prototype.initialize.apply(this, [options]); + }, + + /** + * APIMethod: activate + * Activate the strategy. Register any listeners, do appropriate setup. + * + * Returns: + * {Boolean} The strategy was successfully activated. + */ + activate: function() { + var activated = OpenLayers.Strategy.prototype.activate.call(this); + if(activated) { + if(this.auto) { + if(typeof this.auto === "number") { + this.timer = window.setInterval( + OpenLayers.Function.bind(this.save, this), + this.auto * 1000 + ) + } else { + this.layer.events.on({ + "featureadded": this.triggerSave, + "afterfeaturemodified": this.triggerSave, + scope: this + }); + } + } + } + return activated; + }, + + /** + * APIMethod: deactivate + * Deactivate the strategy. Unregister any listeners, do appropriate + * tear-down. + * + * Returns: + * {Boolean} The strategy was successfully deactivated. + */ + deactivate: function() { + var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); + if(deactivated) { + if(this.auto) { + if(typeof this.auto === "number") { + window.clearInterval(this.timer); + } else { + this.layer.events.un({ + "featureadded": this.triggerSave, + "afterfeaturemodified": this.triggerSave, + scope: this + }) + } + } + } + return deactivated; + }, + + /** + * Method: triggerSave + * Registered as a listener. Calls save if a feature has insert, update, + * or delete state. + * + * Parameters: + * event - {Object} The event this function is listening for. + */ + triggerSave: function(event) { + var feature = event.feature; + if(feature.state === OpenLayers.State.INSERT || + feature.state === OpenLayers.State.UPDATE || + feature.state === OpenLayers.State.DELETE) { + this.save([event.feature]); + } + }, + + /** + * Method: save + * Tell the layer protocol to commit unsaved features. If the layer + * projection differs from the map projection, features will be + * transformed into the layer projection before being committed. + * + * Parameters: + * features - {Array} Features to be saved. If null, then default is all + * features in the layer. Features are assumed to be in the map + * projection. + */ + save: function(features) { + if(!features) { + features = this.layer.features; + } + var remote = this.layer.projection; + var local = this.layer.map.getProjectionObject(); + if(!local.equals(remote)) { + var len = features.length; + var clones = new Array(len); + var orig, clone; + for(var i=0; i} A response object. + */ + onCommit: function(response) { + if(response.success()) { + var features = response.reqFeatures; + // deal with inserts, updates, and deletes + var state, feature; + var destroys = []; + var insertIds = response.insertIds || []; + var j = 0; + for(var i=0, len=features.length; i 0) { + this.layer.destroyFeatures(destroys); + } + } + }, + + CLASS_NAME: "OpenLayers.Strategy.Save" +}); diff --git a/tests/Strategy/Save.html b/tests/Strategy/Save.html new file mode 100644 index 0000000000..655fce8cba --- /dev/null +++ b/tests/Strategy/Save.html @@ -0,0 +1,108 @@ + + + + + + +
+ + diff --git a/tests/list-tests.html b/tests/list-tests.html index 39d72c54da..e382618920 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -133,6 +133,7 @@
  • Strategy/Cluster.html
  • Strategy/Fixed.html
  • Strategy/Paging.html
  • +
  • Strategy/Save.html
  • Style.html
  • StyleMap.html
  • Tile.html