mj-context-menu
Version:
A generic context menu
1 lines • 34.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ContextMenu=e():t.ContextMenu=e()}(this,()=>(()=>{"use strict";var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{ContextMenu:()=>et,CssStyles:()=>st,Info:()=>it,Parser:()=>ot,ParserFactory:()=>ut,Popup:()=>nt,SelectionBox:()=>at,Variable:()=>ht,version:()=>rt});var s,i={};t.r(i),t.d(i,{addMenuStyles:()=>_}),function(t){t[t.RETURN=13]="RETURN",t[t.ESCAPE=27]="ESCAPE",t[t.SPACE=32]="SPACE",t[t.LEFT=37]="LEFT",t[t.UP=38]="UP",t[t.RIGHT=39]="RIGHT",t[t.DOWN=40]="DOWN"}(s||(s={}));const n="click",o="dblclick",r="mousedown",a="mouseup",h="mouseover",u="mouseout",l="selectstart";class c{constructor(){this.bubble=!1}bubbleKey(){this.bubble=!0}keydown(t){switch(t.keyCode){case s.ESCAPE:this.escape(t);break;case s.RIGHT:this.right(t);break;case s.LEFT:this.left(t);break;case s.UP:this.up(t);break;case s.DOWN:this.down(t);break;case s.RETURN:case s.SPACE:this.space(t);break;default:return}this.bubble?this.bubble=!1:this.stop(t)}escape(t){}space(t){}left(t){}right(t){}up(t){}down(t){}stop(t){t&&(t.stopPropagation(),t.preventDefault(),t.cancelBubble=!0)}mousedown(t){return this.stop(t)}mouseup(t){return this.stop(t)}mouseover(t){return this.stop(t)}mouseout(t){return this.stop(t)}click(t){return this.stop(t)}addEvents(t){t.addEventListener(r,this.mousedown.bind(this)),t.addEventListener(a,this.mouseup.bind(this)),t.addEventListener(h,this.mouseover.bind(this)),t.addEventListener(u,this.mouseout.bind(this)),t.addEventListener(n,this.click.bind(this)),t.addEventListener("keydown",this.keydown.bind(this)),t.addEventListener("dragstart",this.stop.bind(this)),t.addEventListener(l,this.stop.bind(this)),t.addEventListener("contextmenu",this.stop.bind(this)),t.addEventListener(o,this.stop.bind(this))}}class d extends c{addAttributes(t){for(const e in t)this.html.setAttribute(e,t[e])}get html(){return this._html||this.generateHtml(),this._html}set html(t){this._html=t,this.addEvents(t)}generateHtml(){const t=document.createElement("div");t.classList.add(this.className),t.setAttribute("role",this.role),this.html=t}focus(){const t=this.html;t.setAttribute("tabindex","0"),setTimeout(()=>t.focus(),50)}unfocus(){const t=this.html;t.hasAttribute("tabindex")&&t.setAttribute("tabindex","-1");try{t.blur()}catch(t){}t.blur()}}class p extends d{constructor(){super(...arguments),this.posted=!1}isPosted(){return this.posted}post(t,e){this.posted||(void 0!==t&&void 0!==e&&this.html.setAttribute("style","left: "+t+"px; top: "+e+"px;"),this.display(),this.posted=!0)}unpost(){if(!this.posted)return;const t=this.html;t.parentNode&&t.parentNode.removeChild(t),this.posted=!1}}function m(t){return"CtxtMenu_"+t}function b(t){return m(t)}function f(t){return m(t)}const E={ATTACHED:b("Attached"),CONTEXTMENU:b("ContextMenu"),MENU:b("Menu"),MENUARROW:b("MenuArrow"),MENUACTIVE:b("MenuActive"),MENUCHECK:b("MenuCheck"),MENUCLOSE:b("MenuClose"),MENUCOMBOBOX:b("MenuComboBox"),MENUDISABLED:b("MenuDisabled"),MENUFRAME:b("MenuFrame"),MENUITEM:b("MenuItem"),MENULABEL:b("MenuLabel"),MENURADIOCHECK:b("MenuRadioCheck"),MENUINPUTBOX:b("MenuInputBox"),MENURULE:b("MenuRule"),MENUSLIDER:b("MenuSlider"),MOUSEPOST:b("MousePost"),RTL:b("RTL"),INFO:b("Info"),INFOCLOSE:b("InfoClose"),INFOCONTENT:b("InfoContent"),INFOSIGNATURE:b("InfoSignature"),INFOTITLE:b("InfoTitle"),SLIDERVALUE:b("SliderValue"),SLIDERBAR:b("SliderBar"),SELECTION:b("Selection"),SELECTIONBOX:b("SelectionBox"),SELECTIONMENU:b("SelectionMenu"),SELECTIONDIVIDER:b("SelectionDivider"),SELECTIONITEM:b("SelectionItem")},g={COUNTER:f("Counter"),KEYDOWNFUNC:f("keydownFunc"),CONTEXTMENUFUNC:f("contextmenuFunc"),OLDTAB:f("Oldtabindex"),TOUCHFUNC:f("TouchFunc")};class v extends d{constructor(t,e){super(),this._menu=t,this._type=e,this.className=E.MENUITEM,this.role="menuitem",this.hidden=!1}get menu(){return this._menu}set menu(t){this._menu=t}get type(){return this._type}hide(){this.hidden=!0,this.menu.generateMenu()}show(){this.hidden=!1,this.menu.generateMenu()}isHidden(){return this.hidden}}function C(t){const e=t.menu;e.baseMenu?e.baseMenu.unpost():e.unpost()}function M(t){const e=t.menu;return(e.baseMenu?e.baseMenu:e).store.active}function x(t,e){console.error("ContextMenu Error: "+e)}function w(){return N++}let N=0;class y extends v{constructor(t,e,s,i){super(t,e),this._content=s,this.disabled=!1,this.callbacks=[],this._id=i||s}get content(){return this._content}set content(t){this._content=t,this.generateHtml(),this.menu&&this.menu.generateHtml()}get id(){return this._id}press(){this.disabled||(this.executeAction(),this.executeCallbacks_())}executeAction(){}registerCallback(t){-1===this.callbacks.indexOf(t)&&this.callbacks.push(t)}unregisterCallback(t){const e=this.callbacks.indexOf(t);-1!==e&&this.callbacks.splice(e,1)}mousedown(t){this.press(),this.stop(t)}mouseover(t){this.focus(),this.stop(t)}mouseout(t){this.deactivate(),this.stop(t)}generateHtml(){super.generateHtml();const t=this.html;t.setAttribute("aria-disabled","false"),t.textContent=this.content}activate(){this.disabled||this.html.classList.add(E.MENUACTIVE)}deactivate(){this.html.classList.remove(E.MENUACTIVE)}focus(){this.menu.focused=this,super.focus(),this.activate()}unfocus(){this.deactivate(),super.unfocus()}escape(t){C(this)}up(t){this.menu.up(t)}down(t){this.menu.down(t)}left(t){this.menu.left(t)}right(t){this.menu.right(t)}space(t){this.press()}disable(){this.disabled=!0;const t=this.html;t.classList.add(E.MENUDISABLED),t.setAttribute("aria-disabled","true")}enable(){this.disabled=!1;const t=this.html;t.classList.remove(E.MENUDISABLED),t.removeAttribute("aria-disabled")}executeCallbacks_(){for(const t of this.callbacks)try{t(this)}catch(t){x(0,"Callback for menu entry "+this.id+" failed.")}}}class L extends y{static fromJson(t,{content:e,menu:s,id:i},n){const o=new this(n,e,i),r=t.get("subMenu")(t,s,o);return o.submenu=r,o}constructor(t,e,s){super(t,"submenu",e,s),this._submenu=null}set submenu(t){this._submenu=t}get submenu(){return this._submenu}mouseover(t){this.focus(),this.stop(t)}mouseout(t){this.stop(t)}unfocus(){if(this.submenu.isPosted()){if(this.menu.focused!==this)return super.unfocus(),void this.menu.unpostSubmenus();this.html.setAttribute("tabindex","-1"),this.html.blur()}else super.unfocus()}focus(){super.focus(),this.submenu.isPosted()||this.disabled||this.submenu.post()}executeAction(){this.submenu.isPosted()?this.submenu.unpost():this.submenu.post()}generateHtml(){super.generateHtml();const t=this.html;this.span=document.createElement("span"),this.span.textContent="\u25ba",this.span.classList.add(E.MENUARROW),this.span.setAttribute("aria-hidden","true"),t.appendChild(this.span),t.setAttribute("aria-haspopup","true")}left(t){this.submenu.isPosted()?this.submenu.unpost():super.left(t)}right(t){this.submenu.isPosted()?this.submenu.down(t):this.submenu.post()}toJson(){return{type:""}}}class A extends p{constructor(){super(...arguments),this.className=E.CONTEXTMENU,this.role="menu",this._items=[],this._baseMenu=null}set baseMenu(t){this._baseMenu=t}get baseMenu(){return this._baseMenu}get items(){return this._items}set items(t){this._items=t}get pool(){return this.variablePool}get focused(){return this._focused}set focused(t){if(this._focused===t)return;this._focused||this.unfocus();const e=this._focused;this._focused=t,e&&e.unfocus()}up(t){const e=this.items.filter(t=>t instanceof y&&!t.isHidden());if(0===e.length)return;if(!this.focused)return void e[e.length-1].focus();let s=e.indexOf(this.focused);-1!==s&&(s=s?--s:e.length-1,e[s].focus())}down(t){const e=this.items.filter(t=>t instanceof y&&!t.isHidden());if(0===e.length)return;if(!this.focused)return void e[0].focus();let s=e.indexOf(this.focused);-1!==s&&(s++,s=s===e.length?0:s,e[s].focus())}generateHtml(){super.generateHtml(),this.generateMenu()}generateMenu(){const t=this.html;t.classList.add(E.MENU);for(const e of this.items){if(!e.isHidden()){t.appendChild(e.html);continue}const s=e.html;s.parentNode&&s.parentNode.removeChild(s)}}post(t,e){this.variablePool.update(),super.post(t,e)}unpostSubmenus(){const t=this.items.filter(t=>t instanceof L);for(const e of t)e.submenu.unpost(),e!==this.focused&&e.unfocus()}unpost(){super.unpost(),this.unpostSubmenus(),this.focused=null}find(t){for(const e of this.items)if("rule"!==e.type){if(e.id===t)return e;if("submenu"===e.type){const s=e.submenu.find(t);if(s)return s}}return null}}class T{constructor(t){this.menu=t,this.store=[],this._active=null,this.counter=0,this.attachedClass=E.ATTACHED+"_"+w(),this.taborder=!0,this.attrMap={}}set active(t){do{if(-1!==this.store.indexOf(t)){this._active=t;break}t=t.parentNode}while(t)}get active(){return this._active}next(){const t=this.store.length;if(0===t)return this.active=null,null;let e=this.store.indexOf(this.active);return e=-1===e?0:e<t-1?e+1:0,this.active=this.store[e],this.active}previous(){const t=this.store.length;if(0===t)return this.active=null,null;const e=t-1;let s=this.store.indexOf(this.active);return s=-1===s||0===s?e:s-1,this.active=this.store[s],this.active}clear(){this.remove(this.store)}insert(t){const e=t instanceof HTMLElement?[t]:t;for(const t of e)this.insertElement(t);this.sort()}remove(t){const e=t instanceof HTMLElement?[t]:t;for(const t of e)this.removeElement(t);this.sort()}inTaborder(t){this.taborder&&!t&&this.removeTaborder(),!this.taborder&&t&&this.insertTaborder(),this.taborder=t}insertTaborder(){this.taborder&&this.insertTaborder_()}removeTaborder(){this.taborder&&this.removeTaborder_()}insertElement(t){t.classList.contains(this.attachedClass)||(t.classList.add(this.attachedClass),this.taborder&&this.addTabindex(t),this.addEvents(t))}removeElement(t){t.classList.contains(this.attachedClass)&&(t.classList.remove(this.attachedClass),this.taborder&&this.removeTabindex(t),this.removeEvents(t))}sort(){const t=document.getElementsByClassName(this.attachedClass);this.store=[].slice.call(t)}insertTaborder_(){this.store.forEach(t=>t.setAttribute("tabindex","0"))}removeTaborder_(){this.store.forEach(t=>t.setAttribute("tabindex","-1"))}addTabindex(t){t.hasAttribute("tabindex")&&t.setAttribute(g.OLDTAB,t.getAttribute("tabindex")),t.setAttribute("tabindex","0")}removeTabindex(t){t.hasAttribute(g.OLDTAB)?(t.setAttribute("tabindex",t.getAttribute(g.OLDTAB)),t.removeAttribute(g.OLDTAB)):t.removeAttribute("tabindex")}addEvents(t){t.hasAttribute(g.COUNTER)||(this.addEvent(t,"contextmenu",this.menu.post.bind(this.menu)),this.addEvent(t,"keydown",this.keydown.bind(this)),t.setAttribute(g.COUNTER,this.counter.toString()),this.counter++)}addEvent(t,e,s){const i=g[e.toUpperCase()+"FUNC"];this.attrMap[i+this.counter]=s,t.addEventListener(e,s)}removeEvents(t){if(!t.hasAttribute(g.COUNTER))return;const e=t.getAttribute(g.COUNTER);this.removeEvent(t,"contextmenu",e),this.removeEvent(t,"keydown",e),t.removeAttribute(g.COUNTER)}removeEvent(t,e,s){const i=g[e.toUpperCase()+"FUNC"],n=this.attrMap[i+s];t.removeEventListener(e,n)}keydown(t){t.keyCode===s.SPACE&&(this.menu.post(t),t.preventDefault(),t.stopImmediatePropagation())}}class S{constructor(){this.pool={}}insert(t){this.pool[t.name]=t}lookup(t){return this.pool[t]}remove(t){delete this.pool[t]}update(){for(const t in this.pool)this.pool[t].update()}}class O extends A{static fromJson(t,{pool:e,items:s,id:i=""}){const n=new this(t);n.id=i;const o=t.get("variable");e.forEach(e=>o(t,e,n.pool));const r=t.get("items")(t,s,n);return n.items=r,n}constructor(t){super(),this.factory=t,this.id="",this.moving=!1,this._store=new T(this),this.widgets=[],this.variablePool=new S}generateHtml(){this.isPosted()&&this.unpost(),super.generateHtml(),this._frame=document.createElement("div"),this._frame.classList.add(E.MENUFRAME);const t="left: 0px; top: 0px; z-index: 200; width: 100%; height: 100%; border: 0px; padding: 0px; margin: 0px;";this._frame.setAttribute("style","position: absolute; "+t);const e=document.createElement("div");e.setAttribute("style","position: fixed; "+t),this._frame.appendChild(e),e.addEventListener("mousedown",function(t){this.unpost(),this.unpostWidgets(),this.stop(t)}.bind(this))}display(){document.body.appendChild(this.frame),this.frame.appendChild(this.html),this.focus()}escape(t){this.unpost(),this.unpostWidgets()}unpost(){if(super.unpost(),this.widgets.length>0)return;this.frame.parentNode.removeChild(this.frame);const t=this.store;this.moving||t.insertTaborder(),t.active.focus()}left(t){this.move_(this.store.previous())}right(t){this.move_(this.store.next())}get frame(){return this._frame}get store(){return this._store}post(t,e){if(void 0!==e)return this.moving||this.store.removeTaborder(),void super.post(t,e);const s=t;let i,n,o;if(s instanceof Event?(i=s.target,this.stop(s)):i=s,s instanceof MouseEvent&&(n=s.pageX,o=s.pageY,n||o||!s.clientX||(n=s.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,o=s.clientY+document.body.scrollTop+document.documentElement.scrollTop)),!n&&!o&&i){const t=window.pageXOffset||document.documentElement.scrollLeft,e=window.pageYOffset||document.documentElement.scrollTop,s=i.getBoundingClientRect();n=(s.right+s.left)/2+t,o=(s.bottom+s.top)/2+e}this.store.active=i,this.anchor=this.store.active;const r=this.html;n+r.offsetWidth>document.body.offsetWidth-5&&(n=document.body.offsetWidth-r.offsetWidth-5),this.post(n,o)}registerWidget(t){this.widgets.push(t)}unregisterWidget(t){const e=this.widgets.indexOf(t);e>-1&&this.widgets.splice(e,1),0===this.widgets.length&&this.unpost()}unpostWidgets(){this.widgets.forEach(t=>t.unpost())}toJson(){return{type:""}}move_(t){this.anchor&&t!==this.anchor&&(this.moving=!0,this.unpost(),this.post(t),this.moving=!1)}}function U(t){return"."+(E[t]||t)}function I(t,e=";"){return"{\n "+t.join(e+"\n ")+";\n}"}const k={};k[U("MENU")]=I(["position: absolute","background-color: white","color: black","width: auto","padding: 5px 0px","border: 1px solid #CCCCCC","margin: 0","cursor: default","font: menu","text-align: left","text-indent: 0","text-transform: none","line-height: normal","letter-spacing: normal","word-spacing: normal","word-wrap: normal","white-space: nowrap","float: none","z-index: 1001","border-radius: 5px","box-shadow: 0px 10px 20px #808080"]),k[U("MENUITEM")]=I(["padding: 1px 2em","background: transparent"]),k[U("MENUARROW")]=I(["position: absolute","right: 0.5em","padding-top: 0.25em","color: #666666","font-family: null","font-size: 0.75em"]),k[U("MENUACTIVE")+" "+U("MENUARROW")]=I(["color: white"]),k[U("MENUARROW")+U("RTL")]=I(["left: 0.5em","right: auto"]),k[U("MENUCHECK")]=I(["position: absolute","left: 0.7em","font-family: null"]),k[U("MENUCHECK")+U("RTL")]=I(["right: 0.7em","left: auto"]),k[U("MENURADIOCHECK")]=I(["position: absolute","left: 0.7em"]),k[U("MENURADIOCHECK")+U("RTL")]=I(["right: 0.7em","left: auto"]),k[U("MENUINPUTBOX")]=I(["padding-left: 1em","right: 0.5em","color: #666666","font-family: null"]),k[U("MENUINPUTBOX")+U("RTL")]=I(["left: 0.1em"]),k[U("MENUCOMBOBOX")]=I(["left: 0.1em","padding-bottom: 0.5em"]),k[U("MENUSLIDER")]=I(["left: 0.1em"]),k[U("SLIDERVALUE")]=I(["position: absolute","right: 0.1em","padding-top: 0.25em","color: #333333","font-size: 0.75em"]),k[U("MENUACTIVE")+" "+U("SLIDERVALUE")]=I(["color: #DDDDDD"]),k[U("SLIDERBAR")]=I(["outline: none","background: #D3D3D3"]),k[U("MENULABEL")]=I(["padding: 1px 2em 3px 1.33em","font-style: italic"]),k[U("MENURULE")]=I(["border-top: 1px solid #DDDDDD","margin: 4px 3px"]),k[U("MENUDISABLED")]=I(["color: #999"]),k[U("MENUACTIVE")]=I(["background-color: #606872","color: white"]),k[U("MENUDISABLED")+":focus"]=I(["background-color: #E8E8E8"]),k[U("MENULABEL")+":focus"]=I(["background-color: #E8E8E8"]),k[U("CONTEXTMENU")+":focus"]=I(["outline: none"]),k[U("CONTEXTMENU")+" "+U("MENUITEM")+":focus"]=I(["outline: none"]),k[U("SELECTIONMENU")]=I(["position: relative","float: left","border-bottom: none","box-shadow: none ! important","border-radius: 0px !important"]),k[U("SELECTIONITEM")]=I(["padding-right: 1em"]),k[U("SELECTION")]=I(["right: 40%","width: 50%"]),k[U("SELECTIONBOX")]=I(["padding: 0em","max-height: 20em","max-width: none","background-color: #FFFFFF"]),k[U("SELECTIONDIVIDER")]=I(["clear: both","border-top: 2px solid #000000"]),k[U("MENU")+" "+U("MENUCLOSE")]=I(["top: -10px","left: -10px"]),k["@media (prefers-color-scheme: dark) /* menu */"]=I([U("MENU")+" "+I(["color: #E0E0E0","background-color: #242436","box-shadow: 0px 10px 20px #000","border: 1px solid #808080"]),U("SLIDERVALUE")+" "+I(["color: #D0D0D0"]),U("MENUDISABLED")+":focus "+I(["background-color: #383838"]),U("MENULABEL")+":focus "+I(["background-color: #585858"]),U("MENURULE")+" "+I(["border-top: 1px solid #808080"]),U("SELECTIONDIVIDER")+" "+I(["border-top: 2px solid #808080"])],"");const D={};D[U("MENUCLOSE")]=I(["position: absolute","cursor: pointer","display: inline-block","border: 2px solid #AAA","border-radius: 18px",'font-family: "Courier New", Courier',"font-size: 24px","color: #F0F0F0"]),D[U("MENUCLOSE")+" span"]=I(["display: block","background-color: #AAA","border: 1.5px solid","border-radius: 18px","line-height: 0","padding: 8px 0 6px"]),D[U("MENUCLOSE")+":hover"]=I(["color: white !important","border: 2px solid #CCC !important"]),D[U("MENUCLOSE")+":hover span"]=I(["background-color: #CCC !important"]),D[U("MENUCLOSE")+":hover:focus"]=I(["outline: none"]);const R="MJX-Menu-styles";function _(t){H(Object.assign(Object.assign({},k),D),t)}function H(t,e=document,s=R){if(e.head.querySelector("#"+s))return;const i=e.createElement("style");i.id=s;const n=[];for(const e in t)n.push(`${e} ${t[e]}`);i.textContent=n.join("\n"),e.head.appendChild(i)}class P extends p{constructor(t){super(),this.element=t,this.className=E.MENUCLOSE,this.role="button"}generateHtml(){const t=document.createElement("span");t.classList.add(this.className),t.setAttribute("role",this.role),t.setAttribute("tabindex","0");const e=document.createElement("span");e.textContent="\xd7",t.appendChild(e),this.html=t}display(){}unpost(){super.unpost(),this.element.unpost()}keydown(t){this.bubbleKey(),super.keydown(t)}space(t){this.unpost(),this.stop(t)}mousedown(t){this.unpost(),this.stop(t)}}class B extends p{constructor(t,e,s){super(),this.title=t,this.signature=s,this.className=E.INFO,this.role="dialog",this.contentDiv=this.generateContent(),this.close=this.generateClose(),this.content=e||function(){return""}}attachMenu(t){this.menu=t}generateHtml(){super.generateHtml();const t=this.html;t.appendChild(this.generateTitle()),t.appendChild(this.contentDiv),t.appendChild(this.generateSignature()),t.appendChild(this.close.html),t.setAttribute("tabindex","0")}post(){super.post();const t=document.documentElement,e=this.html,s=window.innerHeight||t.clientHeight||t.scrollHeight||0,i=Math.floor(-e.offsetWidth/2),n=Math.floor((s-e.offsetHeight)/3);e.setAttribute("style","margin-left: "+i+"px; top: "+n+"px;"),window.event instanceof MouseEvent&&e.classList.add(E.MOUSEPOST),e.focus()}display(){this.menu.registerWidget(this),this.contentDiv.innerHTML=this.content();const t=this.menu.html;t.parentNode&&t.parentNode.removeChild(t),this.menu.frame.appendChild(this.html)}click(t){}keydown(t){this.bubbleKey(),super.keydown(t)}escape(t){this.unpost()}unpost(){super.unpost(),this.html.classList.remove(E.MOUSEPOST),this.menu.unregisterWidget(this)}generateClose(){const t=new P(this),e=t.html;return e.classList.add(E.INFOCLOSE),e.setAttribute("aria-label","Close Dialog Box"),t}generateTitle(){const t=document.createElement("span");return t.innerHTML=this.title,t.classList.add(E.INFOTITLE),t}generateContent(){const t=document.createElement("div");return t.classList.add(E.INFOCONTENT),t.setAttribute("tabindex","0"),t}generateSignature(){const t=document.createElement("span");return t.innerHTML=this.signature,t.classList.add(E.INFOSIGNATURE),t}toJson(){return{type:""}}}class J extends p{constructor(t,e){super(),this.title=t,this.window=null,this.localSettings={left:Math.round((screen.width-400)/2),top:Math.round((screen.height-300)/3)},this.windowList=[],this.mobileFlag=!1,this.active=null,this.content=e||function(){return""}}attachMenu(t){this.menu=t}post(){this.display()}display(){this.active=this.menu.store.active;const t=[];for(const e in J.popupSettings)t.push(e+"="+J.popupSettings[e]);for(const e in this.localSettings)t.push(e+"="+this.localSettings[e]);this.window=window.open("","_blank",t.join(",")),this.windowList.push(this.window);const e=this.window.document;this.mobileFlag?(e.open(),e.write('<html><head><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>'+this.title+'</title></head><body style="font-size:85%">'),e.write("<pre>"+this.generateContent()+"</pre>"),e.write('<hr><input type="button" value="Close" onclick="window.close()" />'),e.write("</body></html>"),e.close()):(e.open(),e.write("<html><head><title>"+this.title+'</title></head><body style="font-size:85%">'),e.write("<table><tr><td><pre>"+this.generateContent()+"</pre></td></tr></table>"),e.write("</body></html>"),e.close(),setTimeout(this.resize.bind(this),50))}unpost(){this.windowList.forEach(t=>t.close()),this.window=null}generateContent(){return this.content(this.active)}resize(){const t=this.window.document.body.firstChild;let e=this.window.outerHeight-this.window.innerHeight||30,s=this.window.outerWidth-this.window.innerWidth||30;s=Math.max(140,Math.min(Math.floor(.5*this.window.screen.width),t.offsetWidth+s+25)),e=Math.max(40,Math.min(Math.floor(.5*this.window.screen.height),t.offsetHeight+e+25)),this.window.resizeTo(s,e);const i=this.active.getBoundingClientRect();if(i){const t=Math.max(0,Math.min(i.right-Math.floor(s/2),this.window.screen.width-s-20)),n=Math.max(0,Math.min(i.bottom-Math.floor(e/2),this.window.screen.height-e-20));this.window.moveTo(t,n)}this.active=null}toJson(){return{type:""}}}J.popupSettings={status:"no",toolbar:"no",locationbar:"no",menubar:"no",directories:"no",personalbar:"no",resizable:"yes",scrollbars:"yes",width:400,height:300};class F extends y{static fromJson(t,{content:e,action:s,id:i},n){return new this(n,e,s,i)}constructor(t,e,s,i){super(t,"command",e,i),this.command=s}mousedown(t){this.stop(t)}mouseup(t){this.press(),this.stop(t)}executeAction(){try{this.command(M(this))}catch(t){x(0,"Illegal command callback.")}C(this)}toJson(){return{type:""}}}class V{static fromJson(t,{name:e,getter:s,setter:i},n){const o=new this(e,s,i);n.insert(o)}constructor(t,e,s){this._name=t,this.getter=e,this.setter=s,this.items=[]}get name(){return this._name}getValue(t){try{return this.getter(t)}catch(t){return x(0,"Command of variable "+this.name+" failed."),null}}setValue(t,e){try{this.setter(t,e)}catch(t){x(0,"Command of variable "+this.name+" failed.")}this.update()}register(t){-1===this.items.indexOf(t)&&this.items.push(t)}unregister(t){const e=this.items.indexOf(t);-1!==e&&this.items.splice(e,1)}update(){this.items.forEach(t=>t.update())}registerCallback(t){this.items.forEach(e=>e.registerCallback(t))}unregisterCallback(t){this.items.forEach(e=>e.unregisterCallback(t))}toJson(){return{type:"variable",name:this.name,getter:this.getter.toString(),setter:this.setter.toString()}}}class W extends y{generateHtml(){super.generateHtml();const t=this.html;this.span||this.generateSpan(),t.appendChild(this.span),this.update()}register(){this.variable.register(this)}unregister(){this.variable.unregister(this)}update(){this.updateAria(),this.span&&this.updateSpan()}}class X extends W{static fromJson(t,{content:e,variable:s,id:i},n){return new this(n,e,s,i)}constructor(t,e,s,i){super(t,"checkbox",e,i),this.role="menuitemcheckbox",this.variable=t.pool.lookup(s),this.register()}executeAction(){this.variable.setValue(!this.variable.getValue()),C(this)}generateSpan(){this.span=document.createElement("span"),this.span.textContent="\u2713",this.span.classList.add(E.MENUCHECK),this.span.setAttribute("aria-hidden","true")}updateAria(){this.html.setAttribute("aria-checked",this.variable.getValue()?"true":"false")}updateSpan(){this.span.style.display=this.variable.getValue()?"":"none"}toJson(){return{type:""}}}class z extends W{static fromJson(t,{content:e,variable:s,id:i},n){return new this(n,e,s,i)}constructor(t,e,s,i){super(t,"combobox",e,i),this.role="combobox",this.inputEvent=!1,this.variable=t.pool.lookup(s),this.register()}executeAction(){this.variable.setValue(this.input.value,M(this))}space(t){super.space(t),C(this)}focus(){super.focus(),this.input.focus()}unfocus(){super.unfocus(),this.updateSpan()}generateHtml(){super.generateHtml();this.html.classList.add(E.MENUCOMBOBOX)}generateSpan(){this.span=document.createElement("span"),this.span.classList.add(E.MENUINPUTBOX),this.input=document.createElement("input"),this.input.addEventListener("keydown",this.inputKey.bind(this)),this.input.setAttribute("size","10em"),this.input.setAttribute("type","text"),this.input.setAttribute("tabindex","-1"),this.span.appendChild(this.input)}inputKey(t){this.bubbleKey(),this.inputEvent=!0}keydown(t){if(this.inputEvent&&t.keyCode!==s.ESCAPE&&t.keyCode!==s.RETURN)return this.inputEvent=!1,void t.stopPropagation();super.keydown(t),t.stopPropagation()}updateAria(){}updateSpan(){let t;try{t=this.variable.getValue(M(this))}catch(e){t=""}this.input.value=t}toJson(){return{type:""}}}class K extends y{static fromJson(t,{content:e,id:s},i){return new this(i,e,s)}constructor(t,e,s){super(t,"label",e,s)}generateHtml(){super.generateHtml();this.html.classList.add(E.MENULABEL)}toJson(){return{type:""}}}class j extends W{static fromJson(t,{content:e,variable:s,id:i},n){return new this(n,e,s,i)}constructor(t,e,s,i){super(t,"radio",e,i),this.role="menuitemradio",this.variable=t.pool.lookup(s),this.register()}executeAction(){this.variable.setValue(this.id),C(this)}generateSpan(){this.span=document.createElement("span"),this.span.textContent="\u2713",this.span.classList.add(E.MENURADIOCHECK),this.span.setAttribute("aria-hidden","true")}updateAria(){this.html.setAttribute("aria-checked",this.variable.getValue()===this.id?"true":"false")}updateSpan(){this.span.style.display=this.variable.getValue()===this.id?"":"none"}toJson(){return{type:""}}}class G extends v{static fromJson(t,e,s){return new this(s)}constructor(t){super(t,"rule"),this.className=E.MENUITEM,this.role="separator"}generateHtml(){super.generateHtml();const t=this.html;t.classList.add(E.MENURULE),t.setAttribute("aria-orientation","vertical")}addEvents(t){}toJson(){return{type:"rule"}}}class Y extends W{static fromJson(t,{content:e,variable:s,id:i},n){return new this(n,e,s,i)}constructor(t,e,s,i){super(t,"slider",e,i),this.role="slider",this.labelId="ctx_slideLabel"+w(),this.valueId="ctx_slideValue"+w(),this.inputEvent=!1,this.variable=t.pool.lookup(s),this.register()}executeAction(){this.variable.setValue(this.input.value,M(this)),this.update()}space(t){super.space(t),C(this)}focus(){super.focus(),this.input.focus()}unfocus(){super.unfocus(),this.updateSpan()}generateHtml(){super.generateHtml();this.html.classList.add(E.MENUSLIDER),this.valueSpan=document.createElement("span"),this.valueSpan.setAttribute("id",this.valueId),this.valueSpan.classList.add(E.SLIDERVALUE),this.html.appendChild(this.valueSpan)}generateSpan(){this.span=document.createElement("span"),this.labelSpan=document.createElement("span"),this.labelSpan.setAttribute("id",this.labelId),this.labelSpan.appendChild(this.html.childNodes[0]),this.html.appendChild(this.labelSpan),this.input=document.createElement("input"),this.input.setAttribute("type","range"),this.input.setAttribute("min","0"),this.input.setAttribute("max","100"),this.input.setAttribute("aria-valuemin","0"),this.input.setAttribute("aria-valuemax","100"),this.input.setAttribute("aria-labelledby",this.labelId),this.input.addEventListener("keydown",this.inputKey.bind(this)),this.input.addEventListener("input",this.executeAction.bind(this)),this.input.classList.add(E.SLIDERBAR),this.span.appendChild(this.input)}inputKey(t){this.inputEvent=!0}mousedown(t){t.stopPropagation()}mouseup(t){C(this),t.stopPropagation()}keydown(t){const e=t.keyCode;return e===s.UP||e===s.DOWN?(t.preventDefault(),void super.keydown(t)):this.inputEvent&&e!==s.ESCAPE&&e!==s.RETURN?(this.inputEvent=!1,void t.stopPropagation()):(super.keydown(t),void t.stopPropagation())}updateAria(){const t=this.variable.getValue();t&&this.input&&(this.input.setAttribute("aria-valuenow",t),this.input.setAttribute("aria-valuetext",t+"%"))}updateSpan(){let t;try{t=this.variable.getValue(M(this)),this.valueSpan.innerHTML=t+"%"}catch(e){t=""}this.input.value=t}toJson(){return{type:""}}}class q extends A{static fromJson(t,{items:e},s){const i=new this(s),n=t.get("items")(t,e,i);return i.items=n,i}constructor(t){super(),this._anchor=t,this.variablePool=this.anchor.menu.pool,this.setBaseMenu()}get anchor(){return this._anchor}post(){if(!this.anchor.menu.isPosted())return;let t=this.anchor.html;const e=this.html,s=this.baseMenu.frame,i=t.offsetWidth;let n=i-2,o=0;for(;t&&t!==s;)n+=t.offsetLeft,o+=t.offsetTop,t=t.parentNode;n+e.offsetWidth>document.body.offsetWidth-5&&(n=Math.max(5,n-i-e.offsetWidth+6)),super.post(n,o)}display(){this.baseMenu.frame.appendChild(this.html)}setBaseMenu(){let t=this;do{t=t.anchor.menu}while(t instanceof q);this.baseMenu=t}left(t){this.focused=null,this.anchor.focus()}toJson(){return{type:""}}}class $ extends A{static fromJson(t,{title:e,values:s,variable:i},n){const o=new this(n),r=t.get("label")(t,{content:e||"",id:e||"id"},o),a=t.get("rule")(t,{},o),h=s.map(e=>t.get("radio")(t,{content:e,variable:i,id:e},o)),u=[r,a].concat(h);return o.items=u,o}constructor(t){super(),this.anchor=t,this.className=E.SELECTIONMENU,this.variablePool=this.anchor.menu.pool,this.baseMenu=this.anchor.menu}generateHtml(){super.generateHtml(),this.items.forEach(t=>t.html.classList.add(E.SELECTIONITEM))}display(){}right(t){this.anchor.right(t)}left(t){this.anchor.left(t)}}class Q extends B{static fromJson(t,{title:e,signature:s,selections:i,order:n,grid:o},r){const a=new this(e,s,n,o);a.attachMenu(r);const h=i.map(e=>t.get("selectionMenu")(t,e,a));return a.selections=h,a}constructor(t,e,s="none",i="vertical"){super(t,null,e),this.style=s,this.grid=i,this._selections=[],this.prefix="ctxt-selection",this._balanced=!0}attachMenu(t){this.menu=t}get selections(){return this._selections}set selections(t){this._selections=[],t.forEach(t=>this.addSelection(t))}addSelection(t){t.anchor=this,this._selections.push(t)}rowDiv(t){const e=document.createElement("div");this.contentDiv.appendChild(e);const s=t.map(t=>(e.appendChild(t.html),t.html.id||(t.html.id=this.prefix+w()),t.html.getBoundingClientRect())),i=s.map(t=>t.width),n=i.reduce((t,e)=>t+e,0),o=s.reduce((t,e)=>Math.max(t,e.height),0);return e.classList.add(E.SELECTIONDIVIDER),e.setAttribute("style","height: "+o+"px;"),[e,n,o,i]}display(){if(super.display(),this.order(),!this.selections.length)return;const t=[];let e=0,s=[];const i=this.getChunkSize(this.selections.length);for(let n=0;n<this.selections.length;n+=i){const o=this.selections.slice(n,n+i),[r,a,h,u]=this.rowDiv(o);t.push(r),e=Math.max(e,a),o.forEach(t=>t.html.style.height=h+"px"),s=this.combineColumn(s,u)}this._balanced&&(this.balanceColumn(t,s),e=s.reduce((t,e)=>t+e,20)),t.forEach(t=>t.style.width=e+"px")}getChunkSize(t){switch(this.grid){case"square":return Math.floor(Math.sqrt(t));case"horizontal":return Math.floor(t/Q.chunkSize);default:return Q.chunkSize}}balanceColumn(t,e){t.forEach(t=>{const s=Array.from(t.children);for(let t,i=0;t=s[i];i++)t.style.width=e[i]+"px"})}combineColumn(t,e){let s=[],i=0;for(;t[i]||e[i];){if(!t[i]){s=s.concat(e.slice(i));break}if(!e[i]){s=s.concat(t.slice(i));break}s.push(Math.max(t[i],e[i])),i++}return s}left(t){this.move(t,t=>(0===t?this.selections.length:t)-1)}right(t){this.move(t,t=>t===this.selections.length-1?0:t+1)}generateHtml(){super.generateHtml(),this.html.classList.add(E.SELECTION)}generateContent(){const t=super.generateContent();return t.classList.add(E.SELECTIONBOX),t.removeAttribute("tabindex"),t}findSelection(t){const e=t.target;let s=null;if(e.id&&(s=this.selections.find(t=>t.html.id===e.id)),!s){const t=e.parentElement.id;s=this.selections.find(e=>e.html.id===t)}return s}move(t,e){const s=this.findSelection(t);s.focused&&s.focused.unfocus();const i=e(this.selections.indexOf(s));this.selections[i].focus()}order(){this.selections.sort(Q.orderMethod.get(this.style))}toJson(){return{type:""}}}Q.chunkSize=4,Q.orderMethod=new Map([["alphabetical",(t,e)=>t.items[0].content.localeCompare(e.items[0].content)],["none",(t,e)=>1],["decreasing",(t,e)=>{const s=t.items.length,i=e.items.length;return s<i?1:i<s?-1:0}],["increasing",(t,e)=>{const s=t.items.length,i=e.items.length;return s<i?-1:i<s?1:0}]]);class Z{constructor(t){this._parser=new Map(t)}get(t){return this._parser.get(t)}add(t,e){this._parser.set(t,e)}}var tt=function(t,e){var s={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(s[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(i=Object.getOwnPropertySymbols(t);n<i.length;n++)e.indexOf(i[n])<0&&Object.prototype.propertyIsEnumerable.call(t,i[n])&&(s[i[n]]=t[i[n]])}return s};const et=O,st=i,it=B,nt=J,ot=class{constructor(t=[]){this._initList=[["command",F.fromJson.bind(F)],["checkbox",X.fromJson.bind(X)],["combo",z.fromJson.bind(z)],["slider",Y.fromJson.bind(Y)],["label",K.fromJson.bind(K)],["radio",j.fromJson.bind(j)],["rule",G.fromJson.bind(G)],["submenu",L.fromJson.bind(L)],["contextMenu",O.fromJson.bind(O)],["subMenu",q.fromJson.bind(q)],["variable",V.fromJson.bind(V)],["items",this.items.bind(this)],["selectionMenu",$.fromJson.bind($)],["selectionBox",Q.fromJson.bind(Q)]],this._factory=new Z(this._initList),t.forEach(([t,e])=>this.factory.add(t,e))}get factory(){return this._factory}items(t,e,s){const i=[];for(const t of e){const e=this.parse(t,s);e&&(s.items.push(e),t.disabled&&e.disable(),t.hidden&&i.push(e))}return i.forEach(t=>t.hide()),s.items}parse(t,...e){var{type:s}=t,i=tt(t,["type"]);const n=this.factory.get(s);return n?n(this.factory,i,...e):null}},rt="1.0.0",at=Q,ht=V,ut=Z;return e})());