ol-contextmenu 5.1.4 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
- * ol-contextmenu - v5.1.4
3
+ * ol-contextmenu - v5.2.0
4
4
  * https://github.com/jonataswalker/ol-contextmenu
5
- * Built: Mon Dec 26 2022 21:16:02 GMT-0300 (Brasilia Standard Time)
5
+ * Built: Mon Mar 13 2023 13:25:53 GMT-0300 (Brasilia Standard Time)
6
6
  */
7
7
 
8
- (function(c,h){typeof exports=="object"&&typeof module<"u"?module.exports=h(require("ol/control/Control"),require("ol/MapBrowserEvent")):typeof define=="function"&&define.amd?define(["ol/control/Control","ol/MapBrowserEvent"],h):(c=typeof globalThis<"u"?globalThis:c||self,c.ContextMenu=h(c.ol.control.Control,c.ol.MapBrowserEvent))})(this,function(c,h){"use strict";var P=Object.defineProperty;var H=(c,h,d)=>h in c?P(c,h,{enumerable:!0,configurable:!0,writable:!0,value:d}):c[h]=d;var l=(c,h,d)=>(H(c,typeof h!="symbol"?h+"":h,d),d);var d=(s=>(s.CONTEXTMENU="contextmenu",s.CLICK="click",s.DBLCLICK="dblclick",s))(d||{}),m=(s=>(s.BEFOREOPEN="beforeopen",s.OPEN="open",s.CLOSE="close",s.ADD_MENU_ENTRY="add-menu-entry",s))(m||{});class C extends h{constructor(n){super(n.type,n.map,n.originalEvent)}}const $={width:150,scrollAt:4,eventType:d.CONTEXTMENU,defaultItems:!0,items:[]},f="ol-ctx-menu",r={namespace:f,container:`${f}-container`,separator:`${f}-separator`,submenu:`${f}-submenu`,hidden:`${f}-hidden`,icon:`${f}-icon`,zoomIn:`${f}-zoom-in`,zoomOut:`${f}-zoom-out`,unselectable:"ol-unselectable"},A=[{text:"Zoom In",classname:`${r.zoomIn} ${r.icon}`,callback:(s,n)=>{const e=n.getView();e.animate({zoom:Number(e.getZoom())+1,duration:700,center:s.coordinate})}},{text:"Zoom Out",classname:`${r.zoomOut} ${r.icon}`,callback:(s,n)=>{const e=n.getView();e.animate({zoom:Number(e.getZoom())-1,duration:700,center:s.coordinate})}}];var x={},S={get exports(){return x},set exports(s){x=s}};function b(){}b.prototype={on:function(s,n,e){var t=this.e||(this.e={});return(t[s]||(t[s]=[])).push({fn:n,ctx:e}),this},once:function(s,n,e){var t=this;function i(){t.off(s,i),n.apply(e,arguments)}return i._=n,this.on(s,i,e)},emit:function(s){var n=[].slice.call(arguments,1),e=((this.e||(this.e={}))[s]||[]).slice(),t=0,i=e.length;for(t;t<i;t++)e[t].fn.apply(e[t].ctx,n);return this},off:function(s,n){var e=this.e||(this.e={}),t=e[s],i=[];if(t&&n)for(var o=0,u=t.length;o<u;o++)t[o].fn!==n&&t[o].fn._!==n&&i.push(t[o]);return i.length?e[s]=i:delete e[s],this}},S.exports=b;var T=x.TinyEmitter=b;const v=new T;function M(s){const n=document.createDocumentFragment(),e=document.createElement("div");for(e.innerHTML=s;e.firstChild;)n.append(e.firstChild);return n}function k(s){var E,a;const n=document.importNode(s),e=s.offsetWidth;n.style.cssText=`position: fixed; top: 0; left: 0; overflow: auto; visibility: hidden; pointer-events: none; height: unset; max-height: unset; width: ${e}px`;const t=M("<span>Foo</span>"),i=M("<span>Foo</span>"),o=document.createElement("li"),u=document.createElement("li");o.append(t),u.append(i),n.append(o),n.append(u),(E=s.parentNode)==null||E.append(n);const g=n.offsetHeight/2;return(a=s.parentNode)==null||a.removeChild(n),g}function N(s,n,e=!1){const t=`_${Math.random().toString(36).slice(2,11)}`;if(typeof n!="string"&&"text"in n){const g=`<span>${n.text}</span>`,E=M(g),a=document.createElement("li");return n.classname=n.classname||"",n.icon&&(n.classname===""?n.classname=r.icon:n.classname.includes(r.icon)===!1&&(n.classname+=` ${r.icon}`),a.setAttribute("style",`background-image:url(${n.icon})`)),a.id=t,a.className=n.classname,a.append(E),s.append(a),v.emit(m.ADD_MENU_ENTRY,{id:t,isSubmenu:e,isSeparator:!1,callback:"callback"in n?n.callback:null,data:"data"in n?n.data:null},a),a}const i=`<li id="${t}" class="${r.separator}"><hr></li>`,o=M(i);s.append(o);const u=s.lastChild;return v.emit(m.ADD_MENU_ENTRY,{id:t,isSubmenu:e,isSeparator:!0,callback:null,data:null},u),u}function L(s,n,e){n.forEach(t=>{if(typeof t!="string"&&"items"in t&&Array.isArray(t.items)){const i=N(s,t,!0);i.classList.add(r.submenu);const o=document.createElement("ul");o.classList.add(r.container),o.style.width=`${e}px`,i.append(o),L(o,t.items,e)}else N(s,t)})}const U="";function w(s,n){if(!s)throw new Error(n)}class O extends c{constructor(e={}){w(typeof e=="object","@param `opts` should be object type!");const t=document.createElement("div");super({element:t});l(this,"map");l(this,"container");l(this,"coordinate",[]);l(this,"pixel",[]);l(this,"contextMenuEventListener");l(this,"entryCallbackEventListener");l(this,"mapMoveListener");l(this,"lineHeight",0);l(this,"disabled");l(this,"opened");l(this,"items",[]);l(this,"menuEntries",new Map);l(this,"options");this.options={...$,...e};const i=document.createElement("ul");t.append(i),t.style.width=`${this.options.width}px`,t.classList.add(r.container,r.unselectable,r.hidden),this.container=t,this.contextMenuEventListener=o=>{this.handleContextMenu(o)},this.entryCallbackEventListener=o=>{this.handleEntryCallback(o)},this.mapMoveListener=()=>{this.handleMapMove()},this.disabled=!1,this.opened=!1}clear(){for(const e of this.menuEntries.keys())this.removeMenuEntry(e);this.container.replaceChildren(),this.container.append(document.createElement("ul"))}enable(){this.disabled=!1}disable(){this.disabled=!0}getDefaultItems(){return A}countItems(){return this.menuEntries.size}extend(e){w(Array.isArray(e),"@param `items` should be an Array."),L(this.container.firstElementChild,e,this.options.width)}closeMenu(){this.opened=!1,this.container.classList.add(r.hidden),this.dispatchEvent(m.CLOSE)}isOpen(){return this.opened}updatePosition(e){w(Array.isArray(e),"@param `pixel` should be an Array."),this.isOpen()&&(this.pixel=e,this.positionContainer())}pop(){const e=Array.from(this.menuEntries.keys()).pop();e&&this.removeMenuEntry(e)}shift(){const e=Array.from(this.menuEntries.keys()).shift();e&&this.removeMenuEntry(e)}push(e){e&&this.extend([e])}setMap(e){if(super.setMap(e),e){this.map=e,this.map.getViewport().addEventListener(this.options.eventType,this.contextMenuEventListener,!1),this.map.on("movestart",()=>{this.handleMapMove()}),v.on(m.ADD_MENU_ENTRY,(i,o)=>{this.handleAddMenuEntry(i,o)},this),this.items=this.options.defaultItems?this.options.items.concat(A):this.options.items,L(this.container.firstElementChild,this.items,this.options.width);const t=this.getMenuEntriesLength();this.lineHeight=t>0?this.container.offsetHeight/t:k(this.container)}else this.removeListeners(),this.clear()}removeListeners(){this.map.getViewport().removeEventListener(this.options.eventType,this.contextMenuEventListener,!1),v.off(m.ADD_MENU_ENTRY)}removeMenuEntry(e){const t=document.getElementById(e);t==null||t.remove(),t==null||t.removeEventListener("click",this.entryCallbackEventListener),this.menuEntries.delete(e)}handleContextMenu(e){var t;this.coordinate=this.map.getEventCoordinate(e),this.pixel=this.map.getEventPixel(e),this.dispatchEvent(new C({type:m.BEFOREOPEN,map:this.map,originalEvent:e})),!this.disabled&&(this.options.eventType===d.CONTEXTMENU&&(e.stopPropagation(),e.preventDefault()),setTimeout(()=>{this.openMenu(e)}),(t=e.target)==null||t.addEventListener("pointerdown",i=>{this.opened&&(i.stopPropagation(),this.closeMenu())},{once:!0}))}openMenu(e){this.menuEntries.size!==0&&(this.opened=!0,this.positionContainer(),this.container.classList.remove(r.hidden),this.dispatchEvent(new C({type:m.OPEN,map:this.map,originalEvent:e})))}getMenuEntriesLength(){return Array.from(this.menuEntries).filter(([,e])=>e.isSeparator===!1||e.isSubmenu===!1).length}positionContainer(){const e=this.map.getSize()||[0,0],t={w:e[0]-this.pixel[0],h:e[1]-this.pixel[1]},i={w:this.container.offsetWidth,h:Math.round(this.lineHeight*this.getMenuEntriesLength())},o=t.w>=i.w?this.pixel[0]+5:this.pixel[0]-i.w;this.container.style.left=`${o}px`,this.container.style.top=t.h>=i.h?`${this.pixel[1]-10}px`:`${this.pixel[1]-i.h}px`,this.container.style.right="auto",this.container.style.bottom="auto",t.w-=i.w;const u=a=>Array.from(a.children).filter(y=>y.tagName==="LI"&&y.classList.contains(r.submenu));let g=0;const E=(a,y)=>{g+=1,u(a).forEach(D=>{const _=y>=i.w?i.w-8:(i.w+8)*-1,p=D.querySelector(`ul.${r.container}`),I=Math.round(this.lineHeight*Array.from(p.children).filter(z=>z.tagName==="LI").length);p.style.left=`${_}px`,p.style.right="auto",p.style.top=t.h>=I+i.h?"0":`-${p.offsetHeight-25}px`,p.style.bottom="auto",p.style.zIndex=String(g),u(p).length>0&&E(p,y-i.w)})};E(this.container.firstElementChild,t.w)}handleMapMove(){this.closeMenu()}handleEntryCallback(e){var u;e.preventDefault(),e.stopPropagation();const t=e.currentTarget,i=this.menuEntries.get(t.id);if(!i)return;const o={coordinate:this.coordinate,data:i.data};this.closeMenu(),(u=i.callback)==null||u.call(i,o,this.map)}handleAddMenuEntry(e,t){this.menuEntries.set(e.id,e),this.positionContainer(),"callback"in e&&typeof e.callback=="function"&&t.addEventListener("click",this.entryCallbackEventListener,!1)}}return O});
8
+ (function(d,l){typeof exports=="object"&&typeof module<"u"?module.exports=l():typeof define=="function"&&define.amd?define(l):(d=typeof globalThis<"u"?globalThis:d||self,d.ContextMenu=l())})(this,function(){"use strict";var pe=Object.defineProperty;var fe=(d,l,g)=>l in d?pe(d,l,{enumerable:!0,configurable:!0,writable:!0,value:g}):d[l]=g;var h=(d,l,g)=>(fe(d,typeof l!="symbol"?l+"":l,g),g);class d{constructor(e){this.propagationStopped,this.defaultPrevented,this.type=e,this.target=null}preventDefault(){this.defaultPrevented=!0}stopPropagation(){this.propagationStopped=!0}}const l=d,g={PROPERTYCHANGE:"propertychange"};class K{constructor(){this.disposed=!1}dispose(){this.disposed||(this.disposed=!0,this.disposeInternal())}disposeInternal(){}}const U=K;function M(){}function S(s){for(const e in s)delete s[e]}function z(s){let e;for(e in s)return!1;return!e}class B extends U{constructor(e){super(),this.eventTarget_=e,this.pendingRemovals_=null,this.dispatching_=null,this.listeners_=null}addEventListener(e,t){if(!e||!t)return;const n=this.listeners_||(this.listeners_={}),i=n[e]||(n[e]=[]);i.includes(t)||i.push(t)}dispatchEvent(e){const t=typeof e=="string",n=t?e:e.type,i=this.listeners_&&this.listeners_[n];if(!i)return;const r=t?new l(e):e;r.target||(r.target=this.eventTarget_||this);const o=this.dispatching_||(this.dispatching_={}),p=this.pendingRemovals_||(this.pendingRemovals_={});n in o||(o[n]=0,p[n]=0),++o[n];let f;for(let u=0,y=i.length;u<y;++u)if("handleEvent"in i[u]?f=i[u].handleEvent(r):f=i[u].call(this,r),f===!1||r.propagationStopped){f=!1;break}if(--o[n]===0){let u=p[n];for(delete p[n];u--;)this.removeEventListener(n,M);delete o[n]}return f}disposeInternal(){this.listeners_&&S(this.listeners_)}getListeners(e){return this.listeners_&&this.listeners_[e]||void 0}hasListener(e){return this.listeners_?e?e in this.listeners_:Object.keys(this.listeners_).length>0:!1}removeEventListener(e,t){const n=this.listeners_&&this.listeners_[e];if(n){const i=n.indexOf(t);i!==-1&&(this.pendingRemovals_&&e in this.pendingRemovals_?(n[i]=M,++this.pendingRemovals_[e]):(n.splice(i,1),n.length===0&&delete this.listeners_[e]))}}}const F=B,V={CHANGE:"change",ERROR:"error",BLUR:"blur",CLEAR:"clear",CONTEXTMENU:"contextmenu",CLICK:"click",DBLCLICK:"dblclick",DRAGENTER:"dragenter",DRAGOVER:"dragover",DROP:"drop",FOCUS:"focus",KEYDOWN:"keydown",KEYPRESS:"keypress",LOAD:"load",RESIZE:"resize",TOUCHMOVE:"touchmove",WHEEL:"wheel"};function _(s,e,t,n,i){if(n&&n!==s&&(t=t.bind(n)),i){const o=t;t=function(){s.removeEventListener(e,t),o.apply(this,arguments)}}const r={target:s,type:e,listener:t};return s.addEventListener(e,t),r}function D(s,e,t,n){return _(s,e,t,n,!0)}function C(s){s&&s.target&&(s.target.removeEventListener(s.type,s.listener),S(s))}class L extends F{constructor(){super(),this.on=this.onInternal,this.once=this.onceInternal,this.un=this.unInternal,this.revision_=0}changed(){++this.revision_,this.dispatchEvent(V.CHANGE)}getRevision(){return this.revision_}onInternal(e,t){if(Array.isArray(e)){const n=e.length,i=new Array(n);for(let r=0;r<n;++r)i[r]=_(this,e[r],t);return i}return _(this,e,t)}onceInternal(e,t){let n;if(Array.isArray(e)){const i=e.length;n=new Array(i);for(let r=0;r<i;++r)n[r]=D(this,e[r],t)}else n=D(this,e,t);return t.ol_key=n,n}unInternal(e,t){const n=t.ol_key;if(n)j(n);else if(Array.isArray(e))for(let i=0,r=e.length;i<r;++i)this.removeEventListener(e[i],t);else this.removeEventListener(e,t)}}L.prototype.on,L.prototype.once,L.prototype.un;function j(s){if(Array.isArray(s))for(let e=0,t=s.length;e<t;++e)C(s[e]);else C(s)}const Y=L;let G=0;function Z(s){return s.ol_uid||(s.ol_uid=String(++G))}class R extends l{constructor(e,t,n){super(e),this.key=t,this.oldValue=n}}class X extends Y{constructor(e){super(),this.on,this.once,this.un,Z(this),this.values_=null,e!==void 0&&this.setProperties(e)}get(e){let t;return this.values_&&this.values_.hasOwnProperty(e)&&(t=this.values_[e]),t}getKeys(){return this.values_&&Object.keys(this.values_)||[]}getProperties(){return this.values_&&Object.assign({},this.values_)||{}}hasProperties(){return!!this.values_}notify(e,t){let n;n=`change:${e}`,this.hasListener(n)&&this.dispatchEvent(new R(n,e,t)),n=g.PROPERTYCHANGE,this.hasListener(n)&&this.dispatchEvent(new R(n,e,t))}addChangeListener(e,t){this.addEventListener(`change:${e}`,t)}removeChangeListener(e,t){this.removeEventListener(`change:${e}`,t)}set(e,t,n){const i=this.values_||(this.values_={});if(n)i[e]=t;else{const r=i[e];i[e]=t,r!==t&&this.notify(e,r)}}setProperties(e,t){for(const n in e)this.set(n,e[n],t)}applyProperties(e){e.values_&&Object.assign(this.values_||(this.values_={}),e.values_)}unset(e,t){if(this.values_&&e in this.values_){const n=this.values_[e];delete this.values_[e],z(this.values_)&&(this.values_=null),t||this.notify(e,n)}}}const W=X,q={POSTRENDER:"postrender",MOVESTART:"movestart",MOVEEND:"moveend",LOADSTART:"loadstart",LOADEND:"loadend"};function P(s){return s&&s.parentNode?s.parentNode.removeChild(s):null}class J extends W{constructor(e){super();const t=e.element;t&&!e.target&&!t.style.pointerEvents&&(t.style.pointerEvents="auto"),this.element=t||null,this.target_=null,this.map_=null,this.listenerKeys=[],e.render&&(this.render=e.render),e.target&&this.setTarget(e.target)}disposeInternal(){P(this.element),super.disposeInternal()}getMap(){return this.map_}setMap(e){this.map_&&P(this.element);for(let t=0,n=this.listenerKeys.length;t<n;++t)C(this.listenerKeys[t]);this.listenerKeys.length=0,this.map_=e,e&&((this.target_?this.target_:e.getOverlayContainerStopEvent()).appendChild(this.element),this.render!==M&&this.listenerKeys.push(_(e,q.POSTRENDER,this.render,this)),e.render())}render(e){}setTarget(e){this.target_=typeof e=="string"?document.getElementById(e):e}}const Q=J;class ee extends l{constructor(e,t,n){super(e),this.map=t,this.frameState=n!==void 0?n:null}}const te=ee;class ne extends te{constructor(e,t,n,i,r,o){super(e,t,r),this.originalEvent=n,this.pixel_=null,this.coordinate_=null,this.dragging=i!==void 0?i:!1,this.activePointers=o}get pixel(){return this.pixel_||(this.pixel_=this.map.getEventPixel(this.originalEvent)),this.pixel_}set pixel(e){this.pixel_=e}get coordinate(){return this.coordinate_||(this.coordinate_=this.map.getCoordinateFromPixel(this.pixel)),this.coordinate_}set coordinate(e){this.coordinate_=e}preventDefault(){super.preventDefault(),"preventDefault"in this.originalEvent&&this.originalEvent.preventDefault()}stopPropagation(){super.stopPropagation(),"stopPropagation"in this.originalEvent&&this.originalEvent.stopPropagation()}}const se=ne;var O=(s=>(s.CONTEXTMENU="contextmenu",s.CLICK="click",s.DBLCLICK="dblclick",s))(O||{}),E=(s=>(s.BEFOREOPEN="beforeopen",s.OPEN="open",s.CLOSE="close",s.ADD_MENU_ENTRY="add-menu-entry",s))(E||{});class $ extends se{constructor(e){super(e.type,e.map,e.originalEvent)}}const ie={width:150,scrollAt:4,eventType:O.CONTEXTMENU,defaultItems:!0,items:[]},v="ol-ctx-menu",a={namespace:v,container:`${v}-container`,separator:`${v}-separator`,submenu:`${v}-submenu`,hidden:`${v}-hidden`,icon:`${v}-icon`,zoomIn:`${v}-zoom-in`,zoomOut:`${v}-zoom-out`,unselectable:"ol-unselectable"},I=[{text:"Zoom In",classname:`${a.zoomIn} ${a.icon}`,callback:(s,e)=>{const t=e.getView();t.animate({zoom:Number(t.getZoom())+1,duration:700,center:s.coordinate})}},{text:"Zoom Out",classname:`${a.zoomOut} ${a.icon}`,callback:(s,e)=>{const t=e.getView();t.animate({zoom:Number(t.getZoom())-1,duration:700,center:s.coordinate})}}];var w={},re={get exports(){return w},set exports(s){w=s}};function A(){}A.prototype={on:function(s,e,t){var n=this.e||(this.e={});return(n[s]||(n[s]=[])).push({fn:e,ctx:t}),this},once:function(s,e,t){var n=this;function i(){n.off(s,i),e.apply(t,arguments)}return i._=e,this.on(s,i,t)},emit:function(s){var e=[].slice.call(arguments,1),t=((this.e||(this.e={}))[s]||[]).slice(),n=0,i=t.length;for(n;n<i;n++)t[n].fn.apply(t[n].ctx,e);return this},off:function(s,e){var t=this.e||(this.e={}),n=t[s],i=[];if(n&&e)for(var r=0,o=n.length;r<o;r++)n[r].fn!==e&&n[r].fn._!==e&&i.push(n[r]);return i.length?t[s]=i:delete t[s],this}},re.exports=A;var oe=w.TinyEmitter=A;const b=new oe;function x(s){const e=document.createDocumentFragment(),t=document.createElement("div");for(t.innerHTML=s;t.firstChild;)e.append(t.firstChild);return e}function ae(s){const e=document.importNode(s),t=s.offsetWidth;e.style.cssText=`position: fixed; top: 0; left: 0; overflow: auto; visibility: hidden; pointer-events: none; height: unset; max-height: unset; width: ${t}px`;const n=x("<span>Foo</span>"),i=x("<span>Foo</span>"),r=document.createElement("li"),o=document.createElement("li");r.append(n),o.append(i),e.append(r),e.append(o),s.parentNode?.append(e);const p=e.offsetHeight/2;return s.parentNode?.removeChild(e),p}function k(s,e,t=!1,n=!1){const i=`_${Math.random().toString(36).slice(2,11)}`;if(typeof e!="string"&&"text"in e){const u=`<span>${e.text}</span>`,y=x(u),c=document.createElement("li");e.classname=e.classname||"",e.icon&&(e.classname===""?e.classname=a.icon:e.classname.includes(a.icon)===!1&&(e.classname+=` ${a.icon}`),c.setAttribute("style",`background-image:url(${e.icon})`)),c.id=i,c.className=e.classname,c.append(y),s.append(c);const H={id:i,isSubmenu:t,isInsideSubmenu:n,isSeparator:!1,callback:"callback"in e?e.callback:null,data:"data"in e?e.data:null};return b.emit(E.ADD_MENU_ENTRY,H,c),c}const r=`<li id="${i}" class="${a.separator}"><hr></li>`,o=x(r);s.append(o);const p=s.lastChild,f={id:i,isSubmenu:!1,isInsideSubmenu:!1,isSeparator:!0,callback:null,data:null};return b.emit(E.ADD_MENU_ENTRY,f,p),p}function T(s,e,t,n){e.forEach(i=>{if(typeof i!="string"&&"items"in i&&Array.isArray(i.items)){const r=k(s,i,!0);r.classList.add(a.submenu);const o=document.createElement("ul");o.classList.add(a.container),o.style.width=`${t}px`,r.append(o),T(o,i.items,t,!0)}else k(s,i,!1,n)})}const me="";function N(s,e){if(!s)throw new Error(e)}class le extends Q{constructor(t={}){N(typeof t=="object","@param `opts` should be object type!");const n=document.createElement("div");super({element:n});h(this,"map");h(this,"container");h(this,"coordinate",[]);h(this,"pixel",[]);h(this,"contextMenuEventListener");h(this,"entryCallbackEventListener");h(this,"mapMoveListener");h(this,"lineHeight",0);h(this,"disabled");h(this,"opened");h(this,"items",[]);h(this,"menuEntries",new Map);h(this,"options");this.options={...ie,...t};const i=document.createElement("ul");n.append(i),n.style.width=`${this.options.width}px`,n.classList.add(a.container,a.unselectable,a.hidden),this.container=n,this.contextMenuEventListener=r=>{this.handleContextMenu(r)},this.entryCallbackEventListener=r=>{this.handleEntryCallback(r)},this.mapMoveListener=()=>{this.handleMapMove()},this.disabled=!1,this.opened=!1,window.addEventListener("beforeunload",()=>{this.removeListeners()},{once:!0})}clear(){for(const t of this.menuEntries.keys())this.removeMenuEntry(t);this.container.replaceChildren(),this.container.append(document.createElement("ul"))}enable(){this.disabled=!1}disable(){this.disabled=!0}getDefaultItems(){return I}countItems(){return this.menuEntries.size}extend(t){N(Array.isArray(t),"@param `items` should be an Array."),T(this.container.firstElementChild,t,this.options.width)}closeMenu(){this.opened=!1,this.container.classList.add(a.hidden),this.dispatchEvent(E.CLOSE)}isOpen(){return this.opened}updatePosition(t){N(Array.isArray(t),"@param `pixel` should be an Array."),this.isOpen()&&(this.pixel=t,this.positionContainer())}pop(){const t=Array.from(this.menuEntries.keys()).pop();t&&this.removeMenuEntry(t)}shift(){const t=Array.from(this.menuEntries.keys()).shift();t&&this.removeMenuEntry(t)}push(t){t&&this.extend([t])}setMap(t){if(super.setMap(t),t){this.map=t,this.map.getViewport().addEventListener(this.options.eventType,this.contextMenuEventListener,!1),this.map.on("movestart",()=>{this.handleMapMove()}),b.on(E.ADD_MENU_ENTRY,(i,r)=>{this.handleAddMenuEntry(i,r)},this),this.items=this.options.defaultItems?this.options.items.concat(I):this.options.items,T(this.container.firstElementChild,this.items,this.options.width);const n=this.getMenuEntriesLength();this.lineHeight=n>0?this.container.offsetHeight/n:ae(this.container)}else this.removeListeners(),this.clear()}removeListeners(){this.map.getViewport().removeEventListener(this.options.eventType,this.contextMenuEventListener,!1),b.off(E.ADD_MENU_ENTRY)}removeMenuEntry(t){const n=document.getElementById(t);n?.remove(),n?.removeEventListener("click",this.entryCallbackEventListener),this.menuEntries.delete(t)}handleContextMenu(t){this.coordinate=this.map.getEventCoordinate(t),this.pixel=this.map.getEventPixel(t),this.dispatchEvent(new $({type:E.BEFOREOPEN,map:this.map,originalEvent:t})),!this.disabled&&(this.options.eventType===O.CONTEXTMENU&&(t.stopPropagation(),t.preventDefault()),setTimeout(()=>{this.openMenu(t)}),t.target?.addEventListener("pointerdown",n=>{this.opened&&(n.stopPropagation(),this.closeMenu())},{once:!0}))}openMenu(t){this.opened=!0,this.positionContainer(),this.container.classList.remove(a.hidden),this.dispatchEvent(new $({type:E.OPEN,map:this.map,originalEvent:t}))}getMenuEntriesLength(){return Array.from(this.menuEntries).filter(([,t])=>t.isSeparator===!1&&t.isSubmenu===!1&&t.isInsideSubmenu===!1).length}positionContainer(){const t=this.map.getSize()||[0,0],n={w:t[0]-this.pixel[0],h:t[1]-this.pixel[1]},i=this.getMenuEntriesLength(),r={w:this.container.offsetWidth,h:Math.round(this.lineHeight*i)},o=n.w>=r.w?this.pixel[0]+5:this.pixel[0]-r.w;this.container.style.left=`${o}px`,this.container.style.top=n.h>=r.h?`${this.pixel[1]-10}px`:`${this.pixel[1]-r.h}px`,this.container.style.right="auto",this.container.style.bottom="auto",n.w-=r.w;const p=y=>Array.from(y.children).filter(c=>c.tagName==="LI"&&c.classList.contains(a.submenu));let f=0;const u=(y,c)=>{f+=1,p(y).forEach(he=>{const ce=c>=r.w?r.w-8:(r.w+8)*-1,m=he.querySelector(`ul.${a.container}`),ue=Math.round(this.lineHeight*Array.from(m.children).filter(de=>de.tagName==="LI").length);m.style.left=`${ce}px`,m.style.right="auto",m.style.top=n.h>=ue+r.h?"0":`-${m.offsetHeight-25}px`,m.style.bottom="auto",m.style.zIndex=String(f),p(m).length>0&&u(m,c-r.w)})};u(this.container.firstElementChild,n.w)}handleMapMove(){this.closeMenu()}handleEntryCallback(t){t.preventDefault(),t.stopPropagation();const n=t.currentTarget,i=this.menuEntries.get(n.id);if(!i)return;const r={coordinate:this.coordinate,data:i.data};this.closeMenu(),i.callback?.(r,this.map)}handleAddMenuEntry(t,n){this.menuEntries.set(t.id,t),this.positionContainer(),"callback"in t&&typeof t.callback=="function"&&n.addEventListener("click",this.entryCallbackEventListener,!1)}}return le});
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import OlMap from 'ol/Map';
2
1
  import type { Coordinate } from 'ol/coordinate';
