Control.Layers: Only add layer events to layers when we are on the map. Fixes #5421 (#5422)

This commit is contained in:
Dave Leaver 2017-03-29 10:10:29 +13:00 committed by Iván Sánchez Ortega
parent 96cfdd3a3d
commit 27263b7afa
2 changed files with 38 additions and 1 deletions

View File

@ -29,6 +29,26 @@ describe("Control.Layers", function () {
expect(spy.args[1][0].layer).to.be(baseLayers["Layer 2"]);
});
it("works after removing and readding the Control.Layers to the map", function () {
var baseLayers = {"Layer 1": L.tileLayer(''), "Layer 2": L.tileLayer('')},
layers = L.control.layers(baseLayers).addTo(map),
spy = sinon.spy();
map.on('baselayerchange', spy);
map.removeControl(layers);
map.addControl(layers);
happen.click(layers._baseLayersList.getElementsByTagName("input")[0]);
expect(spy.called).to.be.ok();
expect(spy.args[0][0].name).to.be("Layer 1");
expect(spy.args[0][0].layer).to.be(baseLayers["Layer 1"]);
happen.click(layers._baseLayersList.getElementsByTagName("input")[1]);
expect(spy.calledTwice).to.be.ok();
expect(spy.args[1][0].name).to.be("Layer 2");
expect(spy.args[1][0].layer).to.be(baseLayers["Layer 2"]);
});
it("is not fired on input that doesn't change the base layer", function () {
var overlays = {"Marker 1": L.marker([0, 0]), "Marker 2": L.marker([0, 0])},
layers = L.control.layers({}, overlays).addTo(map),
@ -126,6 +146,17 @@ describe("Control.Layers", function () {
map.removeLayer(baseLayer);
}).to.not.throwException();
});
it("and layers in the control can still be removed when added after removing control from map", function () {
var baseLayer = L.tileLayer('').addTo(map);
var layersCtrl = L.control.layers().addTo(map);
map.removeControl(layersCtrl);
layersCtrl.addBaseLayer(baseLayer, 'Base');
expect(function () {
map.removeLayer(baseLayer);
}).to.not.throwException();
});
});
describe("is created with an expand link", function () {

View File

@ -100,6 +100,10 @@ export var Layers = Control.extend({
this._map = map;
map.on('zoomend', this._checkDisabledLayers, this);
for (var i = 0; i < this._layers.length; i++) {
this._layers[i].layer.on('add remove', this._onLayerChange, this);
}
return this._container;
},
@ -229,7 +233,9 @@ export var Layers = Control.extend({
},
_addLayer: function (layer, name, overlay) {
if (this._map) {
layer.on('add remove', this._onLayerChange, this);
}
this._layers.push({
layer: layer,