add featureremoved-type events to Layer.Vector, r=fredj (closes #1535)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@7277 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -45,6 +45,15 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
|
|||||||
* - *featuresadded* Triggered after features are added. The event
|
* - *featuresadded* Triggered after features are added. The event
|
||||||
* object passed to listeners will have a *features* property with a
|
* object passed to listeners will have a *features* property with a
|
||||||
* reference to an array of added features.
|
* reference to an array of added features.
|
||||||
|
* - *beforefeatureremoved* Triggered before a feature is removed. Listeners
|
||||||
|
* will receive an object with a *feature* property referencing the
|
||||||
|
* feature to be removed.
|
||||||
|
* - *featureremoved* Triggerd after a feature is removed. The event
|
||||||
|
* object passed to listeners will have a *feature* property with a
|
||||||
|
* reference to the removed feature.
|
||||||
|
* - *featuresremoved* Triggered after features are removed. The event
|
||||||
|
* object passed to listeners will have a *features* property with a
|
||||||
|
* reference to an array of removed features.
|
||||||
* - *featureselected* Triggered after a feature is selected. Listeners
|
* - *featureselected* Triggered after a feature is selected. Listeners
|
||||||
* will receive an object with a *feature* property referencing the
|
* will receive an object with a *feature* property referencing the
|
||||||
* selected feature.
|
* selected feature.
|
||||||
@@ -61,8 +70,9 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
|
|||||||
* Listeners will receive an object with a *feature* property referencing
|
* Listeners will receive an object with a *feature* property referencing
|
||||||
* the modified feature.
|
* the modified feature.
|
||||||
*/
|
*/
|
||||||
EVENT_TYPES: ["beforefeatureadded", "featureadded",
|
EVENT_TYPES: ["beforefeatureadded", "featureadded", "featuresadded",
|
||||||
"featuresadded", "featureselected", "featureunselected",
|
"beforefeatureremoved", "featureremoved", "featuresremoved",
|
||||||
|
"featureselected", "featureunselected",
|
||||||
"beforefeaturemodified", "featuremodified", "afterfeaturemodified"],
|
"beforefeaturemodified", "featuremodified", "afterfeaturemodified"],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -366,14 +376,27 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
|
|||||||
*
|
*
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* features - {Array(<OpenLayers.Feature.Vector>)}
|
* features - {Array(<OpenLayers.Feature.Vector>)}
|
||||||
|
* options - {Object}
|
||||||
*/
|
*/
|
||||||
removeFeatures: function(features) {
|
removeFeatures: function(features, options) {
|
||||||
if (!(features instanceof Array)) {
|
if (!(features instanceof Array)) {
|
||||||
features = [features];
|
features = [features];
|
||||||
}
|
}
|
||||||
|
if (features.length <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var notify = !options || !options.silent;
|
||||||
|
|
||||||
for (var i = features.length - 1; i >= 0; i--) {
|
for (var i = features.length - 1; i >= 0; i--) {
|
||||||
var feature = features[i];
|
var feature = features[i];
|
||||||
|
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent("beforefeatureremoved", {
|
||||||
|
feature: feature
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.features = OpenLayers.Util.removeItem(this.features, feature);
|
this.features = OpenLayers.Util.removeItem(this.features, feature);
|
||||||
|
|
||||||
if (feature.geometry) {
|
if (feature.geometry) {
|
||||||
@@ -385,6 +408,16 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
|
|||||||
if (OpenLayers.Util.indexOf(this.selectedFeatures, feature) != -1){
|
if (OpenLayers.Util.indexOf(this.selectedFeatures, feature) != -1){
|
||||||
OpenLayers.Util.removeItem(this.selectedFeatures, feature);
|
OpenLayers.Util.removeItem(this.selectedFeatures, feature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent("featureremoved", {
|
||||||
|
feature: feature
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent("featuresremoved", {features: features});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
+31
-2
@@ -67,7 +67,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_Layer_Vector_removeFeatures(t) {
|
function test_Layer_Vector_removeFeatures(t) {
|
||||||
t.plan(3);
|
t.plan(6);
|
||||||
|
|
||||||
var layer = new OpenLayers.Layer.Vector(name);
|
var layer = new OpenLayers.Layer.Vector(name);
|
||||||
|
|
||||||
@@ -88,7 +88,36 @@
|
|||||||
|
|
||||||
t.ok(layer.features.length == 0,
|
t.ok(layer.features.length == 0,
|
||||||
"OpenLayers.Layer.Vector.removeFeatures(layer.features) removes all feature from the features array");
|
"OpenLayers.Layer.Vector.removeFeatures(layer.features) removes all feature from the features array");
|
||||||
}
|
|
||||||
|
// 3 tests
|
||||||
|
layer.events.register('beforefeatureremoved', null, function(obj) {
|
||||||
|
t.ok(pointFeature1 == obj.feature,
|
||||||
|
"OpenLayers.Layer.Vector.removeFeatures triggers beforefeatureremoved with correct feature passed to callback");
|
||||||
|
});
|
||||||
|
layer.events.register('featureremoved', null, function(obj) {
|
||||||
|
t.ok(pointFeature1 == obj.feature,
|
||||||
|
"OpenLayers.Layer.Vector.removeFeatures triggers featureremoved with correct feature passed to callback");
|
||||||
|
});
|
||||||
|
layer.events.register('featuresremoved', null, function(obj) {
|
||||||
|
t.ok(pointFeature1 == obj.features[0],
|
||||||
|
"OpenLayers.Layer.Vector.removeFeatures triggers featuresremoved with correct features passed to callback");
|
||||||
|
});
|
||||||
|
layer.addFeatures([pointFeature1]);
|
||||||
|
layer.removeFeatures([pointFeature1]);
|
||||||
|
|
||||||
|
// 0 test
|
||||||
|
layer.events.register('beforefeatureremoved', null, function(obj) {
|
||||||
|
t.fail("OpenLayers.Layer.Vector.removeFeatures triggers beforefeatureremoved while it must not");
|
||||||
|
});
|
||||||
|
layer.events.register('featureremoved', null, function(obj) {
|
||||||
|
t.fail("OpenLayers.Layer.Vector.removeFeatures triggers featureremoved while it must not");
|
||||||
|
});
|
||||||
|
layer.events.register('featuresremoved', null, function(obj) {
|
||||||
|
t.fail("OpenLayers.Layer.Vector.removeFeatures triggers featuresremoved while it must not");
|
||||||
|
});
|
||||||
|
layer.addFeatures([pointFeature1]);
|
||||||
|
layer.removeFeatures([pointFeature1], {silent: true});
|
||||||
|
}
|
||||||
|
|
||||||
function test_Layer_Vector_drawFeature(t) {
|
function test_Layer_Vector_drawFeature(t) {
|
||||||
t.plan(4);
|
t.plan(4);
|
||||||
|
|||||||
Reference in New Issue
Block a user