"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/utils"),n=require("primereact/ripple");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=a(e);function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function l(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function o(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function u(e,t){return u=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},u(e,t)}function s(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&&u(e,t)}function c(e){return c="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},c(e)}function m(e,t){if(t&&("object"===c(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return o(e)}function p(e){return p=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},p(e)}function f(){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 d,v,h,y=function(a){s(b,e.Component);var u,c,d,v,h,y=(u=b,c=f(),function(){var e,t=p(u);if(c){var n=p(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return m(this,e)});function b(e){var t;return i(this,b),(t=y.call(this,e)).state={activeItem:null},t.onLeafClick=t.onLeafClick.bind(o(t)),t}return d=b,(v=[{key:"onLeafClick",value:function(e,t){t.disabled?e.preventDefault():(t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:t}),this.setState({activeItem:null}))}},{key:"onCategoryMouseEnter",value:function(e,t){t.disabled?e.preventDefault():this.state.activeItem&&this.setState({activeItem:t})}},{key:"onCategoryClick",value:function(e,t){t.disabled||(t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:this.props.item}),t.items&&this.setState(this.state.activeItem&&this.state.activeItem===t?{activeItem:null}:{activeItem:t})),e.preventDefault()}},{key:"onCategoryKeyDown",value:function(e,t){var n=e.currentTarget.parentElement;switch(e.which){case 40:this.isHorizontal()?this.expandMenu(t):this.navigateToNextItem(n),e.preventDefault();break;case 38:this.isVertical()?this.navigateToPrevItem(n):t.items&&t===this.state.activeItem&&this.collapseMenu(),e.preventDefault();break;case 39:this.isHorizontal()?this.navigateToNextItem(n):this.expandMenu(t),e.preventDefault();break;case 37:this.isHorizontal()?this.navigateToPrevItem(n):t.items&&t===this.state.activeItem&&this.collapseMenu(),e.preventDefault()}}},{key:"expandMenu",value:function(e){e.items&&this.setState({activeItem:e})}},{key:"collapseMenu",value:function(e){this.setState({activeItem:null})}},{key:"findNextItem",value:function(e){var n=e.nextElementSibling;return n?t.DomHandler.hasClass(n,"p-disabled")||!t.DomHandler.hasClass(n,"p-menuitem")?this.findNextItem(n):n:null}},{key:"findPrevItem",value:function(e){var n=e.previousElementSibling;return n?t.DomHandler.hasClass(n,"p-disabled")||!t.DomHandler.hasClass(n,"p-menuitem")?this.findPrevItem(n):n:null}},{key:"navigateToNextItem",value:function(e){var t=this.findNextItem(e);t&&t.children[0].focus()}},{key:"navigateToPrevItem",value:function(e){var t=this.findPrevItem(e);t&&t.children[0].focus()}},{key:"isHorizontal",value:function(){return"horizontal"===this.props.orientation}},{key:"isVertical",value:function(){return"vertical"===this.props.orientation}},{key:"getColumnClassName",value:function(e){var t;switch(e.items?e.items.length:0){case 2:t="p-megamenu-col-6";break;case 3:t="p-megamenu-col-4";break;case 4:t="p-megamenu-col-3";break;case 6:t="p-megamenu-col-2";break;default:t="p-megamenu-col-12"}return t}},{key:"componentDidMount",value:function(){var e=this;this.documentClickListener||(this.documentClickListener=function(t){e.container&&!e.container.contains(t.target)&&e.setState({activeItem:null})},document.addEventListener("click",this.documentClickListener))}},{key:"componentWillUnmount",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null)}},{key:"renderSeparator",value:function(e){return r.default.createElement("li",{key:"separator_"+e,className:"p-menu-separator",role:"separator"})}},{key:"renderSubmenuIcon",value:function(e){if(e.items){var n=t.classNames("p-submenu-icon pi",{"pi-angle-down":this.isHorizontal(),"pi-angle-right":this.isVertical()});return r.default.createElement("span",{className:n})}return null}},{key:"renderSubmenuItem",value:function(e,a){var i=this;if(e.separator)return this.renderSeparator(a);var l=t.classNames("p-menuitem",e.className),o=t.classNames("p-menuitem-link",{"p-disabled":e.disabled}),u=t.classNames(e.icon,"p-menuitem-icon"),s=r.default.createElement("a",{href:e.url||"#",className:o,target:e.target,onClick:function(t){return i.onLeafClick(t,e)},role:"menuitem","aria-disabled":e.disabled},e.icon&&r.default.createElement("span",{className:u}),e.label&&r.default.createElement("span",{className:"p-menuitem-text"},e.label),r.default.createElement(n.Ripple,null));return e.template&&(s=t.ObjectUtils.getJSXElement(e.template,e,{onClick:function(t){return i.onLeafClick(t,e)},className:o,labelClassName:"p-menuitem-text",iconClassName:u,element:s,props:this.props})),r.default.createElement("li",{key:e.label+"_"+a,className:l,style:e.style,role:"none"},s)}},{key:"renderSubmenu",value:function(e){var n=this,a=t.classNames("p-megamenu-submenu-header",{"p-disabled":e.disabled},e.className),i=e.items.map((function(e,t){return n.renderSubmenuItem(e,t)}));return r.default.createElement(r.default.Fragment,{key:e.label},r.default.createElement("li",{className:a,style:e.style,role:"presentation","aria-disabled":e.disabled},e.label),i)}},{key:"renderSubmenus",value:function(e){var t=this;return e.map((function(e,n){return t.renderSubmenu(e,n)}))}},{key:"renderColumn",value:function(e,t,n,a){var i=this.renderSubmenus(t);return r.default.createElement("div",{key:e.label+"_column_"+n,className:a},r.default.createElement("ul",{className:"p-megamenu-submenu",role:"menu"},i))}},{key:"renderColumns",value:function(e){var t=this;if(e.items){var n=this.getColumnClassName(e);return e.items.map((function(a,r){return t.renderColumn(e,a,r,n)}))}return null}},{key:"renderCategoryPanel",value:function(e){if(e.items){var t=this.renderColumns(e);return r.default.createElement("div",{className:"p-megamenu-panel"},r.default.createElement("div",{className:"p-megamenu-grid"},t))}return null}},{key:"renderCategory",value:function(e,a){var i=this,l=t.classNames("p-menuitem",{"p-menuitem-active":e===this.state.activeItem},e.className),o=t.classNames("p-menuitem-link",{"p-disabled":e.disabled}),u=t.classNames("p-menuitem-icon",e.icon),s=e.icon&&r.default.createElement("span",{className:u}),c=e.label&&r.default.createElement("span",{className:"p-menuitem-text"},e.label),m=e.template?t.ObjectUtils.getJSXElement(e.template,e):null,p=this.renderSubmenuIcon(e),f=this.renderCategoryPanel(e);return r.default.createElement("li",{key:e.label+"_"+a,className:l,style:e.style,onMouseEnter:function(t){return i.onCategoryMouseEnter(t,e)},role:"none"},r.default.createElement("a",{href:e.url||"#",className:o,target:e.target,onClick:function(t){return i.onCategoryClick(t,e)},onKeyDown:function(t){return i.onCategoryKeyDown(t,e)},role:"menuitem","aria-haspopup":null!=e.items},s,c,m,p,r.default.createElement(n.Ripple,null)),f)}},{key:"renderMenu",value:function(){var e=this;return this.props.model?this.props.model.map((function(t,n){return e.renderCategory(t,n,!0)})):null}},{key:"renderCustomContent",value:function(){return this.props.children?r.default.createElement("div",{className:"p-megamenu-custom"},this.props.children):null}},{key:"render",value:function(){var e=this,n=t.classNames("p-megamenu p-component",{"p-megamenu-horizontal":"horizontal"===this.props.orientation,"p-megamenu-vertical":"vertical"===this.props.orientation},this.props.className),a=this.renderMenu(),i=this.renderCustomContent();return r.default.createElement("div",{ref:function(t){return e.container=t},id:this.props.id,className:n,style:this.props.style},r.default.createElement("ul",{className:"p-megamenu-root-list",role:"menubar"},a),i)}}])&&l(d.prototype,v),h&&l(d,h),Object.defineProperty(d,"prototype",{writable:!1}),b}();h={id:null,model:null,style:null,className:null,orientation:"horizontal"},(v="defaultProps")in(d=y)?Object.defineProperty(d,v,{value:h,enumerable:!0,configurable:!0,writable:!0}):d[v]=h,exports.MegaMenu=y;
