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
@@ -217,6 +208,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"
});

View File

@@ -373,6 +373,30 @@
map.events.triggerEvent("click", evt);
}
function test_setLayer(t) {
t.plan(5);
var map = new OpenLayers.Map("map");
var layer1 = new OpenLayers.Layer.Vector();
var layer2 = new OpenLayers.Layer.Vector();
var layer3 = new OpenLayers.Layer.Vector();
map.addLayer(layer1, layer2, layer3);
// initialize it with a single layer
var control = new OpenLayers.Control.SelectFeature(layer1);
map.addControl(control);
control.activate();
control.setLayer([layer2, layer3]);
t.eq(control.layer instanceof OpenLayers.Layer.Vector.RootContainer, true, "Root container created correctly on setLayer with multiple layers");
t.eq(control.active, true, "Control should be active still after using setLayer");
t.eq((control.handlers.feature.layer === control.layer), true, "Feature handler layer set correctly");
control.destroy();
// initialize with an array of layers
control = new OpenLayers.Control.SelectFeature([layer1, layer2]);
t.eq((control.layers !== null), true, "Control has a layers property");
control.setLayer(layer3);
t.eq((control.layers === null), true, "When using setLayer with a single layer, the layers property is removed if present before");
map.destroy();
}
</script>
</head>
<body>