Skip to content

Commit

Permalink
addLayer - works like setLayers, but doesnt unselect all other layers…
Browse files Browse the repository at this point in the history
… so that selectFeature still will be working for the old layers

This depends on pull request: tomcollins/openlayers@0a50181 otherwise you get problem described by:#958
  • Loading branch information
emoen committed Mar 27, 2014
1 parent 8ddafa5 commit 6add5bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/OpenLayers/Control/SelectFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,33 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
this.activate();
}
},

/**
* APIMethod: addLayer
* Add a layer to the control, making the existing layers still selectable
*
* Parameters:
* layer - element <OpenLayers.Layer.Vector>
*/
addLayer: function( alayer ) {
var isActive = this.active;
this.deactivate();
if (this.layers == null) {
if (this.layer != null) {
this.layers = [this.layer];
this.layers.push(alayer);
} else {
this.layers = [alayer];
}
} else {
this.layers.push(alayer);
}
this.initLayer(this.layers);
this.handlers.feature.layer = this.layer;
if (isActive) {
this.activate();
}
},

CLASS_NAME: "OpenLayers.Control.SelectFeature"
});
20 changes: 20 additions & 0 deletions tests/Control/SelectFeature.html
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,26 @@
t.eq(layer.selectedFeatures[0].id, 3,
'the remaining selected features is the one expected');
}

function test_addLayer(t) {
t.plan(4);

var layer = new OpenLayers.Layer.Vector();
var layers = [new OpenLayers.Layer.Vector(), new OpenLayers.Layer.Vector()]
var layerToAdd = new OpenLayers.Layer.Vector();

var controlWithLayer = new OpenLayers.Control.SelectFeature(layer);
var controlWithLayers = new OpenLayers.Control.SelectFeature(layers);

controlWithLayer.addLayer(layerToAdd);
controlWithLayers.addLayer(layerToAdd);

t.eq(controlWithLayer.layers.length, 2, "the added layer is added and all layers are active");
t.eq(controlWithLayers.layers.length, 3, "the added layer is added and all layers are active");

t.eq(controlWithLayer.layer.length, null, "layer is empty");
t.eq(controlWithLayers.layer.length, null, "layer is empty");
}

</script>
</head>
Expand Down

0 comments on commit 6add5bc

Please sign in to comment.