2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
import e,{Component as t}from"react";import{DomHandler as n,classNames as i,ObjectUtils as r,ZIndexUtils as o}from"primereact/utils";import{CSSTransition as s}from"primereact/csstransition";import{Ripple as u}from"primereact/ripple";import{Portal as c}from"primereact/portal";import l from"primereact/api";function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function m(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function f(e,t,n){return t&&m(e.prototype,t),n&&m(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function p(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function d(e,t){return d=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},d(e,t)}function h(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&d(e,t)}function v(e){return v="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},v(e)}function b(e,t){if(t&&("object"===v(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return p(e)}function y(e){return y=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},y(e)}function E(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function k(e){var t=L();return function(){var n,i=y(e);if(t){var r=y(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return b(this,n)}}function L(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var R=function(o){h(l,t);var c=k(l);function l(t){var n;return a(this,l),(n=c.call(this,t)).state={activeItem:null},n.onEnter=n.onEnter.bind(p(n)),n.submenuRef=e.createRef(),n}return f(l,[{key:"onItemMouseEnter",value:function(e,t){t.disabled?e.preventDefault():this.setState({activeItem:t})}},{key:"onItemClick",value:function(e,t){t.disabled?e.preventDefault():(t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:t}),t.items||this.props.onLeafClick(e))}},{key:"position",value:function(){var e=this.submenuRef.current.parentElement,t=n.getOffset(this.submenuRef.current.parentElement),i=n.getViewport(),r=this.submenuRef.current.offsetParent?this.submenuRef.current.offsetWidth:n.getHiddenElementOuterWidth(this.submenuRef.current),o=n.getOuterWidth(e.children[0]);this.submenuRef.current.style.top="0px",this.submenuRef.current.style.left=parseInt(t.left,10)+o+r>i.width-n.calculateScrollbarWidth()?-1*r+"px":o+"px"}},{key:"onEnter",value:function(){this.position()}},{key:"isActive",value:function(){return this.props.root||!this.props.resetMenu}},{key:"componentDidUpdate",value:function(){this.isActive()&&this.position()}},{key:"renderSeparator",value:function(t){return e.createElement("li",{key:"separator_"+t,className:"p-menu-separator",role:"separator"})}},{key:"renderSubmenu",value:function(t){return t.items?e.createElement(l,{model:t.items,resetMenu:t!==this.state.activeItem,onLeafClick:this.props.onLeafClick}):null}},{key:"renderMenuitem",value:function(t,n){var o=this,s=this.state.activeItem===t,c=i("p-menuitem",{"p-menuitem-active":s},t.className),l=i("p-menuitem-link",{"p-disabled":t.disabled}),a=i("p-menuitem-icon",t.icon),m="p-submenu-icon pi pi-angle-right",f=t.icon&&e.createElement("span",{className:a}),p=t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label),d=t.items&&e.createElement("span",{className:m}),h=this.renderSubmenu(t),v=e.createElement("a",{href:t.url||"#",className:l,target:t.target,onClick:function(e){return o.onItemClick(e,t,n)},role:"menuitem","aria-haspopup":null!=t.items,"aria-disabled":t.disabled},f,p,d,e.createElement(u,null));t.template&&(v=r.getJSXElement(t.template,t,{onClick:function(e){return o.onItemClick(e,t,n)},className:l,labelClassName:"p-menuitem-text",iconClassName:a,submenuIconClassName:m,element:v,props:this.props,active:s}));return e.createElement("li",{key:t.label+"_"+n,role:"none",className:c,style:t.style,onMouseEnter:function(e){return o.onItemMouseEnter(e,t)}},v,h)}},{key:"renderItem",value:function(e,t){return e.separator?this.renderSeparator(t):this.renderMenuitem(e,t)}},{key:"renderMenu",value:function(){var e=this;return this.props.model?this.props.model.map((function(t,n){return e.renderItem(t,n)})):null}},{key:"render",value:function(){var t=i({"p-submenu-list":!this.props.root}),n=this.renderMenu(),r=this.isActive();return e.createElement(s,{nodeRef:this.submenuRef,classNames:"p-contextmenusub",in:r,timeout:{enter:0,exit:0},unmountOnExit:!0,onEnter:this.onEnter},e.createElement("ul",{ref:this.submenuRef,className:t},n))}}],[{key:"getDerivedStateFromProps",value:function(e,t){return!0===e.resetMenu?{activeItem:null}:null}}]),l}();E(R,"defaultProps",{model:null,root:!1,className:null,resetMenu:!1,onLeafClick:null});var x=function(r){h(m,t);var u=k(m);function m(t){var n;return a(this,m),(n=u.call(this,t)).state={visible:!1,reshow:!1,resetMenu:!1},n.onMenuClick=n.onMenuClick.bind(p(n)),n.onLeafClick=n.onLeafClick.bind(p(n)),n.onMenuMouseEnter=n.onMenuMouseEnter.bind(p(n)),n.onEnter=n.onEnter.bind(p(n)),n.onEntered=n.onEntered.bind(p(n)),n.onExit=n.onExit.bind(p(n)),n.onExited=n.onExited.bind(p(n)),n.menuRef=e.createRef(),n}return f(m,[{key:"onMenuClick",value:function(){this.setState({resetMenu:!1})}},{key:"onMenuMouseEnter",value:function(){this.setState({resetMenu:!1})}},{key:"show",value:function(e){var t=this;e instanceof Event||e.persist(),e.stopPropagation(),e.preventDefault(),this.currentEvent=e,this.state.visible?this.setState({reshow:!0}):this.setState({visible:!0},(function(){t.props.onShow&&t.props.onShow(t.currentEvent)}))}},{key:"hide",value:function(e){var t=this;e instanceof Event||e.persist(),this.currentEvent=e,this.setState({visible:!1,reshow:!1},(function(){t.props.onHide&&t.props.onHide(t.currentEvent)}))}},{key:"onEnter",value:function(){this.props.autoZIndex&&o.set("menu",this.menuRef.current,l.autoZIndex,this.props.baseZIndex||l.zIndex.menu),this.position(this.currentEvent)}},{key:"onEntered",value:function(){this.bindDocumentListeners()}},{key:"onExit",value:function(){this.currentEvent=null,this.unbindDocumentListeners()}},{key:"onExited",value:function(){o.clear(this.menuRef.current)}},{key:"position",value:function(e){if(e){var t=e.pageX+1,i=e.pageY+1,r=this.menuRef.current.offsetParent?this.menuRef.current.offsetWidth:n.getHiddenElementOuterWidth(this.menuRef.current),o=this.menuRef.current.offsetParent?this.menuRef.current.offsetHeight:n.getHiddenElementOuterHeight(this.menuRef.current),s=n.getViewport();t+r-document.body.scrollLeft>s.width&&(t-=r),i+o-document.body.scrollTop>s.height&&(i-=o),t<document.body.scrollLeft&&(t=document.body.scrollLeft),i<document.body.scrollTop&&(i=document.body.scrollTop),this.menuRef.current.style.left=t+"px",this.menuRef.current.style.top=i+"px"}}},{key:"onLeafClick",value:function(e){this.setState({resetMenu:!0}),this.hide(e),e.stopPropagation()}},{key:"isOutsideClicked",value:function(e){return this.menuRef&&this.menuRef.current&&!(this.menuRef.current.isSameNode(e.target)||this.menuRef.current.contains(e.target))}},{key:"bindDocumentListeners",value:function(){this.bindDocumentResizeListener(),this.bindDocumentClickListener()}},{key:"unbindDocumentListeners",value:function(){this.unbindDocumentResizeListener(),this.unbindDocumentClickListener()}},{key:"bindDocumentClickListener",value:function(){var e=this;this.documentClickListener||(this.documentClickListener=function(t){e.isOutsideClicked(t)&&2!==t.button&&(e.hide(t),e.setState({resetMenu:!0}))},document.addEventListener("click",this.documentClickListener))}},{key:"bindDocumentContextMenuListener",value:function(){var e=this;this.documentContextMenuListener||(this.documentContextMenuListener=function(t){e.show(t)},document.addEventListener("contextmenu",this.documentContextMenuListener))}},{key:"bindDocumentResizeListener",value:function(){var e=this;this.documentResizeListener||(this.documentResizeListener=function(t){e.state.visible&&!n.isTouchDevice()&&e.hide(t)},window.addEventListener("resize",this.documentResizeListener))}},{key:"unbindDocumentClickListener",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null)}},{key:"unbindDocumentContextMenuListener",value:function(){this.documentContextMenuListener&&(document.removeEventListener("contextmenu",this.documentContextMenuListener),this.documentContextMenuListener=null)}},{key:"unbindDocumentResizeListener",value:function(){this.documentResizeListener&&(window.removeEventListener("resize",this.documentResizeListener),this.documentResizeListener=null)}},{key:"componentDidMount",value:function(){this.props.global&&this.bindDocumentContextMenuListener()}},{key:"componentDidUpdate",value:function(e,t){var n=this;if(this.state.visible&&(t.reshow!==this.state.reshow||e.model!==this.props.model)){var i=this.currentEvent;this.setState({visible:!1,reshow:!1,rePosition:!1,resetMenu:!0},(function(){return n.show(i)}))}}},{key:"componentWillUnmount",value:function(){this.unbindDocumentListeners(),this.unbindDocumentContextMenuListener(),o.clear(this.menuRef.current)}},{key:"renderContextMenu",value:function(){var t=i("p-contextmenu p-component",this.props.className);return e.createElement(s,{nodeRef:this.menuRef,classNames:"p-contextmenu",in:this.state.visible,timeout:{enter:250,exit:0},options:this.props.transitionOptions,unmountOnExit:!0,onEnter:this.onEnter,onEntered:this.onEntered,onExit:this.onExit,onExited:this.onExited},e.createElement("div",{ref:this.menuRef,id:this.props.id,className:t,style:this.props.style,onClick:this.onMenuClick,onMouseEnter:this.onMenuMouseEnter},e.createElement(R,{model:this.props.model,root:!0,resetMenu:this.state.resetMenu,onLeafClick:this.onLeafClick})))}},{key:"render",value:function(){var t=this.renderContextMenu();return e.createElement(c,{element:t,appendTo:this.props.appendTo})}}]),m}();E(x,"defaultProps",{id:null,model:null,style:null,className:null,global:!1,autoZIndex:!0,baseZIndex:0,appendTo:null,transitionOptions:null,onShow:null,onHide:null});export{x as ContextMenu};
|