2
+ import OlMap from 'ol/Map';
3
3
  import MapBrowserEvent from 'ol/MapBrowserEvent';
4
4
  export declare enum EventTypes {
5
5
  CONTEXTMENU = "contextmenu",
@@ -19,33 +19,34 @@ export declare class ContextMenuEvent extends MapBrowserEvent<MouseEvent> {
19
19
  originalEvent: MouseEvent;
20
20
  });
21
21
  }
22
- export declare type CallbackObject = {
22
+ export type CallbackObject = {
23
23
  coordinate: Coordinate;
24
24
  data: unknown;
25
25
  };
26
- export declare type ItemSeparator = '-';
27
- export declare type SingleItem = {
26
+ export type ItemSeparator = '-';
27
+ export type SingleItem = {
28
28
  text: string;
29
29
  classname?: string;
30
30
  icon?: string;
31
31
  callback: (object: CallbackObject, map: OlMap) => void;
32
32
  data?: unknown;
33
33
  };
34
- export declare type MenuEntry = {
34
+ export type MenuEntry = {
35
35
  id: string;
36
36
  isSubmenu: boolean;
37
+ isInsideSubmenu: boolean;
37
38
  isSeparator: boolean;
38
39
  callback: SingleItem['callback'] | null;
39
40
  data: unknown;
40
41
  };
41
- export declare type ItemWithNested = {
42
+ export type ItemWithNested = {
42
43
  text: string;
43
44
  classname?: string;
44
45
  icon?: string;
45
46
  items: Item[];
46
47
  };
47
- export declare type Item = SingleItem | ItemSeparator | ItemWithNested;
48
- export declare type Options = {
48
+ export type Item = SingleItem | ItemSeparator | ItemWithNested;
49
+ export type Options = {
49
50
  width: number;
50
51
  scrollAt: number;
51
52
  eventType: `${EventTypes}`;
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "ol-contextmenu",
3
- "version": "5.1.4",
3
+ "version": "5.2.0",
4
4
  "description": "Custom Context Menu for Openlayers",
5
5
  "type": "module",
6
- "browser": "./dist/ol-contextmenu.umd.cjs",
6
+ "main": "./dist/ol-contextmenu.umd.cjs",
7
+ "jsdelivr": "./dist/ol-contextmenu.iife.js",
7
8
  "module": "./dist/ol-contextmenu.js",
8
9
  "exports": {
9
10
  ".": {
@@ -13,7 +14,8 @@
13
14
  },
14
15
  "./ol-contextmenu.css": {
15
16
  "import": "./dist/ol-contextmenu.css",
16
- "require": "./dist/ol-contextmenu.css"
17
+ "require": "./dist/ol-contextmenu.css",
18
+ "style": "./dist/ol-contextmenu.css"
17
19
  }
18
20
  },
19
21
  "files": [
@@ -38,32 +40,33 @@
38
40
  "dev": "vite",
39
41
  "build": "rimraf ./dist && vite build",
40
42
  "preview": "vite preview",
41
- "lint": "eslint --cache --ext .js .",
43
+ "lint": "eslint .",
42
44
  "lint:fix": "npm run lint -- --fix",
43
45
  "test": "jest",
44
46
  "prepublishOnly": "npm run lint && npm test && npm run build",
45
47
  "prepare": "husky install"
46
48
  },
47
49
  "devDependencies": {
48
- "@swc/core": "^1.3.24",
50
+ "@swc/core": "^1.3.40",
49
51
  "@swc/jest": "^0.2.24",
50
- "@types/jest": "^29.2.4",
51
- "eslint": "^8.30.0",
52
- "eslint-config-jwalker": "^7.10.0",
53
- "eslint-config-prettier": "^8.5.0",
54
- "eslint-plugin-jest": "^27.1.7",
52
+ "@types/jest": "^29.4.0",
53
+ "eslint": "^8.36.0",
54
+ "eslint-config-jwalker": "^8.0.0",
55
+ "eslint-config-prettier": "^8.7.0",
56
+ "eslint-plugin-jest": "^27.2.1",
55
57
  "eslint-plugin-prettier": "^4.2.1",
56
- "husky": "^8.0.2",
58
+ "husky": "^8.0.3",
57
59
  "identity-obj-proxy": "^3.0.0",
58
- "jest": "^29.3.1",
59
- "jest-environment-jsdom": "^29.3.1",
60
- "ol": "^7.2.2",
61
- "prettier": "^2.8.1",
62
- "rimraf": "^3.0.2",
63
- "typescript": "^4.9.4",
64
- "vite": "^4.0.3",
60
+ "jest": "^29.5.0",
61
+ "jest-environment-jsdom": "^29.5.0",
62
+ "ol": "^7.3.0",
63
+ "prettier": "^2.8.4",
64
+ "rimraf": "^4.4.0",
65
+ "sass": "^1.59.2",
66
+ "typescript": "^4.9.5",
67
+ "vite": "^4.1.4",
65
68
  "vite-plugin-banner": "^0.7.0",
66
- "vite-plugin-dts": "^1.7.1"
69
+ "vite-plugin-dts": "^2.1.0"
67
70
  },
68
71
  "peerDependencies": {
69
72
  "ol": "^7.1.0"
@@ -1 +0,0 @@
1
- /// <reference types="vite/client" />