/*! @preserve
 * Leaflet Panel Layers v1.3.1 - 2022-11-18
 *
 * Copyright 2022 Stefano Cudini
 * stefano.cudini@gmail.com
 * https://opengeo.tech/
 *
 * Licensed under the MIT license.
 *
 * Demos:
 * https://opengeo.tech/maps/leaflet-panel-layers/
 *
 * Source:
 * git@github.com:stefanocudini/leaflet-panel-layers.git
 */
!function(e){if("function"==typeof define&&define.amd)define(["leaflet"],e);else if("undefined"!=typeof module)module.exports=e(require("leaflet"));else{if(void 0===window.L)throw"Leaflet must be loaded first";e(window.L)}}(function(l){return l.Control.PanelLayers=l.Control.Layers.extend({includes:"1"===l.version[0]?l.Evented.prototype:l.Mixin.Events,options:{compact:!1,compactOffset:0,collapsed:!1,autoZIndex:!0,collapsibleGroups:!1,selectorGroup:!1,buildItem:null,title:"",className:"",position:"topright"},initialize:function(e,t,a){var i,s,r;for(i in l.setOptions(this,a),this._layers=[],this._groups={},this._items={},this._layersActives=[],this._lastZIndex=0,this._handlingClick=!1,this.className="leaflet-panel-layers",e)if(e[i].group&&e[i].layers)for(s in r=e[i].collapsed||!1,e[i].layers)this._addLayer(e[i].layers[s],!1,e[i].group,r);else this._addLayer(e[i],!1);for(i in t)if(t[i].group&&t[i].layers)for(s in r=t[i].collapsed||!1,t[i].layers)this._addLayer(t[i].layers[s],!0,t[i].group,r);else this._addLayer(t[i],!0)},onAdd:function(e){var t,a=this;for(t in this._layersActives)e.addLayer(this._layersActives[t]);return l.Control.Layers.prototype.onAdd.call(this,e),this._map.on("resize",function(e){a._updateHeight(e.newSize.y)}),this._onInputClick(),this._container},addBaseLayer:function(e,t,a){return e.name=t||e.name||"",this._addLayer(e,!1,a),this._update(),this},addOverlay:function(e,t,a){return e.name=t||e.name||"",this._addLayer(e,!0,a),this._update(),this},removeLayer:function(e){e=e.hasOwnProperty("layer")?this._layerFromDef(e):e;return this._map.removeLayer(e),l.Control.Layers.prototype.removeLayer.call(this,e),this},clearLayers:function(){for(var e=0;e<this._layers.length;e++)this.removeLayer(this._layers[e])},_layerFromDef:function(e){for(var t=0;t<this._layers.length;t++){var a=l.stamp(this._layers[t].layer);if(this._getLayer(a).name===e.name)return this._getLayer(a).layer}},_update:function(){this._groups={},this._items={},l.Control.Layers.prototype._update.call(this)},_getLayer:function(e){for(var t=0;t<this._layers.length;t++)if(this._layers[t]&&this._layers[t].id==e)return this._layers[t]},_addLayer:function(e,t,a,i){if(!e.layer)throw new Error("layer not defined in item: "+(e.name||""));e.layer instanceof l.Class||!e.layer.type||!e.layer.args||(e.layer=this._getPath(l,e.layer.type).apply(l,e.layer.args)),e.hasOwnProperty("id")||(e.id=l.stamp(e.layer)),e.active&&this._layersActives.push(e.layer),this._layers.push(l.Util.extend(e,{collapsed:i,overlay:t,group:a})),this.options.autoZIndex&&e.layer&&e.layer.setZIndex&&(this._lastZIndex++,e.layer.setZIndex(this._lastZIndex))},_createItem:function(e){var t,a,i=this,s=l.DomUtil.create("div",this.className+"-item"+(e.active?" active":"")),r=this._map.hasLayer(e.layer),r=(e.overlay?void 0!==e.exclusiveGroup?t=this._createRadioElement(e.exclusiveGroup.replace("","_"),r,e):((t=l.DomUtil.create("input",this.className+"-selector")).type="checkbox",t.defaultChecked=r):t=this._createRadioElement("leaflet-base-layers",r,e),t.value=e.id,t.layerId=e.id,t.id=e.id,t._layer=e,l.DomEvent.on(t,"click",function(e){i._onInputClick(),e.target.checked?i.fire("panel:selected",e.target._layer):i.fire("panel:unselected",e.target._layer)},this),l.DomUtil.create("label",this.className+"-title")),o=l.DomUtil.create("span");return o.innerHTML=e.name||"",e.icon&&(a=l.DomUtil.create("i",this.className+"-icon"),"string"==typeof e.icon?a.innerHTML=e.icon||"":a.appendChild(e.icon),r.appendChild(a)),r.appendChild(t),r.appendChild(o),s.appendChild(r),this.options.buildItem&&("string"==typeof(a=this.options.buildItem.call(this,e))?((o=l.DomUtil.create("div")).innerHTML=a,s.appendChild(o.firstChild)):s.appendChild(a)),this._items[t.value]=s},_createRadioElement:function(e,t,a){e='<input type="radio" class="'+this.className+'-selector" name="'+e+'" id="'+a.id+'"',t&&(e+=' checked="checked"'),e+=" />",a=document.createElement("div");return a.innerHTML=e,a.firstChild},_addItem:function(e){var t,a=e.overlay?this._overlaysList:this._baseLayersList;return e.group&&(e.group.hasOwnProperty("name")||(e.group={name:e.group}),this._groups[e.group.name]||(!(t=!1)===e.collapsed&&(t=!0),this._groups[e.group.name]=this._createGroup(e.group,t,e.overlay)),a.appendChild(this._groups[e.group.name]),a=this._groups[e.group.name]),t=this._createItem(e),a.appendChild(t),t},_createGroup:function(e,t,a){var i,s=this,r=l.DomUtil.create("div",this.className+"-group"),o=l.DomUtil.create("label",this.className+"-grouplabel",r);return this.options.collapsibleGroups&&(l.DomUtil.addClass(r,"collapsible"),(i=l.DomUtil.create("i",this.className+"-icon",o)).innerHTML=!0===t?" + ":" - ",l.DomEvent.on(o,"click",function(e){e.stopPropagation(),e.preventDefault(),l.DomUtil.hasClass(r,"expanded")?(l.DomUtil.removeClass(r,"expanded"),i.innerHTML=" + "):(l.DomUtil.addClass(r,"expanded"),i.innerHTML=" - "),s._updateHeight()}),!1===t)&&l.DomUtil.addClass(r,"expanded"),l.DomUtil.create("span",this.className+"-title",o).innerHTML=e.name,a&&this.options.selectorGroup&&((t=l.DomUtil.create("input",this.className+"-selector",o)).type="checkbox",t.value="group",t.name=e.name,t.title="select all",t.defaultChecked=!1,l.DomEvent.on(t,"click",this._onGroupClick,this)),r},_onGroupClick:function(e){var t,a,i,s,r=e.target,o=this._form.getElementsByClassName(this.className+"-selector"),n=o.length,l=[];for(e.stopPropagation(),t=0;t<n;t++)"group"!=(i=o[t]).value&&(s=this._getLayer(i.value)).group&&s.group.name===r.name&&i.checked!==r.checked&&(l.push(i),i.checked=r.checked);for(a in this._onInputClick(),l)this.fire(r.checked?"panel:selected":"panel:unselected",l[a]._layer)},_onInputClick:function(){var e,t,a,i,s,r=this._form.getElementsByClassName(this.className+"-selector"),o=r.length,n={};for(i in this._groups)n[i]={input:null,checked:0,total:0};for(this._handlingClick=!0,e=0;e<o;e++)"group"==(t=r[e]).value?n[t.name].input=t:((a=this._getLayer(t.value)).group&&(n[a.group.name].total++,t.checked)&&n[a.group.name].checked++,t.checked&&!this._map.hasLayer(a.layer)?(l.DomUtil.addClass(t.parentNode.parentNode,"active"),this._map.addLayer(a.layer)):!t.checked&&this._map.hasLayer(a.layer)&&(l.DomUtil.removeClass(t.parentNode.parentNode,"active"),this._map.removeLayer(a.layer)));for(i in this._handlingClick=!1,n)(s=n[i]).input&&(s.checked===s.total?(s.input.indeterminate=!1,s.input.checked=!0):0===s.checked?(s.input.indeterminate=!1,s.input.checked=!1):(s.input.indeterminate=!0,s.input.checked=.5<=s.checked/s.total));this._refocusOnMap()},_initLayout:function(){var e,t=this._container=l.DomUtil.create("div",this.className);this.options.compact&&l.DomUtil.addClass(t,"compact"),t.setAttribute("aria-haspopup",!0),l.DomEvent.disableClickPropagation(t).disableScrollPropagation(t),this.options.className&&l.DomUtil.addClass(t,this.options.className),this._section=this._form=l.DomUtil.create("form",this.className+"-list"),this._updateHeight(),this.options.collapsed?(l.Browser.android?l.DomEvent.on(t,"click",this._expand,this):l.DomEvent.on(t,"mouseenter",this._expand,this).on(t,"mouseleave",this._collapse,this),this._map.on("click",this._collapse,this)):this._expand(),this._baseLayersList=l.DomUtil.create("div",this.className+"-base",this._form),this._separator=l.DomUtil.create("div",this.className+"-separator",this._form),this._overlaysList=l.DomUtil.create("div",this.className+"-overlays",this._form),this.options.title&&((e=l.DomUtil.create("label",this.className+"-title")).innerHTML="<span>"+this.options.title+"</span>",t.appendChild(e)),t.appendChild(this._form)},_updateHeight:function(e){e=e||this._map.getSize().y,this.options.compact?this._form.style.maxHeight=e-this.options.compactOffset+"px":this._form.style.height=e+"px"},_expand:function(){l.DomUtil.addClass(this._container,"expanded")},_collapse:function(){this._container.className=this._container.className.replace("expanded","")},_getPath:function(e,t){var a=t.split("."),t=a.pop(),i=a.length,s=a[0],r=1;if(0<i)for(;(e=e[s])&&r<i;)s=a[r++];if(e)return e[t]}}),l.control.panelLayers=function(e,t,a){return new l.Control.PanelLayers(e,t,a)},l.Control.PanelLayers});