add setLayer function to SelectFeature control so that we can change the layer attached to the control on the fly, r=ahocevar (closes #2340)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9789 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
bartvde
2009-11-08 16:58:33 +00:00
parent 5fd5085eba
commit e77a804559
2 changed files with 100 additions and 41 deletions

View File

@@ -182,16 +182,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
if(this.scope === null) {
this.scope = this;
}
if(layers instanceof Array) {
this.layers = layers;
this.layer = new OpenLayers.Layer.Vector.RootContainer(
this.id + "_container", {
layers: layers
}
);
} else {
this.layer = layers;
}
this.initLayer(layers);
var callbacks = {
click: this.clickFeature,
clickout: this.clickoutFeature
@@ -216,6 +207,27 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
);
}
},
/**
* Method: initLayer
* Assign the layer property. If layers is an array, we need to use
* a RootContainer.
*
* Parameters:
* layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers.
*/
initLayer: function(layers) {
if(layers instanceof Array) {
this.layers = layers;
this.layer = new OpenLayers.Layer.Vector.RootContainer(
this.id + "_container", {
layers: layers
}
);
} else {
this.layer = layers;
}
},
/**
* Method: destroy
@@ -558,5 +570,28 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
OpenLayers.Control.prototype.setMap.apply(this, arguments);
},
/**
* Method: setLayer
* Attach a new layer to the control, overriding any existing layers.
*
* Parameters:
* layers - Array of {<OpenLayers.Layer.Vector>} or a single
* {<OpenLayers.Layer.Vector>}
*/
setLayer: function(layers) {
var isActive = this.active;
this.unselectAll();
this.deactivate();
if(this.layers) {
this.layer.destroy();
this.layers = null;
}
this.initLayer(layers);
this.handlers.feature.layer = this.layer;
if (isActive) {
this.activate();
}
},
CLASS_NAME: "OpenLayers.Control.SelectFeature"
});