2 lines
8.7 KiB
JavaScript
2 lines
8.7 KiB
JavaScript
import e,{Component as t}from"react";import{classNames as n,ObjectUtils as r,UniqueComponentId as i}from"primereact/utils";import{CSSTransition as a}from"primereact/csstransition";function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function o(e){if(Array.isArray(e))return l(e)}function s(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function u(e,t){if(e){if("string"==typeof e)return l(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?l(e,t):void 0}}function c(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function p(e){return o(e)||s(e)||u(e)||c()}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;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,t,n){return t&&f(e.prototype,t),n&&f(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function h(e,t){return h=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},h(e,t)}function v(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&&h(e,t)}function y(e){return y="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},y(e)}function b(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function I(e,t){if(t&&("object"===y(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return b(e)}function g(e){return g=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},g(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=N();return function(){var n,r=g(e);if(t){var i=g(this).constructor;n=Reflect.construct(r,arguments,i)}else n=r.apply(this,arguments);return I(this,n)}}function N(){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 x=function(i){v(o,t);var l=k(o);function o(e){var t;return m(this,o),(t=l.call(this,e)).state={activeItem:t.findActiveItem()},t}return d(o,[{key:"onItemClick",value:function(e,t){if(t.disabled)e.preventDefault();else{t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:t});var n=this.state.activeItem;this.isItemActive(t)?(t.expanded=!1,this.setState({activeItem:this.props.multiple?n.filter((function(e){return e!==t})):null})):(!this.props.multiple&&n&&(n.expanded=!1),t.expanded=!0,this.setState({activeItem:this.props.multiple?[].concat(p(n||[]),[t]):t}))}}},{key:"findActiveItem",value:function(){if(this.props.model){if(this.props.multiple)return this.props.model.filter((function(e){return e.expanded}));var e=null;return this.props.model.forEach((function(t){t.expanded&&(e?t.expanded=!1:e=t)})),e}return null}},{key:"isItemActive",value:function(e){return this.state.activeItem&&(this.props.multiple?this.state.activeItem.indexOf(e)>-1:this.state.activeItem===e)}},{key:"renderSeparator",value:function(t){return e.createElement("li",{key:"separator_"+t,className:"p-menu-separator"})}},{key:"renderSubmenu",value:function(t,r){var i=n("p-toggleable-content",{"p-toggleable-content-collapsed":!r}),l=e.createRef();return t.items?e.createElement(a,{nodeRef:l,classNames:"p-toggleable-content",timeout:{enter:1e3,exit:450},in:r,unmountOnExit:!0},e.createElement("div",{ref:l,className:i},e.createElement(o,{model:t.items,multiple:this.props.multiple}))):null}},{key:"renderMenuitem",value:function(t,i){var a=this,l=this.isItemActive(t),o=n("p-menuitem",t.className),s=n("p-menuitem-link",{"p-disabled":t.disabled}),u=n("p-menuitem-icon",t.icon),c=n("p-panelmenu-icon pi pi-fw",{"pi-angle-right":!l,"pi-angle-down":l}),p=t.icon&&e.createElement("span",{className:u}),m=t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label),f=t.items&&e.createElement("span",{className:c}),d=this.renderSubmenu(t,l),h=e.createElement("a",{href:t.url||"#",className:s,target:t.target,onClick:function(e){return a.onItemClick(e,t,i)},role:"menuitem","aria-disabled":t.disabled},f,p,m);t.template&&(h=r.getJSXElement(t.template,t,{onClick:function(e){return a.onItemClick(e,t,i)},className:s,labelClassName:"p-menuitem-text",iconClassName:u,submenuIconClassName:c,element:h,props:this.props,leaf:!t.items,active:l}));return e.createElement("li",{key:t.label+"_"+i,className:o,style:t.style,role:"none"},h,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=n("p-submenu-list",this.props.className),r=this.renderMenu();return e.createElement("ul",{className:t,role:"tree"},r)}}]),o}();E(x,"defaultProps",{model:null,multiple:!1});var O=function(l){v(s,t);var o=k(s);function s(e){var t;return m(this,s),(t=o.call(this,e)).state={id:e.id,activeItem:t.findActiveItem()},t}return d(s,[{key:"onItemClick",value:function(e,t){if(t.disabled)e.preventDefault();else{t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:t});var n=this.state.activeItem;this.isItemActive(t)?(t.expanded=!1,this.setState({activeItem:this.props.multiple?n.filter((function(e){return e!==t})):null})):(!this.props.multiple&&n&&(n.expanded=!1),t.expanded=!0,this.setState({activeItem:this.props.multiple?[].concat(p(n||[]),[t]):t}))}}},{key:"findActiveItem",value:function(){if(this.props.model){if(this.props.multiple)return this.props.model.filter((function(e){return e.expanded}));var e=null;return this.props.model.forEach((function(t){t.expanded&&(e?t.expanded=!1:e=t)})),e}return null}},{key:"isItemActive",value:function(e){return this.state.activeItem&&(this.props.multiple?this.state.activeItem.indexOf(e)>-1:this.state.activeItem===e)}},{key:"componentDidMount",value:function(){this.state.id||this.setState({id:i()})}},{key:"renderPanel",value:function(t,i){var l=this,o=this.isItemActive(t),s=n("p-panelmenu-panel",t.className),u=n("p-component p-panelmenu-header",{"p-highlight":o,"p-disabled":t.disabled}),c=n("p-panelmenu-icon pi",{"pi-chevron-right":!o," pi-chevron-down":o}),p=n("p-menuitem-icon",t.icon),m=t.items&&e.createElement("span",{className:c}),f=t.icon&&e.createElement("span",{className:p}),d=t.label&&e.createElement("span",{className:"p-menuitem-text"},t.label),h=n("p-toggleable-content",{"p-toggleable-content-collapsed":!o}),v=e.createRef(),y=e.createElement("a",{href:t.url||"#",className:"p-panelmenu-header-link",onClick:function(e){return l.onItemClick(e,t)},"aria-expanded":o,id:this.state.id+"_header","aria-controls":this.state.id+"content","aria-disabled":t.disabled},m,f,d);t.template&&(y=r.getJSXElement(t.template,t,{onClick:function(e){return l.onItemClick(e,t)},className:"p-panelmenu-header-link",labelClassName:"p-menuitem-text",submenuIconClassName:c,iconClassName:p,element:y,props:this.props,leaf:!t.items,active:o}));return e.createElement("div",{key:t.label+"_"+i,className:s,style:t.style},e.createElement("div",{className:u,style:t.style},y),e.createElement(a,{nodeRef:v,classNames:"p-toggleable-content",timeout:{enter:1e3,exit:450},in:o,unmountOnExit:!0,options:this.props.transitionOptions},e.createElement("div",{ref:v,className:h,role:"region",id:this.state.id+"_content","aria-labelledby":this.state.id+"_header"},e.createElement("div",{className:"p-panelmenu-content"},e.createElement(x,{model:t.items,className:"p-panelmenu-root-submenu",multiple:this.props.multiple})))))}},{key:"renderPanels",value:function(){var e=this;return this.props.model?this.props.model.map((function(t,n){return e.renderPanel(t,n)})):null}},{key:"render",value:function(){var t=n("p-panelmenu p-component",this.props.className),r=this.renderPanels();return e.createElement("div",{id:this.props.id,className:t,style:this.props.style},r)}}]),s}();E(O,"defaultProps",{id:null,model:null,style:null,className:null,multiple:!1,transitionOptions:null});export{O as PanelMenu};
|