Update the Layers control when layers are added or removed
authorTom Hughes <tom@compton.nu>
Thu, 22 Nov 2012 12:27:12 +0000 (12:27 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 22 Nov 2012 12:35:49 +0000 (12:35 +0000)
Submitted upstream as https://github.com/CloudMade/Leaflet/pull/1169.

vendor/assets/leaflet/leaflet.js

index 92112044ef4b7fbeab1cb356fa319b486c73f879..5503592a0d2ec2bce818ab281060a2ac5bf79603 100644 (file)
@@ -7295,6 +7295,7 @@ L.Control.Layers = L.Control.extend({
 
                this._layers = {};
                this._lastZIndex = 0;
+               this._handlingClick = false;
 
                for (var i in baseLayers) {
                        if (baseLayers.hasOwnProperty(i)) {
@@ -7313,9 +7314,19 @@ L.Control.Layers = L.Control.extend({
                this._initLayout();
                this._update();
 
+               map
+                   .on('layeradd', this._update, this)
+                   .on('layerremove', this._update, this);
+
                return this._container;
        },
 
+       onRemove: function (map) {
+               map
+                   .off('layeradd', this._update)
+                   .off('layerremove', this._update);
+       },
+
        addBaseLayer: function (layer, name) {
                this._addLayer(layer, name);
                this._update();
@@ -7395,7 +7406,7 @@ L.Control.Layers = L.Control.extend({
        },
 
        _update: function () {
-               if (!this._container) {
+               if (!this._container || this._handlingClick) {
                        return;
                }
 
@@ -7466,6 +7477,8 @@ L.Control.Layers = L.Control.extend({
                    inputsLen = inputs.length,
                    baseLayer;
 
+               this._handlingClick = true;
+
                for (i = 0; i < inputsLen; i++) {
                        input = inputs[i];
                        obj = this._layers[input.layerId];
@@ -7483,6 +7496,8 @@ L.Control.Layers = L.Control.extend({
                if (baseLayer) {
                        this._map.fire('baselayerchange', {layer: baseLayer});
                }
+
+               this._handlingClick = false;
        },
 
        _expand: function () {
@@ -7990,4 +8005,4 @@ L.Map.include({
 
 
 
-}(this));
\ No newline at end of file
+}(this));