2 lines
8.0 KiB
JavaScript
2 lines
8.0 KiB
JavaScript
import e,{Component as t}from"react";import{DomHandler as n,ZIndexUtils as r,ConnectedOverlayScrollHandler as i,classNames as o,ObjectUtils as s}from"primereact/utils";import{CSSTransition as l}from"primereact/csstransition";import{OverlayService as u}from"primereact/overlayservice";import{Portal as a}from"primereact/portal";import c from"primereact/api";function p(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 r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function d(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e,t){return f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},f(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&&f(e,t)}function b(e){return b="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},b(e)}function v(e,t){if(t&&("object"===b(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return d(e)}function y(e){return y=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},y(e)}function k(){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 E,w,x,L=function(f){h(C,t);var b,E,w,x,L,g=(b=C,E=k(),function(){var e,t=y(b);if(E){var n=y(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return v(this,e)});function C(t){var n;return p(this,C),(n=g.call(this,t)).state={visible:!t.popup},n.onEnter=n.onEnter.bind(d(n)),n.onEntered=n.onEntered.bind(d(n)),n.onExit=n.onExit.bind(d(n)),n.onExited=n.onExited.bind(d(n)),n.onPanelClick=n.onPanelClick.bind(d(n)),n.menuRef=e.createRef(),n}return w=C,(x=[{key:"onPanelClick",value:function(e){this.props.popup&&u.emit("overlay-click",{originalEvent:e,target:this.target})}},{key:"onItemClick",value:function(e,t){t.disabled?e.preventDefault():(t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:t}),this.props.popup&&this.hide(e))}},{key:"onItemKeyDown",value:function(e,t){var n=e.currentTarget.parentElement;switch(e.which){case 40:var r=this.findNextItem(n);r&&r.children[0].focus(),e.preventDefault();break;case 38:var i=this.findPrevItem(n);i&&i.children[0].focus(),e.preventDefault()}}},{key:"findNextItem",value:function(e){var t=e.nextElementSibling;return t?n.hasClass(t,"p-disabled")||!n.hasClass(t,"p-menuitem")?this.findNextItem(t):t:null}},{key:"findPrevItem",value:function(e){var t=e.previousElementSibling;return t?n.hasClass(t,"p-disabled")||!n.hasClass(t,"p-menuitem")?this.findPrevItem(t):t:null}},{key:"toggle",value:function(e){this.props.popup&&(this.state.visible?this.hide(e):this.show(e))}},{key:"show",value:function(e){var t=this;this.target=e.currentTarget;var n=e;this.setState({visible:!0},(function(){t.props.onShow&&t.props.onShow(n)}))}},{key:"hide",value:function(e){var t=this,n=e;this.setState({visible:!1},(function(){t.props.onHide&&t.props.onHide(n)}))}},{key:"onEnter",value:function(){r.set("menu",this.menuRef.current,c.autoZIndex,this.props.baseZIndex||c.zIndex.menu),n.absolutePosition(this.menuRef.current,this.target)}},{key:"onEntered",value:function(){this.bindDocumentListeners(),this.bindScrollListener()}},{key:"onExit",value:function(){this.target=null,this.unbindDocumentListeners(),this.unbindScrollListener()}},{key:"onExited",value:function(){r.clear(this.menuRef.current)}},{key:"bindDocumentListeners",value:function(){var e=this;this.documentClickListener||(this.documentClickListener=function(t){e.state.visible&&e.isOutsideClicked(t)&&e.hide(t)},document.addEventListener("click",this.documentClickListener)),this.documentResizeListener||(this.documentResizeListener=function(t){e.state.visible&&!n.isTouchDevice()&&e.hide(t)},window.addEventListener("resize",this.documentResizeListener))}},{key:"unbindDocumentListeners",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null),this.documentResizeListener&&(window.removeEventListener("resize",this.documentResizeListener),this.documentResizeListener=null)}},{key:"bindScrollListener",value:function(){var e=this;this.scrollHandler||(this.scrollHandler=new i(this.target,(function(t){e.state.visible&&e.hide(t)}))),this.scrollHandler.bindScrollListener()}},{key:"unbindScrollListener",value:function(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()}},{key:"isOutsideClicked",value:function(e){return this.menuRef&&this.menuRef.current&&!(this.menuRef.current.isSameNode(e.target)||this.menuRef.current.contains(e.target))}},{key:"componentWillUnmount",value:function(){this.unbindDocumentListeners(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),r.clear(this.menuRef.current)}},{key:"renderSubmenu",value:function(t,n){var r=this,i=o("p-submenu-header",{"p-disabled":t.disabled},t.className),s=t.items.map((function(e,t){return r.renderMenuitem(e,t)}));return e.createElement(e.Fragment,{key:t.label+"_"+n},e.createElement("li",{className:i,style:t.style,role:"presentation","aria-disabled":t.disabled},t.label),s)}},{key:"renderSeparator",value:function(t){return e.createElement("li",{key:"separator_"+t,className:"p-menu-separator",role:"separator"})}},{key:"renderMenuitem",value:function(t,n){var r=this,i=o("p-menuitem",t.className),l=o("p-menuitem-link",{"p-disabled":t.disabled}),u=o("p-menuitem-icon",t.icon),a=t.disabled?null:0,c=e.createElement("a",{href:t.url||"#",className:l,role:"menuitem",target:t.target,onClick:function(e){return r.onItemClick(e,t)},onKeyDown:function(e){return r.onItemKeyDown(e,t)},tabIndex:a,"aria-disabled":t.disabled},t.icon&&e.createElement("span",{className:u}),t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label));return t.template&&(c=s.getJSXElement(t.template,t,{onClick:function(e){return r.onItemClick(e,t)},onKeyDown:function(e){return r.onItemKeyDown(e,t)},className:l,tabIndex:a,labelClassName:"p-menuitem-text",iconClassName:u,element:c,props:this.props})),e.createElement("li",{key:t.label+"_"+n,className:i,style:t.style,role:"none"},c)}},{key:"renderItem",value:function(e,t){return e.separator?this.renderSeparator(t):e.items?this.renderSubmenu(e,t):this.renderMenuitem(e,t)}},{key:"renderMenu",value:function(){var e=this;return this.props.model.map((function(t,n){return e.renderItem(t,n)}))}},{key:"renderElement",value:function(){if(this.props.model){var t=o("p-menu p-component",this.props.className,{"p-menu-overlay":this.props.popup}),n=this.renderMenu();return e.createElement(l,{nodeRef:this.menuRef,classNames:"p-connected-overlay",in:this.state.visible,timeout:{enter:120,exit:100},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.onPanelClick},e.createElement("ul",{className:"p-menu-list p-reset",role:"menu"},n)))}return null}},{key:"render",value:function(){var t=this.renderElement();return this.props.popup?e.createElement(a,{element:t,appendTo:this.props.appendTo}):t}}])&&m(w.prototype,x),L&&m(w,L),Object.defineProperty(w,"prototype",{writable:!1}),C}();x={id:null,model:null,popup:!1,style:null,className:null,autoZIndex:!0,baseZIndex:0,appendTo:null,transitionOptions:null,onShow:null,onHide:null},(w="defaultProps")in(E=L)?Object.defineProperty(E,w,{value:x,enumerable:!0,configurable:!0,writable:!0}):E[w]=x;export{L as Menu};
|