6 lines
2.6 KiB
JavaScript
6 lines
2.6 KiB
JavaScript
/*!
|
|
SelectLayersControl - v0.2.2 - 2014-10-10
|
|
https://github.com/vogdb/SelectLayersControl.git
|
|
Copyright (c) 2014 Sanin Aleksey aka vogdb; Licensed MIT
|
|
*/
|
|
L.Control.SelectLayers=L.Control.ActiveLayers.extend({_initLayout:function(){var a="leaflet-control-layers",b=this._container=L.DomUtil.create("div",a);L.DomEvent.disableClickPropagation(b),L.Browser.touch?L.DomEvent.on(b,"click",L.DomEvent.stopPropagation):L.DomEvent.on(b,"mousewheel",L.DomEvent.stopPropagation);var c=this._form=L.DomUtil.create("form",a+"-list");if(this.options.collapsed){var d=this._layersLink=L.DomUtil.create("a",a+"-toggle",b);d.href="#",d.title="Layers",L.Browser.touch?L.DomEvent.on(d,"click",L.DomEvent.stopPropagation).on(d,"click",L.DomEvent.preventDefault).on(d,"click",this._expand,this):(L.DomEvent.on(b,"mouseover",this._expand,this).on(b,"mouseout",this._collapse,this),L.DomEvent.on(d,"focus",this._expand,this)),this._map.on("movestart",this._collapse,this)}else this._expand();this._baseLayersList=L.DomUtil.create("select",a+"-base",c),L.DomEvent.on(this._baseLayersList,"change",this._onBaseLayerOptionChange,this),this._separator=L.DomUtil.create("div",a+"-separator",c),this._overlaysList=L.DomUtil.create("select",a+"-overlays",c),this._overlaysList.setAttribute("multiple",!0),this._overlaysList.style.width="100%",L.DomEvent.on(this._overlaysList,"change",this._onOverlayLayerOptionChange,this),b.appendChild(c)},_onBaseLayerOptionChange:function(){var a=this._baseLayersList.selectedIndex,b=this._baseLayersList.options[a],c=this._layers[b.layerId];this._changeBaseLayer(c)},_changeBaseLayer:function(a){this._handlingClick=!0,this._map.addLayer(a.layer),this._map.removeLayer(this._activeBaseLayer.layer),this._map.setZoom(this._map.getZoom()),this._map.fire("baselayerchange",{layer:a.layer}),this._activeBaseLayer=a,this._handlingClick=!1},_onOverlayLayerOptionChange:function(){this._handlingClick=!0;for(var a=this._overlaysList.options,b=0;b<a.length;b++){var c=a[b],d=this._layers[c.layerId].layer;c.selected?this._map.hasLayer(d)||this._map.addLayer(d):this._map.hasLayer(d)&&this._map.removeLayer(d)}this._handlingClick=!1},_addItem:function(a){var b=this._createOptionElement(a);a.overlay?this._overlaysList.appendChild(b):this._baseLayersList.appendChild(b)},_createOptionElement:function(a){var b=document.createElement("option");return b.layerId=L.stamp(a.layer),b.innerHTML=a.name,this._map.hasLayer(a.layer)&&b.setAttribute("selected",!0),b},_collapse:function(a){a.target===this._container&&L.Control.Layers.prototype._collapse.apply(this,arguments)}}),L.control.selectLayers=function(a,b,c){return new L.Control.SelectLayers(a,b,c)}; |