leaflet-customlayer
Version:
Leaflet overlay plugin: L.CustomLayer - fully custom Layer.
11 lines (10 loc) • 6.09 kB
JavaScript
/*!
* Leaflet.CustomLayer.js v2.1.0
*
* Copyright (c) 2019-present Derek Li
* Released under the MIT License - https://choosealicense.com/licenses/mit/
*
* https://github.com/iDerekLi/Leaflet.CustomLayer
*/
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports,require("leaflet")):"function"==typeof define&&define.amd?define(["exports","leaflet"],i):i(((t=t||self).Leaflet=t.Leaflet||{},t.Leaflet.CustomLayer={}),t.L)}(this,function(t,i){"use strict";var e=(i=i&&i.hasOwnProperty("default")?i["default"]:i).Layer.extend({options:{padding:0,tolerance:0,container:null,opacity:1,visible:!0,zIndex:undefined,minZoom:0,maxZoom:18},initialize:function(t){i.setOptions(this,t),i.stamp(this),this._map=undefined,this._container=undefined,this._bounds=undefined,this._center=undefined,this._zoom=undefined,this._padding=undefined},beforeAdd:function(){this._zoomVisible=!0},getEvents:function(){var t={viewreset:this._onLayerViewReset,zoom:this._onLayerZoom,moveend:this._onLayerMoveEnd,zoomend:this._onLayerZoomEnd};return this._zoomAnimated&&(t.zoomanim=this._onLayerAnimZoom),t},onAdd:function(){if(this.fire("layer-beforemount"),this._container||this._initContainer(),this.setOpacity(this.options.opacity),window.isNaN(this.options.zIndex))switch(this._container.tagName){case"CANVAS":this.setZIndex(100);break;case"svg":this.setZIndex(200);break;default:this.setZIndex(100)}else this.setZIndex(this.options.zIndex);this.getPane().appendChild(this._container),this._onZoomVisible(),this.fire("layer-mounted"),this._update()},onRemove:function(){this.fire("layer-beforedestroy"),this._destroyContainer(),this.fire("layer-destroyed")},_onLayerViewReset:function(){this._reset()},_onLayerAnimZoom:function(t){this._updateTransform(t.center,t.zoom)},_onLayerZoom:function(){this._updateTransform(this._map.getCenter(),this._map.getZoom())},_onLayerZoomEnd:function(){},_onLayerMoveEnd:function(){this._isZoomVisible()?this._update():this._zoomHide()},_onZoomVisible:function(){this._isZoomVisible()?this._zoomShow():this._zoomHide()},_initContainer:function(){var t=this._container=this.options.container;i.DomUtil.addClass(t,"leaflet-layer"),this._zoomAnimated&&i.DomUtil.addClass(this._container,"leaflet-zoom-animated")},_destroyContainer:function(){i.DomUtil.remove(this._container),delete this._container},_isZoomVisible:function(){var t=this.options.minZoom,i=this.options.maxZoom,e=this._map.getZoom();return e>=t&&e<=i},_zoomShow:function(){this._zoomVisible||(this._zoomVisible=!0,this._map.off({zoomend:this._onZoomVisible},this),this.options.visible&&(this._map.on(this.getEvents(),this),this.getContainer().style.display=""))},_zoomHide:function(){this._zoomVisible&&(this._zoomVisible=!1,this._map.off(this.getEvents(),this),this._map.on({zoomend:this._onZoomVisible},this),this.getContainer().style.display="none")},_updateTransform:function(t,e){var n=this._map.getZoomScale(e,this._zoom),o=i.DomUtil.getPosition(this._container),s=this._map.getSize().multiplyBy(.5+this.options.padding),a=this._map.project(this._center,e),h=this._map.project(t,e).subtract(a),r=s.multiplyBy(-n).add(o).add(s).subtract(h);i.Browser.any3d?i.DomUtil.setTransform(this._container,r,n):i.DomUtil.setPosition(this._container,r)},_update:function(){if(!this._map._animatingZoom||!this._bounds){this.__update();var t=this._bounds,e=this._container;i.DomUtil.setPosition(e,t.min),this.fire("layer-render")}},__update:function(){var t=this.options.padding,e=this._map.getSize(),n=this._map.containerPointToLayerPoint(e.multiplyBy(-t));this._padding=e.multiplyBy(t),this._bounds=new i.Bounds(n,n.add(e.multiplyBy(1+2*t))),this._center=this._map.getCenter(),this._zoom=this._map.getZoom()},_reset:function(){this._update(),this._updateTransform(this._center,this._zoom)},getContainer:function(){return this._container},setContainer:function(t){var i=this.getContainer(),e=i.parentNode;if(delete this._container,this.options.container=t,this._container||this._initContainer(),this.setOpacity(this.options.opacity),window.isNaN(this.options.zIndex))switch(this._container.tagName){case"CANVAS":this.setZIndex(100);break;case"svg":this.setZIndex(200);break;default:this.setZIndex(100)}else this.setZIndex(this.options.zIndex);return e?e.replaceChild(t,i):this.getPane().appendChild(t),this._update(),this},getOpacity:function(){return this.options.opacity},setOpacity:function(t){return this.getContainer().style.opacity=this.options.opacity=1*t,this},getZIndex:function(){return this.options.zIndex},setZIndex:function(t){return this.getContainer().style.zIndex=this.options.zIndex=1*t,this},show:function(){if(!this.options.visible){if(this.options.visible=!0,this._isZoomVisible())return this._map.on(this.getEvents(),this),this.getContainer().style.display="",this._update(),this;this._zoomHide()}},hide:function(){if(this.options.visible)return this.options.visible=!1,this._zoomHide(),this._map.off(this.getEvents(),this),this.getContainer().style.display="none",this},setFullLayerBounds:function(){var t=this.getContainer(),e=this._bounds.getSize(),n=this._padding;switch(t.tagName){case"CANVAS":var o=i.Browser.retina?2:1;t.width=o*e.x,t.height=o*e.y,t.style.width=e.x+"px",t.style.height=e.y+"px";var s=t.getContext("2d");return i.Browser.retina&&s.scale(o,o),s.translate(n.x,n.y),{container:t,ctx:s,dpr:o};case"svg":return t.setAttribute("width",e.x),t.setAttribute("height",e.y),t.style.width=e.x+"px",t.style.height=e.y+"px",t.setAttribute("viewBox","".concat(-n.x," ").concat(-n.y," ").concat(e.x," ").concat(e.y)),{container:t};case"DIV":return t.style.boxSizing="content-box",t.style.width=e.x-n.x+"px",t.style.height=e.y-n.y+"px",t.style.padding="".concat(n.y,"px ").concat(n.x,"px"),{container:t};default:return t.setAttribute("width",e.x),t.setAttribute("height",e.y),t.style.width=e.x+"px",t.style.height=e.y+"px",{container:t}}}});function n(t){return i.customLayer?new e(t):null}i.CustomLayer=e,i.customLayer=n,t.CustomLayer=e,t.customLayer=n,t["default"]=n,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=Leaflet.CustomLayer.min.js.map