Adding a strategy for saving newly added or modified features. Setting the auto property to true gives you auto-saves with every addition/modification. Setting auto to a time (in seconds) gives periodic auto-save of all new/modified features. By default, the save method must be called to trigger save. r=ahocevar (closes #1924)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@8818 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -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",
|
||||
|
||||
194
lib/OpenLayers/Strategy/Save.js
Normal file
194
lib/OpenLayers/Strategy/Save.js
Normal file
@@ -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 <save> before persisting changes. By
|
||||
* configuring the strategy with the <auto> option, changes can be saved
|
||||
* automatically.
|
||||
*
|
||||
* Inherits from:
|
||||
* - <OpenLayers.Strategy>
|
||||
*/
|
||||
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<len; ++i) {
|
||||
orig = features[i];
|
||||
clone = orig.clone();
|
||||
clone.fid = orig.fid;
|
||||
clone.state = orig.state;
|
||||
clone._original = orig;
|
||||
clone.geometry.transform(local, remote);
|
||||
clones[i] = clone;
|
||||
}
|
||||
features = clones;
|
||||
}
|
||||
this.layer.protocol.commit(features, {
|
||||
callback: this.onCommit,
|
||||
scope: this
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: onCommit
|
||||
* Called after protocol commit.
|
||||
*
|
||||
* Parameters:
|
||||
* response - {<OpenLayers.Protocol.Response>} 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<len; ++i) {
|
||||
feature = features[i];
|
||||
// if projection was different, we may be dealing with clones
|
||||
feature = feature._original || feature;
|
||||
state = feature.state;
|
||||
if(state) {
|
||||
if(state == OpenLayers.State.DELETE) {
|
||||
destroys.push(feature);
|
||||
} else if(state == OpenLayers.State.INSERT) {
|
||||
feature.fid = insertIds[j];
|
||||
++j;
|
||||
}
|
||||
feature.state = null;
|
||||
}
|
||||
}
|
||||
if(destroys.length > 0) {
|
||||
this.layer.destroyFeatures(destroys);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.Strategy.Save"
|
||||
});
|
||||
108
tests/Strategy/Save.html
Normal file
108
tests/Strategy/Save.html
Normal file
@@ -0,0 +1,108 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
function test_initialize(t) {
|
||||
t.plan(1);
|
||||
var strategy = new OpenLayers.Strategy.Save();
|
||||
t.eq(strategy.auto, false, "auto is false by default");
|
||||
}
|
||||
|
||||
function test_activate(t) {
|
||||
|
||||
t.plan(3);
|
||||
|
||||
var strategy = new OpenLayers.Strategy.Save();
|
||||
var layer = new OpenLayers.Layer.Vector(null, {
|
||||
isBaseLayer: true,
|
||||
protocol: new OpenLayers.Protocol(),
|
||||
strategies: [strategy]
|
||||
});
|
||||
var map = new OpenLayers.Map("map");
|
||||
map.addLayer(layer);
|
||||
map.zoomToMaxExtent();
|
||||
|
||||
// check that auto true registers listeners
|
||||
strategy.deactivate();
|
||||
strategy.auto = true;
|
||||
strategy.activate();
|
||||
t.ok(layer.events.listeners["featureadded"][0].func === strategy.triggerSave,
|
||||
"[auto true] triggerSave registered as listener for featureadded");
|
||||
t.ok(layer.events.listeners["afterfeaturemodified"][0].func === strategy.triggerSave,
|
||||
"[auto true] triggerSave registered as listener for afterfeaturemodified");
|
||||
|
||||
// check that auto can be set to interval
|
||||
strategy.deactivate();
|
||||
strategy.auto = 1;
|
||||
strategy.activate();
|
||||
t.ok(strategy.timer != null, "[auto number] timer set")
|
||||
|
||||
map.destroy();
|
||||
|
||||
}
|
||||
|
||||
function test_save(t) {
|
||||
t.plan(5);
|
||||
|
||||
var strategy = new OpenLayers.Strategy.Save();
|
||||
|
||||
// mock up a protocol for synchronous and successful commits
|
||||
var protocol = new OpenLayers.Protocol({
|
||||
commit: function(features, options) {
|
||||
var response = new OpenLayers.Protocol.Response();
|
||||
response.reqFeatures = features;
|
||||
response.insertIds = [];
|
||||
for(var i=0; i<features.length; ++i) {
|
||||
if(features[i].state == OpenLayers.State.INSERT) {
|
||||
response.insertIds.push("new_" + i);
|
||||
}
|
||||
}
|
||||
response.code = OpenLayers.Protocol.Response.SUCCESS;
|
||||
options.callback.call(options.scope, response);
|
||||
}
|
||||
});
|
||||
|
||||
var layer = new OpenLayers.Layer.Vector(null, {
|
||||
isBaseLayer: true,
|
||||
protocol: protocol,
|
||||
strategies: [strategy]
|
||||
});
|
||||
var map = new OpenLayers.Map("map");
|
||||
map.addLayer(layer);
|
||||
map.zoomToMaxExtent();
|
||||
|
||||
// give the layer some features
|
||||
var features = [
|
||||
new OpenLayers.Feature.Vector(), // insert
|
||||
new OpenLayers.Feature.Vector(), // delete
|
||||
new OpenLayers.Feature.Vector(), // update
|
||||
new OpenLayers.Feature.Vector() // nothing
|
||||
];
|
||||
features[0].state = OpenLayers.State.INSERT;
|
||||
features[1].state = OpenLayers.State.DELETE;
|
||||
features[2].state = OpenLayers.State.UPDATE;
|
||||
layer.addFeatures(features);
|
||||
|
||||
// save feature modifications
|
||||
strategy.save(features);
|
||||
|
||||
// confirm that newly created feature has an id and no longer has insert state
|
||||
t.eq(features[0].fid, "new_0", "newly created feature gets fid");
|
||||
t.ok(features[0].state == null, "newly created feature no longer insert state");
|
||||
|
||||
// confirm that deleted features are not on layer
|
||||
t.eq(layer.features.length, 3, "layer only has three features");
|
||||
t.ok(features[1].layer == null, "deleted feature has no layer");
|
||||
|
||||
// confirm that updated feature no longer has update state
|
||||
t.ok(features[2].state == null, "updated feature no longer update state");
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map" style="width: 400px; height: 200px" />
|
||||
</body>
|
||||
</html>
|
||||
@@ -133,6 +133,7 @@
|
||||
<li>Strategy/Cluster.html</li>
|
||||
<li>Strategy/Fixed.html</li>
|
||||
<li>Strategy/Paging.html</li>
|
||||
<li>Strategy/Save.html</li>
|
||||
<li>Style.html</li>
|
||||
<li>StyleMap.html</li>
|
||||
<li>Tile.html</li>
|
||||
|
||||
Reference in New Issue
Block a user