simplify control layers events code #1634

This commit is contained in:
Vladimir Agafonkin 2013-06-24 10:18:01 -04:00
parent 4b0ef3917e
commit 35a5fdb0e5

View File

@ -147,18 +147,20 @@ L.Control.Layers = L.Control.extend({
}, },
_onLayerChange: function (e) { _onLayerChange: function (e) {
var id = L.stamp(e.layer); var obj = this._layers[L.stamp(e.layer)];
if (this._layers[id] && !this._handlingClick) {
if (!obj) { return; }
if (!this._handlingClick) {
this._update(); this._update();
var overlayEvent; }
if (this._layers[id].overlay) {
overlayEvent = (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove'); var type = obj.overlay ?
} else { (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove') :
overlayEvent = (e.type === 'layeradd' ? 'baselayerchange' : null); (e.type === 'layeradd' ? 'baselayerchange' : null);
}
if (overlayEvent) { if (type) {
this._map.fire(overlayEvent, {layer: this._layers[id]}); this._map.fire(type, obj);
}
} }
}, },
@ -210,8 +212,7 @@ L.Control.Layers = L.Control.extend({
_onInputClick: function () { _onInputClick: function () {
var i, input, obj, var i, input, obj,
inputs = this._form.getElementsByTagName('input'), inputs = this._form.getElementsByTagName('input'),
inputsLen = inputs.length, inputsLen = inputs.length;
baseLayer;
this._handlingClick = true; this._handlingClick = true;
@ -221,24 +222,12 @@ L.Control.Layers = L.Control.extend({
if (input.checked && !this._map.hasLayer(obj.layer)) { if (input.checked && !this._map.hasLayer(obj.layer)) {
this._map.addLayer(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)) { } else if (!input.checked && this._map.hasLayer(obj.layer)) {
this._map.removeLayer(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; this._handlingClick = false;
}, },