UNPKG

primevue

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primevue.svg)](https://badge.fury.io/js/primevue) [![Discord Chat](https://img.shields.io/discord/55794023

2 lines (1 loc) 17.4 kB
import e from"primevue/basecomponent";import t from"primevue/icons/search";import n from"primevue/icons/spinner";import{DomHandler as i,ObjectUtils as l}from"primevue/utils";import o from"primevue/icons/check";import s from"primevue/icons/chevrondown";import d from"primevue/icons/chevronright";import r from"primevue/icons/minus";import c from"primevue/ripple";import{resolveComponent as a,resolveDirective as h,openBlock as p,createElementBlock as u,mergeProps as g,createElementVNode as y,withDirectives as f,createBlock as k,resolveDynamicComponent as m,createCommentVNode as x,Fragment as b,createTextVNode as C,toDisplayString as K,renderList as v,renderSlot as S,vModelText as T,createVNode as N}from"vue";var w={name:"TreeNode",extends:e,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||i.hasClass(e.target,"p-tree-toggler")||i.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=i.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=i.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=[...i.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=i.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 i of e.children)this.propagateDown(i,t,n)},propagateUp(e){let t=e.check,n={...e.selectionKeys},i=0,l=!1;for(let e of this.node.children)n[e.key]&&n[e.key].checked?i++:n[e.key]&&n[e.key].partialChecked&&(l=!0);t&&i===this.node.children.length?n[this.node.key]={checked:!0,partialChecked:!1}:(t||delete n[this.node.key],l||i>0&&i!==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 i.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:s,ChevronRightIcon:d,CheckIcon:o,MinusIcon:r},directives:{ripple:c}};const M=["aria-label","aria-selected","aria-expanded","aria-setsize","aria-posinset","aria-level","aria-checked","tabindex"];w.render=function(e,t,n,i,l,o){const s=a("TreeNode",!0),d=h("ripple");return p(),u("li",g({ref:"currentNode",class:o.containerClass,role:"treeitem","aria-label":o.label(n.node),"aria-selected":o.ariaSelected,"aria-expanded":o.expanded,"aria-setsize":n.node.children?n.node.children.length:0,"aria-posinset":n.index+1,"aria-level":n.level,"aria-checked":o.ariaChecked,tabindex:0===n.index?0:-1,onKeydown:t[3]||(t[3]=(...e)=>o.onKeyDown&&o.onKeyDown(...e))},o.getPTOptions("node")),[y("div",g({class:o.contentClass,onClick:t[1]||(t[1]=(...e)=>o.onClick&&o.onClick(...e)),onTouchend:t[2]||(t[2]=(...e)=>o.onTouchEnd&&o.onTouchEnd(...e)),style:n.node.style},o.getPTOptions("content")),[f((p(),u("button",g({type:"button",class:"p-tree-toggler p-link",onClick:t[0]||(t[0]=(...e)=>o.toggle&&o.toggle(...e)),tabindex:"-1","aria-hidden":"true"},o.getPTOptions("toggler")),[n.templates.togglericon?(p(),k(m(n.templates.togglericon),{key:0,node:n.node,expanded:o.expanded,class:"p-tree-toggler-icon"},null,8,["node","expanded"])):o.expanded?(p(),k(m(n.node.expandedIcon?"span":"ChevronDownIcon"),g({key:1,class:"p-tree-toggler-icon"},o.getPTOptions("togglerIcon")),null,16)):(p(),k(m(n.node.collapsedIcon?"span":"ChevronRightIcon"),g({key:2,class:"p-tree-toggler-icon"},o.getPTOptions("togglerIcon")),null,16))],16)),[[d]]),o.checkboxMode?(p(),u("div",g({key:0,class:"p-checkbox p-component","aria-hidden":"true"},o.getPTOptions("checkboxContainer")),[y("div",g({class:o.checkboxClass,role:"checkbox"},o.getPTOptions("checkbox")),[n.templates.checkboxicon?(p(),k(m(n.templates.checkboxicon),{key:0,checked:o.checked,partialChecked:o.partialChecked,class:"p-checkbox-icon"},null,8,["checked","partialChecked"])):(p(),k(m(o.checked?"CheckIcon":o.partialChecked?"MinusIcon":null),g({key:1,class:"p-checkbox-icon"},o.getPTOptions("checkboxIcon")),null,16))],16)],16)):x("",!0),y("span",g({class:o.icon},o.getPTOptions("nodeIcon")),null,16),y("span",g({class:"p-treenode-label"},o.getPTOptions("label")),[n.templates[n.node.type]||n.templates.default?(p(),k(m(n.templates[n.node.type]||n.templates.default),{key:0,node:n.node},null,8,["node"])):(p(),u(b,{key:1},[C(K(o.label(n.node)),1)],64))],16)],16),o.hasChildren&&o.expanded?(p(),u("ul",g({key:0,class:"p-treenode-children",role:"group"},e.ptm("subgroup")),[(p(!0),u(b,null,v(n.node.children,(t=>(p(),k(s,{key:t.key,node:t,templates:n.templates,level:n.level+1,expandedKeys:n.expandedKeys,onNodeToggle:o.onChildNodeToggle,onNodeClick:o.onChildNodeClick,selectionMode:n.selectionMode,selectionKeys:n.selectionKeys,onCheckboxChange:o.propagateUp,pt:e.pt},null,8,["node","templates","level","expandedKeys","onNodeToggle","onNodeClick","selectionMode","selectionKeys","onCheckboxChange","pt"])))),128))],16)):x("",!0)],16,M)};var I={name:"Tree",extends:e,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,i=t.metaKey||t.ctrlKey;let l;return this.isNodeSelected(n)&&i?(this.isSingleSelectionMode()?l={}:(l={...this.selectionKeys},delete l[n.key]),this.$emit("node-unselect",n)):(this.isSingleSelectionMode()?l={}:this.isMultipleSelectionMode()&&(l=i&&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 i;return this.isSingleSelectionMode()?n?(i={},this.$emit("node-unselect",t)):(i={},i[t.key]=!0,this.$emit("node-select",t)):n?(i={...this.selectionKeys},delete i[t.key],this.$emit("node-unselect",t)):(i=this.selectionKeys?{...this.selectionKeys}:{},i[t.key]=!0,this.$emit("node-select",t)),i},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 i=[...e.children];e.children=[];for(let l of i){let i={...l};this.isFilterMatched(i,t)&&(n=!0,e.children.push(i))}}if(n)return!0}},isFilterMatched(e,{searchFields:t,filterText:n,strict:i}){let o=!1;for(let i of t){String(l.resolveFieldData(e,i)).toLocaleLowerCase(this.filterLocale).indexOf(n)>-1&&(o=!0)}return(!o||i&&!this.isNodeLeaf(e))&&(o=this.findFilteredNodes(e,{searchFields:t,filterText:n,strict:i})||o),o}},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),i="strict"===this.filterMode;for(let l of this.value){let o={...l},s={searchFields:t,filterText:n,strict:i};(i&&(this.findFilteredNodes(o,s)||this.isFilterMatched(o,s))||!i&&(this.isFilterMatched(o,s)||this.findFilteredNodes(o,s)))&&e.push(o)}return e},valueToRender(){return this.filterValue&&this.filterValue.trim().length>0?this.filteredValue:this.value}},components:{TreeNode:w,SearchIcon:t,SpinnerIcon:n}};const $=["placeholder"],E=["aria-labelledby","aria-label"];!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var i=document.head||document.getElementsByTagName("head")[0],l=document.createElement("style");l.type="text/css","top"===n&&i.firstChild?i.insertBefore(l,i.firstChild):i.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"),I.render=function(e,t,n,i,l,o){const s=a("SpinnerIcon"),d=a("SearchIcon"),r=a("TreeNode");return p(),u("div",g({class:o.containerClass},e.ptm("root")),[n.loading?(p(),u("div",g({key:0,class:"p-tree-loading-overlay p-component-overlay"},e.ptm("loadingOverlay")),[S(e.$slots,"loadingicon",{},(()=>[n.loadingIcon?(p(),u("i",g({key:0,class:["p-tree-loading-icon pi-spin",n.loadingIcon]},e.ptm("loadingIcon")),null,16)):(p(),k(s,g({key:1,spin:"",class:"p-tree-loading-icon"},e.ptm("loadingIcon")),null,16))]))],16)):x("",!0),n.filter?(p(),u("div",g({key:1,class:"p-tree-filter-container"},e.ptm("filterContainer")),[f(y("input",g({"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)=>o.onFilterKeydown&&o.onFilterKeydown(...e))},e.ptm("input")),null,16,$),[[T,l.filterValue]]),S(e.$slots,"searchicon",{},(()=>[N(d,g({class:"p-tree-filter-icon"},e.ptm("searchIcon")),null,16)]))],16)):x("",!0),y("div",g({class:"p-tree-wrapper",style:{maxHeight:n.scrollHeight}},e.ptm("wrapper")),[y("ul",g({class:"p-tree-container",role:"tree","aria-labelledby":e.ariaLabelledby,"aria-label":e.ariaLabel},e.ptm("container")),[(p(!0),u(b,null,v(o.valueToRender,((t,i)=>(p(),k(r,{key:t.key,node:t,templates:e.$slots,level:n.level+1,index:i,expandedKeys:l.d_expandedKeys,onNodeToggle:o.onNodeToggle,onNodeClick:o.onNodeClick,selectionMode:n.selectionMode,selectionKeys:n.selectionKeys,onCheckboxChange:o.onCheckboxChange,pt:e.pt},null,8,["node","templates","level","index","expandedKeys","onNodeToggle","onNodeClick","selectionMode","selectionKeys","onCheckboxChange","pt"])))),128))],16,E)],16)],16)};export{I as default};