Adding a removeAllFeatures method to the vector layer. This bypasses a few unnecessary steps when removing all features but retains the same behavior as the removeFeatures method otherwise. Optimizing the clear method on elements renderers a bit by avoiding length calculation of the live collection and looking up the first child once per node removal. r=ahocevar (closes #2774)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10597 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -616,10 +616,13 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
|
|||||||
if(!features || features.length === 0) {
|
if(!features || features.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (features === this.features) {
|
||||||
|
return this.removeAllFeatures(options);
|
||||||
|
}
|
||||||
if (!(features instanceof Array)) {
|
if (!(features instanceof Array)) {
|
||||||
features = [features];
|
features = [features];
|
||||||
}
|
}
|
||||||
if (features === this.features || features === this.selectedFeatures) {
|
if (features === this.selectedFeatures) {
|
||||||
features = features.slice();
|
features = features.slice();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -680,6 +683,49 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
|
|||||||
this.events.triggerEvent("featuresremoved", {features: features});
|
this.events.triggerEvent("featuresremoved", {features: features});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIMethod: removeAllFeatures
|
||||||
|
* Remove all features from the layer.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* options - {Object} Optional properties for changing behavior of the
|
||||||
|
* removal.
|
||||||
|
*
|
||||||
|
* Valid options:
|
||||||
|
* silent - {Boolean} Supress event triggering. Default is false.
|
||||||
|
*/
|
||||||
|
removeAllFeatures: function(options) {
|
||||||
|
var notify = !options || !options.silent;
|
||||||
|
var features = this.features;
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent(
|
||||||
|
"beforefeaturesremoved", {features: features}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
var feature;
|
||||||
|
for (var i = features.length-1; i >= 0; i--) {
|
||||||
|
feature = features[i];
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent("beforefeatureremoved", {
|
||||||
|
feature: feature
|
||||||
|
});
|
||||||
|
}
|
||||||
|
feature.layer = null;
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent("featureremoved", {
|
||||||
|
feature: feature
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.renderer.clear();
|
||||||
|
this.features = [];
|
||||||
|
this.unrenderedFeatures = {};
|
||||||
|
this.selectedFeatures = [];
|
||||||
|
if (notify) {
|
||||||
|
this.events.triggerEvent("featuresremoved", {features: features});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* APIMethod: destroyFeatures
|
* APIMethod: destroyFeatures
|
||||||
|
|||||||
@@ -442,14 +442,17 @@ OpenLayers.Renderer.Elements = OpenLayers.Class(OpenLayers.Renderer, {
|
|||||||
* Remove all the elements from the root
|
* Remove all the elements from the root
|
||||||
*/
|
*/
|
||||||
clear: function() {
|
clear: function() {
|
||||||
if (this.vectorRoot) {
|
var child;
|
||||||
while (this.vectorRoot.childNodes.length > 0) {
|
var root = this.vectorRoot;
|
||||||
this.vectorRoot.removeChild(this.vectorRoot.firstChild);
|
if (root) {
|
||||||
|
while (child = root.firstChild) {
|
||||||
|
root.removeChild(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.textRoot) {
|
root = this.textRoot;
|
||||||
while (this.textRoot.childNodes.length > 0) {
|
if (root) {
|
||||||
this.textRoot.removeChild(this.textRoot.firstChild);
|
while (child = root.firstChild) {
|
||||||
|
root.removeChild(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.indexer) {
|
if (this.indexer) {
|
||||||
|
|||||||
Reference in New Issue
Block a user