import e,{Component as t}from"react";import{DomHandler as n,classNames as i,ObjectUtils as r,ZIndexUtils as o,ConnectedOverlayScrollHandler as s}from"primereact/utils";import{Ripple as l}from"primereact/ripple";import{CSSTransition as u}from"primereact/csstransition";import{OverlayService as c}from"primereact/overlayservice";import{Portal as a}from"primereact/portal";import p from"primereact/api";function m(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function f(e,t){for(var n=0;ni.width-n.calculateScrollbarWidth()&&n.addClass(this.element,"p-submenu-list-flipped")}}},{key:"onItemMouseEnter",value:function(e,t){t.disabled?e.preventDefault():this.props.root?(this.state.activeItem||this.props.popup)&&this.setState({activeItem:t}):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}),this.props.root&&t.items&&this.setState(this.state.activeItem&&t===this.state.activeItem?{activeItem:null}:{activeItem:t}),t.items||this.onLeafClick())}},{key:"onItemKeyDown",value:function(e,t){var n=e.currentTarget.parentElement;switch(e.which){case 40:var i=this.findNextItem(n);i&&i.children[0].focus(),e.preventDefault();break;case 38:var r=this.findPrevItem(n);r&&r.children[0].focus(),e.preventDefault();break;case 39:t.items&&(this.setState({activeItem:t}),setTimeout((function(){n.children[1].children[0].children[0].focus()}),50)),e.preventDefault()}this.props.onKeyDown&&this.props.onKeyDown(e,n)}},{key:"onChildItemKeyDown",value:function(e,t){37===e.which&&(this.setState({activeItem:null}),t.parentElement.previousElementSibling.focus())}},{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:"onLeafClick",value:function(){this.setState({activeItem:null}),this.props.onLeafClick&&this.props.onLeafClick()}},{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(a,{model:t.items,onLeafClick:this.onLeafClick,popup:this.props.popup,onKeyDown:this.onChildItemKeyDown,parentActive:t===this.state.activeItem}):null}},{key:"renderMenuitem",value:function(t,n){var o=this,s=this.state.activeItem===t,u=i("p-menuitem",{"p-menuitem-active":s},t.className),c=i("p-menuitem-link",{"p-disabled":t.disabled}),a=i("p-menuitem-icon",t.icon),p="p-submenu-icon pi pi-angle-right",m=t.icon&&e.createElement("span",{className:a}),f=t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label),h=t.items&&e.createElement("span",{className:p}),d=this.renderSubmenu(t),v=e.createElement("a",{href:t.url||"#",className:c,target:t.target,role:"menuitem","aria-haspopup":null!=t.items,onClick:function(e){return o.onItemClick(e,t)},onKeyDown:function(e){return o.onItemKeyDown(e,t)},"aria-disabled":t.disabled},m,f,h,e.createElement(l,null));t.template&&(v=r.getJSXElement(t.template,t,{onClick:function(e){return o.onItemClick(e,t)},onKeyDown:function(e){return o.onItemKeyDown(e,t)},className:c,labelClassName:"p-menuitem-text",iconClassName:a,submenuIconClassName:p,element:v,props:this.props,active:s}));return e.createElement("li",{key:t.label+"_"+n,className:u,style:t.style,onMouseEnter:function(e){return o.onItemMouseEnter(e,t)},role:"none"},v,d)}},{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=this,n=i({"p-submenu-list":!this.props.root}),r=this.renderMenu();return e.createElement("ul",{ref:function(e){return t.element=e},className:n,role:this.props.root?"menubar":"menu","aria-orientation":"horizontal"},r)}}]),a}();function w(){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}}L(I,"defaultProps",{model:null,root:!1,className:null,popup:!1,onLeafClick:null,onKeyDown:null,parentActive:!1});var D=function(r){y(b,t);var l,f,v=(l=b,f=w(),function(){var e,t=E(l);if(f){var n=E(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return k(this,e)});function b(t){var n;return m(this,b),(n=v.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 h(b,[{key:"onPanelClick",value:function(e){this.props.popup&&c.emit("overlay-click",{originalEvent:e,target:this.target})}},{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(){this.props.autoZIndex&&o.set("menu",this.menuRef.current,p.autoZIndex,this.props.baseZIndex||p.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(){o.clear(this.menuRef.current)}},{key:"bindDocumentListeners",value:function(){this.bindDocumentClickListener(),this.bindDocumentResizeListener()}},{key:"unbindDocumentListeners",value:function(){this.unbindDocumentClickListener(),this.unbindDocumentResizeListener()}},{key:"bindDocumentClickListener",value:function(){var e=this;this.documentClickListener||(this.documentClickListener=function(t){e.props.popup&&e.state.visible&&e.menuRef.current&&!e.menuRef.current.contains(t.target)&&e.hide(t)},document.addEventListener("click",this.documentClickListener))}},{key:"unbindDocumentClickListener",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null)}},{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:"unbindDocumentResizeListener",value:function(){this.documentResizeListener&&(window.removeEventListener("resize",this.documentResizeListener),this.documentResizeListener=null)}},{key:"bindScrollListener",value:function(){var e=this;this.scrollHandler||(this.scrollHandler=new s(this.target,(function(t){e.state.visible&&e.hide(t)}))),this.scrollHandler.bindScrollListener()}},{key:"unbindScrollListener",value:function(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()}},{key:"componentWillUnmount",value:function(){this.unbindDocumentListeners(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),o.clear(this.menuRef.current)}},{key:"renderElement",value:function(){var t=i("p-tieredmenu p-component",{"p-tieredmenu-overlay":this.props.popup},this.props.className);return e.createElement(u,{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(I,{model:this.props.model,root:!0,popup:this.props.popup})))}},{key:"render",value:function(){var t=this.renderElement();return this.props.popup?e.createElement(a,{element:t,appendTo:this.props.appendTo}):t}}]),b}();L(D,"defaultProps",{id:null,model:null,popup:!1,style:null,className:null,autoZIndex:!0,baseZIndex:0,appendTo:null,transitionOptions:null,onShow:null,onHide:null});export{D as TieredMenu};