provide a standalone mode for the ModifyFeature control. Thanks tschaub
for the fix in deactivate() and the tests. r=tschaub (closes #2199) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9591 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -44,6 +44,17 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
*/
|
*/
|
||||||
toggle: true,
|
toggle: true,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: standalone
|
||||||
|
* {Boolean} Set to true to create a control without SelectFeature
|
||||||
|
* capabilities. Default is false. If standalone is true, to modify
|
||||||
|
* a feature, call the <selectFeature> method with the target feature.
|
||||||
|
* Note that you must call the <unselectFeature> method to finish
|
||||||
|
* feature modification in standalone mode (before starting to modify
|
||||||
|
* another feature).
|
||||||
|
*/
|
||||||
|
standalone: false,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Property: layer
|
* Property: layer
|
||||||
* {<OpenLayers.Layer.Vector>}
|
* {<OpenLayers.Layer.Vector>}
|
||||||
@@ -207,9 +218,11 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
onUnselect: this.unselectFeature,
|
onUnselect: this.unselectFeature,
|
||||||
scope: this
|
scope: this
|
||||||
};
|
};
|
||||||
this.selectControl = new OpenLayers.Control.SelectFeature(
|
if(this.standalone === false) {
|
||||||
layer, selectOptions
|
this.selectControl = new OpenLayers.Control.SelectFeature(
|
||||||
);
|
layer, selectOptions
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// configure the drag control
|
// configure the drag control
|
||||||
var dragOptions = {
|
var dragOptions = {
|
||||||
@@ -244,7 +257,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
*/
|
*/
|
||||||
destroy: function() {
|
destroy: function() {
|
||||||
this.layer = null;
|
this.layer = null;
|
||||||
this.selectControl.destroy();
|
this.standalone || this.selectControl.destroy();
|
||||||
this.dragControl.destroy();
|
this.dragControl.destroy();
|
||||||
OpenLayers.Control.prototype.destroy.apply(this, []);
|
OpenLayers.Control.prototype.destroy.apply(this, []);
|
||||||
},
|
},
|
||||||
@@ -257,7 +270,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* {Boolean} Successfully activated the control.
|
* {Boolean} Successfully activated the control.
|
||||||
*/
|
*/
|
||||||
activate: function() {
|
activate: function() {
|
||||||
return (this.selectControl.activate() &&
|
return ((this.standalone || this.selectControl.activate()) &&
|
||||||
this.handlers.keyboard.activate() &&
|
this.handlers.keyboard.activate() &&
|
||||||
OpenLayers.Control.prototype.activate.apply(this, arguments));
|
OpenLayers.Control.prototype.activate.apply(this, arguments));
|
||||||
},
|
},
|
||||||
@@ -277,11 +290,19 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
this.layer.removeFeatures(this.virtualVertices, {silent: true});
|
this.layer.removeFeatures(this.virtualVertices, {silent: true});
|
||||||
this.vertices = [];
|
this.vertices = [];
|
||||||
this.dragControl.deactivate();
|
this.dragControl.deactivate();
|
||||||
if(this.feature && this.feature.geometry && this.feature.layer) {
|
var feature = this.feature;
|
||||||
this.selectControl.unselect.apply(this.selectControl,
|
var valid = feature && feature.geometry && feature.layer;
|
||||||
[this.feature]);
|
if(this.standalone === false) {
|
||||||
|
if(valid) {
|
||||||
|
this.selectControl.unselect.apply(this.selectControl,
|
||||||
|
[feature]);
|
||||||
|
}
|
||||||
|
this.selectControl.deactivate();
|
||||||
|
} else {
|
||||||
|
if(valid) {
|
||||||
|
this.unselectFeature(feature);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.selectControl.deactivate();
|
|
||||||
this.handlers.keyboard.deactivate();
|
this.handlers.keyboard.deactivate();
|
||||||
deactivated = true;
|
deactivated = true;
|
||||||
}
|
}
|
||||||
@@ -364,7 +385,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
// only change behavior if the feature is not in the vertices array
|
// only change behavior if the feature is not in the vertices array
|
||||||
if(feature != this.feature && !feature.geometry.parent &&
|
if(feature != this.feature && !feature.geometry.parent &&
|
||||||
feature != this.dragHandle && feature != this.radiusHandle) {
|
feature != this.dragHandle && feature != this.radiusHandle) {
|
||||||
if(this.feature) {
|
if(this.standalone === false && this.feature) {
|
||||||
// unselect the currently selected feature
|
// unselect the currently selected feature
|
||||||
this.selectControl.clickFeature.apply(this.selectControl,
|
this.selectControl.clickFeature.apply(this.selectControl,
|
||||||
[this.feature]);
|
[this.feature]);
|
||||||
@@ -374,8 +395,8 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
OpenLayers.Util.indexOf(this.geometryTypes,
|
OpenLayers.Util.indexOf(this.geometryTypes,
|
||||||
feature.geometry.CLASS_NAME) != -1) {
|
feature.geometry.CLASS_NAME) != -1) {
|
||||||
// select the point
|
// select the point
|
||||||
this.selectControl.clickFeature.apply(this.selectControl,
|
this.standalone || this.selectControl.clickFeature.apply(
|
||||||
[feature]);
|
this.selectControl, [feature]);
|
||||||
/**
|
/**
|
||||||
* TBD: These lines improve workflow by letting the user
|
* TBD: These lines improve workflow by letting the user
|
||||||
* immediately start dragging after the mouse down.
|
* immediately start dragging after the mouse down.
|
||||||
@@ -452,7 +473,8 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
this.layer.destroyFeatures(this.virtualVertices, {silent: true});
|
this.layer.destroyFeatures(this.virtualVertices, {silent: true});
|
||||||
this.virtualVertices = [];
|
this.virtualVertices = [];
|
||||||
}
|
}
|
||||||
this.layer.drawFeature(this.feature, this.selectControl.renderIntent);
|
this.layer.drawFeature(this.feature, this.standalone ? undefined :
|
||||||
|
this.selectControl.renderIntent);
|
||||||
}
|
}
|
||||||
// keep the vertex on top so it gets the mouseout after dragging
|
// keep the vertex on top so it gets the mouseout after dragging
|
||||||
// this should be removed in favor of an option to draw under or
|
// this should be removed in favor of an option to draw under or
|
||||||
@@ -558,7 +580,8 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
vertex.geometry.parent) {
|
vertex.geometry.parent) {
|
||||||
// remove the vertex
|
// remove the vertex
|
||||||
vertex.geometry.parent.removeComponent(vertex.geometry);
|
vertex.geometry.parent.removeComponent(vertex.geometry);
|
||||||
this.layer.drawFeature(this.feature,
|
this.layer.drawFeature(this.feature, this.standalone ?
|
||||||
|
undefined :
|
||||||
this.selectControl.renderIntent);
|
this.selectControl.renderIntent);
|
||||||
this.resetVertices();
|
this.resetVertices();
|
||||||
this.setFeatureState();
|
this.setFeatureState();
|
||||||
@@ -708,7 +731,7 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* map - {<OpenLayers.Map>} The control's map.
|
* map - {<OpenLayers.Map>} The control's map.
|
||||||
*/
|
*/
|
||||||
setMap: function(map) {
|
setMap: function(map) {
|
||||||
this.selectControl.setMap(map);
|
this.standalone || this.selectControl.setMap(map);
|
||||||
this.dragControl.setMap(map);
|
this.dragControl.setMap(map);
|
||||||
OpenLayers.Control.prototype.setMap.apply(this, arguments);
|
OpenLayers.Control.prototype.setMap.apply(this, arguments);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -610,6 +610,70 @@
|
|||||||
map.destroy();
|
map.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_standalone(t) {
|
||||||
|
|
||||||
|
t.plan(13);
|
||||||
|
var map = new OpenLayers.Map("map");
|
||||||
|
var layer = new OpenLayers.Layer.Vector();
|
||||||
|
|
||||||
|
var f1 = new OpenLayers.Feature.Vector(
|
||||||
|
OpenLayers.Geometry.fromWKT("LINESTRING(3 4,10 50,20 25)")
|
||||||
|
);
|
||||||
|
var f2 = new OpenLayers.Feature.Vector(
|
||||||
|
OpenLayers.Geometry.fromWKT("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2))")
|
||||||
|
);
|
||||||
|
layer.addFeatures([f1, f2]);
|
||||||
|
|
||||||
|
map.addLayer(layer);
|
||||||
|
var control = new OpenLayers.Control.ModifyFeature(layer, {standalone: true});
|
||||||
|
map.addControl(control);
|
||||||
|
|
||||||
|
var log = [];
|
||||||
|
layer.events.on({
|
||||||
|
beforefeaturemodified: function(evt) {
|
||||||
|
log.push(evt);
|
||||||
|
},
|
||||||
|
featuremodified: function(evt) {
|
||||||
|
log.push(evt);
|
||||||
|
},
|
||||||
|
afterfeaturemodified: function(evt) {
|
||||||
|
log.push(evt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// activate control
|
||||||
|
control.activate();
|
||||||
|
t.eq(control.active, true, "[activate] control activated");
|
||||||
|
t.eq(control.selectControl, null, "[activate] no select control");
|
||||||
|
|
||||||
|
// manually select feature for editing
|
||||||
|
control.selectFeature(f1);
|
||||||
|
t.ok(control.feature === f1, "[select f1] control.feature set to f1");
|
||||||
|
|
||||||
|
// manually unselect feature for editing
|
||||||
|
control.unselectFeature(f1);
|
||||||
|
t.eq(control.feature, null, "[unselect f1] control.feature set to null");
|
||||||
|
t.eq(log.length, 1, "[unselect f1] event logged");
|
||||||
|
t.eq(log[0].type, "afterfeaturemodified", "[unselect f1] afterfeaturemodified triggered");
|
||||||
|
t.ok(log[0].feature === f1, "[unselect f1] correct feature");
|
||||||
|
t.eq(log[0].modified, false, "[unselect f1] feature not actually modified");
|
||||||
|
|
||||||
|
// clear log and select new feature for editing
|
||||||
|
log = [];
|
||||||
|
control.selectFeature(f2);
|
||||||
|
t.ok(control.feature === f2, "[select f2] control.feature set to f2");
|
||||||
|
|
||||||
|
// deactivate control and confirm feature is unselected
|
||||||
|
control.deactivate();
|
||||||
|
t.eq(log.length, 1, "[deactivate] event logged");
|
||||||
|
t.eq(log[0].type, "afterfeaturemodified", "[deactivate] afterfeaturemodified triggered");
|
||||||
|
t.ok(log[0].feature === f2, "[deactivate] correct feature");
|
||||||
|
t.eq(log[0].modified, false, "[deactivate] feature not actually modified");
|
||||||
|
|
||||||
|
map.destroy();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user