primevue-mirror/components/lib/treeselect/TreeSelect.vue

516 lines
19 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:46 +00:00
<div ref="container" :class="cx('root')" :style="sx('root')" @click="onClick" v-bind="ptmi('root')">
2024-05-06 15:32:04 +00:00
<div class="p-hidden-accessible" v-bind="ptm('hiddenInputContainer')" :data-p-hidden-accessible="true">
2022-09-14 11:26:01 +00:00
<input
ref="focusInput"
:id="inputId"
type="text"
role="combobox"
:class="inputClass"
:style="inputStyle"
readonly
:disabled="disabled"
:tabindex="!disabled ? tabindex : -1"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
aria-haspopup="tree"
:aria-expanded="overlayVisible"
:aria-controls="listId"
@focus="onFocus($event)"
@blur="onBlur($event)"
@keydown="onKeyDown($event)"
2023-05-07 19:51:06 +00:00
v-bind="{ ...inputProps, ...ptm('hiddenInput') }"
2022-09-14 11:26:01 +00:00
/>
2022-09-06 12:03:37 +00:00
</div>
2023-05-26 11:58:40 +00:00
<div :class="cx('labelContainer')" v-bind="ptm('labelContainer')">
<div :class="cx('label')" v-bind="ptm('label')">
2022-09-06 12:03:37 +00:00
<slot name="value" :value="selectedNodes" :placeholder="placeholder">
<template v-if="display === 'comma'">
2022-09-14 11:26:01 +00:00
{{ label || 'empty' }}
2022-09-06 12:03:37 +00:00
</template>
<template v-else-if="display === 'chip'">
2024-05-15 07:35:36 +00:00
<div v-for="node of selectedNodes" :key="node.key" :class="cx('chipItem')" v-bind="ptm('chipItem')">
<Chip :class="cx('pcChip')" :label="node.label" :unstyled="unstyled" :pt="ptm('pcChip')" />
2022-09-06 12:03:37 +00:00
</div>
2022-09-14 11:26:01 +00:00
<template v-if="emptyValue">{{ placeholder || 'empty' }}</template>
2022-09-06 12:03:37 +00:00
</template>
</slot>
</div>
</div>
<div :class="cx('dropdown')" role="button" aria-haspopup="tree" :aria-expanded="overlayVisible" v-bind="ptm('dropdown')">
<!-- TODO: triggericon is deprecated since v4.0 -->
2024-05-06 10:52:41 +00:00
<slot :name="$slots.dropdownicon ? 'dropdownicon' : 'triggericon'" :class="cx('dropdownIcon')">
<component :is="'ChevronDownIcon'" :class="cx('dropdownIcon')" v-bind="ptm('dropdownIcon')" />
2022-09-06 12:03:37 +00:00
</slot>
</div>
<Portal :appendTo="appendTo">
<transition name="p-connected-overlay" @enter="onOverlayEnter" @after-enter="onOverlayAfterEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave" v-bind="ptm('transition')">
2023-05-26 11:58:40 +00:00
<div v-if="overlayVisible" :ref="overlayRef" @click="onOverlayClick" :class="[cx('panel'), panelClass]" @keydown="onOverlayKeydown" v-bind="{ ...panelProps, ...ptm('panel') }">
<span
ref="firstHiddenFocusableElementOnOverlay"
role="presentation"
aria-hidden="true"
class="p-hidden-accessible p-hidden-focusable"
:tabindex="0"
@focus="onFirstHiddenFocus"
v-bind="ptm('hiddenFirstFocusableEl')"
:data-p-hidden-accessible="true"
:data-p-hidden-focusable="true"
></span>
2022-09-06 12:03:37 +00:00
<slot name="header" :value="modelValue" :options="options"></slot>
2024-05-06 13:10:13 +00:00
<div :class="cx('treeContainer')" :style="{ 'max-height': scrollHeight }" v-bind="ptm('treeContainer')">
2022-09-14 11:26:01 +00:00
<TSTree
2022-12-08 11:04:25 +00:00
ref="tree"
2022-09-14 11:26:01 +00:00
:id="listId"
:value="options"
:selectionMode="selectionMode"
@update:selectionKeys="onSelectionChange"
:selectionKeys="modelValue"
:expandedKeys="expandedKeys"
@update:expandedKeys="onNodeToggle"
:metaKeySelection="metaKeySelection"
@node-expand="$emit('node-expand', $event)"
@node-collapse="$emit('node-collapse', $event)"
@node-select="onNodeSelect"
@node-unselect="onNodeUnselect"
:level="0"
:unstyled="unstyled"
2024-05-06 15:23:01 +00:00
:pt="ptm('pcTree')"
>
2024-05-27 09:24:31 +00:00
<template v-if="$slots.itemtoggleicon" #toggleicon="iconProps">
<slot name="itemtoggleicon" :node="iconProps.node" :expanded="iconProps.expanded" :class="iconProps.class" />
</template>
<!--TODO: itemtogglericon deprecated since v4.0-->
<template v-else-if="$slots.itemtogglericon" #togglericon="iconProps">
<slot name="itemtogglericon" :node="iconProps.node" :expanded="iconProps.expanded" :class="iconProps.class" />
</template>
<template v-if="$slots.itemcheckboxicon" #checkboxicon="iconProps">
<slot name="itemcheckboxicon" :checked="iconProps.checked" :partialChecked="iconProps.partialChecked" :class="iconProps.class" />
</template>
</TSTree>
2023-05-26 11:58:40 +00:00
<div v-if="emptyOptions" :class="cx('emptyMessage')" v-bind="ptm('emptyMessage')">
2022-09-14 11:26:01 +00:00
<slot name="empty">{{ emptyMessageText }}</slot>
2022-09-06 12:03:37 +00:00
</div>
</div>
<slot name="footer" :value="modelValue" :options="options"></slot>
<span
ref="lastHiddenFocusableElementOnOverlay"
role="presentation"
aria-hidden="true"
class="p-hidden-accessible p-hidden-focusable"
:tabindex="0"
@focus="onLastHiddenFocus"
v-bind="ptm('hiddenLastFocusableEl')"
:data-p-hidden-accessible="true"
:data-p-hidden-focusable="true"
></span>
2022-09-06 12:03:37 +00:00
</div>
</transition>
</Portal>
</div>
</template>
<script>
2024-04-08 12:21:07 +00:00
import Chip from 'primevue/chip';
import ChevronDownIcon from 'primevue/icons/chevrondown';
2022-09-06 12:03:37 +00:00
import OverlayEventBus from 'primevue/overlayeventbus';
import Portal from 'primevue/portal';
2022-12-08 11:04:25 +00:00
import Ripple from 'primevue/ripple';
import Tree from 'primevue/tree';
import { ConnectedOverlayScrollHandler, DomHandler, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
2023-05-26 11:58:40 +00:00
import BaseTreeSelect from './BaseTreeSelect.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'TreeSelect',
2023-05-26 11:58:40 +00:00
extends: BaseTreeSelect,
2024-02-11 23:48:46 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'before-show', 'before-hide', 'change', 'show', 'hide', 'node-select', 'node-unselect', 'node-expand', 'node-collapse', 'focus', 'blur'],
2022-09-14 11:26:01 +00:00
data() {
return {
2024-02-02 15:48:06 +00:00
id: this.$attrs.id,
2022-09-14 11:26:01 +00:00
focused: false,
overlayVisible: false,
expandedKeys: {}
};
},
2022-09-06 12:03:37 +00:00
watch: {
2024-04-16 09:35:02 +00:00
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
2024-02-02 15:48:06 +00:00
},
2022-09-06 12:03:37 +00:00
modelValue: {
2022-09-14 11:26:01 +00:00
handler: function () {
2022-09-06 12:03:37 +00:00
if (!this.selfChange) {
this.updateTreeState();
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.selfChange = false;
},
immediate: true
},
options() {
this.updateTreeState();
}
},
outsideClickListener: null,
resizeListener: null,
scrollHandler: null,
overlay: null,
selfChange: false,
selfClick: false,
2022-09-06 12:03:37 +00:00
beforeUnmount() {
this.unbindOutsideClickListener();
this.unbindResizeListener();
if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
if (this.overlay) {
ZIndexUtils.clear(this.overlay);
this.overlay = null;
}
},
mounted() {
2024-04-16 09:35:02 +00:00
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
this.updateTreeState();
},
methods: {
show() {
this.$emit('before-show');
this.overlayVisible = true;
},
hide() {
this.$emit('before-hide');
this.overlayVisible = false;
this.$refs.focusInput.focus();
},
onFocus(event) {
this.focused = true;
this.$emit('focus', event);
},
onBlur(event) {
this.focused = false;
this.$emit('blur', event);
},
onClick(event) {
if (this.disabled) {
return;
}
2023-05-26 11:58:40 +00:00
if (!this.disabled && (!this.overlay || !this.overlay.contains(event.target))) {
2022-09-14 11:26:01 +00:00
if (this.overlayVisible) this.hide();
else this.show();
2022-09-06 12:03:37 +00:00
DomHandler.focus(this.$refs.focusInput);
2022-09-06 12:03:37 +00:00
}
},
onSelectionChange(keys) {
this.selfChange = true;
this.$emit('update:modelValue', keys);
this.$emit('change', keys);
},
onNodeSelect(node) {
this.$emit('node-select', node);
if (this.selectionMode === 'single') {
this.hide();
}
},
onNodeUnselect(node) {
this.$emit('node-unselect', node);
},
onNodeToggle(keys) {
this.expandedKeys = keys;
},
onFirstHiddenFocus(event) {
const focusableEl = event.relatedTarget === this.$refs.focusInput ? DomHandler.getFirstFocusableElement(this.overlay, ':not([data-p-hidden-focusable="true"])') : this.$refs.focusInput;
DomHandler.focus(focusableEl);
},
onLastHiddenFocus(event) {
const focusableEl = event.relatedTarget === this.$refs.focusInput ? DomHandler.getLastFocusableElement(this.overlay, ':not([data-p-hidden-focusable="true"])') : this.$refs.focusInput;
DomHandler.focus(focusableEl);
},
2022-09-06 12:03:37 +00:00
onKeyDown(event) {
2022-09-14 11:26:01 +00:00
switch (event.code) {
2022-09-06 12:03:37 +00:00
case 'ArrowDown':
2022-12-08 11:04:25 +00:00
this.onArrowDownKey(event);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'Space':
case 'Enter':
case 'NumpadEnter':
2022-12-08 11:04:25 +00:00
this.onEnterKey(event);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'Escape':
2022-12-08 11:04:25 +00:00
this.onEscapeKey(event);
break;
2022-09-14 11:26:01 +00:00
case 'Tab':
this.onTabKey(event);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
default:
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
}
},
2022-12-08 11:04:25 +00:00
onArrowDownKey(event) {
if (this.overlayVisible) return;
this.show();
this.$nextTick(() => {
2023-05-26 11:58:40 +00:00
const treeNodeEl = DomHandler.find(this.$refs.tree.$el, '[data-pc-section="treeitem"]');
2022-12-08 11:04:25 +00:00
const focusedElement = [...treeNodeEl].find((item) => item.getAttribute('tabindex') === '0');
DomHandler.focus(focusedElement);
});
event.preventDefault();
},
onEnterKey(event) {
if (this.overlayVisible) {
this.hide();
} else {
this.onArrowDownKey(event);
}
event.preventDefault();
},
onEscapeKey(event) {
if (this.overlayVisible) {
this.hide();
event.preventDefault();
}
},
onTabKey(event, pressedInInputText = false) {
if (!pressedInInputText) {
if (this.overlayVisible && this.hasFocusableElements()) {
DomHandler.focus(this.$refs.firstHiddenFocusableElementOnOverlay);
event.preventDefault();
}
}
},
hasFocusableElements() {
return DomHandler.getFocusableElements(this.overlay, ':not([data-p-hidden-focusable="true"])').length > 0;
},
2022-09-06 12:03:37 +00:00
onOverlayEnter(el) {
ZIndexUtils.set('overlay', el, this.$primevue.config.zIndex.overlay);
2023-05-26 11:58:40 +00:00
DomHandler.addStyles(el, { position: 'absolute', top: '0', left: '0' });
2022-09-06 12:03:37 +00:00
this.alignOverlay();
this.focus();
},
onOverlayAfterEnter() {
2022-09-06 12:03:37 +00:00
this.bindOutsideClickListener();
this.bindScrollListener();
this.bindResizeListener();
this.scrollValueInView();
this.$emit('show');
},
onOverlayLeave() {
this.unbindOutsideClickListener();
this.unbindScrollListener();
this.unbindResizeListener();
this.$emit('hide');
this.overlay = null;
},
onOverlayAfterLeave(el) {
ZIndexUtils.clear(el);
},
focus() {
let focusableElements = DomHandler.getFocusableElements(this.overlay);
if (focusableElements && focusableElements.length > 0) {
focusableElements[0].focus();
}
},
2022-09-06 12:03:37 +00:00
alignOverlay() {
if (this.appendTo === 'self') {
DomHandler.relativePosition(this.overlay, this.$el);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.overlay.style.minWidth = DomHandler.getOuterWidth(this.$el) + 'px';
DomHandler.absolutePosition(this.overlay, this.$el);
}
},
bindOutsideClickListener() {
if (!this.outsideClickListener) {
this.outsideClickListener = (event) => {
if (this.overlayVisible && !this.selfClick && this.isOutsideClicked(event)) {
2022-09-06 12:03:37 +00:00
this.hide();
}
2023-04-17 12:19:26 +00:00
this.selfClick = false;
2022-09-06 12:03:37 +00:00
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
document.addEventListener('click', this.outsideClickListener);
}
},
unbindOutsideClickListener() {
if (this.outsideClickListener) {
document.removeEventListener('click', this.outsideClickListener);
this.outsideClickListener = null;
}
},
bindScrollListener() {
if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.$refs.container, () => {
if (this.overlayVisible) {
this.hide();
}
});
}
this.scrollHandler.bindScrollListener();
},
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
}
},
bindResizeListener() {
if (!this.resizeListener) {
this.resizeListener = () => {
if (this.overlayVisible && !DomHandler.isTouchDevice()) {
this.hide();
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
window.addEventListener('resize', this.resizeListener);
}
},
unbindResizeListener() {
if (this.resizeListener) {
window.removeEventListener('resize', this.resizeListener);
this.resizeListener = null;
}
},
isOutsideClicked(event) {
return !(this.$el.isSameNode(event.target) || this.$el.contains(event.target) || (this.overlay && this.overlay.contains(event.target)));
},
overlayRef(el) {
this.overlay = el;
},
onOverlayClick(event) {
OverlayEventBus.emit('overlay-click', {
originalEvent: event,
target: this.$el
});
this.selfClick = true;
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
onOverlayKeydown(event) {
if (event.code === 'Escape') this.hide();
},
2022-09-06 12:03:37 +00:00
findSelectedNodes(node, keys, selectedNodes) {
if (node) {
if (this.isSelected(node, keys)) {
selectedNodes.push(node);
delete keys[node.key];
}
if (Object.keys(keys).length && node.children) {
for (let childNode of node.children) {
this.findSelectedNodes(childNode, keys, selectedNodes);
}
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
for (let childNode of this.options) {
this.findSelectedNodes(childNode, keys, selectedNodes);
}
}
},
isSelected(node, keys) {
return this.selectionMode === 'checkbox' ? keys[node.key] && keys[node.key].checked : keys[node.key];
},
updateTreeState() {
2022-09-14 11:26:01 +00:00
let keys = { ...this.modelValue };
2022-09-06 12:03:37 +00:00
this.expandedKeys = {};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (keys && this.options) {
this.updateTreeBranchState(null, null, keys);
}
},
updateTreeBranchState(node, path, keys) {
if (node) {
if (this.isSelected(node, keys)) {
this.expandPath(path);
delete keys[node.key];
}
if (Object.keys(keys).length && node.children) {
for (let childNode of node.children) {
path.push(node.key);
this.updateTreeBranchState(childNode, path, keys);
}
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
for (let childNode of this.options) {
this.updateTreeBranchState(childNode, [], keys);
}
}
},
expandPath(path) {
if (path.length > 0) {
for (let key of path) {
this.expandedKeys[key] = true;
}
}
},
scrollValueInView() {
if (this.overlay) {
let selectedItem = DomHandler.findSingle(this.overlay, '[data-p-selected="true"]');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (selectedItem) {
selectedItem.scrollIntoView({ block: 'nearest', inline: 'start' });
}
}
}
},
computed: {
selectedNodes() {
let selectedNodes = [];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.modelValue && this.options) {
2022-09-14 11:26:01 +00:00
let keys = { ...this.modelValue };
2022-09-06 12:03:37 +00:00
this.findSelectedNodes(null, keys, selectedNodes);
}
return selectedNodes;
},
label() {
let value = this.selectedNodes;
2022-09-14 11:26:01 +00:00
return value.length ? value.map((node) => node.label).join(', ') : this.placeholder;
2022-09-06 12:03:37 +00:00
},
emptyMessageText() {
return this.emptyMessage || this.$primevue.config.locale.emptyMessage;
},
emptyValue() {
return !this.modelValue || Object.keys(this.modelValue).length === 0;
},
emptyOptions() {
return !this.options || this.options.length === 0;
},
listId() {
2024-02-02 15:48:06 +00:00
return this.id + '_list';
2022-09-06 12:03:37 +00:00
}
},
components: {
2022-09-14 11:26:01 +00:00
TSTree: Tree,
2024-04-08 12:21:07 +00:00
Chip,
2023-04-04 12:43:10 +00:00
Portal: Portal,
ChevronDownIcon: ChevronDownIcon
2022-09-06 12:03:37 +00:00
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
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>