UNPKG

@openagenda/leaflet-gesture-handling

Version:

A Leaflet plugin that allows to prevent default map scroll/touch behaviours

3 lines (2 loc) 8.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["leaflet-gesture-handling"]={})}(this,(function(t){"use strict";var e={touch:"Use two fingers to move the map",scroll:"Use ctrl + scroll to zoom the map",scrollMac:"Use ⌘ + scroll to zoom the map"},n=!1,i={text:{},duration:1700,importLocale:t=>import(`./locales/${t}.js`)},o=L.Handler.extend({_isScrolling:!1,_isTouching:!1,_isFading:!1,addHooks:function(){this._handleTouch=L.bind(this._handleTouch,this),this._setGestureHandlingOptions(),this._disableInteractions(),L.version>="1.8"?(L.DomEvent.on(this._map._container,"touchstart",this._handleTouch,this),L.DomEvent.on(this._map._container,"touchmove",this._handleTouch,this),L.DomEvent.on(this._map._container,"touchend",this._handleTouch,this),L.DomEvent.on(this._map._container,"touchcancel",this._handleTouch,this),L.DomEvent.on(this._map._container,"click",this._handleTouch,this)):(this._map._container.addEventListener("touchstart",this._handleTouch),this._map._container.addEventListener("touchmove",this._handleTouch),this._map._container.addEventListener("touchend",this._handleTouch),this._map._container.addEventListener("touchcancel",this._handleTouch),this._map._container.addEventListener("click",this._handleTouch)),L.DomEvent.on(this._map._container,"wheel",this._handleScroll,this),L.DomEvent.on(this._map._container,"mouseenter",this._handleMouseOver,this),L.DomEvent.on(this._map._container,"mouseleave",this._handleMouseOut,this),L.DomEvent.on(this._map,"movestart",this._handleDragging,this),L.DomEvent.on(this._map,"move",this._handleDragging,this),L.DomEvent.on(this._map,"moveend",this._handleDragging,this),this._map.on("popupopen",this._handleScrollOnPopup,this),this._map.on("popupclose",this._handleScrollOnPopup,this),L.DomEvent.off(this._map,"enterFullscreen",this._onEnterFullscreen,this),L.DomEvent.off(this._map,"exitFullscreen",this._onExitFullscreen,this),L.DomEvent.on(this._map,"enterFullscreen",this._onEnterFullscreen,this),L.DomEvent.on(this._map,"exitFullscreen",this._onExitFullscreen,this),L.DomUtil.addClass(this._map._container,"leaflet-gesture-handling")},removeHooks:function(){this._enableInteractions(),L.version>="1.8"?(L.DomEvent.off(this._map._container,"touchstart",this._handleTouch,this),L.DomEvent.off(this._map._container,"touchmove",this._handleTouch,this),L.DomEvent.off(this._map._container,"touchend",this._handleTouch,this),L.DomEvent.off(this._map._container,"touchcancel",this._handleTouch,this),L.DomEvent.off(this._map._container,"click",this._handleTouch,this)):(this._map._container.removeEventListener("touchstart",this._handleTouch),this._map._container.removeEventListener("touchmove",this._handleTouch),this._map._container.removeEventListener("touchend",this._handleTouch),this._map._container.removeEventListener("touchcancel",this._handleTouch),this._map._container.removeEventListener("click",this._handleTouch)),L.DomEvent.off(this._map._container,"wheel",this._handleScroll,this),L.DomEvent.off(this._map._container,"mouseenter",this._handleMouseOver,this),L.DomEvent.off(this._map._container,"mouseleave",this._handleMouseOut,this),L.DomEvent.off(this._map,"movestart",this._handleDragging,this),L.DomEvent.off(this._map,"move",this._handleDragging,this),L.DomEvent.off(this._map,"moveend",this._handleDragging,this),this._map.off("popupopen",this._handleScrollOnPopup,this),this._map.off("popupclose",this._handleScrollOnPopup,this),L.DomUtil.removeClass(this._map._container,"leaflet-gesture-handling")},_handleDragging:function(t){"movestart"==t.type||"move"==t.type?n=!0:"moveend"==t.type&&(n=!1)},_disableInteraction:function(t){this._map.options[t]&&this._map[t]&&this._map[t].disable()},_enableInteraction:function(t){this._map.options[t]&&this._map[t]&&this._map[t].enable()},_disableInteractions:function(){this._disableInteraction("dragging"),this._disableInteraction("scrollWheelZoom"),this._disableInteraction("tap")},_enableInteractions:function(){this._enableInteraction("dragging"),this._enableInteraction("scrollWheelZoom"),this._enableInteraction("tap")},_enableWarning:function(t){clearTimeout(this._isFading),L.DomUtil.addClass(this._map._container,"leaflet-gesture-handling-"+t),L.DomUtil.addClass(this._map._container,"leaflet-gesture-handling-warning")},_disableWarning:function(t,e){clearTimeout(this._isFading),this._isFading=setTimeout(L.bind((function(t){L.DomUtil.removeClass(this._map._container,"leaflet-gesture-handling-"+t)}),this,t),e||this._map.options.gestureHandlingOptions.duration),L.DomUtil.removeClass(this._map._container,"leaflet-gesture-handling-warning")},_isLanguageContent:function(t){return t&&t.touch&&t.scroll&&t.scrollMac},_isMacUser:function(){return navigator.platform.toUpperCase().indexOf("MAC")>=0},_parseGestureHandlingOptions:function(){var t=this._map.options.gestureHandlingOptions.text||this._map.options.gestureHandlingText||i.text,e=this._map.options.gestureHandlingOptions.duration||this._map.options.gestureHandlingDuration||i.duration,n=this._map.options.gestureHandlingOptions.importLocale||i.importLocale,o=L.extend(this._map.options.gestureHandlingOptions,i);return o.text=t,o.duration=e,o.importLocale=n,o},_setGestureHandlingOptions:function(){var t=this._parseGestureHandlingOptions();(this._isLanguageContent(t.text)?Promise.resolve(t.text):this._getLanguageContent(t.locale,t.importLocale)).then((t=>{this._map._container.setAttribute("data-gesture-handling-touch-content",t.touch),this._map._container.setAttribute("data-gesture-handling-scroll-content",t.scroll),this._touchWarning=t.touch,this._scrollWarning=t.scroll}))},_getUserLanguage:function(){return navigator.languages?navigator.languages[0]:navigator.language||navigator.userLanguage},_getLanguageContent:function(t,n){t=t||this._getUserLanguage()||"en";var i,o=new Promise((t=>{i=t})),s=t=>{var e=t.default||t||{};e.scroll=this._isMacUser()?e.scrollMac:e.scroll,i(e)};return"function"==typeof n?Promise.resolve(n(t)).then(s).catch((()=>Promise.resolve(n(t.split("-")[0])).then(s).catch((()=>Promise.resolve(e).then(s))))):s(e),o},_handleTouch:function(t){L.DomUtil.hasClass(t.target,"leaflet-interactive")||t.target.closest(".leaflet-control-container")||t.target.closest(".leaflet-popup-pane")?L.DomUtil.hasClass(t.target,"leaflet-interactive")&&"touchmove"===t.type&&1===t.touches.length?this._enableTouchWarning():this._disableTouchWarning():"touchmove"!==t.type&&"touchstart"!==t.type?this._disableTouchWarning():1===t.touches.length?this._enableTouchWarning():(t.preventDefault(),this._disableTouchWarning(),this._enableInteractions())},_enableTouchWarning:function(){this._enableWarning("touch"),this._disableInteractions()},_disableTouchWarning:function(t){clearTimeout(this._isTouching),this._isTouching=setTimeout(L.bind((function(){this._disableWarning("touch")}),this),t||0)},_enableScrollWarning:function(){this._enableWarning("scroll"),this._disableInteraction("scrollWheelZoom")},_disableScrollWarning:function(t){clearTimeout(this._isScrolling),this._isScrolling=setTimeout(L.bind((function(){this._disableWarning("scroll"),this._enableInteraction("scrollWheelZoom")}),this),t||0)},_handleScroll:function(t){this._map.scrollWheelZoom&&this._map.scrollWheelZoom.enabled()&&(t.metaKey||t.ctrlKey||t.shiftKey&&this._map._rotate?(t.preventDefault(),this._disableScrollWarning()):(this._enableScrollWarning(),this._disableScrollWarning(this._map.options.gestureHandlingOptions.duration)))},_handleScrollOnPopup:function(t){L.DomEvent["popupopen"==t.type?"on":"off"](t.popup._contentNode,"wheel",this._handleScroll,this)},_handleMouseOver:function(t){this._enableInteractions()},_handleMouseOut:function(t){n||this._disableInteractions()},_onExitFullscreen:function(){this._map.options.gestureHandling&&this._map.gestureHandling.enable()},_onEnterFullscreen:function(){this._map.options.gestureHandling&&this._map.gestureHandling.disable()}});L.Map.mergeOptions({gestureHandlingOptions:i}),L.Map.addInitHook("addHandler","gestureHandling",o),t.GestureHandling=o})); //# sourceMappingURL=leaflet-gesture-handling.min.js.map