ModifyFeature makes SelectFeature control behave/become like ModifyFeature, patch=bjornharrtell,tschaub, r=tschaub,me (closes #1741)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8302 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2008-11-07 08:46:02 +00:00
parent d82f73e9e7
commit 7c0fadd14a
3 changed files with 51 additions and 45 deletions

View File

@@ -195,17 +195,15 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
var selectOptions = { var selectOptions = {
geometryTypes: this.geometryTypes, geometryTypes: this.geometryTypes,
clickout: this.clickout, clickout: this.clickout,
toggle: this.toggle toggle: this.toggle,
onBeforeSelect: this.beforeSelectFeature,
onSelect: this.selectFeature,
onUnselect: this.unselectFeature,
scope: this
}; };
this.selectControl = new OpenLayers.Control.SelectFeature( this.selectControl = new OpenLayers.Control.SelectFeature(
layer, selectOptions layer, selectOptions
); );
this.layer.events.on({
"beforefeatureselected": this.beforeSelectFeature,
"featureselected": this.selectFeature,
"featureunselected": this.unselectFeature,
scope: this
});
// configure the drag control // configure the drag control
var dragOptions = { var dragOptions = {
@@ -239,12 +237,6 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
* Take care of things that are not handled in superclass. * Take care of things that are not handled in superclass.
*/ */
destroy: function() { destroy: function() {
this.layer.events.un({
"beforefeatureselected": this.beforeSelectFeature,
"featureselected": this.selectFeature,
"featureunselected": this.unselectFeature,
scope: this
});
this.layer = null; this.layer = null;
this.selectControl.destroy(); this.selectControl.destroy();
this.dragControl.destroy(); this.dragControl.destroy();
@@ -295,12 +287,11 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
* Called before a feature is selected. * Called before a feature is selected.
* *
* Parameters: * Parameters:
* object - {Object} Object with a feature property referencing the * feature - {<OpenLayers.Feature.Vector>} The feature about to be selected.
* selected feature.
*/ */
beforeSelectFeature: function(object) { beforeSelectFeature: function(feature) {
return this.layer.events.triggerEvent( return this.layer.events.triggerEvent(
"beforefeaturemodified", {feature: object.feature} "beforefeaturemodified", {feature: feature}
); );
}, },
@@ -309,11 +300,10 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
* Called when the select feature control selects a feature. * Called when the select feature control selects a feature.
* *
* Parameters: * Parameters:
* object - {Object} Object with a feature property referencing the * feature - {<OpenLayers.Feature.Vector>} the selected feature.
* selected feature.
*/ */
selectFeature: function(object) { selectFeature: function(feature) {
this.feature = object.feature; this.feature = feature;
this.resetVertices(); this.resetVertices();
this.dragControl.activate(); this.dragControl.activate();
this.onModificationStart(this.feature); this.onModificationStart(this.feature);
@@ -324,10 +314,9 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
* Called when the select feature control unselects a feature. * Called when the select feature control unselects a feature.
* *
* Parameters: * Parameters:
* object - {Object} Object with a feature property referencing the * feature - {<OpenLayers.Feature.Vector>} The unselected feature.
* unselected feature.
*/ */
unselectFeature: function(object) { unselectFeature: function(feature) {
this.layer.removeFeatures(this.vertices, {silent: true}); this.layer.removeFeatures(this.vertices, {silent: true});
this.vertices = []; this.vertices = [];
this.layer.destroyFeatures(this.virtualVertices, {silent: true}); this.layer.destroyFeatures(this.virtualVertices, {silent: true});
@@ -342,9 +331,9 @@ OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {
} }
this.feature = null; this.feature = null;
this.dragControl.deactivate(); this.dragControl.deactivate();
this.onModificationEnd(object.feature); this.onModificationEnd(feature);
this.layer.events.triggerEvent("afterfeaturemodified", this.layer.events.triggerEvent("afterfeaturemodified",
{feature: object.feature}); {feature: feature});
}, },
/** /**

View File

@@ -65,6 +65,13 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
*/ */
box: false, box: false,
/**
* Property: onBeforeSelect
* {Function} Optional function to be called before a feature is selected.
* The function should expect to be called with a feature.
*/
onBeforeSelect: function() {},
/** /**
* APIProperty: onSelect * APIProperty: onSelect
* {Function} Optional function to be called when a feature is selected. * {Function} Optional function to be called when a feature is selected.
@@ -79,6 +86,13 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
*/ */
onUnselect: function() {}, onUnselect: function() {},
/**
* Property: scope
* {Object} The scope to use with the onBeforeSelect, onSelect, onUnselect
* callbacks. If null the scope will be this control.
*/
scope: this,
/** /**
* APIProperty: geometryTypes * APIProperty: geometryTypes
* {Array(String)} To restrict selecting to a limited set of geometry types, * {Array(String)} To restrict selecting to a limited set of geometry types,
@@ -312,7 +326,9 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
* feature - {<OpenLayers.Feature.Vector>} * feature - {<OpenLayers.Feature.Vector>}
*/ */
select: function(feature) { select: function(feature) {
var cont = this.layer.events.triggerEvent("beforefeatureselected", { var cont = this.onBeforeSelect.call(this.scope, feature);
if(cont !== false) {
cont = this.layer.events.triggerEvent("beforefeatureselected", {
feature: feature feature: feature
}); });
if(cont !== false) { if(cont !== false) {
@@ -322,7 +338,8 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
this.layer.drawFeature(feature, selectStyle); this.layer.drawFeature(feature, selectStyle);
this.layer.events.triggerEvent("featureselected", {feature: feature}); this.layer.events.triggerEvent("featureselected", {feature: feature});
this.onSelect(feature); this.onSelect.call(this.scope, feature);
}
} }
}, },
@@ -339,7 +356,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
this.layer.drawFeature(feature, "default"); this.layer.drawFeature(feature, "default");
OpenLayers.Util.removeItem(this.layer.selectedFeatures, feature); OpenLayers.Util.removeItem(this.layer.selectedFeatures, feature);
this.layer.events.triggerEvent("featureunselected", {feature: feature}); this.layer.events.triggerEvent("featureunselected", {feature: feature});
this.onUnselect(feature); this.onUnselect.call(this.scope, feature);
}, },
/** /**

View File

@@ -155,7 +155,7 @@
layer.destroyFeatures = function(verts) { layer.destroyFeatures = function(verts) {
t.ok(verts == 'b', "Virtual verts destroyed correctly"); t.ok(verts == 'b', "Virtual verts destroyed correctly");
} }
control.unselectFeature({feature: fakeFeature}); control.unselectFeature(fakeFeature);
t.eq(control.feature, null, "feature is set to null"); t.eq(control.feature, null, "feature is set to null");
layer.destroyFeatures = function() {}; layer.destroyFeatures = function() {};
@@ -225,7 +225,7 @@
var fakeFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0, 0)); var fakeFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0, 0));
// Points don't call collectVertices // Points don't call collectVertices
control.selectFeature({feature: fakeFeature}); control.selectFeature(fakeFeature);
control.collectVertices = function() { control.collectVertices = function() {
t.ok(true, "collectVertices called"); t.ok(true, "collectVertices called");
@@ -247,7 +247,7 @@
]); ]);
// OnSelect calls collectVertices and passes features to layer // OnSelect calls collectVertices and passes features to layer
control.selectFeature({feature: fakeFeature}); control.selectFeature(fakeFeature);
control.vertices = ['a']; control.vertices = ['a'];
control.virtualVertices = ['b']; control.virtualVertices = ['b'];
@@ -259,7 +259,7 @@
} }
// Features are removed whenever they exist // Features are removed whenever they exist
control.selectFeature({feature: fakeFeature}); control.selectFeature(fakeFeature);
control.destroy(); control.destroy();
@@ -424,7 +424,7 @@
t.eq(feature.id, testFeature.id, t.eq(feature.id, testFeature.id,
"onModificationStart called with the right feature"); "onModificationStart called with the right feature");
}; };
control.selectFeature({feature: testFeature}); control.selectFeature(testFeature);
map.destroy(); map.destroy();
} }
@@ -494,7 +494,7 @@
t.eq(feature.id, testFeature.id, t.eq(feature.id, testFeature.id,
"onModificationEnd called with the right feature"); "onModificationEnd called with the right feature");
}; };
control.unselectFeature({feature: testFeature}); control.unselectFeature(testFeature);
map.destroy(); map.destroy();
} }