2 lines
8.6 KiB
JavaScript
2 lines
8.6 KiB
JavaScript
import e,{Component as t}from"react";import{DomHandler as n,classNames as r,ObjectUtils as a}from"primereact/utils";import{Ripple as i}from"primereact/ripple";function l(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(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 u(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function s(e,t){return s=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},s(e,t)}function c(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&&s(e,t)}function m(e){return m="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},m(e)}function p(e,t){if(t&&("object"===m(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return u(e)}function f(e){return f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},f(e)}function d(){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 v,h,y,b=function(s){c(g,t);var m,v,h,y,b,k=(m=g,v=d(),function(){var e,t=f(m);if(v){var n=f(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return p(this,e)});function g(e){var t;return l(this,g),(t=k.call(this,e)).state={activeItem:null},t.onLeafClick=t.onLeafClick.bind(u(t)),t}return h=g,(y=[{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 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:"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(t){return e.createElement("li",{key:"separator_"+t,className:"p-menu-separator",role:"separator"})}},{key:"renderSubmenuIcon",value:function(t){if(t.items){var n=r("p-submenu-icon pi",{"pi-angle-down":this.isHorizontal(),"pi-angle-right":this.isVertical()});return e.createElement("span",{className:n})}return null}},{key:"renderSubmenuItem",value:function(t,n){var l=this;if(t.separator)return this.renderSeparator(n);var o=r("p-menuitem",t.className),u=r("p-menuitem-link",{"p-disabled":t.disabled}),s=r(t.icon,"p-menuitem-icon"),c=e.createElement("a",{href:t.url||"#",className:u,target:t.target,onClick:function(e){return l.onLeafClick(e,t)},role:"menuitem","aria-disabled":t.disabled},t.icon&&e.createElement("span",{className:s}),t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label),e.createElement(i,null));return t.template&&(c=a.getJSXElement(t.template,t,{onClick:function(e){return l.onLeafClick(e,t)},className:u,labelClassName:"p-menuitem-text",iconClassName:s,element:c,props:this.props})),e.createElement("li",{key:t.label+"_"+n,className:o,style:t.style,role:"none"},c)}},{key:"renderSubmenu",value:function(t){var n=this,a=r("p-megamenu-submenu-header",{"p-disabled":t.disabled},t.className),i=t.items.map((function(e,t){return n.renderSubmenuItem(e,t)}));return e.createElement(e.Fragment,{key:t.label},e.createElement("li",{className:a,style:t.style,role:"presentation","aria-disabled":t.disabled},t.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(t,n,r,a){var i=this.renderSubmenus(n);return e.createElement("div",{key:t.label+"_column_"+r,className:a},e.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(r,a){return t.renderColumn(e,r,a,n)}))}return null}},{key:"renderCategoryPanel",value:function(t){if(t.items){var n=this.renderColumns(t);return e.createElement("div",{className:"p-megamenu-panel"},e.createElement("div",{className:"p-megamenu-grid"},n))}return null}},{key:"renderCategory",value:function(t,n){var l=this,o=r("p-menuitem",{"p-menuitem-active":t===this.state.activeItem},t.className),u=r("p-menuitem-link",{"p-disabled":t.disabled}),s=r("p-menuitem-icon",t.icon),c=t.icon&&e.createElement("span",{className:s}),m=t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label),p=t.template?a.getJSXElement(t.template,t):null,f=this.renderSubmenuIcon(t),d=this.renderCategoryPanel(t);return e.createElement("li",{key:t.label+"_"+n,className:o,style:t.style,onMouseEnter:function(e){return l.onCategoryMouseEnter(e,t)},role:"none"},e.createElement("a",{href:t.url||"#",className:u,target:t.target,onClick:function(e){return l.onCategoryClick(e,t)},onKeyDown:function(e){return l.onCategoryKeyDown(e,t)},role:"menuitem","aria-haspopup":null!=t.items},c,m,p,f,e.createElement(i,null)),d)}},{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?e.createElement("div",{className:"p-megamenu-custom"},this.props.children):null}},{key:"render",value:function(){var t=this,n=r("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 e.createElement("div",{ref:function(e){return t.container=e},id:this.props.id,className:n,style:this.props.style},e.createElement("ul",{className:"p-megamenu-root-list",role:"menubar"},a),i)}}])&&o(h.prototype,y),b&&o(h,b),Object.defineProperty(h,"prototype",{writable:!1}),g}();y={id:null,model:null,style:null,className:null,orientation:"horizontal"},(h="defaultProps")in(v=b)?Object.defineProperty(v,h,{value:y,enumerable:!0,configurable:!0,writable:!0}):v[h]=y;export{b as MegaMenu};
|