2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<div :class="containerClass">
|
|
|
|
<template v-if="loading">
|
|
|
|
<div class="p-tree-loading-overlay p-component-overlay">
|
2023-04-07 06:49:49 +00:00
|
|
|
<slot name="loadingicon">
|
|
|
|
<component :is="loadingIcon ? 'i' : 'SpinnerIcon'" spin :class="['p-tree-loading-icon', loadingIcon]" />
|
|
|
|
</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
2022-09-14 11:26:01 +00:00
|
|
|
<div v-if="filter" class="p-tree-filter-container">
|
|
|
|
<input v-model="filterValue" type="text" autocomplete="off" class="p-tree-filter p-inputtext p-component" :placeholder="filterPlaceholder" @keydown="onFilterKeydown" />
|
2023-04-07 06:49:49 +00:00
|
|
|
<slot name="searchicon">
|
|
|
|
<SearchIcon class="p-tree-filter-icon" />
|
|
|
|
</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
2022-09-14 11:26:01 +00:00
|
|
|
<div class="p-tree-wrapper" :style="{ maxHeight: scrollHeight }">
|
2022-12-08 11:04:25 +00:00
|
|
|
<ul class="p-tree-container" role="tree" :aria-labelledby="ariaLabelledby" :aria-label="ariaLabel">
|
2022-09-14 11:26:01 +00:00
|
|
|
<TreeNode
|
|
|
|
v-for="(node, index) of valueToRender"
|
|
|
|
:key="node.key"
|
|
|
|
:node="node"
|
|
|
|
:templates="$slots"
|
|
|
|
:level="level + 1"
|
|
|
|
:index="index"
|
|
|
|
:expandedKeys="d_expandedKeys"
|
|
|
|
@node-toggle="onNodeToggle"
|
|
|
|
@node-click="onNodeClick"
|
|
|
|
:selectionMode="selectionMode"
|
|
|
|
:selectionKeys="selectionKeys"
|
|
|
|
@checkbox-change="onCheckboxChange"
|
|
|
|
></TreeNode>
|
2022-09-06 12:03:37 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-04-07 06:49:49 +00:00
|
|
|
import SearchIcon from 'primevue/icon/search';
|
|
|
|
import SpinnerIcon from 'primevue/icon/spinner';
|
2022-09-14 11:26:01 +00:00
|
|
|
import { ObjectUtils } from 'primevue/utils';
|
2022-12-08 11:04:25 +00:00
|
|
|
import TreeNode from './TreeNode.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Tree',
|
|
|
|
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: true
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
loadingIcon: {
|
|
|
|
type: String,
|
2023-04-07 06:49:49 +00:00
|
|
|
default: undefined
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
filterBy: {
|
|
|
|
type: String,
|
|
|
|
default: 'label'
|
|
|
|
},
|
|
|
|
filterMode: {
|
|
|
|
type: String,
|
|
|
|
default: 'lenient'
|
|
|
|
},
|
|
|
|
filterPlaceholder: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
filterLocale: {
|
|
|
|
type: String,
|
|
|
|
default: undefined
|
|
|
|
},
|
|
|
|
scrollHeight: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
level: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
'aria-labelledby': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
'aria-label': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
d_expandedKeys: this.expandedKeys || {},
|
|
|
|
filterValue: null
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
expandedKeys(newValue) {
|
|
|
|
this.d_expandedKeys = newValue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onNodeToggle(node) {
|
|
|
|
const key = node.key;
|
|
|
|
|
|
|
|
if (this.d_expandedKeys[key]) {
|
|
|
|
delete this.d_expandedKeys[key];
|
|
|
|
this.$emit('node-collapse', node);
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2022-09-06 12:03:37 +00:00
|
|
|
this.d_expandedKeys[key] = true;
|
|
|
|
this.$emit('node-expand', node);
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
this.d_expandedKeys = { ...this.d_expandedKeys };
|
2022-09-06 12:03:37 +00:00
|
|
|
this.$emit('update:expandedKeys', this.d_expandedKeys);
|
|
|
|
},
|
|
|
|
onNodeClick(event) {
|
|
|
|
if (this.selectionMode != null && event.node.selectable !== false) {
|
|
|
|
const metaSelection = event.nodeTouched ? false : this.metaKeySelection;
|
|
|
|
const _selectionKeys = metaSelection ? this.handleSelectionWithMetaKey(event) : this.handleSelectionWithoutMetaKey(event);
|
|
|
|
|
|
|
|
this.$emit('update:selectionKeys', _selectionKeys);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onCheckboxChange(event) {
|
|
|
|
this.$emit('update:selectionKeys', event.selectionKeys);
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
if (event.check) this.$emit('node-select', event.node);
|
|
|
|
else this.$emit('node-unselect', event.node);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
handleSelectionWithMetaKey(event) {
|
|
|
|
const originalEvent = event.originalEvent;
|
|
|
|
const node = event.node;
|
2022-09-14 11:26:01 +00:00
|
|
|
const metaKey = originalEvent.metaKey || originalEvent.ctrlKey;
|
2022-09-06 12:03:37 +00:00
|
|
|
const selected = this.isNodeSelected(node);
|
|
|
|
let _selectionKeys;
|
|
|
|
|
|
|
|
if (selected && metaKey) {
|
|
|
|
if (this.isSingleSelectionMode()) {
|
|
|
|
_selectionKeys = {};
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
|
|
|
_selectionKeys = { ...this.selectionKeys };
|
2022-09-06 12:03:37 +00:00
|
|
|
delete _selectionKeys[node.key];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$emit('node-unselect', node);
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2022-09-06 12:03:37 +00:00
|
|
|
if (this.isSingleSelectionMode()) {
|
|
|
|
_selectionKeys = {};
|
2022-09-14 11:26:01 +00:00
|
|
|
} else if (this.isMultipleSelectionMode()) {
|
|
|
|
_selectionKeys = !metaKey ? {} : this.selectionKeys ? { ...this.selectionKeys } : {};
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_selectionKeys[node.key] = true;
|
|
|
|
this.$emit('node-select', node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _selectionKeys;
|
|
|
|
},
|
|
|
|
handleSelectionWithoutMetaKey(event) {
|
|
|
|
const node = event.node;
|
|
|
|
const selected = this.isNodeSelected(node);
|
|
|
|
let _selectionKeys;
|
|
|
|
|
|
|
|
if (this.isSingleSelectionMode()) {
|
|
|
|
if (selected) {
|
|
|
|
_selectionKeys = {};
|
|
|
|
this.$emit('node-unselect', node);
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2022-09-06 12:03:37 +00:00
|
|
|
_selectionKeys = {};
|
|
|
|
_selectionKeys[node.key] = true;
|
|
|
|
this.$emit('node-select', node);
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2022-09-06 12:03:37 +00:00
|
|
|
if (selected) {
|
2022-09-14 11:26:01 +00:00
|
|
|
_selectionKeys = { ...this.selectionKeys };
|
2022-09-06 12:03:37 +00:00
|
|
|
delete _selectionKeys[node.key];
|
|
|
|
|
|
|
|
this.$emit('node-unselect', node);
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
|
|
|
_selectionKeys = this.selectionKeys ? { ...this.selectionKeys } : {};
|
2022-09-06 12:03:37 +00:00
|
|
|
_selectionKeys[node.key] = true;
|
|
|
|
|
|
|
|
this.$emit('node-select', node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _selectionKeys;
|
|
|
|
},
|
|
|
|
isSingleSelectionMode() {
|
|
|
|
return this.selectionMode === 'single';
|
|
|
|
},
|
|
|
|
isMultipleSelectionMode() {
|
|
|
|
return this.selectionMode === 'multiple';
|
|
|
|
},
|
|
|
|
isNodeSelected(node) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return this.selectionMode && this.selectionKeys ? this.selectionKeys[node.key] === true : false;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
isChecked(node) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return this.selectionKeys ? this.selectionKeys[node.key] && this.selectionKeys[node.key].checked : false;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
isNodeLeaf(node) {
|
|
|
|
return node.leaf === false ? false : !(node.children && node.children.length);
|
|
|
|
},
|
|
|
|
onFilterKeydown(event) {
|
|
|
|
if (event.which === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
findFilteredNodes(node, paramsWithoutNode) {
|
|
|
|
if (node) {
|
|
|
|
let matched = false;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
if (node.children) {
|
|
|
|
let childNodes = [...node.children];
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
node.children = [];
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
for (let childNode of childNodes) {
|
2022-09-14 11:26:01 +00:00
|
|
|
let copyChildNode = { ...childNode };
|
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
if (this.isFilterMatched(copyChildNode, paramsWithoutNode)) {
|
|
|
|
matched = true;
|
|
|
|
node.children.push(copyChildNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matched) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
isFilterMatched(node, { searchFields, filterText, strict }) {
|
2022-09-06 12:03:37 +00:00
|
|
|
let matched = false;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
|
|
|
for (let field of searchFields) {
|
2022-09-06 12:03:37 +00:00
|
|
|
let fieldValue = String(ObjectUtils.resolveFieldData(node, field)).toLocaleLowerCase(this.filterLocale);
|
2022-09-14 11:26:01 +00:00
|
|
|
|
|
|
|
if (fieldValue.indexOf(filterText) > -1) {
|
2022-09-06 12:03:37 +00:00
|
|
|
matched = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matched || (strict && !this.isNodeLeaf(node))) {
|
2022-09-14 11:26:01 +00:00
|
|
|
matched = this.findFilteredNodes(node, { searchFields, filterText, strict }) || matched;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return matched;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
containerClass() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return [
|
|
|
|
'p-tree p-component',
|
|
|
|
{
|
|
|
|
'p-tree-selectable': this.selectionMode != null,
|
|
|
|
'p-tree-loading': this.loading,
|
|
|
|
'p-tree-flex-scrollable': this.scrollHeight === 'flex'
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
filteredValue() {
|
|
|
|
let filteredNodes = [];
|
|
|
|
const searchFields = this.filterBy.split(',');
|
|
|
|
const filterText = this.filterValue.trim().toLocaleLowerCase(this.filterLocale);
|
|
|
|
const strict = this.filterMode === 'strict';
|
|
|
|
|
|
|
|
for (let node of this.value) {
|
2022-09-14 11:26:01 +00:00
|
|
|
let _node = { ...node };
|
|
|
|
let paramsWithoutNode = { searchFields, filterText, strict };
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
if (
|
|
|
|
(strict && (this.findFilteredNodes(_node, paramsWithoutNode) || this.isFilterMatched(_node, paramsWithoutNode))) ||
|
|
|
|
(!strict && (this.isFilterMatched(_node, paramsWithoutNode) || this.findFilteredNodes(_node, paramsWithoutNode)))
|
|
|
|
) {
|
2022-09-06 12:03:37 +00:00
|
|
|
filteredNodes.push(_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredNodes;
|
|
|
|
},
|
|
|
|
valueToRender() {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.filterValue && this.filterValue.trim().length > 0) return this.filteredValue;
|
|
|
|
else return this.value;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
2023-04-07 06:49:49 +00:00
|
|
|
TreeNode: TreeNode,
|
|
|
|
SearchIcon: SearchIcon,
|
|
|
|
SpinnerIcon: SpinnerIcon
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.p-tree-container {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style-type: none;
|
|
|
|
overflow: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-treenode-children {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style-type: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-wrapper {
|
|
|
|
overflow: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-treenode-selectable {
|
|
|
|
cursor: pointer;
|
|
|
|
user-select: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-toggler {
|
|
|
|
cursor: pointer;
|
|
|
|
user-select: none;
|
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
overflow: hidden;
|
|
|
|
position: relative;
|
|
|
|
flex-shrink: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-treenode-leaf > .p-treenode-content .p-tree-toggler {
|
|
|
|
visibility: hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-treenode-content {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-filter {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-filter-container {
|
|
|
|
position: relative;
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-filter-icon {
|
|
|
|
position: absolute;
|
|
|
|
top: 50%;
|
2022-09-14 11:26:01 +00:00
|
|
|
margin-top: -0.5rem;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-loading {
|
|
|
|
position: relative;
|
|
|
|
min-height: 4rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree .p-tree-loading-overlay {
|
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-flex-scrollable {
|
|
|
|
display: flex;
|
|
|
|
flex: 1;
|
|
|
|
height: 100%;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-tree-flex-scrollable .p-tree-wrapper {
|
|
|
|
flex: 1;
|
|
|
|
}
|
|
|
|
</style>
|