primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 18.6 kB
JavaScript
"use strict";var e=require("primevue/basecomponent"),t=require("primevue/icons/search"),n=require("primevue/icons/spinner"),o=require("primevue/utils"),l=require("primevue/icons/check"),i=require("primevue/icons/chevrondown"),s=require("primevue/icons/chevronright"),r=require("primevue/icons/minus"),c=require("primevue/ripple"),d=require("vue");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var h=a(e),p=a(t),u=a(n),g=a(l),m=a(i),k=a(s),y=a(r),f=a(c),x={name:"TreeNode",extends:h.default,emits:["node-toggle","node-click","checkbox-change"],props:{node:{type:null,default:null},expandedKeys:{type:null,default:null},selectionKeys:{type:null,default:null},selectionMode:{type:String,default:null},templates:{type:null,default:null},level:{type:Number,default:null},index:{type:Number,default:null}},nodeTouched:!1,toggleClicked:!1,mounted(){this.$refs.currentNode.closest(".p-treeselect-items-wrapper")&&this.setAllNodesTabIndexes()},methods:{toggle(){this.$emit("node-toggle",this.node),this.toggleClicked=!0},label:e=>"function"==typeof e.label?e.label():e.label,onChildNodeToggle(e){this.$emit("node-toggle",e)},getPTOptions(e){return this.ptm(e,{context:{expanded:this.expanded,selected:this.selected,checked:this.checked}})},onClick(e){this.toggleClicked||o.DomHandler.hasClass(e.target,"p-tree-toggler")||o.DomHandler.hasClass(e.target.parentElement,"p-tree-toggler")?this.toggleClicked=!1:(this.isCheckboxSelectionMode()?this.toggleCheckbox():this.$emit("node-click",{originalEvent:e,nodeTouched:this.nodeTouched,node:this.node}),this.nodeTouched=!1)},onChildNodeClick(e){this.$emit("node-click",e)},onTouchEnd(){this.nodeTouched=!0},onKeyDown(e){if(this.isSameNode(e))switch(e.code){case"Tab":this.onTabKey(e);break;case"ArrowDown":this.onArrowDown(e);break;case"ArrowUp":this.onArrowUp(e);break;case"ArrowRight":this.onArrowRight(e);break;case"ArrowLeft":this.onArrowLeft(e);break;case"Enter":case"Space":this.onEnterKey(e)}},onArrowDown(e){const t=e.target,n=t.children[1];if(n)this.focusRowChange(t,n.children[0]);else if(t.nextElementSibling)this.focusRowChange(t,t.nextElementSibling);else{let e=this.findNextSiblingOfAncestor(t);e&&this.focusRowChange(t,e)}e.preventDefault()},onArrowUp(e){const t=e.target;if(t.previousElementSibling)this.focusRowChange(t,t.previousElementSibling,this.findLastVisibleDescendant(t.previousElementSibling));else{let e=this.getParentNodeElement(t);e&&this.focusRowChange(t,e)}e.preventDefault()},onArrowRight(e){this.leaf||this.expanded||(e.currentTarget.tabIndex=-1,this.$emit("node-toggle",this.node),this.$nextTick((()=>{this.onArrowDown(e)})))},onArrowLeft(e){const t=o.DomHandler.findSingle(e.currentTarget,".p-tree-toggler");if(0===this.level&&!this.expanded)return!1;if(this.expanded&&!this.leaf)return t.click(),!1;const n=this.findBeforeClickableNode(e.currentTarget);n&&this.focusRowChange(e.currentTarget,n)},onEnterKey(e){this.setTabIndexForSelectionMode(e,this.nodeTouched),this.onClick(e),e.preventDefault()},onTabKey(){this.setAllNodesTabIndexes()},setAllNodesTabIndexes(){const e=o.DomHandler.find(this.$refs.currentNode.closest(".p-tree-container"),".p-treenode"),t=[...e].some((e=>"true"===e.getAttribute("aria-selected")||"true"===e.getAttribute("aria-checked")));if([...e].forEach((e=>{e.tabIndex=-1})),t){[...e].filter((e=>"true"===e.getAttribute("aria-selected")||"true"===e.getAttribute("aria-checked")))[0].tabIndex=0}else[...e][0].tabIndex=0},setTabIndexForSelectionMode(e,t){if(null!==this.selectionMode){const n=[...o.DomHandler.find(this.$refs.currentNode.parentElement,".p-treenode")];e.currentTarget.tabIndex=!1===t?-1:0,n.every((e=>-1===e.tabIndex))&&(n[0].tabIndex=0)}},focusRowChange(e,t,n){e.tabIndex="-1",t.tabIndex="0",this.focusNode(n||t)},findBeforeClickableNode(e){const t=e.closest("ul").closest("li");if(t){const n=o.DomHandler.findSingle(t,"button");return n&&"hidden"!==n.style.visibility?t:this.findBeforeClickableNode(e.previousElementSibling)}return null},toggleCheckbox(){let e=this.selectionKeys?{...this.selectionKeys}:{};const t=!this.checked;this.propagateDown(this.node,t,e),this.$emit("checkbox-change",{node:this.node,check:t,selectionKeys:e})},propagateDown(e,t,n){if(t?n[e.key]={checked:!0,partialChecked:!1}:delete n[e.key],e.children&&e.children.length)for(let o of e.children)this.propagateDown(o,t,n)},propagateUp(e){let t=e.check,n={...e.selectionKeys},o=0,l=!1;for(let e of this.node.children)n[e.key]&&n[e.key].checked?o++:n[e.key]&&n[e.key].partialChecked&&(l=!0);t&&o===this.node.children.length?n[this.node.key]={checked:!0,partialChecked:!1}:(t||delete n[this.node.key],l||o>0&&o!==this.node.children.length?n[this.node.key]={checked:!1,partialChecked:!0}:delete n[this.node.key]),this.$emit("checkbox-change",{node:e.node,check:e.check,selectionKeys:n})},onChildCheckboxChange(e){this.$emit("checkbox-change",e)},findNextSiblingOfAncestor(e){let t=this.getParentNodeElement(e);return t?t.nextElementSibling?t.nextElementSibling:this.findNextSiblingOfAncestor(t):null},findLastVisibleDescendant(e){const t=e.children[1];if(t){const e=t.children[t.children.length-1];return this.findLastVisibleDescendant(e)}return e},getParentNodeElement(e){const t=e.parentElement.parentElement;return o.DomHandler.hasClass(t,"p-treenode")?t:null},focusNode(e){e.focus()},isCheckboxSelectionMode(){return"checkbox"===this.selectionMode},isSameNode:e=>e.currentTarget&&(e.currentTarget.isSameNode(e.target)||e.currentTarget.isSameNode(e.target.closest(".p-treenode")))},computed:{hasChildren(){return this.node.children&&this.node.children.length>0},expanded(){return this.expandedKeys&&!0===this.expandedKeys[this.node.key]},leaf(){return!1!==this.node.leaf&&!(this.node.children&&this.node.children.length)},selectable(){return!1!==this.node.selectable&&null!=this.selectionMode},selected(){return!(!this.selectionMode||!this.selectionKeys)&&!0===this.selectionKeys[this.node.key]},containerClass(){return["p-treenode",{"p-treenode-leaf":this.leaf}]},contentClass(){return["p-treenode-content",this.node.styleClass,{"p-treenode-selectable":this.selectable,"p-highlight":this.checkboxMode?this.checked:this.selected}]},icon(){return["p-treenode-icon",this.node.icon]},checkboxClass(){return["p-checkbox-box",{"p-highlight":this.checked,"p-indeterminate":this.partialChecked}]},checkboxMode(){return"checkbox"===this.selectionMode&&!1!==this.node.selectable},checked(){return!!this.selectionKeys&&(this.selectionKeys[this.node.key]&&this.selectionKeys[this.node.key].checked)},partialChecked(){return!!this.selectionKeys&&(this.selectionKeys[this.node.key]&&this.selectionKeys[this.node.key].partialChecked)},ariaChecked(){return"single"===this.selectionMode||"multiple"===this.selectionMode?this.selected:void 0},ariaSelected(){return this.checkboxMode?this.checked:void 0}},components:{ChevronDownIcon:m.default,ChevronRightIcon:k.default,CheckIcon:g.default,MinusIcon:y.default},directives:{ripple:f.default}};const b=["aria-label","aria-selected","aria-expanded","aria-setsize","aria-posinset","aria-level","aria-checked","tabindex"];x.render=function(e,t,n,o,l,i){const s=d.resolveComponent("TreeNode",!0),r=d.resolveDirective("ripple");return d.openBlock(),d.createElementBlock("li",d.mergeProps({ref:"currentNode",class:i.containerClass,role:"treeitem","aria-label":i.label(n.node),"aria-selected":i.ariaSelected,"aria-expanded":i.expanded,"aria-setsize":n.node.children?n.node.children.length:0,"aria-posinset":n.index+1,"aria-level":n.level,"aria-checked":i.ariaChecked,tabindex:0===n.index?0:-1,onKeydown:t[3]||(t[3]=(...e)=>i.onKeyDown&&i.onKeyDown(...e))},i.getPTOptions("node")),[d.createElementVNode("div",d.mergeProps({class:i.contentClass,onClick:t[1]||(t[1]=(...e)=>i.onClick&&i.onClick(...e)),onTouchend:t[2]||(t[2]=(...e)=>i.onTouchEnd&&i.onTouchEnd(...e)),style:n.node.style},i.getPTOptions("content")),[d.withDirectives((d.openBlock(),d.createElementBlock("button",d.mergeProps({type:"button",class:"p-tree-toggler p-link",onClick:t[0]||(t[0]=(...e)=>i.toggle&&i.toggle(...e)),tabindex:"-1","aria-hidden":"true"},i.getPTOptions("toggler")),[n.templates.togglericon?(d.openBlock(),d.createBlock(d.resolveDynamicComponent(n.templates.togglericon),{key:0,node:n.node,expanded:i.expanded,class:"p-tree-toggler-icon"},null,8,["node","expanded"])):i.expanded?(d.openBlock(),d.createBlock(d.resolveDynamicComponent(n.node.expandedIcon?"span":"ChevronDownIcon"),d.mergeProps({key:1,class:"p-tree-toggler-icon"},i.getPTOptions("togglerIcon")),null,16)):(d.openBlock(),d.createBlock(d.resolveDynamicComponent(n.node.collapsedIcon?"span":"ChevronRightIcon"),d.mergeProps({key:2,class:"p-tree-toggler-icon"},i.getPTOptions("togglerIcon")),null,16))],16)),[[r]]),i.checkboxMode?(d.openBlock(),d.createElementBlock("div",d.mergeProps({key:0,class:"p-checkbox p-component","aria-hidden":"true"},i.getPTOptions("checkboxContainer")),[d.createElementVNode("div",d.mergeProps({class:i.checkboxClass,role:"checkbox"},i.getPTOptions("checkbox")),[n.templates.checkboxicon?(d.openBlock(),d.createBlock(d.resolveDynamicComponent(n.templates.checkboxicon),{key:0,checked:i.checked,partialChecked:i.partialChecked,class:"p-checkbox-icon"},null,8,["checked","partialChecked"])):(d.openBlock(),d.createBlock(d.resolveDynamicComponent(i.checked?"CheckIcon":i.partialChecked?"MinusIcon":null),d.mergeProps({key:1,class:"p-checkbox-icon"},i.getPTOptions("checkboxIcon")),null,16))],16)],16)):d.createCommentVNode("",!0),d.createElementVNode("span",d.mergeProps({class:i.icon},i.getPTOptions("nodeIcon")),null,16),d.createElementVNode("span",d.mergeProps({class:"p-treenode-label"},i.getPTOptions("label")),[n.templates[n.node.type]||n.templates.default?(d.openBlock(),d.createBlock(d.resolveDynamicComponent(n.templates[n.node.type]||n.templates.default),{key:0,node:n.node},null,8,["node"])):(d.openBlock(),d.createElementBlock(d.Fragment,{key:1},[d.createTextVNode(d.toDisplayString(i.label(n.node)),1)],64))],16)],16),i.hasChildren&&i.expanded?(d.openBlock(),d.createElementBlock("ul",d.mergeProps({key:0,class:"p-treenode-children",role:"group"},e.ptm("subgroup")),[(d.openBlock(!0),d.createElementBlock(d.Fragment,null,d.renderList(n.node.children,(t=>(d.openBlock(),d.createBlock(s,{key:t.key,node:t,templates:n.templates,level:n.level+1,expandedKeys:n.expandedKeys,onNodeToggle:i.onChildNodeToggle,onNodeClick:i.onChildNodeClick,selectionMode:n.selectionMode,selectionKeys:n.selectionKeys,onCheckboxChange:i.propagateUp,pt:e.pt},null,8,["node","templates","level","expandedKeys","onNodeToggle","onNodeClick","selectionMode","selectionKeys","onCheckboxChange","pt"])))),128))],16)):d.createCommentVNode("",!0)],16,b)};var C={name:"Tree",extends:h.default,emits:["node-expand","node-collapse","update:expandedKeys","update:selectionKeys","node-select","node-unselect"],props:{value:{type:null,default:null},expandedKeys:{type:null,default:null},selectionKeys:{type:null,default:null},selectionMode:{type:String,default:null},metaKeySelection:{type:Boolean,default:!0},loading:{type:Boolean,default:!1},loadingIcon:{type:String,default:void 0},filter:{type:Boolean,default:!1},filterBy:{type:String,default:"label"},filterMode:{type:String,default:"lenient"},filterPlaceholder:{type:String,default:null},filterLocale:{type:String,default:void 0},scrollHeight:{type:String,default:null},level:{type:Number,default:0},"aria-labelledby":{type:String,default:null},"aria-label":{type:String,default:null}},data(){return{d_expandedKeys:this.expandedKeys||{},filterValue:null}},watch:{expandedKeys(e){this.d_expandedKeys=e}},methods:{onNodeToggle(e){const t=e.key;this.d_expandedKeys[t]?(delete this.d_expandedKeys[t],this.$emit("node-collapse",e)):(this.d_expandedKeys[t]=!0,this.$emit("node-expand",e)),this.d_expandedKeys={...this.d_expandedKeys},this.$emit("update:expandedKeys",this.d_expandedKeys)},onNodeClick(e){if(null!=this.selectionMode&&!1!==e.node.selectable){const t=!e.nodeTouched&&this.metaKeySelection?this.handleSelectionWithMetaKey(e):this.handleSelectionWithoutMetaKey(e);this.$emit("update:selectionKeys",t)}},onCheckboxChange(e){this.$emit("update:selectionKeys",e.selectionKeys),e.check?this.$emit("node-select",e.node):this.$emit("node-unselect",e.node)},handleSelectionWithMetaKey(e){const t=e.originalEvent,n=e.node,o=t.metaKey||t.ctrlKey;let l;return this.isNodeSelected(n)&&o?(this.isSingleSelectionMode()?l={}:(l={...this.selectionKeys},delete l[n.key]),this.$emit("node-unselect",n)):(this.isSingleSelectionMode()?l={}:this.isMultipleSelectionMode()&&(l=o&&this.selectionKeys?{...this.selectionKeys}:{}),l[n.key]=!0,this.$emit("node-select",n)),l},handleSelectionWithoutMetaKey(e){const t=e.node,n=this.isNodeSelected(t);let o;return this.isSingleSelectionMode()?n?(o={},this.$emit("node-unselect",t)):(o={},o[t.key]=!0,this.$emit("node-select",t)):n?(o={...this.selectionKeys},delete o[t.key],this.$emit("node-unselect",t)):(o=this.selectionKeys?{...this.selectionKeys}:{},o[t.key]=!0,this.$emit("node-select",t)),o},isSingleSelectionMode(){return"single"===this.selectionMode},isMultipleSelectionMode(){return"multiple"===this.selectionMode},isNodeSelected(e){return!(!this.selectionMode||!this.selectionKeys)&&!0===this.selectionKeys[e.key]},isChecked(e){return!!this.selectionKeys&&(this.selectionKeys[e.key]&&this.selectionKeys[e.key].checked)},isNodeLeaf:e=>!1!==e.leaf&&!(e.children&&e.children.length),onFilterKeydown(e){13===e.which&&e.preventDefault()},findFilteredNodes(e,t){if(e){let n=!1;if(e.children){let o=[...e.children];e.children=[];for(let l of o){let o={...l};this.isFilterMatched(o,t)&&(n=!0,e.children.push(o))}}if(n)return!0}},isFilterMatched(e,{searchFields:t,filterText:n,strict:l}){let i=!1;for(let l of t){String(o.ObjectUtils.resolveFieldData(e,l)).toLocaleLowerCase(this.filterLocale).indexOf(n)>-1&&(i=!0)}return(!i||l&&!this.isNodeLeaf(e))&&(i=this.findFilteredNodes(e,{searchFields:t,filterText:n,strict:l})||i),i}},computed:{containerClass(){return["p-tree p-component",{"p-tree-selectable":null!=this.selectionMode,"p-tree-loading":this.loading,"p-tree-flex-scrollable":"flex"===this.scrollHeight}]},filteredValue(){let e=[];const t=this.filterBy.split(","),n=this.filterValue.trim().toLocaleLowerCase(this.filterLocale),o="strict"===this.filterMode;for(let l of this.value){let i={...l},s={searchFields:t,filterText:n,strict:o};(o&&(this.findFilteredNodes(i,s)||this.isFilterMatched(i,s))||!o&&(this.isFilterMatched(i,s)||this.findFilteredNodes(i,s)))&&e.push(i)}return e},valueToRender(){return this.filterValue&&this.filterValue.trim().length>0?this.filteredValue:this.value}},components:{TreeNode:x,SearchIcon:p.default,SpinnerIcon:u.default}};const v=["placeholder"],K=["aria-labelledby","aria-label"];!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],l=document.createElement("style");l.type="text/css","top"===n&&o.firstChild?o.insertBefore(l,o.firstChild):o.appendChild(l),l.styleSheet?l.styleSheet.cssText=e:l.appendChild(document.createTextNode(e))}}("\n.p-tree-container {\n margin: 0;\n padding: 0;\n list-style-type: none;\n overflow: auto;\n}\n.p-treenode-children {\n margin: 0;\n padding: 0;\n list-style-type: none;\n}\n.p-tree-wrapper {\n overflow: auto;\n}\n.p-treenode-selectable {\n cursor: pointer;\n user-select: none;\n}\n.p-tree-toggler {\n cursor: pointer;\n user-select: none;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n overflow: hidden;\n position: relative;\n flex-shrink: 0;\n}\n.p-treenode-leaf > .p-treenode-content .p-tree-toggler {\n visibility: hidden;\n}\n.p-treenode-content {\n display: flex;\n align-items: center;\n}\n.p-tree-filter {\n width: 100%;\n}\n.p-tree-filter-container {\n position: relative;\n display: block;\n width: 100%;\n}\n.p-tree-filter-icon {\n position: absolute;\n top: 50%;\n margin-top: -0.5rem;\n}\n.p-tree-loading {\n position: relative;\n min-height: 4rem;\n}\n.p-tree .p-tree-loading-overlay {\n position: absolute;\n z-index: 1;\n display: flex;\n align-items: center;\n justify-content: center;\n}\n.p-tree-flex-scrollable {\n display: flex;\n flex: 1;\n height: 100%;\n flex-direction: column;\n}\n.p-tree-flex-scrollable .p-tree-wrapper {\n flex: 1;\n}\n"),C.render=function(e,t,n,o,l,i){const s=d.resolveComponent("SpinnerIcon"),r=d.resolveComponent("SearchIcon"),c=d.resolveComponent("TreeNode");return d.openBlock(),d.createElementBlock("div",d.mergeProps({class:i.containerClass},e.ptm("root")),[n.loading?(d.openBlock(),d.createElementBlock("div",d.mergeProps({key:0,class:"p-tree-loading-overlay p-component-overlay"},e.ptm("loadingOverlay")),[d.renderSlot(e.$slots,"loadingicon",{},(()=>[n.loadingIcon?(d.openBlock(),d.createElementBlock("i",d.mergeProps({key:0,class:["p-tree-loading-icon pi-spin",n.loadingIcon]},e.ptm("loadingIcon")),null,16)):(d.openBlock(),d.createBlock(s,d.mergeProps({key:1,spin:"",class:"p-tree-loading-icon"},e.ptm("loadingIcon")),null,16))]))],16)):d.createCommentVNode("",!0),n.filter?(d.openBlock(),d.createElementBlock("div",d.mergeProps({key:1,class:"p-tree-filter-container"},e.ptm("filterContainer")),[d.withDirectives(d.createElementVNode("input",d.mergeProps({"onUpdate:modelValue":t[0]||(t[0]=e=>l.filterValue=e),type:"text",autocomplete:"off",class:"p-tree-filter p-inputtext p-component",placeholder:n.filterPlaceholder,onKeydown:t[1]||(t[1]=(...e)=>i.onFilterKeydown&&i.onFilterKeydown(...e))},e.ptm("input")),null,16,v),[[d.vModelText,l.filterValue]]),d.renderSlot(e.$slots,"searchicon",{},(()=>[d.createVNode(r,d.mergeProps({class:"p-tree-filter-icon"},e.ptm("searchIcon")),null,16)]))],16)):d.createCommentVNode("",!0),d.createElementVNode("div",d.mergeProps({class:"p-tree-wrapper",style:{maxHeight:n.scrollHeight}},e.ptm("wrapper")),[d.createElementVNode("ul",d.mergeProps({class:"p-tree-container",role:"tree","aria-labelledby":e.ariaLabelledby,"aria-label":e.ariaLabel},e.ptm("container")),[(d.openBlock(!0),d.createElementBlock(d.Fragment,null,d.renderList(i.valueToRender,((t,o)=>(d.openBlock(),d.createBlock(c,{key:t.key,node:t,templates:e.$slots,level:n.level+1,index:o,expandedKeys:l.d_expandedKeys,onNodeToggle:i.onNodeToggle,onNodeClick:i.onNodeClick,selectionMode:n.selectionMode,selectionKeys:n.selectionKeys,onCheckboxChange:i.onCheckboxChange,pt:e.pt},null,8,["node","templates","level","index","expandedKeys","onNodeToggle","onNodeClick","selectionMode","selectionKeys","onCheckboxChange","pt"])))),128))],16,K)],16)],16)},module.exports=C;