From 35a5fdb0e5c781c760a8a6aeafb0c8d1b9a87161 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Mon, 24 Jun 2013 10:18:01 -0400 Subject: [PATCH] simplify control layers events code #1634 --- src/control/Control.Layers.js | 41 +++++++++++++---------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/control/Control.Layers.js b/src/control/Control.Layers.js index ee29793c..d2d3245f 100644 --- a/src/control/Control.Layers.js +++ b/src/control/Control.Layers.js @@ -147,18 +147,20 @@ L.Control.Layers = L.Control.extend({ }, _onLayerChange: function (e) { - var id = L.stamp(e.layer); - if (this._layers[id] && !this._handlingClick) { + var obj = this._layers[L.stamp(e.layer)]; + + if (!obj) { return; } + + if (!this._handlingClick) { this._update(); - var overlayEvent; - if (this._layers[id].overlay) { - overlayEvent = (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove'); - } else { - overlayEvent = (e.type === 'layeradd' ? 'baselayerchange' : null); - } - if (overlayEvent) { - this._map.fire(overlayEvent, {layer: this._layers[id]}); - } + } + + var type = obj.overlay ? + (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove') : + (e.type === 'layeradd' ? 'baselayerchange' : null); + + if (type) { + this._map.fire(type, obj); } }, @@ -210,8 +212,7 @@ L.Control.Layers = L.Control.extend({ _onInputClick: function () { var i, input, obj, inputs = this._form.getElementsByTagName('input'), - inputsLen = inputs.length, - baseLayer; + inputsLen = inputs.length; this._handlingClick = true; @@ -221,24 +222,12 @@ L.Control.Layers = L.Control.extend({ if (input.checked && !this._map.hasLayer(obj.layer)) { this._map.addLayer(obj.layer); - if (!obj.overlay) { - baseLayer = obj; - } else { - this._map.fire('overlayadd', {layer: obj}); - } + } else if (!input.checked && this._map.hasLayer(obj.layer)) { this._map.removeLayer(obj.layer); - if (obj.overlay) { - this._map.fire('overlayremove', {layer: obj}); - } } } - if (baseLayer) { - this._map.setZoom(this._map.getZoom()); - this._map.fire('baselayerchange', {layer: baseLayer}); - } - this._handlingClick = false; },