UNPKG

integralui-web-grid

Version:

IntegralUI Web - Grid is a native Web Component that displays tabular data sets

15 lines (12 loc) 13.5 kB
/* filename: integralui.contextmenu.js version : 21.4.0 Copyright © 2016-2021 Lidor Systems. All rights reserved. This file is part of the "IntegralUI Web" Library. The contents of this file are subject to the IntegralUI Web License, and may not be used except in compliance with the License. A copy of the License should have been installed in the product's root installation directory or it can be found at http://www.lidorsystems.com/products/web/studio/license-agreement.aspx. This SOFTWARE is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Any infringement will be prosecuted under applicable laws. */ import{L as LitElement,c as css,T as TemplateResult,a as defaultTemplateProcessor,h as html}from"../external/lit-element.js";import{c as classMap}from"../external/class-map.js";import{s as styleMap}from"../external/style-map.js";import IntegralUIBase,{IntegralUITComponent}from"./integralui.base.js";import IntegralUICommonService from"../services/integralui.common.service.js";import{IntegralUITheme,IntegralUIDirection}from"./integralui.enums.js";import"./integralui.menuitem.js";import{iuiContextMenuDefaultStyle}from"../styles/integralui.contextmenu.style.js";import{iuiContextMenuOfficeStyle}from"../themes/office/integralui.contextmenu.office.js";import{iuiContextMenuMidnightStyle}from"../themes/midnight/integralui.contextmenu.midnight.js";class IntegralUIContextMenuWindow extends LitElement{constructor(){super();this._commonService=new IntegralUICommonService();this._currentOptions={};this._menuItems=[];this._updateOptions();this._blockDisplay="none";this._blockElemWidth="0";this._blockElemHeight="0";this._blockOverflow="hidden";this._blockOpacity=0;this._currentResourcePath="../icons";this._currentControlStyleSettings=iuiContextMenuDefaultStyle;this._currentCustomStyle=[];this._currentTheme=IntegralUITheme.None;this._currentThemeSettings=css``;this._ctrlClass={};this._generalClassName="iui-contextmenu";this._initTimout=null;this._initStyle()}_initStyle(){this._defaultStyle={general:{disabled:this._generalClassName+"-disabled",focused:this._generalClassName+"-focused",normal:this._generalClassName,hovered:this._generalClassName+"-hovered",selected:this._generalClassName+"-selected"}};this.refresh();this._initFocus()}_initFocus(){let e=this;e._initTimout=setTimeout(function(){if(e._elemRef)e._elemRef.focus()},1)}connectedCallback(){}disconnectedCallback(){this._rt();if(this._initTimout)clearTimeout(this._initTimout);this._initTimout=null}attributeChangedCallback(e,t,i){super.attributeChangedCallback(e,t,i)}static get properties(){return{allowAnimation:{type:Boolean,attribute:"allow-animation",relect:!0},customStyle:{type:Object,attribute:"custom-style"},options:{type:Object},resourcePath:{type:String,attribute:"resource-path",relect:!0},theme:{converter:{fromAttribute:e=>{switch((e=e.replace(/"|'/,"").replace(/"|'/,"")).toLowerCase()){case"office":return IntegralUITheme.Office;case"midnight":return IntegralUITheme.Midnight;default:return IntegralUITheme.None}},toAttribute:e=>{switch(e){case IntegralUITheme.Office:return"Office";case IntegralUITheme.Midnight:return"Midnight";default:return"None"}}},reflect:!0}}}get allowAnimation(){return this._isAnimationAllowed}set allowAnimation(e){if(this._isAnimationAllowed!==e){const t=this._isAnimationAllowed;this._isAnimationAllowed=e;this.requestUpdate("allowAnimation",t)}}get customStyle(){return this._currentCustomStyle}set customStyle(e){if(this._currentCustomStyle!==e){const t=this._currentCustomStyle;this._currentCustomStyle=e;this.requestUpdate("customStyle",t);this.refresh()}}get options(){return this._currentOptions}set options(e){const t=this._currentOptions;this._updateOptions(e);this.refresh();this.requestUpdate("options",t)}get resourcePath(){return this._currentResourcePath}set resourcePath(e){if(this._currentResourcePath!==e){const t=this._currentResourcePath;this._currentResourcePath=e;this._updateControlStyleSettings(e);this.requestUpdate("resourcePath",t)}}get theme(){return this._currentTheme}set theme(e){if(this._currentTheme!==e){const t=this._currentTheme;this._currentTheme=e;this._updateThemeSettings(e);this.requestUpdate("theme",t)}}_updateOptions(e){if(e)this._currentOptions={adjustment:this._commonService.isFieldAvailable(e.adjustment,{top:0,left:0}),autoClose:this._commonService.isFieldAvailable(e.autoClose,!0),direction:this._commonService.isFieldAvailable(e.direction,IntegralUIDirection.None),inverse:this._commonService.isFieldAvailable(e.inverse,!1),items:this._commonService.isFieldAvailable(e.items,[]),mode:this._commonService.isFieldAvailable(e.mode,"normal"),position:this._commonService.isFieldAvailable(e.position,{x:0,y:-9999999})};else this._currentOptions={adjustment:{top:0,left:0},autoClose:!0,direction:IntegralUIDirection.None,inverse:!1,items:[],mode:"normal",position:{x:0,y:-9999999}}}_invokeEvent(e,t,i,n){let s=new CustomEvent(e,{detail:t,bubbles:void 0!==i?i:!1,composed:void 0!==n?n:!1});this.dispatchEvent(s);return t}_onBlur(e){if(!1!==this.autoClose)this._invokeEvent("closed",{event:e})}_onRightClick(e){e.preventDefault()}_defaultFunc(){}invokeCtrlMethod(e,t){switch(e){case"MENU_CLICK":this._invokeEvent("menuClick",{event:t.event,item:t.item});break;default:this._defaultFunc()}}_getSize(){return{width:this._elemRef.offsetWidth,height:this._elemRef.offsetHeight}}_adjustPosition(e,t,i,n,s){let o=this,r=o._commonService.getShiftPos();setTimeout(function(){let l=o._getSize(),c=i;if("manual"===t&&s){c.x=s.left+s.width+r.x+o._currentOptions.adjustment.left;c.y=s.bottom+r.y+4+o._currentOptions.adjustment.top;if(o._currentOptions.direction&IntegralUIDirection.Above)c.y-=l.height;if(o._currentOptions.inverse||o._currentOptions.direction&IntegralUIDirection.Left)c.x-=l.width}if(n)if(c.y+l.height>n.height){let e=c.y+l.height-n.height;c.y-=e}o._currentOptions.position=c;o.update();o._updateReferences();o._invokeEvent("menuOpened",{event:e})},10)}open(e,t,i,n,s){let o=this;if(o._currentOptions.items){o._blockDisplay="block";o._blockOpacity=1;o._ut();if(!o._tCmp)o._at();o._adjustPosition(e,t,i,n,s)}}_getClassName(){return this._generalClassName}_getControlClass(){return this._ctrlClass}_updateControlClass(){this._ctrlClass={};this._ctrlClass["iui-contextmenu"]=!0}_updateThemeSettings(e){this._currentThemeSettings=css``;switch(e){case IntegralUITheme.Office:this._currentThemeSettings.cssText=this._commonService.replaceAll(iuiContextMenuOfficeStyle.cssText,"../icons",this._currentResourcePath);break;case IntegralUITheme.Midnight:this._currentThemeSettings.cssText=this._commonService.replaceAll(iuiContextMenuMidnightStyle.cssText,"../icons",this._currentResourcePath);break;default:this._currentThemeSettings.cssText=""}}_getItemTemplate(e){if(this.itemTemplate){let t=this.itemTemplate(e);return new TemplateResult(t.strings,t.values,"html",defaultTemplateProcessor)}else{let t={},i=this._commonService.isString(e.icon)?e.icon.split(" "):[];i.map(e=>t[e]=!0);return html` ${i.length>0?html`<span class=${classMap(t)}></span>`:html``} ${e.iconUrl?html`<img style=${styleMap({verticalAlign:"middle"})} src="${e.iconUrl} />`:html``} <span class="iui-item-label" >${e.text}</span> `}}firstUpdated(e){this._updateReferences()}refresh(){this._updateControlClass();this.update();this._updateReferences()}render(){return html` <style> ${this._currentControlStyleSettings} ${this._currentThemeSettings} ${this._currentCustomStyle} </style> <div data-ctrl="contextmenu-win" class=${classMap(this._getControlClass())} style=${styleMap({top:this._currentOptions.position.y+"px",left:this._currentOptions.position.x+"px",height:"auto",opacity:this._blockOpacity})} @blur="${e=>this._onBlur(e)}" @contextmenu="${e=>this._onRightClick(e)}" tabindex="999"> <ul id="content"> ${this._currentOptions.items.map(e=>html` <iui-menuitem .allowAnimation="${this.allowAnimation}" .customStyle="${[this._currentControlStyleSettings,this._currentThemeSettings,this._currentCustomStyle]}" .data="${e}" .items="${e.items}" .enabled="${e.enabled}" .icon="${e.icon}" .iconUrl="${e.iconUrl}" .level="0" .resourcePath="${this.resourcePath}" .text="${e.text}" .templateRef="${this._getItemTemplate(e)}" .theme="${this.theme}"></iui-menuitem> `)} </ul> </div> `}_updateControlStyleSettings(e){this._currentControlStyleSettings=css``;this._currentControlStyleSettings.cssText=this._commonService.replaceAll(iuiContextMenuDefaultStyle.cssText,"../icons",e)}_updateReferences(){this._elemRef=this.shadowRoot.querySelector("div[data-ctrl=contextmenu-win]");this._contentElem=this.shadowRoot.querySelector("#content");if(this._contentElem){this._menuItems=this._contentElem.querySelectorAll("iui-menuitem");if(this._menuItems)this._menuItems.forEach(e=>e.setParent(this))}}_at(){if(this._elemRef){this._tCmp=document.createElement("iui-tc",{is:IntegralUITComponent});if(this._tCmp){this._elemRef.appendChild(this._tCmp);this._ut()}}}_rt(){if(this._tCmp)this._tCmp.parentNode.removeChild(this._tCmp);this._tCmp=null}_ut(){this._tCmp=this._elemRef?this._elemRef.querySelector("iui-tc"):null}}window.customElements.define("iui-contextmenu-win",IntegralUIContextMenuWindow);class IntegralUIContextMenu extends IntegralUIBase{constructor(){super();this._currentSettings=null;this._winScrollPos={top:0,left:0};this._cmpRef=null;this._cmp=null;this._currentThemeSettings=css``}connectedCallback(){}disconnectedCallback(){this._removeContextMenuWindow()}attributeChangedCallback(e,t,i){super.attributeChangedCallback(e,t,i)}static get properties(){return{settings:{type:Object}}}get settings(){return this._currentSettings}set settings(e){if(this._currentSettings!==e){const t=this._currentSettings;this._currentSettings=e;this.requestUpdate("settings",t)}}_closeContextMenu(){if(this._cmpRef)this._cmpRef.close()}_removeContextMenu(){this._removeContextMenuWindow()}_menuItemClick(e){this._invokeEvent("menuClick",{event:e.detail.event,item:e.detail.item});this._removeContextMenuWindow()}_getSize(){return{width:this._elemRef.offsetWidth,height:this._elemRef.offsetHeight}}_isInsideClick(e){this._contentSlotElem=this.shadowRoot.querySelector("slot").assignedNodes();if(this._contentSlotElem){this._contentSlotElem=this._contentSlotElem.filter(e=>1===e.nodeType);if(this._contentSlotElem&&this._contentSlotElem.length>0){let t=this._commonService.getPageRect(this._contentSlotElem[0]),i=this._commonService.getMousePos(e),n=this._commonService.getShiftPos();i.x-=n.x;i.y-=n.y;return i.x>=t.left&&i.x<=t.right&&i.y>=t.top&&i.y<=t.bottom}}return!1}_ctrlContextMenu(e){if("manual"!==this._currentSettings.position)this._processMenuOpen(e)}_ctrlMouseDown(e){if("manual"===this._currentSettings.position&&(this._currentSettings.buttonType<0||e.which===this._currentSettings.buttonType))this._processMenuOpen(e,"manual")}_processMenuOpen(e,t){if(!this._isInsideClick(e))return;e.preventDefault();let i=this;i._winScrollPos={top:document.documentElement.scrollTop,left:document.documentElement.scrollLeft};let n={cancel:!1,event:e};i._invokeEvent("menuOpening",n);if(!0!==n.cancel){i._addContextMenuWindow();if(i._cmpRef){i._cmpRef.allowAnimation=i.allowAnimation;i._cmpRef.customStyle=i.customStyle;i._cmpRef.itemTemplate=i.itemTemplate;i._cmpRef.options=i._currentSettings;i._cmpRef.options.mode=t;i._cmpRef.resourcePath=i._currentResourcePath;i._cmpRef.theme=i._currentTheme;let n=i._currentSettings.appSize?i._currentSettings.appSize:{width:window.innerWidth,height:window.innerHeight};if("manual"===t)i._cmpRef.open(e,t,{x:0,y:0},n,i._elemRef.getBoundingClientRect());else i._cmpRef.open(e,t,{x:e.pageX,y:e.pageY},n);setTimeout(function(){window.scrollTo(i._winScrollPos.left,i._winScrollPos.top)},5);this._removeContextMenu=this._removeContextMenu.bind(this);this._cmpRef.addEventListener("closed",this._removeContextMenu);this._menuItemClick=this._menuItemClick.bind(this);this._cmpRef.addEventListener("menuClick",this._menuItemClick);if("manual"===t)e.stopPropagation()}}}_addContextMenuWindow(){this._removeContextMenuWindow();this._cmpRef=document.createElement("iui-contextmenu-win",{is:IntegralUIContextMenuWindow});document.body.appendChild(this._cmpRef)}_removeContextMenuWindow(){if(this._cmpRef){this._cmpRef.removeEventListener("menuClick",this._menuItemClick);this._cmpRef.removeEventListener("closed",this._removeContextMenu);this._cmpRef.parentNode.removeChild(this._cmpRef)}this._cmpRef=null}firstUpdated(e){this._updateReferences()}render(){return html` <style> </style> <div data-ctrl="contextmenu" style=${styleMap(this._getControlStyle())} @contextmenu="${e=>this._ctrlContextMenu(e)}" @mousedown="${e=>this._ctrlMouseDown(e)}"> <slot></slot> </div> `}_updateReferences(){this._elemRef=this.shadowRoot.querySelector("div[data-ctrl=contextmenu]")}}window.customElements.define("iui-contextmenu",IntegralUIContextMenu);export default IntegralUIContextMenu;export{IntegralUIContextMenuWindow};