forked from nkappler/ctxmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.min.js
2 lines (2 loc) · 7.98 KB
/
index.min.js
1
2
"use strict";function e(e,t,n){if(n||2===arguments.length)for(var i,o=0,r=t.length;o<r;o++)!i&&o in t||(i||(i=Array.prototype.slice.call(t,0,o)),i[o]=t[o]);return e.concat(i||Array.prototype.slice.call(t))}function t(e){return"function"==typeof e?e():e}function n(e){return i(e)||o(e)||r(e)||function(e){return e.hasOwnProperty("html")||e.hasOwnProperty("element")}(e)}function i(e){return e.hasOwnProperty("action")}function o(e){return e.hasOwnProperty("href")}function r(e){return e.hasOwnProperty("subMenu")}function a(e,t){var n;e.addEventListener("mouseenter",(function(e){n=setTimeout((function(){return t(e)}),150)})),e.addEventListener("mouseleave",(function(){return clearTimeout(n)}))}function d(e){return t(e.disabled)||r(e)&&"function"!=typeof e.subMenu&&0===e.subMenu.length}function c(e){var a=document.createElement("li");if(function(e){return e.hasOwnProperty("isDivider")}(e))return a.className="divider",a;if(function(e,n){var i=t(e.html),o="<span>".concat(t(e.text),"</span>"),r=t(e.element);r?n.append(r):n.innerHTML=i||o,n.title=t(e.tooltip)||"",e.style&&n.setAttribute("style",t(e.style));e.icon&&(n.classList.add("icon"),n.innerHTML+='<img class="icon" src="'.concat(t(e.icon),'" />'));for(var a=0,d=Object.entries(t(e.events)||{});a<d.length;a++){var c=d[a],s=c[0],u=c[1],l="function"==typeof u?{listener:u,options:{}}:u,h=l.listener,p=l.options;n.addEventListener(s,h,p)}}(e,a),!n(e))return a.classList.add("heading"),a;if(d(e))return a.classList.add("disabled"),r(e)&&a.classList.add("submenu"),a;if(a.classList.add("interactive"),o(e)){var c=document.createElement("a");return c.append.apply(c,Array.from(a.childNodes)),c.href=t(e.href),e.hasOwnProperty("download")&&(c.download=t(e.download)),e.hasOwnProperty("target")&&(c.target=t(e.target)),a.append(c),a}return i(e)?(a.addEventListener("click",e.action),a):(a.classList.add("submenu"),a)}Object.defineProperty(exports,"__esModule",{value:!0}),"function"==typeof SuppressedError&&SuppressedError;var s="r",u="d";function l(e,t){var n=f(),i=window.visualViewport,o=i.width,r=i.height;Object.assign(e.style,{maxHeight:r/n.y+"px",maxWidth:o/n.x+"px"});var a=function(e){var t=e.cloneNode(!0);t.style.visibility="hidden",document.body.appendChild(t);var n=p(t);return document.body.removeChild(t),n}(e);a.width=Math.trunc(a.width)+1,a.height=Math.trunc(a.height)+1;var d={x:0,y:0};if(t instanceof Element){var c=p(t),l=c.x,m=c.width,v=c.y;d={x:"r"===s?l+m:l-a.width,y:v},t.className.includes("submenu")&&(d.y+="d"===u?4:-12);var y=h(a,d);d.x!==y.x&&(s="r"===s?"l":"r",d.x="r"===s?l+m:l-a.width),d.y!==y.y&&(u="u"===u?"d":"u",d.y=y.y),d=h(a,d)}else{var x=""!==document.body.style.transform?document.body.getBoundingClientRect():{x:0,y:0};d=h(a,{x:(t.clientX-x.x)/n.x,y:(t.clientY-x.y)/n.y})}Object.assign(e.style,{left:d.x+"px",top:d.y+"px",width:a.width+"px",height:a.height+"px"})}function h(e,t){var n=window.visualViewport,i=n.width,o=n.height,r=""!==document.body.style.transform?document.body.getBoundingClientRect():{left:0,top:0},a=r.left,d=r.top,c=f(),l=-a/c.x,h=-d/c.y,p=(i-a)/c.x,m=(o-d)/c.y;return{x:"r"===s?t.x+e.width>p?p-e.width:t.x:t.x<l?l:t.x,y:"d"===u?t.y+e.height>m?m-e.height:t.y:t.y<h?h:t.y+e.height>m?m-e.height:t.y}}function p(e){var t=e.offsetLeft,n=e.offsetTop,i=e.offsetHeight,o=e.offsetWidth;if(e.offsetParent instanceof HTMLElement){var r=p(e.offsetParent);return{x:t+r.x,y:n+r.y,width:o,height:i}}return{x:t,y:n,width:o,height:i}}function f(){var e=document.body,t=e.getBoundingClientRect();return{x:t.width/e.offsetWidth,y:t.height/e.offsetHeight}}var m=function(){function i(){var e=this;this.cache={},this.preventCloseOnScroll=!1,window.addEventListener("click",(function(t){var n=t.target instanceof Element&&t.target.parentElement;n&&"interactive"===n.className||e.hide()})),window.addEventListener("resize",(function(){e.hide()}));var t=0;window.addEventListener("wheel",(function(){clearTimeout(t),t=setTimeout((function(){e.preventCloseOnScroll?e.preventCloseOnScroll=!1:e.hide()}))}),{passive:!0}),window.addEventListener("keydown",(function(t){"Escape"===t.key&&e.hide()})),i.addStylesToDom()}return i.getInstance=function(){i.instance||(i.instance=new i);var e=i.instance;return{attach:e.attach.bind(e),delete:e.delete.bind(e),hide:e.hide.bind(e),show:e.show.bind(e),update:e.update.bind(e)}},i.prototype.attach=function(t,n,i){var o=this;void 0===i&&(i=function(e){return e});var r=document.querySelector(t);if(void 0===this.cache[t])if(r){var a=function(t){var r=i(e([],n,!0),t);o.show(r,t)};this.cache[t]={ctxMenu:n,handler:a,beforeRender:i},r.addEventListener("contextmenu",a)}else console.error("target element ".concat(t," not found"));else console.error("target element ".concat(t," already has a context menu assigned. Use ContextMenu.update() intstead."))},i.prototype.update=function(e,t,n){var i=this.cache[e],o=document.querySelector(e);i&&(null==o||o.removeEventListener("contextmenu",i.handler)),delete this.cache[e],this.attach(e,t||(null==i?void 0:i.ctxMenu)||[],n||(null==i?void 0:i.beforeRender))},i.prototype.delete=function(e){var t=this.cache[e];if(t){var n=document.querySelector(e);n?(n.removeEventListener("contextmenu",t.handler),delete this.cache[e]):console.error("target element ".concat(e," does not exist (anymore)"))}else console.error("no context menu for target element ".concat(e," found"))},i.prototype.show=function(t,n){var i=this;n instanceof MouseEvent&&n.stopImmediatePropagation(),this.hide(),this.menu=this.generateDOM(e([],t,!0),n),document.body.appendChild(this.menu),this.menu.addEventListener("wheel",(function(){i.preventCloseOnScroll=!0}),{passive:!0}),n instanceof MouseEvent&&n.preventDefault()},i.prototype.hide=function(e){var t;void 0===e&&(e=this.menu),s="r",u="d",e&&(e===this.menu&&delete this.menu,null===(t=e.parentElement)||void 0===t||t.removeChild(e))},i.prototype.generateDOM=function(e,i){var o=this,s=document.createElement("ul");return 0===e.length&&(s.style.display="none"),e.forEach((function(e){var i=c(e);a(i,(function(){var e,t=null===(e=i.parentElement)||void 0===e?void 0:e.querySelector("ul");t&&t.parentElement!==i&&o.hide(t)})),n(e)&&!d(e)&&(r(e)?a(i,(function(n){i.querySelector("ul")||o.openSubMenu(n,t(e.subMenu),i)})):i.addEventListener("click",(function(){o.hide()}))),s.appendChild(i)})),s.className="ctxmenu",l(s,i),s.addEventListener("contextmenu",(function(e){e.stopPropagation(),e.preventDefault()})),s.addEventListener("click",(function(e){var t=e.target instanceof Element&&e.target.parentElement;t&&"interactive"!==t.className&&e.stopPropagation()})),s},i.prototype.openSubMenu=function(e,t,n){var i,o=null===(i=n.parentElement)||void 0===i?void 0:i.querySelector("li > ul");o&&o.parentElement!==n&&this.hide(o),n.appendChild(this.generateDOM(t,n))},i.addStylesToDom=function(){var e=function(){if("loading"===document.readyState)return document.addEventListener("readystatechange",(function(){e()}));var t=document.createElement("style");t.innerHTML='html{min-height:100%}.ctxmenu{position:fixed;border:1px solid #999;padding:2px 0;box-shadow:#aaa 3px 3px 3px;background:#fff;margin:0;z-index:9999;overflow-y:auto;font:15px Verdana, sans-serif;box-sizing:border-box}.ctxmenu li{margin:1px 0;display:block;position:relative;user-select:none}.ctxmenu li.heading{font-weight:bold;margin-left:-5px}.ctxmenu li span{display:block;padding:2px 20px;cursor:default}.ctxmenu li a{color:inherit;text-decoration:none}.ctxmenu li.icon{padding-left:15px}.ctxmenu img.icon{position:absolute;width:18px;left:10px;top:2px}.ctxmenu li.disabled{color:#ccc}.ctxmenu li.divider{border-bottom:1px solid #aaa;margin:5px 0}.ctxmenu li.interactive:hover{background:rgba(0, 0, 0, .1)}.ctxmenu li.submenu::after{content:"";position:absolute;display:block;top:0;bottom:0;right:.4em;margin:auto .1rem auto auto;border-right:1px solid #000;border-top:1px solid #000;transform:rotate(45deg);width:.3rem;height:.3rem}.ctxmenu li.submenu.disabled::after{border-color:#ccc}',document.head.insertBefore(t,document.head.childNodes[0]),e=function(){}};e()},i}().getInstance();
/*! ctxMenu v1.6.2 | (c) Nikolaj Kappler | https://github.com/nkappler/ctxmenu/blob/master/LICENSE !*/exports.ctxmenu=m;