2 lines
20 KiB
JavaScript
2 lines
20 KiB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/utils"),n=require("primereact/csstransition"),i=require("primereact/ripple"),o=require("primereact/overlayservice"),r=require("primereact/portal"),l=require("primereact/api");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=s(e),p=s(l);function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function h(e,t,n){return t&&c(e.prototype,t),n&&c(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function d(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e,t){return f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},f(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&&f(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,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 d(e)}function m(e){return m=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},m(e)}function O(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,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=S(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var i=0,o=function(){};return{s:o,n:function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}},e:function(e){throw e},f:o}}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 r,l=!0,s=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return l=e.done,e},e:function(e){s=!0,r=e},f:function(){try{l||null==n.return||n.return()}finally{if(s)throw r}}}}function S(e,t){if(e){if("string"==typeof e)return g(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)?g(e,t):void 0}}function g(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=new Array(t);n<t;n++)i[n]=e[n];return i}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}}var E=function(n){v(s,e.Component);var o,r,l=(o=s,r=w(),function(){var e,t=m(o);if(r){var n=m(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return b(this,e)});function s(e){var t;return u(this,s),(t=l.call(this,e)).state={activeOption:null},t.onOptionSelect=t.onOptionSelect.bind(d(t)),t.onOptionGroupSelect=t.onOptionGroupSelect.bind(d(t)),t}return h(s,[{key:"componentDidMount",value:function(){if(this.props.selectionPath&&this.props.options&&!this.props.dirty){var e,t=k(this.props.options);try{for(t.s();!(e=t.n()).done;){var n=e.value;if(this.props.selectionPath.includes(n)){this.setState({activeOption:n});break}}}catch(e){t.e(e)}finally{t.f()}}this.props.root||this.position()}},{key:"componentDidUpdate",value:function(e){e.parentActive!==this.props.parentActive&&this.setState({activeOption:null})}},{key:"position",value:function(){var e=this.element.parentElement,n=t.DomHandler.getOffset(e),i=t.DomHandler.getViewport(),o=this.element.offsetParent?this.element.offsetWidth:t.DomHandler.getHiddenElementOuterWidth(this.element),r=t.DomHandler.getOuterWidth(e.children[0]);parseInt(n.left,10)+r+o>i.width-t.DomHandler.calculateScrollbarWidth()&&(this.element.style.left="-100%")}},{key:"onOptionSelect",value:function(e){this.props.onOptionSelect&&this.props.onOptionSelect(e)}},{key:"onKeyDown",value:function(e,t){var n=e.currentTarget.parentElement;switch(e.key){case"Down":case"ArrowDown":var i=this.findNextItem(n);i&&i.children[0].focus();break;case"Up":case"ArrowUp":var o=this.findPrevItem(n);o&&o.children[0].focus();break;case"Right":case"ArrowRight":this.isOptionGroup(t)&&(this.state.activeOption===t?n.children[1].children[0].children[0].focus():this.setState({activeOption:t}));break;case"Left":case"ArrowLeft":this.setState({activeOption:null});var r=e.currentTarget.parentElement.parentElement.previousElementSibling;r&&r.focus();break;case"Enter":this.onOptionClick(e,t);break;case"Tab":case"Escape":this.props.onPanelHide&&(this.props.onPanelHide(),e.preventDefault())}e.preventDefault()}},{key:"findNextItem",value:function(e){var n=e.nextElementSibling;return n?t.DomHandler.hasClass(n,"p-disabled")||!t.DomHandler.hasClass(n,"p-cascadeselect-item")?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-cascadeselect-item")?this.findPrevItem(n):n:null}},{key:"onOptionClick",value:function(e,t){this.isOptionGroup(t)?(this.setState({activeOption:this.state.activeOption===t?null:t}),this.props.onOptionGroupSelect&&this.props.onOptionGroupSelect({originalEvent:e,value:t})):this.props.onOptionSelect&&this.props.onOptionSelect({originalEvent:e,value:this.getOptionValue(t)})}},{key:"onOptionGroupSelect",value:function(e){this.props.onOptionGroupSelect&&this.props.onOptionGroupSelect(e)}},{key:"getOptionLabel",value:function(e){return this.props.optionLabel?t.ObjectUtils.resolveFieldData(e,this.props.optionLabel):e}},{key:"getOptionValue",value:function(e){return this.props.optionValue?t.ObjectUtils.resolveFieldData(e,this.props.optionValue):e}},{key:"getOptionGroupLabel",value:function(e){return this.props.optionGroupLabel?t.ObjectUtils.resolveFieldData(e,this.props.optionGroupLabel):null}},{key:"getOptionGroupChildren",value:function(e){return t.ObjectUtils.resolveFieldData(e,this.props.optionGroupChildren[this.props.level])}},{key:"isOptionGroup",value:function(e){return Object.prototype.hasOwnProperty.call(e,this.props.optionGroupChildren[this.props.level])}},{key:"getOptionLabelToRender",value:function(e){return this.isOptionGroup(e)?this.getOptionGroupLabel(e):this.getOptionLabel(e)}},{key:"renderSubmenu",value:function(e){return this.isOptionGroup(e)&&this.state.activeOption===e?a.default.createElement(s,{options:this.getOptionGroupChildren(e),className:"p-cascadeselect-sublist",selectionPath:this.props.selectionPath,optionLabel:this.props.optionLabel,optionValue:this.props.optionValue,level:this.props.level+1,onOptionSelect:this.onOptionSelect,onOptionGroupSelect:this.onOptionGroupSelect,parentActive:this.state.activeOption===e,optionGroupLabel:this.props.optionGroupLabel,optionGroupChildren:this.props.optionGroupChildren,dirty:this.props.dirty,template:this.props.template,onPanelHide:this.props.onPanelHide}):null}},{key:"renderOption",value:function(e,n){var o=this,r=t.classNames("p-cascadeselect-item",{"p-cascadeselect-item-group":this.isOptionGroup(e),"p-cascadeselect-item-active p-highlight":this.state.activeOption===e},e.className),l=this.renderSubmenu(e),s=this.props.template?t.ObjectUtils.getJSXElement(this.props.template,this.getOptionValue(e)):a.default.createElement("span",{className:"p-cascadeselect-item-text"},this.getOptionLabelToRender(e)),p=this.isOptionGroup(e)&&a.default.createElement("span",{className:"p-cascadeselect-group-icon pi pi-angle-right"});return a.default.createElement("li",{key:this.getOptionLabelToRender(e)+"_"+n,className:r,style:e.style,role:"none"},a.default.createElement("div",{className:"p-cascadeselect-item-content",onClick:function(t){return o.onOptionClick(t,e)},tabIndex:0,onKeyDown:function(t){return o.onKeyDown(t,e)}},s,p,a.default.createElement(i.Ripple,null)),l)}},{key:"renderMenu",value:function(){var e=this;return this.props.options?this.props.options.map((function(t,n){return e.renderOption(t,n)})):null}},{key:"render",value:function(){var e=this,n=t.classNames("p-cascadeselect-panel p-cascadeselect-items",this.props.className),i=this.renderMenu();return a.default.createElement("ul",{ref:function(t){return e.element=t},className:n,role:"listbox","aria-orientation":"horizontal"},i)}}]),s}();function L(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=C(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var i=0,o=function(){};return{s:o,n:function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}},e:function(e){throw e},f:o}}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 r,l=!0,s=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return l=e.done,e},e:function(e){s=!0,r=e},f:function(){try{l||null==n.return||n.return()}finally{if(s)throw r}}}}function C(e,t){if(e){if("string"==typeof e)return G(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)?G(e,t):void 0}}function G(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=new Array(t);n<t;n++)i[n]=e[n];return i}function R(){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}}O(E,"defaultProps",{options:null,selectionPath:!1,className:null,optionLabel:null,optionValue:null,level:null,optionGroupLabel:null,optionGroupChildren:null,parentActive:null,dirty:null,root:null,template:null,onOptionSelect:null,onOptionGroupSelect:null});var I=function(i){v(f,e.Component);var l,s,c=(l=f,s=R(),function(){var e,t=m(l);if(s){var n=m(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return b(this,e)});function f(t){var n;return u(this,f),(n=c.call(this,t)).state={focused:!1,overlayVisible:!1},n.dirty=!1,n.selectionPath=null,n.overlayRef=e.createRef(),n.inputRef=e.createRef(n.props.inputRef),n.hide=n.hide.bind(d(n)),n.onClick=n.onClick.bind(d(n)),n.onInputFocus=n.onInputFocus.bind(d(n)),n.onInputBlur=n.onInputBlur.bind(d(n)),n.onInputKeyDown=n.onInputKeyDown.bind(d(n)),n.onOverlayEnter=n.onOverlayEnter.bind(d(n)),n.onOverlayEntered=n.onOverlayEntered.bind(d(n)),n.onOverlayExit=n.onOverlayExit.bind(d(n)),n.onOverlayExited=n.onOverlayExited.bind(d(n)),n.onOptionSelect=n.onOptionSelect.bind(d(n)),n.onOptionGroupSelect=n.onOptionGroupSelect.bind(d(n)),n.onPanelClick=n.onPanelClick.bind(d(n)),n}return h(f,[{key:"onOptionSelect",value:function(e){this.props.onChange&&this.props.onChange({originalEvent:e,value:e.value}),this.updateSelectionPath(),this.hide(),this.inputRef.current.focus()}},{key:"onOptionGroupSelect",value:function(e){this.dirty=!0,this.props.onGroupChange&&this.props.onGroupChange(e)}},{key:"getOptionLabel",value:function(e){return this.props.optionLabel?t.ObjectUtils.resolveFieldData(e,this.props.optionLabel):e}},{key:"getOptionValue",value:function(e){return this.props.optionValue?t.ObjectUtils.resolveFieldData(e,this.props.optionValue):e}},{key:"getOptionGroupChildren",value:function(e,n){return t.ObjectUtils.resolveFieldData(e,this.props.optionGroupChildren[n])}},{key:"isOptionGroup",value:function(e,t){return Object.prototype.hasOwnProperty.call(e,this.props.optionGroupChildren[t])}},{key:"updateSelectionPath",value:function(){var e;if(null!=this.props.value&&this.props.options){var t,n=L(this.props.options);try{for(n.s();!(t=n.n()).done;){if(e=this.findModelOptionInGroup(t.value,0))break}}catch(e){n.e(e)}finally{n.f()}}this.selectionPath=e}},{key:"findModelOptionInGroup",value:function(e,n){if(this.isOptionGroup(e,n)){var i,o,r=L(this.getOptionGroupChildren(e,n));try{for(r.s();!(o=r.n()).done;){if(i=this.findModelOptionInGroup(o.value,n+1))return i.unshift(e),i}}catch(e){r.e(e)}finally{r.f()}}else if(t.ObjectUtils.equals(this.props.value,this.getOptionValue(e),this.props.dataKey))return[e];return null}},{key:"onClick",value:function(e){if(!this.props.disabled){var t=this.overlayRef?this.overlayRef.current:null;t&&t.contains(e.target)||(this.inputRef.current.focus(),this.state.overlayVisible?this.hide():this.show())}}},{key:"onInputFocus",value:function(){this.setState({focused:!0})}},{key:"onInputBlur",value:function(){this.setState({focused:!1})}},{key:"onInputKeyDown",value:function(e){switch(e.which){case 40:this.state.overlayVisible?t.DomHandler.findSingle(this.overlayRef.current,".p-cascadeselect-item").children[0].focus():e.altKey&&this.props.options&&this.props.options.length&&this.show(),e.preventDefault();break;case 32:this.state.overlayVisible?this.hide():this.show(),e.preventDefault();break;case 9:this.hide()}}},{key:"onPanelClick",value:function(e){o.OverlayService.emit("overlay-click",{originalEvent:e,target:this.container})}},{key:"show",value:function(){this.props.onBeforeShow&&this.props.onBeforeShow(),this.setState({overlayVisible:!0})}},{key:"hide",value:function(){var e=this;this.props.onBeforeHide&&this.props.onBeforeHide(),this.setState({overlayVisible:!1},(function(){e.inputRef.current.focus()}))}},{key:"onOverlayEnter",value:function(){t.ZIndexUtils.set("overlay",this.overlayRef.current,p.default.autoZIndex,p.default.zIndex.overlay),this.alignOverlay()}},{key:"onOverlayEntered",value:function(){this.bindOutsideClickListener(),this.bindScrollListener(),this.bindResizeListener(),this.props.onShow&&this.props.onShow()}},{key:"onOverlayExit",value:function(){this.unbindOutsideClickListener(),this.unbindScrollListener(),this.unbindResizeListener(),this.dirty=!1}},{key:"onOverlayExited",value:function(){t.ZIndexUtils.clear(this.overlayRef.current),this.props.onHide&&this.props.onHide()}},{key:"alignOverlay",value:function(){t.DomHandler.alignOverlay(this.overlayRef.current,this.label.parentElement,this.props.appendTo||p.default.appendTo)}},{key:"bindOutsideClickListener",value:function(){var e=this;this.outsideClickListener||(this.outsideClickListener=function(t){e.state.overlayVisible&&e.isOutsideClicked(t)&&e.hide()},document.addEventListener("click",this.outsideClickListener))}},{key:"unbindOutsideClickListener",value:function(){this.outsideClickListener&&(document.removeEventListener("click",this.outsideClickListener),this.outsideClickListener=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:"updateInputRef",value:function(){var e=this.props.inputRef;e&&("function"==typeof e?e(this.inputRef.current):e.current=this.inputRef.current)}},{key:"componentDidMount",value:function(){this.updateInputRef(),this.updateSelectionPath()}},{key:"componentWillUnmount",value:function(){this.unbindOutsideClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),t.ZIndexUtils.clear(this.overlayRef.current)}},{key:"componentDidUpdate",value:function(e){e.value!==this.props.value&&this.updateSelectionPath()}},{key:"renderKeyboardHelper",value:function(){var e=this.props.value?this.getOptionLabel(this.props.value):null;return a.default.createElement("div",{className:"p-hidden-accessible"},a.default.createElement("input",{ref:this.inputRef,type:"text",id:this.props.inputId,name:this.props.name,defaultValue:e,readOnly:!0,disabled:this.props.disabled,onFocus:this.onInputFocus,onBlur:this.onInputBlur,onKeyDown:this.onInputKeyDown,tabIndex:this.props.tabIndex,"aria-haspopup":"listbox","aria-labelledby":this.props.ariaLabelledBy}))}},{key:"renderLabel",value:function(){var e=this,n=this.props.value?this.getOptionLabel(this.props.value):this.props.placeholder||"p-emptylabel",i=t.classNames("p-cascadeselect-label ",{"p-placeholder":n===this.props.placeholder,"p-cascadeselect-label-empty":!this.props.value&&"p-emptylabel"===n});return a.default.createElement("span",{ref:function(t){return e.label=t},className:i},n)}},{key:"renderDropdownIcon",value:function(){var e=t.classNames("p-cascadeselect-trigger-icon",this.props.dropdownIcon);return a.default.createElement("div",{className:"p-cascadeselect-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":this.state.overlayVisible},a.default.createElement("span",{className:e}))}},{key:"renderOverlay",value:function(){return a.default.createElement(r.Portal,{element:a.default.createElement(n.CSSTransition,{nodeRef:this.overlayRef,classNames:"p-connected-overlay",in:this.state.overlayVisible,timeout:{enter:120,exit:100},options:this.props.transitionOptions,unmountOnExit:!0,onEnter:this.onOverlayEnter,onEntered:this.onOverlayEntered,onExit:this.onOverlayExit,onExited:this.onOverlayExited},a.default.createElement("div",{ref:this.overlayRef,className:"p-cascadeselect-panel p-component",onClick:this.onPanelClick},a.default.createElement("div",{className:"p-cascadeselect-items-wrapper"},a.default.createElement(E,{options:this.props.options,selectionPath:this.selectionPath,className:"p-cascadeselect-items",optionLabel:this.props.optionLabel,optionValue:this.props.optionValue,level:0,optionGroupLabel:this.props.optionGroupLabel,optionGroupChildren:this.props.optionGroupChildren,onOptionSelect:this.onOptionSelect,onOptionGroupSelect:this.onOptionGroupSelect,root:!0,template:this.props.itemTemplate,onPanelHide:this.hide})))),appendTo:this.props.appendTo})}},{key:"renderElement",value:function(){var e=this,n=t.classNames("p-cascadeselect p-component p-inputwrapper",this.props.className,{"p-disabled":this.props.disabled,"p-focus":this.state.focused,"p-inputwrapper-filled":this.props.value,"p-inputwrapper-focus":this.state.focused||this.state.overlayVisible}),i=this.renderKeyboardHelper(),o=this.renderLabel(),r=this.renderDropdownIcon(),l=this.renderOverlay();return a.default.createElement("div",{id:this.props.id,ref:function(t){return e.container=t},className:n,style:this.props.style,onClick:this.onClick},i,o,r,l)}},{key:"render",value:function(){return this.renderElement()}}]),f}();O(I,"defaultProps",{id:null,inputRef:null,style:null,className:null,value:null,name:null,options:null,optionLabel:null,optionValue:null,optionGroupLabel:null,optionGroupChildren:null,placeholder:null,itemTemplate:null,disabled:!1,dataKey:null,inputId:null,tabIndex:null,ariaLabelledBy:null,appendTo:null,transitionOptions:null,dropdownIcon:"pi pi-chevron-down",onChange:null,onGroupChange:null,onBeforeShow:null,onBeforeHide:null,onShow:null,onHide:null}),exports.CascadeSelect=I;
|