ol-contextmenu 5.2.1 → 5.3.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.
- package/dist/constants.d.ts +1 -1
- package/dist/helpers/dom.d.ts +1 -1
- package/dist/main.d.ts +9 -8
- package/dist/ol-contextmenu.css +2 -2
- package/dist/ol-contextmenu.iife.js +3 -3
- package/dist/ol-contextmenu.js +208 -795
- package/dist/ol-contextmenu.umd.cjs +3 -3
- package/dist/types.d.ts +1 -1
- package/package.json +31 -29
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
|
-
* ol-contextmenu - v5.
|
|
3
|
+
* ol-contextmenu - v5.3.0
|
|
4
4
|
* https://github.com/jonataswalker/ol-contextmenu
|
|
5
|
-
* Built: Fri
|
|
5
|
+
* Built: Fri Sep 22 2023 13:13:39 GMT-0300 (Brasilia Standard Time)
|
|
6
6
|
*/
|
|
7
7
|
|
|
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 de=Object.defineProperty;var pe=(d,l,g)=>l in d?de(d,l,{enumerable:!0,configurable:!0,writable:!0,value:g}):d[l]=g;var c=(d,l,g)=>(pe(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 D(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_={}),r=n[e]||(n[e]=[]);r.includes(t)||r.push(t)}dispatchEvent(e){const t=typeof e=="string",n=t?e:e.type,r=this.listeners_&&this.listeners_[n];if(!r)return;const i=t?new l(e):e;i.target||(i.target=this.eventTarget_||this);const o=this.dispatching_||(this.dispatching_={}),h=this.pendingRemovals_||(this.pendingRemovals_={});n in o||(o[n]=0,h[n]=0),++o[n];let p;for(let u=0,y=r.length;u<y;++u)if("handleEvent"in r[u]?p=r[u].handleEvent(i):p=r[u].call(this,i),p===!1||i.propagationStopped){p=!1;break}if(--o[n]===0){let u=h[n];for(delete h[n];u--;)this.removeEventListener(n,M);delete o[n]}return p}disposeInternal(){this.listeners_&&D(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 r=n.indexOf(t);r!==-1&&(this.pendingRemovals_&&e in this.pendingRemovals_?(n[r]=M,++this.pendingRemovals_[e]):(n.splice(r,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 b(s,e,t,n,r){if(n&&n!==s&&(t=t.bind(n)),r){const o=t;t=function(){s.removeEventListener(e,t),o.apply(this,arguments)}}const i={target:s,type:e,listener:t};return s.addEventListener(e,t),i}function R(s,e,t,n){return b(s,e,t,n,!0)}function C(s){s&&s.target&&(s.target.removeEventListener(s.type,s.listener),D(s))}class x 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,r=new Array(n);for(let i=0;i<n;++i)r[i]=b(this,e[i],t);return r}return b(this,e,t)}onceInternal(e,t){let n;if(Array.isArray(e)){const r=e.length;n=new Array(r);for(let i=0;i<r;++i)n[i]=R(this,e[i],t)}else n=R(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 r=0,i=e.length;r<i;++r)this.removeEventListener(e[r],t);else this.removeEventListener(e,t)}}x.prototype.on,x.prototype.once,x.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=x;let G=0;function Z(s){return s.ol_uid||(s.ol_uid=String(++G))}class P extends l{constructor(e,t,n){super(e),this.key=t,this.oldValue=n}}class W 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 P(n,e,t)),n=g.PROPERTYCHANGE,this.hasListener(n)&&this.dispatchEvent(new P(n,e,t))}addChangeListener(e,t){this.addEventListener(`change:${e}`,t)}removeChangeListener(e,t){this.removeEventListener(`change:${e}`,t)}set(e,t,n){const r=this.values_||(this.values_={});if(n)r[e]=t;else{const i=r[e];r[e]=t,i!==t&&this.notify(e,i)}}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 X=W,q={POSTRENDER:"postrender",MOVESTART:"movestart",MOVEEND:"moveend",LOADSTART:"loadstart",LOADEND:"loadend"};function $(s){return s&&s.parentNode?s.parentNode.removeChild(s):null}class J extends X{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(){$(this.element),super.disposeInternal()}getMap(){return this.map_}setMap(e){this.map_&&$(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(b(e,q.POSTRENDER,this.render,this)),e.render())}render(e){}setTarget(e){this.target_=typeof e=="string"?document.getElementById(e):e}}const Q=J;var O={},ee={get exports(){return O},set exports(s){O=s}};function w(){}w.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 r(){n.off(s,r),e.apply(t,arguments)}return r._=e,this.on(s,r,t)},emit:function(s){var e=[].slice.call(arguments,1),t=((this.e||(this.e={}))[s]||[]).slice(),n=0,r=t.length;for(n;n<r;n++)t[n].fn.apply(t[n].ctx,e);return this},off:function(s,e){var t=this.e||(this.e={}),n=t[s],r=[];if(n&&e)for(var i=0,o=n.length;i<o;i++)n[i].fn!==e&&n[i].fn._!==e&&r.push(n[i]);return r.length?t[s]=r:delete t[s],this}},ee.exports=w;var te=O.TinyEmitter=w;class ne extends l{constructor(e,t,n){super(e),this.map=t,this.frameState=n!==void 0?n:null}}const se=ne;class ie extends se{constructor(e,t,n,r,i,o){super(e,t,i),this.originalEvent=n,this.pixel_=null,this.coordinate_=null,this.dragging=r!==void 0?r:!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 re=ie;var A=(s=>(s.CONTEXTMENU="contextmenu",s.CLICK="click",s.DBLCLICK="dblclick",s))(A||{}),m=(s=>(s.BEFOREOPEN="beforeopen",s.OPEN="open",s.CLOSE="close",s.ADD_MENU_ENTRY="add-menu-entry",s))(m||{});class I extends re{constructor(e){super(e.type,e.map,e.originalEvent)}}const oe={width:150,scrollAt:4,eventType:A.CONTEXTMENU,defaultItems:!0,items:[]},E="ol-ctx-menu",a={namespace:E,container:`${E}-container`,separator:`${E}-separator`,submenu:`${E}-submenu`,hidden:`${E}-hidden`,icon:`${E}-icon`,zoomIn:`${E}-zoom-in`,zoomOut:`${E}-zoom-out`,unselectable:"ol-unselectable"},H=[{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})}}];function L(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=L("<span>Foo</span>"),r=L("<span>Foo</span>"),i=document.createElement("li"),o=document.createElement("li");i.append(n),o.append(r),e.append(i),e.append(o),s.parentNode?.append(e);const h=e.offsetHeight/2;return s.parentNode?.removeChild(e),h}function k({parentNode:s,item:e,isSubmenu:t=!1,isInsideSubmenu:n=!1,emitter:r}){const i=`_${Math.random().toString(36).slice(2,11)}`;if(typeof e!="string"&&"text"in e){const y=`<span>${e.text}</span>`,_=L(y),v=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}`),v.setAttribute("style",`background-image:url(${e.icon})`)),v.id=i,v.className=e.classname,v.append(_),s.append(v);const S={id:i,isSubmenu:t,isInsideSubmenu:n,isSeparator:!1,callback:"callback"in e?e.callback:null,data:"data"in e?e.data:null};return r.emit(m.ADD_MENU_ENTRY,S,v),v}const o=`<li id="${i}" class="${a.separator}"><hr></li>`,h=L(o);s.append(h);const p=s.lastChild,u={id:i,isSubmenu:!1,isInsideSubmenu:!1,isSeparator:!0,callback:null,data:null};return r.emit(m.ADD_MENU_ENTRY,u,p),p}function N({container:s,items:e,menuWidth:t,isInsideSubmenu:n,emitter:r}){e.forEach(i=>{if(typeof i!="string"&&"items"in i&&Array.isArray(i.items)){const o=k({parentNode:s,item:i,isSubmenu:!0,emitter:r});o.classList.add(a.submenu);const h=document.createElement("ul");h.classList.add(a.container),h.style.width=`${t}px`,o.append(h),N({emitter:r,menuWidth:t,container:h,items:i.items,isInsideSubmenu:!0})}else k({parentNode:s,item:i,isSubmenu:!1,isInsideSubmenu:n,emitter:r})})}const fe="";function T(s,e){if(!s)throw new Error(e)}class le extends Q{constructor(t={}){T(typeof t=="object","@param `opts` should be object type!");const n=document.createElement("div");super({element:n});c(this,"map");c(this,"emitter",new te);c(this,"container");c(this,"coordinate",[]);c(this,"pixel",[]);c(this,"contextMenuEventListener");c(this,"entryCallbackEventListener");c(this,"mapMoveListener");c(this,"lineHeight",0);c(this,"disabled");c(this,"opened");c(this,"items",[]);c(this,"menuEntries",new Map);c(this,"options");this.options={...oe,...t};const r=document.createElement("ul");n.append(r),n.style.width=`${this.options.width}px`,n.classList.add(a.container,a.unselectable,a.hidden),this.container=n,this.contextMenuEventListener=i=>{this.handleContextMenu(i)},this.entryCallbackEventListener=i=>{this.handleEntryCallback(i)},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 H}countItems(){return this.menuEntries.size}extend(t){T(Array.isArray(t),"@param `items` should be an Array."),N({items:t,emitter:this.emitter,menuWidth:this.options.width,container:this.container.firstElementChild})}closeMenu(){this.opened=!1,this.container.classList.add(a.hidden),this.dispatchEvent(m.CLOSE)}isOpen(){return this.opened}updatePosition(t){T(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,t.getViewport().addEventListener(this.options.eventType,this.contextMenuEventListener,!1),t.on("movestart",()=>{this.handleMapMove()}),this.emitter.on(m.ADD_MENU_ENTRY,(r,i)=>{this.handleAddMenuEntry(r,i)},this),this.items=this.options.defaultItems?this.options.items.concat(H):this.options.items,N({items:this.items,emitter:this.emitter,menuWidth:this.options.width,container:this.container.firstElementChild});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),this.emitter.off(m.ADD_MENU_ENTRY)}removeMenuEntry(t){let n=document.getElementById(t);n?.remove(),n=null,this.menuEntries.delete(t)}handleContextMenu(t){this.coordinate=this.map.getEventCoordinate(t),this.pixel=this.map.getEventPixel(t),this.dispatchEvent(new I({map:this.map,originalEvent:t,type:m.BEFOREOPEN})),!this.disabled&&(this.options.eventType===A.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 I({map:this.map,originalEvent:t,type:m.OPEN}))}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]},r=this.getMenuEntriesLength(),i={w:this.container.offsetWidth,h:Math.round(this.lineHeight*r)},o=n.w>=i.w?this.pixel[0]+5:this.pixel[0]-i.w;this.container.style.left=`${o}px`,this.container.style.top=n.h>=i.h?`${this.pixel[1]-10}px`:`${this.pixel[1]-i.h}px`,this.container.style.right="auto",this.container.style.bottom="auto",n.w-=i.w;const h=y=>Array.from(y.children).filter(_=>_.tagName==="LI"&&_.classList.contains(a.submenu));let p=0;const u=(y,_)=>{p+=1,h(y).forEach(S=>{const he=_>=i.w?i.w-8:(i.w+8)*-1,f=S.querySelector(`ul.${a.container}`),ce=Math.round(this.lineHeight*Array.from(f.children).filter(ue=>ue.tagName==="LI").length);f.style.left=`${he}px`,f.style.right="auto",f.style.top=n.h>=ce+i.h?"0":`-${f.offsetHeight-25}px`,f.style.bottom="auto",f.style.zIndex=String(p),h(f).length>0&&u(f,_-i.w)})};u(this.container.firstElementChild,n.w)}handleMapMove(){this.closeMenu()}handleEntryCallback(t){t.preventDefault(),t.stopPropagation();const n=t.currentTarget,r=this.menuEntries.get(n.id);if(!r)return;const i={data:r.data,coordinate:this.coordinate};this.closeMenu(),r.callback?.(i,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});
|
|
8
|
+
(function(l,c){typeof exports=="object"&&typeof module<"u"?module.exports=c(require("ol/control/Control"),require("ol/MapBrowserEvent")):typeof define=="function"&&define.amd?define(["ol/control/Control","ol/MapBrowserEvent"],c):(l=typeof globalThis<"u"?globalThis:l||self,l.ContextMenu=c(l.ol.control.Control,l.ol.MapBrowserEvent))})(this,function(l,c){"use strict";var P=Object.defineProperty;var H=(l,c,p)=>c in l?P(l,c,{enumerable:!0,configurable:!0,writable:!0,value:p}):l[c]=p;var a=(l,c,p)=>(H(l,typeof c!="symbol"?c+"":c,p),p);var p={exports:{}};function x(){}x.prototype={on:function(s,t,e){var n=this.e||(this.e={});return(n[s]||(n[s]=[])).push({fn:t,ctx:e}),this},once:function(s,t,e){var n=this;function o(){n.off(s,o),t.apply(e,arguments)}return o._=t,this.on(s,o,e)},emit:function(s){var t=[].slice.call(arguments,1),e=((this.e||(this.e={}))[s]||[]).slice(),n=0,o=e.length;for(n;n<o;n++)e[n].fn.apply(e[n].ctx,t);return this},off:function(s,t){var e=this.e||(this.e={}),n=e[s],o=[];if(n&&t)for(var i=0,h=n.length;i<h;i++)n[i].fn!==t&&n[i].fn._!==t&&o.push(n[i]);return o.length?e[s]=o:delete e[s],this}},p.exports=x;var T=p.exports.TinyEmitter=x,L=(s=>(s.CONTEXTMENU="contextmenu",s.CLICK="click",s.DBLCLICK="dblclick",s))(L||{}),m=(s=>(s.BEFOREOPEN="beforeopen",s.OPEN="open",s.CLOSE="close",s.ADD_MENU_ENTRY="add-menu-entry",s))(m||{});class S extends c{constructor(t){super(t.type,t.map,t.originalEvent)}}const k={width:150,scrollAt:4,eventType:L.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,t)=>{const e=t.getView();e.animate({zoom:Number(e.getZoom())+1,duration:700,center:s.coordinate})}},{text:"Zoom Out",classname:`${r.zoomOut} ${r.icon}`,callback:(s,t)=>{const e=t.getView();e.animate({zoom:Number(e.getZoom())-1,duration:700,center:s.coordinate})}}];function M(s){const t=document.createDocumentFragment(),e=document.createElement("div");for(e.innerHTML=s;e.firstChild;)t.append(e.firstChild);return t}function I(s){const t=document.importNode(s),e=s.offsetWidth;t.style.cssText=`position: fixed; top: 0; left: 0; overflow: auto; visibility: hidden; pointer-events: none; height: unset; max-height: unset; width: ${e}px`;const n=M("<span>Foo</span>"),o=M("<span>Foo</span>"),i=document.createElement("li"),h=document.createElement("li");i.append(n),h.append(o),t.append(i),t.append(h),s.parentNode?.append(t);const u=t.offsetHeight/2;return s.parentNode?.removeChild(t),u}function $({parentNode:s,item:t,isSubmenu:e=!1,isInsideSubmenu:n=!1,emitter:o}){const i=`_${Math.random().toString(36).slice(2,11)}`;if(typeof t!="string"&&"text"in t){const v=`<span>${t.text}</span>`,g=M(v),E=document.createElement("li");t.classname=t.classname||"",t.icon&&(t.classname===""?t.classname=r.icon:t.classname.includes(r.icon)===!1&&(t.classname+=` ${r.icon}`),E.setAttribute("style",`background-image:url(${t.icon})`)),E.id=i,E.className=t.classname,E.append(g),s.append(E);const N={id:i,isSubmenu:e,isInsideSubmenu:n,isSeparator:!1,callback:"callback"in t?t.callback:null,data:"data"in t?t.data:null};return o.emit(m.ADD_MENU_ENTRY,N,E),E}const h=`<li id="${i}" class="${r.separator}"><hr></li>`,u=M(h);s.append(u);const y=s.lastChild,b={id:i,isSubmenu:!1,isInsideSubmenu:!1,isSeparator:!0,callback:null,data:null};return o.emit(m.ADD_MENU_ENTRY,b,y),y}function w({container:s,items:t,menuWidth:e,isInsideSubmenu:n,emitter:o}){t.forEach(i=>{if(typeof i!="string"&&"items"in i&&Array.isArray(i.items)){const h=$({parentNode:s,item:i,isSubmenu:!0,emitter:o});h.classList.add(r.submenu);const u=document.createElement("ul");u.classList.add(r.container),u.style.width=`${e}px`,h.append(u),w({emitter:o,menuWidth:e,container:u,items:i.items,isInsideSubmenu:!0})}else $({parentNode:s,item:i,isSubmenu:!1,isInsideSubmenu:n,emitter:o})})}const U="";function C(s,t){if(!s)throw new Error(t)}class O extends l{constructor(e={}){C(typeof e=="object","@param `opts` should be object type!");const n=document.createElement("div");super({element:n});a(this,"map");a(this,"emitter",new T);a(this,"container");a(this,"coordinate",[]);a(this,"pixel",[]);a(this,"contextMenuEventListener");a(this,"entryCallbackEventListener");a(this,"mapMoveListener");a(this,"lineHeight",0);a(this,"disabled");a(this,"opened");a(this,"items",[]);a(this,"menuEntries",new Map);a(this,"options");this.options={...k,...e};const o=document.createElement("ul");n.append(o),n.style.width=`${this.options.width}px`,n.classList.add(r.container,r.unselectable,r.hidden),this.container=n,this.contextMenuEventListener=i=>{this.handleContextMenu(i)},this.entryCallbackEventListener=i=>{this.handleEntryCallback(i)},this.mapMoveListener=()=>{this.handleMapMove()},this.disabled=!1,this.opened=!1,window.addEventListener("beforeunload",()=>{this.removeListeners()},{once:!0})}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){C(Array.isArray(e),"@param `items` should be an Array."),w({items:e,emitter:this.emitter,menuWidth:this.options.width,container:this.container.firstElementChild})}closeMenu(){this.opened=!1,this.container.classList.add(r.hidden),this.dispatchEvent(m.CLOSE)}isOpen(){return this.opened}updatePosition(e){C(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,e.getViewport().addEventListener(this.options.eventType,this.contextMenuEventListener,!1),e.on("movestart",()=>{this.handleMapMove()}),this.emitter.on(m.ADD_MENU_ENTRY,(o,i)=>{this.handleAddMenuEntry(o,i)},this),this.items=this.options.defaultItems?this.options.items.concat(A):this.options.items,w({items:this.items,emitter:this.emitter,menuWidth:this.options.width,container:this.container.firstElementChild});const n=this.getMenuEntriesLength();this.lineHeight=n>0?this.container.offsetHeight/n:I(this.container)}else this.removeListeners(),this.clear()}removeListeners(){this.map.getViewport().removeEventListener(this.options.eventType,this.contextMenuEventListener,!1),this.emitter.off(m.ADD_MENU_ENTRY)}removeMenuEntry(e){let n=document.getElementById(e);n?.remove(),n=null,this.menuEntries.delete(e)}handleContextMenu(e){this.coordinate=this.map.getEventCoordinate(e),this.pixel=this.map.getEventPixel(e),this.dispatchEvent(new S({map:this.map,originalEvent:e,type:m.BEFOREOPEN})),!this.disabled&&(this.options.eventType===L.CONTEXTMENU&&(e.stopPropagation(),e.preventDefault()),setTimeout(()=>{this.openMenu(e)}),e.target?.addEventListener("pointerdown",n=>{this.opened&&(n.stopPropagation(),this.closeMenu())},{once:!0}))}openMenu(e){this.opened=!0,this.positionContainer(),this.container.classList.remove(r.hidden),this.dispatchEvent(new S({map:this.map,originalEvent:e,type:m.OPEN}))}getMenuEntriesLength(){return Array.from(this.menuEntries).filter(([,e])=>e.isSeparator===!1&&e.isSubmenu===!1&&e.isInsideSubmenu===!1).length}positionContainer(){const e=this.map.getSize()||[0,0],n={w:e[0]-this.pixel[0],h:e[1]-this.pixel[1]},o=this.getMenuEntriesLength(),i={w:this.container.offsetWidth,h:Math.round(this.lineHeight*o)},h=n.w>=i.w?this.pixel[0]+5:this.pixel[0]-i.w;this.container.style.left=`${h}px`,this.container.style.top=n.h>=i.h?`${this.pixel[1]-10}px`:`${this.pixel[1]-i.h}px`,this.container.style.right="auto",this.container.style.bottom="auto",n.w-=i.w;const u=v=>Array.from(v.children).filter(g=>g.tagName==="LI"&&g.classList.contains(r.submenu));let y=0;const b=(v,g)=>{y+=1,u(v).forEach(N=>{const D=g>=i.w?i.w-8:(i.w+8)*-1,d=N.querySelector(`ul.${r.container}`),_=Math.round(this.lineHeight*Array.from(d.children).filter(z=>z.tagName==="LI").length);d.style.left=`${D}px`,d.style.right="auto",d.style.top=n.h>=_+i.h?"0":`-${d.offsetHeight-25}px`,d.style.bottom="auto",d.style.zIndex=String(y),u(d).length>0&&b(d,g-i.w)})};b(this.container.firstElementChild,n.w)}handleMapMove(){this.closeMenu()}handleEntryCallback(e){e.preventDefault(),e.stopPropagation();const n=e.currentTarget,o=this.menuEntries.get(n.id);if(!o)return;const i={data:o.data,coordinate:this.coordinate};this.closeMenu(),o.callback?.(i,this.map)}handleAddMenuEntry(e,n){this.menuEntries.set(e.id,e),this.positionContainer(),"callback"in e&&typeof e.callback=="function"&&n.addEventListener("click",this.entryCallbackEventListener,!1)}}return O});
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ol-contextmenu",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Custom Context Menu for Openlayers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/ol-contextmenu.umd.cjs",
|
|
@@ -8,15 +8,17 @@
|
|
|
8
8
|
"module": "./dist/ol-contextmenu.js",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"import":
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/ol-contextmenu.d.ts",
|
|
13
|
+
"default": "./dist/ol-contextmenu.js"
|
|
14
|
+
}
|
|
14
15
|
},
|
|
15
|
-
"
|
|
16
|
-
"import": "
|
|
17
|
-
"require": "
|
|
18
|
-
"style": "
|
|
19
|
-
}
|
|
16
|
+
"./*": {
|
|
17
|
+
"import": "./*",
|
|
18
|
+
"require": "./*",
|
|
19
|
+
"style": "./*"
|
|
20
|
+
},
|
|
21
|
+
"./ol-contextmenu.css": "./dist/ol-contextmenu.css"
|
|
20
22
|
},
|
|
21
23
|
"files": [
|
|
22
24
|
"./dist"
|
|
@@ -33,7 +35,7 @@
|
|
|
33
35
|
"url": "https://github.com/jonataswalker/ol-contextmenu/issues"
|
|
34
36
|
},
|
|
35
37
|
"engines": {
|
|
36
|
-
"node": "
|
|
38
|
+
"node": ">=16",
|
|
37
39
|
"npm": ">=8"
|
|
38
40
|
},
|
|
39
41
|
"scripts": {
|
|
@@ -47,29 +49,29 @@
|
|
|
47
49
|
"prepare": "husky install"
|
|
48
50
|
},
|
|
49
51
|
"devDependencies": {
|
|
50
|
-
"@swc/core": "^1.3.
|
|
51
|
-
"@swc/jest": "^0.2.
|
|
52
|
-
"@types/jest": "^29.5.
|
|
53
|
-
"eslint": "^8.
|
|
54
|
-
"eslint-config-jwalker": "^8.
|
|
55
|
-
"eslint-config-prettier": "^
|
|
56
|
-
"eslint-plugin-jest": "^27.
|
|
57
|
-
"eslint-plugin-prettier": "^
|
|
52
|
+
"@swc/core": "^1.3.87",
|
|
53
|
+
"@swc/jest": "^0.2.29",
|
|
54
|
+
"@types/jest": "^29.5.5",
|
|
55
|
+
"eslint": "^8.49.0",
|
|
56
|
+
"eslint-config-jwalker": "^8.7.0",
|
|
57
|
+
"eslint-config-prettier": "^9.0.0",
|
|
58
|
+
"eslint-plugin-jest": "^27.4.0",
|
|
59
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
58
60
|
"husky": "^8.0.3",
|
|
59
61
|
"identity-obj-proxy": "^3.0.0",
|
|
60
|
-
"jest": "^29.
|
|
61
|
-
"jest-environment-jsdom": "^29.
|
|
62
|
-
"ol": "^
|
|
63
|
-
"prettier": "^
|
|
64
|
-
"rimraf": "^
|
|
65
|
-
"sass": "^1.
|
|
66
|
-
"typescript": "^5.
|
|
67
|
-
"vite": "^4.
|
|
68
|
-
"vite-plugin-banner": "^0.7.
|
|
69
|
-
"vite-plugin-dts": "^
|
|
62
|
+
"jest": "^29.7.0",
|
|
63
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
64
|
+
"ol": "^8.1.0",
|
|
65
|
+
"prettier": "^3.0.3",
|
|
66
|
+
"rimraf": "^5.0.1",
|
|
67
|
+
"sass": "^1.68.0",
|
|
68
|
+
"typescript": "^5.2.2",
|
|
69
|
+
"vite": "^4.4.9",
|
|
70
|
+
"vite-plugin-banner": "^0.7.1",
|
|
71
|
+
"vite-plugin-dts": "^3.5.4"
|
|
70
72
|
},
|
|
71
73
|
"peerDependencies": {
|
|
72
|
-
"ol": "
|
|
74
|
+
"ol": "> 7.x <= 8.x"
|
|
73
75
|
},
|
|
74
76
|
"dependencies": {
|
|
75
77
|
"tiny-emitter": "^2.1.0"
|