better backwards compatibility -- only create the root container if
Control.SelectFeature constructor was called with an array of layers. r=crschmidt (closes #1666) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9275 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -123,13 +123,16 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
/**
|
/**
|
||||||
* Property: layer
|
* Property: layer
|
||||||
* {<OpenLayers.Layer.Vector>} The vector layer with a common renderer
|
* {<OpenLayers.Layer.Vector>} The vector layer with a common renderer
|
||||||
* root for all layers this control is configured with.
|
* root for all layers this control is configured with (if an array of
|
||||||
|
* layers was passed to the constructor), or the vector layer the control
|
||||||
|
* was configured with (if a single layer was passed to the constructor).
|
||||||
*/
|
*/
|
||||||
layer: null,
|
layer: null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Property: layers
|
* Property: layers
|
||||||
* {Array(<OpenLayers.Layer.Vector>} The layers this control will work on.
|
* {Array(<OpenLayers.Layer.Vector>} The layers this control will work on,
|
||||||
|
* or null if the control was configured with a single layer
|
||||||
*/
|
*/
|
||||||
layers: null,
|
layers: null,
|
||||||
|
|
||||||
@@ -164,7 +167,8 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* Create a new control for selecting features.
|
* Create a new control for selecting features.
|
||||||
*
|
*
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers
|
* layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers. The
|
||||||
|
* layer(s) this control will select features from.
|
||||||
* options - {Object}
|
* options - {Object}
|
||||||
*/
|
*/
|
||||||
initialize: function(layers, options) {
|
initialize: function(layers, options) {
|
||||||
@@ -174,15 +178,16 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
OpenLayers.Control.prototype.EVENT_TYPES
|
OpenLayers.Control.prototype.EVENT_TYPES
|
||||||
);
|
);
|
||||||
OpenLayers.Control.prototype.initialize.apply(this, [options]);
|
OpenLayers.Control.prototype.initialize.apply(this, [options]);
|
||||||
if(!(layers instanceof Array)) {
|
if(layers instanceof Array) {
|
||||||
layers = [layers];
|
|
||||||
}
|
|
||||||
this.layers = layers;
|
this.layers = layers;
|
||||||
this.layer = new OpenLayers.Layer.Vector.RootContainer(
|
this.layer = new OpenLayers.Layer.Vector.RootContainer(
|
||||||
this.id + "_container", {
|
this.id + "_container", {
|
||||||
layers: layers
|
layers: layers
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
this.layer = layers;
|
||||||
|
}
|
||||||
var callbacks = {
|
var callbacks = {
|
||||||
click: this.clickFeature,
|
click: this.clickFeature,
|
||||||
clickout: this.clickoutFeature
|
clickout: this.clickoutFeature
|
||||||
@@ -213,7 +218,9 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
*/
|
*/
|
||||||
destroy: function() {
|
destroy: function() {
|
||||||
OpenLayers.Control.prototype.destroy.apply(this, arguments);
|
OpenLayers.Control.prototype.destroy.apply(this, arguments);
|
||||||
|
if(this.layers) {
|
||||||
this.layer.destroy();
|
this.layer.destroy();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -225,7 +232,9 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
*/
|
*/
|
||||||
activate: function () {
|
activate: function () {
|
||||||
if (!this.active) {
|
if (!this.active) {
|
||||||
|
if(this.layers) {
|
||||||
this.map.addLayer(this.layer);
|
this.map.addLayer(this.layer);
|
||||||
|
}
|
||||||
this.handlers.feature.activate();
|
this.handlers.feature.activate();
|
||||||
if(this.box && this.handlers.box) {
|
if(this.box && this.handlers.box) {
|
||||||
this.handlers.box.activate();
|
this.handlers.box.activate();
|
||||||
@@ -249,8 +258,10 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
if(this.handlers.box) {
|
if(this.handlers.box) {
|
||||||
this.handlers.box.deactivate();
|
this.handlers.box.deactivate();
|
||||||
}
|
}
|
||||||
|
if(this.layers) {
|
||||||
this.map.removeLayer(this.layer);
|
this.map.removeLayer(this.layer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return OpenLayers.Control.prototype.deactivate.apply(
|
return OpenLayers.Control.prototype.deactivate.apply(
|
||||||
this, arguments
|
this, arguments
|
||||||
);
|
);
|
||||||
@@ -266,9 +277,10 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
*/
|
*/
|
||||||
unselectAll: function(options) {
|
unselectAll: function(options) {
|
||||||
// we'll want an option to supress notification here
|
// we'll want an option to supress notification here
|
||||||
|
var layers = this.layers || [this.layer];
|
||||||
var layer, feature;
|
var layer, feature;
|
||||||
for(var l=0; l<this.layers.length; ++l) {
|
for(var l=0; l<layers.length; ++l) {
|
||||||
layer = this.layers[l];
|
layer = layers[l];
|
||||||
for(var i=layer.selectedFeatures.length-1; i>=0; --i) {
|
for(var i=layer.selectedFeatures.length-1; i>=0; --i) {
|
||||||
feature = layer.selectedFeatures[i];
|
feature = layer.selectedFeatures[i];
|
||||||
if(!options || options.except != feature) {
|
if(!options || options.except != feature) {
|
||||||
@@ -455,7 +467,6 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
});
|
});
|
||||||
if(cont !== false) {
|
if(cont !== false) {
|
||||||
layer.selectedFeatures.push(feature);
|
layer.selectedFeatures.push(feature);
|
||||||
this.layerData = {};
|
|
||||||
this.highlight(feature);
|
this.highlight(feature);
|
||||||
layer.events.triggerEvent("featureselected", {feature: feature});
|
layer.events.triggerEvent("featureselected", {feature: feature});
|
||||||
this.onSelect.call(this.scope, feature);
|
this.onSelect.call(this.scope, feature);
|
||||||
@@ -508,9 +519,10 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
// because we're using a box, we consider we want multiple selection
|
// because we're using a box, we consider we want multiple selection
|
||||||
var prevMultiple = this.multiple;
|
var prevMultiple = this.multiple;
|
||||||
this.multiple = true;
|
this.multiple = true;
|
||||||
|
var layers = this.layers || [this.layer];
|
||||||
var layer;
|
var layer;
|
||||||
for(var l=0; l<this.layers.length; ++l) {
|
for(var l=0; l<layers.length; ++l) {
|
||||||
layer = this.layers[l];
|
layer = layers[l];
|
||||||
for(var i=0, len = layer.features.length; i<len; ++i) {
|
for(var i=0, len = layer.features.length; i<len; ++i) {
|
||||||
var feature = layer.features[i];
|
var feature = layer.features[i];
|
||||||
if (this.geometryTypes == null || OpenLayers.Util.indexOf(
|
if (this.geometryTypes == null || OpenLayers.Util.indexOf(
|
||||||
|
|||||||
@@ -3,19 +3,23 @@
|
|||||||
<script src="../../lib/OpenLayers.js"></script>
|
<script src="../../lib/OpenLayers.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function test_Control_SelectFeature_constructor(t) {
|
function test_Control_SelectFeature_constructor(t) {
|
||||||
t.plan(3);
|
t.plan(5);
|
||||||
var options = {
|
var options = {
|
||||||
// geometryTypes: "foo"
|
// geometryTypes: "foo"
|
||||||
};
|
};
|
||||||
var layer = "bar";
|
var layer = "bar";
|
||||||
var control = new OpenLayers.Control.SelectFeature(layer, options);
|
var control = new OpenLayers.Control.SelectFeature([layer], options);
|
||||||
t.ok(control instanceof OpenLayers.Control.SelectFeature,
|
t.ok(control instanceof OpenLayers.Control.SelectFeature,
|
||||||
"new OpenLayers.Control.SelectFeature returns an instance");
|
"new OpenLayers.Control.SelectFeature returns an instance");
|
||||||
t.eq(control.layers[0], "bar",
|
t.eq(control.layers[0], "bar",
|
||||||
"constructor sets layer correctly");
|
"constructor with array of layers sets layer correctly");
|
||||||
// t.eq(control.handlers.feature.geometryTypes, "foo",
|
// t.eq(control.handlers.feature.geometryTypes, "foo",
|
||||||
// "constructor sets options correctly on feature handler");
|
// "constructor sets options correctly on feature handler");
|
||||||
t.ok(control.layer instanceof OpenLayers.Layer.Vector.RootContainer, "control has a root container layer");
|
t.ok(control.layer instanceof OpenLayers.Layer.Vector.RootContainer, "control has a root container layer if constructor was called with an array of layers");
|
||||||
|
|
||||||
|
control = new OpenLayers.Control.SelectFeature(layer, options);
|
||||||
|
t.eq(control.layers, null, "this.layers is null if constructor called with a single layer");
|
||||||
|
t.eq(control.layer, layer, "this.layer holds the layer that was passed with the constructor if called with a single layer")
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_Control_SelectFeature_destroy(t) {
|
function test_Control_SelectFeature_destroy(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user