/* * L.Control.Layers is a control to allow users to switch between different layers on the map. */ L.Control.Layers = L.Control.extend({ options: { collapsed: true, position: 'topright', autoZIndex: true }, initialize: function (baseLayers, overlays, options) { L.setOptions(this, options); this._layers = {}; this._lastZIndex = 0; this._handlingClick = false; for (var i in baseLayers) { this._addLayer(baseLayers[i], i); } for (i in overlays) { this._addLayer(overlays[i], i, true); } }, onAdd: function (map) { this._initLayout(); this._update(); map .on('layeradd', this._onLayerChange, this) .on('layerremove', this._onLayerChange, this); return this._container; }, onRemove: function (map) { map .off('layeradd', this._onLayerChange) .off('layerremove', this._onLayerChange); }, addBaseLayer: function (layer, name) { this._addLayer(layer, name); this._update(); return this; }, addOverlay: function (layer, name) { this._addLayer(layer, name, true); this._update(); return this; }, removeLayer: function (layer) { var id = L.stamp(layer); delete this._layers[id]; this._update(); return this; }, _initLayout: function () { var className = 'leaflet-control-layers', container = this._container = L.DomUtil.create('div', className); //Makes this work on IE10 Touch devices by stopping it from firing a mouseout event when the touch is released container.setAttribute('aria-haspopup', true); if (!L.Browser.touch) { L.DomEvent .disableClickPropagation(container) .disableScrollPropagation(container); } else { L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation); } var form = this._form = L.DomUtil.create('form', className + '-list'); if (this.options.collapsed) { if (!L.Browser.android) { L.DomEvent .on(container, 'mouseenter', this._expand, this) .on(container, 'mouseleave', this._collapse, this); } var link = this._layersLink = L.DomUtil.create('a', className + '-toggle', container); link.href = '#'; link.title = 'Layers'; if (L.Browser.touch) { L.DomEvent .on(link, 'click', L.DomEvent.stop) .on(link, 'click', this._expand, this); } else { L.DomEvent.on(link, 'focus', this._expand, this); } //Work around for Firefox android issue https://github.com/Leaflet/Leaflet/issues/2033 L.DomEvent.on(form, 'click', function () { setTimeout(L.bind(this._onInputClick, this), 0); }, this); this._map.on('click', this._collapse, this); // TODO keyboard accessibility } else { this._expand(); } this._baseLayersList = L.DomUtil.create('div', className + '-base', form); this._separator = L.DomUtil.create('div', className + '-separator', form); this._overlaysList = L.DomUtil.create('div', className + '-overlays', form); container.appendChild(form); }, _addLayer: function (layer, name, overlay) { var id = L.stamp(layer); this._layers[id] = { layer: layer, name: name, overlay: overlay }; if (this.options.autoZIndex && layer.setZIndex) { this._lastZIndex++; layer.setZIndex(this._lastZIndex); } }, _update: function () { if (!this._container) { return; } this._baseLayersList.innerHTML = ''; this._overlaysList.innerHTML = ''; var baseLayersPresent = false, overlaysPresent = false, i, obj; for (i in this._layers) { obj = this._layers[i]; this._addItem(obj); overlaysPresent = overlaysPresent || obj.overlay; baseLayersPresent = baseLayersPresent || !obj.overlay; } this._separator.style.display = overlaysPresent && baseLayersPresent ? '' : 'none'; }, _onLayerChange: function (e) { var obj = this._layers[L.stamp(e.layer)]; if (!obj) { return; } if (!this._handlingClick) { this._update(); } var type = obj.overlay ? (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove') : (e.type === 'layeradd' ? 'baselayerchange' : null); if (type) { this._map.fire(type, obj); } }, // IE7 bugs out if you create a radio dynamically, so you have to do it this hacky way (see http://bit.ly/PqYLBe) _createRadioElement: function (name, checked) { var radioHtml = '