2 lines
19 KiB
JavaScript
2 lines
19 KiB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/utils"),n=require("primereact/api"),r=require("primereact/overlayservice"),i=require("primereact/tree"),l=require("primereact/csstransition"),o=require("primereact/portal"),s=require("primereact/ripple");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=a(e),p=a(n);function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function h(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&&h(e.prototype,t),n&&h(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function f(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function y(e,t){return y=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},y(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&&y(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 b(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 f(e)}function g(e){return g=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},g(e)}function k(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function E(){return E=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},E.apply(this,arguments)}function O(){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 S=function(n){v(a,e.Component);var r,i,s=(r=a,i=O(),function(){var e,t=g(r);if(i){var n=g(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return b(this,e)});function a(){return u(this,a),s.apply(this,arguments)}return d(a,[{key:"renderElement",value:function(){var e=t.classNames("p-treeselect-panel p-component",this.props.panelClassName);return c.default.createElement(l.CSSTransition,{nodeRef:this.props.forwardRef,classNames:"p-connected-overlay",in:this.props.in,timeout:{enter:120,exit:100},options:this.props.transitionOptions,unmountOnExit:!0,onEnter:this.props.onEnter,onEntering:this.props.onEntering,onEntered:this.props.onEntered,onExit:this.props.onExit,onExited:this.props.onExited},c.default.createElement("div",{ref:this.props.forwardRef,className:e,style:this.props.panelStyle,onClick:this.props.onClick},this.props.header,c.default.createElement("div",{className:"p-treeselect-items-wrapper",style:{maxHeight:this.props.scrollHeight||"auto"}},this.props.children),this.props.footer))}},{key:"render",value:function(){var e=this.renderElement();return c.default.createElement(o.Portal,{element:e,appendTo:this.props.appendTo})}}]),a}(),C=c.default.forwardRef((function(e,t){return c.default.createElement(S,E({forwardRef:t},e))}));function w(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=N(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,i=function(){};return{s:i,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var l,o=!0,s=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){s=!0,l=e},f:function(){try{o||null==n.return||n.return()}finally{if(s)throw l}}}}function N(e,t){if(e){if("string"==typeof e)return x(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)?x(e,t):void 0}}function x(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 I(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function L(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?I(Object(n),!0).forEach((function(t){k(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):I(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function V(){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 j=function(l){v(y,e.Component);var o,a,h=(o=y,a=V(),function(){var e,t=g(o);if(a){var n=g(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return b(this,e)});function y(t){var n;return u(this,y),(n=h.call(this,t)).state={focused:!1,overlayVisible:!1,expandedKeys:{}},n.props.onFilterValueChange||(n.state.filterValue=""),n.onClick=n.onClick.bind(f(n)),n.onInputFocus=n.onInputFocus.bind(f(n)),n.onInputBlur=n.onInputBlur.bind(f(n)),n.onInputKeyDown=n.onInputKeyDown.bind(f(n)),n.onFilterInputChange=n.onFilterInputChange.bind(f(n)),n.onFilterInputKeyDown=n.onFilterInputKeyDown.bind(f(n)),n.onOverlayClick=n.onOverlayClick.bind(f(n)),n.onOverlayEnter=n.onOverlayEnter.bind(f(n)),n.onOverlayEntered=n.onOverlayEntered.bind(f(n)),n.onOverlayExit=n.onOverlayExit.bind(f(n)),n.onOverlayExited=n.onOverlayExited.bind(f(n)),n.onSelectionChange=n.onSelectionChange.bind(f(n)),n.onNodeSelect=n.onNodeSelect.bind(f(n)),n.onNodeUnselect=n.onNodeUnselect.bind(f(n)),n.onNodeToggle=n.onNodeToggle.bind(f(n)),n.onFilterValueChange=n.onFilterValueChange.bind(f(n)),n.hide=n.hide.bind(f(n)),n.show=n.show.bind(f(n)),n.overlayRef=e.createRef(),n}return d(y,[{key:"getFilterValue",value:function(){return this.props.onFilterValueChange?this.props.filterValue:this.state.filterValue}},{key:"getSelectedNodes",value:function(){var e=[];if(t.ObjectUtils.isNotEmpty(this.props.value)&&this.props.options){var n="single"===this.props.selectionMode?k({},"".concat(this.props.value),!0):L({},this.props.value);this.findSelectedNodes(null,n,e)}return e}},{key:"getLabel",value:function(){var e=this.getSelectedNodes();return e.length?e.map((function(e){return e.label})).join(", "):this.props.placeholder}},{key:"isValueEmpty",value:function(){return!this.props.value||0===Object.keys(this.props.value).length}},{key:"hasNoOptions",value:function(){return!this.props.options||0===this.props.options.length}},{key:"show",value:function(){this.setState({overlayVisible:!0})}},{key:"hide",value:function(){this.setState({overlayVisible:!1})}},{key:"onInputFocus",value:function(){this.setState({focused:!0})}},{key:"onInputBlur",value:function(){this.setState({focused:!1})}},{key:"onClick",value:function(e){this.props.disabled||this.overlayRef&&this.overlayRef.current&&this.overlayRef.current.contains(e.target)||t.DomHandler.hasClass(e.target,"p-treeselect-close")||(this.focusInput.focus(),this.state.overlayVisible?this.hide():this.show())}},{key:"onSelectionChange",value:function(e){this.props.onChange&&(this.selfChange=!0,this.props.onChange({originalEvent:e.originalEvent,value:e.value,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:e.value}}))}},{key:"onNodeSelect",value:function(e){this.props.onNodeSelect&&this.props.onNodeSelect(e),"single"===this.props.selectionMode&&this.hide()}},{key:"onNodeUnselect",value:function(e){this.props.onNodeUnselect&&this.props.onNodeUnselect(e)}},{key:"onNodeToggle",value:function(e){this.setState({expandedKeys:e.value})}},{key:"onFilterValueChange",value:function(e){this.setState({filterValue:e.value})}},{key:"onOverlayClick",value:function(e){r.OverlayService.emit("overlay-click",{originalEvent:e,target:this.container})}},{key:"onInputKeyDown",value:function(e){switch(e.which){case 40:!this.state.overlayVisible&&e.altKey&&this.show();break;case 32:this.state.overlayVisible||(this.show(),e.preventDefault());break;case 13:case 27:this.state.overlayVisible&&(this.hide(),e.preventDefault());break;case 9:this.hide()}}},{key:"onFilterInputKeyDown",value:function(e){13===e.which&&e.preventDefault()}},{key:"onFilterInputChange",value:function(e){var t=e.target.value;this.props.onFilterValueChange?this.props.onFilterValueChange({originalEvent:e,value:t}):this.setState({filterValue:t})}},{key:"resetFilter",value:function(){this.setState({filterValue:""})}},{key:"onOverlayEnter",value:function(){t.ZIndexUtils.set("overlay",this.overlayRef.current,p.default.autoZIndex,p.default.zIndex.overlay),this.alignOverlay(),this.scrollInView()}},{key:"onOverlayEntered",value:function(){this.bindDocumentClickListener(),this.bindScrollListener(),this.bindResizeListener(),this.props.filter&&this.props.filterInputAutoFocus&&this.filterInput.focus(),this.props.onShow&&this.props.onShow()}},{key:"onOverlayExit",value:function(){this.unbindDocumentClickListener(),this.unbindScrollListener(),this.unbindResizeListener()}},{key:"onOverlayExited",value:function(){this.props.filter&&this.props.resetFilterOnHide&&this.resetFilter(),t.ZIndexUtils.clear(this.overlayRef.current),this.props.onHide&&this.props.onHide()}},{key:"alignOverlay",value:function(){t.DomHandler.alignOverlay(this.overlayRef.current,this.trigger.parentElement,this.props.appendTo||p.default.appendTo)}},{key:"scrollInView",value:function(){var e=t.DomHandler.findSingle(this.overlayRef.current,".p-treenode-content.p-highlight");e&&e.scrollIntoView({block:"nearest",inline:"start"})}},{key:"bindDocumentClickListener",value:function(){var e=this;this.documentClickListener||(this.documentClickListener=function(t){e.state.overlayVisible&&e.isOutsideClicked(t)&&e.hide()},document.addEventListener("click",this.documentClickListener))}},{key:"unbindDocumentClickListener",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null)}},{key:"bindScrollListener",value:function(){var e=this;this.scrollHandler||(this.scrollHandler=new t.ConnectedOverlayScrollHandler(this.container,(function(){e.state.overlayVisible&&e.hide()}))),this.scrollHandler.bindScrollListener()}},{key:"unbindScrollListener",value:function(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()}},{key:"bindResizeListener",value:function(){var e=this;this.resizeListener||(this.resizeListener=function(){e.state.overlayVisible&&!t.DomHandler.isTouchDevice()&&e.hide()},window.addEventListener("resize",this.resizeListener))}},{key:"unbindResizeListener",value:function(){this.resizeListener&&(window.removeEventListener("resize",this.resizeListener),this.resizeListener=null)}},{key:"isOutsideClicked",value:function(e){return this.container&&!(this.container.isSameNode(e.target)||this.container.contains(e.target)||this.overlayRef&&this.overlayRef.current.contains(e.target))}},{key:"findSelectedNodes",value:function(e,t,n){if(e){if(this.isSelected(e,t)&&(n.push(e),delete t[e.key]),Object.keys(t).length&&e.children){var r,i=w(e.children);try{for(i.s();!(r=i.n()).done;){this.findSelectedNodes(r.value,t,n)}}catch(e){i.e(e)}finally{i.f()}}}else{var l,o=w(this.props.options);try{for(o.s();!(l=o.n()).done;){this.findSelectedNodes(l.value,t,n)}}catch(e){o.e(e)}finally{o.f()}}}},{key:"isSelected",value:function(e,t){return"checkbox"===this.props.selectionMode?t[e.key]&&t[e.key].checked:t[e.key]}},{key:"updateTreeState",value:function(){var e="single"===this.props.selectionMode?k({},"".concat(this.props.value),!0):L({},this.props.value);this.setState({expandedKeys:{}}),e&&this.props.options&&this.updateTreeBranchState(null,null,e)}},{key:"updateTreeBranchState",value:function(e,t,n){if(e){if(this.isSelected(e,n)&&(this.expandPath(t),delete n[e.key]),Object.keys(n).length&&e.children){var r,i=w(e.children);try{for(i.s();!(r=i.n()).done;){var l=r.value;t.push(e.key),this.updateTreeBranchState(l,t,n)}}catch(e){i.e(e)}finally{i.f()}}}else{var o,s=w(this.props.options);try{for(s.s();!(o=s.n()).done;){this.updateTreeBranchState(o.value,[],n)}}catch(e){s.e(e)}finally{s.f()}}}},{key:"expandPath",value:function(e){if(e.length>0){var t,n=L({},this.state.expandedKeys||{}),r=w(e);try{for(r.s();!(t=r.n()).done;){n[t.value]=!0}}catch(e){r.e(e)}finally{r.f()}this.setState({expandedKeys:n})}}},{key:"componentDidMount",value:function(){this.updateTreeState()}},{key:"componentDidUpdate",value:function(e,t){this.state.overlayVisible&&((this.props.filter||t.expandedKeys!==this.state.expandedKeys)&&this.alignOverlay(),e.value!==this.props.value&&(this.selfChange||this.updateTreeState(),this.scrollInView(),this.selfChange=!1)),e.options!==this.props.options&&this.updateTreeState()}},{key:"componentWillUnmount",value:function(){this.unbindDocumentClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),t.ZIndexUtils.clear(this.overlayRef.current)}},{key:"renderKeyboardHelper",value:function(){var e=this;return c.default.createElement("div",{className:"p-hidden-accessible"},c.default.createElement("input",{ref:function(t){return e.focusInput=t},role:"listbox",id:this.props.inputId,type:"text",readOnly:!0,"aria-haspopup":"true","aria-expanded":this.state.overlayVisible,onFocus:this.onInputFocus,onBlur:this.onInputBlur,onKeyDown:this.onInputKeyDown,disabled:this.props.disabled,tabIndex:this.props.tabIndex,"aria-label":this.props.ariaLabel,"aria-labelledby":this.props.ariaLabelledBy}))}},{key:"renderLabel",value:function(e){var n=this.isValueEmpty(),r=t.classNames("p-treeselect-label",{"p-placeholder":this.getLabel()===this.props.placeholder,"p-treeselect-label-empty":!this.props.placeholder&&n}),i=null;if(this.props.valueTemplate)i=t.ObjectUtils.getJSXElement(this.props.valueTemplate,e,this.props);else if("comma"===this.props.display)i=this.getLabel()||"empty";else if("chip"===this.props.display){var l=this.getSelectedNodes();i=c.default.createElement(c.default.Fragment,null,l&&l.map((function(e,t){return c.default.createElement("div",{className:"p-treeselect-token",key:"".concat(e.key,"_").concat(t)},c.default.createElement("span",{className:"p-treeselect-token-label"},e.label))})),n&&(this.props.placeholder||"empty"))}return c.default.createElement("div",{className:"p-treeselect-label-container"},c.default.createElement("div",{className:r},i))}},{key:"renderDropdownIcon",value:function(){var e=this,n=t.classNames("p-treeselect-trigger-icon p-clickable",this.props.dropdownIcon);return c.default.createElement("div",{ref:function(t){return e.trigger=t},className:"p-treeselect-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":this.state.overlayVisible},c.default.createElement("span",{className:n}))}},{key:"renderContent",value:function(){var e=this.getFilterValue();return c.default.createElement(c.default.Fragment,null,c.default.createElement(i.Tree,{value:this.props.options,selectionMode:this.props.selectionMode,selectionKeys:this.props.value,metaKeySelection:this.props.metaKeySelection,onSelectionChange:this.onSelectionChange,onSelect:this.onNodeSelect,onUnselect:this.onNodeUnselect,expandedKeys:this.state.expandedKeys,onToggle:this.onNodeToggle,onExpand:this.props.onNodeExpand,onCollapse:this.props.onNodeCollapse,filter:this.props.filter,filterValue:e,filterBy:this.props.filterBy,filterMode:this.props.filterMode,filterPlaceholder:this.props.filterPlaceholder,filterLocale:this.props.filterLocale,showHeader:!1,onFilterValueChange:this.onFilterValueChange}),this.hasNoOptions()&&c.default.createElement("div",{className:"p-treeselect-empty-message"},this.props.emptyMessage||n.localeOption("emptyMessage")))}},{key:"renderFilterElement",value:function(){var e=this;if(this.props.filter){var n=this.getFilterValue();return n=t.ObjectUtils.isNotEmpty(n)?n:"",c.default.createElement("div",{className:"p-treeselect-filter-container"},c.default.createElement("input",{ref:function(t){return e.filterInput=t},type:"text",value:n,autoComplete:"off",className:"p-treeselect-filter p-inputtext p-component",placeholder:this.props.filterPlaceholder,onKeyDown:this.onFilterInputKeyDown,onChange:this.onFilterInputChange,disabled:this.props.disabled}),c.default.createElement("span",{className:"p-treeselect-filter-icon pi pi-search"}))}return null}},{key:"renderHeader",value:function(){var e=this.renderFilterElement(),n=c.default.createElement("button",{type:"button",className:"p-treeselect-close p-link",onClick:this.hide},c.default.createElement("span",{className:"p-treeselect-close-icon pi pi-times"}),c.default.createElement(s.Ripple,null)),r=c.default.createElement("div",{className:"p-treeselect-header"},e,n);return this.props.header?t.ObjectUtils.getJSXElement(this.props.header,{className:"p-treeselect-header",filterElement:e,closeElement:n,closeElementClassName:"p-treeselect-close p-link",closeIconClassName:"p-treeselect-close-icon pi pi-times",onCloseClick:this.hide,element:r,props:this.props}):r}},{key:"render",value:function(){var e=this,n=t.classNames("p-treeselect p-component p-inputwrapper",{"p-treeselect-chip":"chip"===this.props.display,"p-disabled":this.props.disabled,"p-focus":this.state.focused,"p-inputwrapper-filled":!this.isValueEmpty(),"p-inputwrapper-focus":this.state.focused||this.state.overlayVisible},this.props.className),r=this.getSelectedNodes(),i=this.renderKeyboardHelper(),l=this.renderLabel(r),o=this.renderDropdownIcon(),s=this.renderContent(),a=this.renderHeader(),p=t.ObjectUtils.getJSXElement(this.props.footer,this.props);return c.default.createElement("div",{id:this.props.id,ref:function(t){return e.container=t},className:n,style:this.props.style,onClick:this.onClick},i,l,o,c.default.createElement(C,{ref:this.overlayRef,appendTo:this.props.appendTo,panelStyle:this.props.panelStyle,panelClassName:this.props.panelClassName,scrollHeight:this.props.scrollHeight,onClick:this.onOverlayClick,header:a,footer:p,transitionOptions:this.props.transitionOptions,in:this.state.overlayVisible,onEnter:this.onOverlayEnter,onEntered:this.onOverlayEntered,onExit:this.onOverlayExit,onExited:this.onOverlayExited},s))}}]),y}();k(j,"defaultProps",{id:null,value:null,name:null,style:null,className:null,disabled:!1,options:null,scrollHeight:"400px",placeholder:null,tabIndex:null,inputId:null,ariaLabel:null,ariaLabelledBy:null,selectionMode:"single",panelStyle:null,panelClassName:null,appendTo:null,emptyMessage:null,display:"comma",metaKeySelection:!0,valueTemplate:null,panelHeaderTemplate:null,panelFooterTemplate:null,transitionOptions:null,dropdownIcon:"pi pi-chevron-down",filter:!1,filterValue:null,filterBy:"label",filterMode:"lenient",filterPlaceholder:null,filterLocale:void 0,filterInputAutoFocus:!0,resetFilterOnHide:!1,onShow:null,onHide:null,onChange:null,onNodeSelect:null,onNodeUnselect:null,onNodeExpand:null,onNodeCollapse:null,onFilterValueChange:null}),exports.TreeSelect=j;
|