primevue-mirror/components/lib/contextmenu/ContextMenu.vue

600 lines
22 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<Portal :appendTo="appendTo">
2023-08-02 12:00:28 +00:00
<transition name="p-contextmenu" @enter="onEnter" @after-enter="onAfterEnter" @leave="onLeave" @after-leave="onAfterLeave" v-bind="ptm('transition')">
2024-02-12 13:02:20 +00:00
<div v-if="visible" :ref="containerRef" :class="cx('root')" v-bind="ptmi('root')">
2022-12-08 11:04:25 +00:00
<ContextMenuSub
:ref="listRef"
:id="id + '_list'"
2023-05-29 11:12:29 +00:00
:class="cx('menu')"
2022-12-08 11:04:25 +00:00
role="menubar"
:root="true"
:tabindex="tabindex"
aria-orientation="vertical"
:aria-activedescendant="focused ? focusedItemIdx : undefined"
2022-12-08 11:04:25 +00:00
:menuId="id"
:focusedItemId="focused ? focusedItemIdx : undefined"
2022-12-08 11:04:25 +00:00
:items="processedItems"
:templates="$slots"
2022-12-08 11:04:25 +00:00
:activeItemPath="activeItemPath"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
:level="0"
:visible="submenuVisible"
2023-04-26 09:57:43 +00:00
:pt="pt"
2023-08-30 06:27:36 +00:00
:unstyled="unstyled"
2022-12-08 11:04:25 +00:00
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@item-click="onItemClick"
@item-mouseenter="onItemMouseEnter"
@item-mousemove="onItemMouseMove"
2022-12-08 11:04:25 +00:00
/>
2022-09-06 12:03:37 +00:00
</div>
</transition>
</Portal>
</template>
<script>
import Portal from 'primevue/portal';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
2023-05-29 11:12:29 +00:00
import BaseContextMenu from './BaseContextMenu.vue';
2022-12-08 11:04:25 +00:00
import ContextMenuSub from './ContextMenuSub.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'ContextMenu',
2023-05-29 11:12:29 +00:00
extends: BaseContextMenu,
2022-09-06 12:03:37 +00:00
inheritAttrs: false,
2022-12-08 11:04:25 +00:00
emits: ['focus', 'blur', 'show', 'hide'],
2022-09-06 12:03:37 +00:00
target: null,
outsideClickListener: null,
resizeListener: null,
documentContextMenuListener: null,
pageX: null,
pageY: null,
container: null,
2022-12-08 11:04:25 +00:00
list: null,
2022-09-06 12:03:37 +00:00
data() {
return {
id: this.$attrs.id,
2022-12-08 11:04:25 +00:00
focused: false,
focusedItemInfo: { index: -1, level: 0, parentKey: '' },
activeItemPath: [],
visible: false,
submenuVisible: false
2022-09-06 12:03:37 +00:00
};
},
2022-12-08 11:04:25 +00:00
watch: {
'$attrs.id': {
immediate: true,
handler: function (newValue) {
this.id = newValue || UniqueComponentId();
2024-03-27 23:27:46 +00:00
}
},
2022-12-08 11:04:25 +00:00
activeItemPath(newPath) {
if (ObjectUtils.isNotEmpty(newPath)) {
this.bindOutsideClickListener();
this.bindResizeListener();
} else if (!this.visible) {
this.unbindOutsideClickListener();
this.unbindResizeListener();
}
}
},
mounted() {
2023-08-30 06:27:36 +00:00
if (this.global) {
this.bindDocumentContextMenuListener();
}
},
2022-09-06 12:03:37 +00:00
beforeUnmount() {
this.unbindResizeListener();
this.unbindOutsideClickListener();
this.unbindDocumentContextMenuListener();
if (this.container && this.autoZIndex) {
ZIndexUtils.clear(this.container);
}
2022-09-14 11:26:01 +00:00
2022-12-08 11:04:25 +00:00
this.target = null;
2022-09-06 12:03:37 +00:00
this.container = null;
},
methods: {
2022-12-08 11:04:25 +00:00
getItemProp(item, name) {
return item ? ObjectUtils.getItemValue(item[name]) : undefined;
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getItemLabel(item) {
return this.getItemProp(item, 'label');
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
isItemDisabled(item) {
return this.getItemProp(item, 'disabled');
},
isItemVisible(item) {
return this.getItemProp(item, 'visible') !== false;
},
2022-12-08 11:04:25 +00:00
isItemGroup(item) {
return ObjectUtils.isNotEmpty(this.getItemProp(item, 'items'));
},
isItemSeparator(item) {
return this.getItemProp(item, 'separator');
},
getProccessedItemLabel(processedItem) {
return processedItem ? this.getItemLabel(processedItem.item) : undefined;
},
isProccessedItemGroup(processedItem) {
return processedItem && ObjectUtils.isNotEmpty(processedItem.items);
},
toggle(event) {
this.visible ? this.hide() : this.show(event);
2022-09-06 12:03:37 +00:00
},
show(event) {
2022-12-08 11:04:25 +00:00
this.activeItemPath = [];
this.focusedItemInfo = { index: -1, level: 0, parentKey: '' };
DomHandler.focus(this.list);
2022-09-06 12:03:37 +00:00
this.pageX = event.pageX;
this.pageY = event.pageY;
2022-12-08 11:04:25 +00:00
this.visible ? this.position() : (this.visible = true);
2022-09-06 12:03:37 +00:00
event.stopPropagation();
event.preventDefault();
},
hide() {
this.visible = false;
2022-12-08 11:04:25 +00:00
this.activeItemPath = [];
this.focusedItemInfo = { index: -1, level: 0, parentKey: '' };
},
onFocus(event) {
this.focused = true;
this.focusedItemInfo = this.focusedItemInfo.index !== -1 ? this.focusedItemInfo : { index: -1, level: 0, parentKey: '' };
this.$emit('focus', event);
},
onBlur(event) {
this.focused = false;
this.focusedItemInfo = { index: -1, level: 0, parentKey: '' };
this.searchValue = '';
this.$emit('blur', event);
},
onKeyDown(event) {
const metaKey = event.metaKey || event.ctrlKey;
switch (event.code) {
case 'ArrowDown':
this.onArrowDownKey(event);
break;
case 'ArrowUp':
this.onArrowUpKey(event);
break;
case 'ArrowLeft':
this.onArrowLeftKey(event);
break;
case 'ArrowRight':
this.onArrowRightKey(event);
break;
case 'Home':
this.onHomeKey(event);
break;
case 'End':
this.onEndKey(event);
break;
case 'Space':
this.onSpaceKey(event);
break;
case 'Enter':
case 'NumpadEnter':
2022-12-08 11:04:25 +00:00
this.onEnterKey(event);
break;
case 'Escape':
this.onEscapeKey(event);
break;
case 'Tab':
this.onTabKey(event);
break;
case 'PageDown':
case 'PageUp':
case 'Backspace':
case 'ShiftLeft':
case 'ShiftRight':
//NOOP
break;
default:
if (!metaKey && ObjectUtils.isPrintableCharacter(event.key)) {
this.searchItems(event, event.key);
}
break;
}
},
onItemChange(event) {
const { processedItem, isFocus } = event;
if (ObjectUtils.isEmpty(processedItem)) return;
const { index, key, level, parentKey, items } = processedItem;
const grouped = ObjectUtils.isNotEmpty(items);
const activeItemPath = this.activeItemPath.filter((p) => p.parentKey !== parentKey && p.parentKey !== key);
if (grouped) {
activeItemPath.push(processedItem);
this.submenuVisible = true;
}
this.focusedItemInfo = { index, level, parentKey };
this.activeItemPath = activeItemPath;
isFocus && DomHandler.focus(this.list);
},
onItemClick(event) {
const { processedItem } = event;
const grouped = this.isProccessedItemGroup(processedItem);
const selected = this.isSelected(processedItem);
if (selected) {
const { index, key, level, parentKey } = processedItem;
this.activeItemPath = this.activeItemPath.filter((p) => key !== p.key && key.startsWith(p.key));
this.focusedItemInfo = { index, level, parentKey };
DomHandler.focus(this.list);
} else {
grouped ? this.onItemChange(event) : this.hide();
}
},
onItemMouseEnter(event) {
this.onItemChange(event);
},
onItemMouseMove(event) {
if (this.focused) {
this.changeFocusedItemIndex(event, event.processedItem.index);
}
},
2022-12-08 11:04:25 +00:00
onArrowDownKey(event) {
const itemIndex = this.focusedItemInfo.index !== -1 ? this.findNextItemIndex(this.focusedItemInfo.index) : this.findFirstFocusedItemIndex();
this.changeFocusedItemIndex(event, itemIndex);
event.preventDefault();
},
onArrowUpKey(event) {
if (event.altKey) {
if (this.focusedItemInfo.index !== -1) {
const processedItem = this.visibleItems[this.focusedItemInfo.index];
const grouped = this.isProccessedItemGroup(processedItem);
!grouped && this.onItemChange({ originalEvent: event, processedItem });
}
this.popup && this.hide();
event.preventDefault();
} else {
const itemIndex = this.focusedItemInfo.index !== -1 ? this.findPrevItemIndex(this.focusedItemInfo.index) : this.findLastFocusedItemIndex();
this.changeFocusedItemIndex(event, itemIndex);
event.preventDefault();
}
},
onArrowLeftKey(event) {
const processedItem = this.visibleItems[this.focusedItemInfo.index];
const parentItem = this.activeItemPath.find((p) => p.key === processedItem.parentKey);
const root = ObjectUtils.isEmpty(processedItem.parent);
if (!root) {
this.focusedItemInfo = { index: -1, parentKey: parentItem ? parentItem.parentKey : '' };
this.searchValue = '';
this.onArrowDownKey(event);
}
this.activeItemPath = this.activeItemPath.filter((p) => p.parentKey !== this.focusedItemInfo.parentKey);
event.preventDefault();
},
onArrowRightKey(event) {
const processedItem = this.visibleItems[this.focusedItemInfo.index];
const grouped = this.isProccessedItemGroup(processedItem);
if (grouped) {
this.onItemChange({ originalEvent: event, processedItem });
this.focusedItemInfo = { index: -1, parentKey: processedItem.key };
this.searchValue = '';
this.onArrowDownKey(event);
}
event.preventDefault();
},
onHomeKey(event) {
this.changeFocusedItemIndex(event, this.findFirstItemIndex());
event.preventDefault();
},
onEndKey(event) {
this.changeFocusedItemIndex(event, this.findLastItemIndex());
event.preventDefault();
},
onEnterKey(event) {
if (this.focusedItemInfo.index !== -1) {
const element = DomHandler.findSingle(this.list, `li[id="${`${this.focusedItemIdx}`}"]`);
2023-05-29 11:12:29 +00:00
const anchorElement = element && DomHandler.findSingle(element, 'a[data-pc-section="action"]');
2022-12-08 11:04:25 +00:00
anchorElement ? anchorElement.click() : element && element.click();
const processedItem = this.visibleItems[this.focusedItemInfo.index];
const grouped = this.isProccessedItemGroup(processedItem);
!grouped && (this.focusedItemInfo.index = this.findFirstFocusedItemIndex());
}
event.preventDefault();
},
onSpaceKey(event) {
this.onEnterKey(event);
},
onEscapeKey(event) {
this.hide();
!this.popup && (this.focusedItemInfo.index = this.findFirstFocusedItemIndex());
event.preventDefault();
},
onTabKey(event) {
if (this.focusedItemInfo.index !== -1) {
const processedItem = this.visibleItems[this.focusedItemInfo.index];
const grouped = this.isProccessedItemGroup(processedItem);
!grouped && this.onItemChange({ originalEvent: event, processedItem });
}
this.hide();
2022-09-06 12:03:37 +00:00
},
onEnter(el) {
2023-05-29 11:12:29 +00:00
DomHandler.addStyles(el, { position: 'absolute' });
2022-09-06 12:03:37 +00:00
this.position();
if (this.autoZIndex) {
ZIndexUtils.set('menu', el, this.baseZIndex + this.$primevue.config.zIndex.menu);
}
},
2022-12-08 11:04:25 +00:00
onAfterEnter() {
this.bindOutsideClickListener();
this.bindResizeListener();
this.$emit('show');
DomHandler.focus(this.list);
},
2022-09-06 12:03:37 +00:00
onLeave() {
2022-12-08 11:04:25 +00:00
this.$emit('hide');
this.container = null;
2022-09-06 12:03:37 +00:00
},
onAfterLeave(el) {
if (this.autoZIndex) {
ZIndexUtils.clear(el);
}
2022-12-08 11:04:25 +00:00
this.unbindOutsideClickListener();
this.unbindResizeListener();
2022-09-06 12:03:37 +00:00
},
position() {
let left = this.pageX + 1;
let top = this.pageY + 1;
let width = this.container.offsetParent ? this.container.offsetWidth : DomHandler.getHiddenElementOuterWidth(this.container);
let height = this.container.offsetParent ? this.container.offsetHeight : DomHandler.getHiddenElementOuterHeight(this.container);
let viewport = DomHandler.getViewport();
//flip
if (left + width - document.body.scrollLeft > viewport.width) {
left -= width;
}
//flip
if (top + height - document.body.scrollTop > viewport.height) {
top -= height;
}
//fit
if (left < document.body.scrollLeft) {
left = document.body.scrollLeft;
}
//fit
if (top < document.body.scrollTop) {
top = document.body.scrollTop;
}
this.container.style.left = left + 'px';
this.container.style.top = top + 'px';
},
bindOutsideClickListener() {
if (!this.outsideClickListener) {
this.outsideClickListener = (event) => {
2022-12-08 11:04:25 +00:00
const isOutsideContainer = this.container && !this.container.contains(event.target);
const isOutsideTarget = this.visible ? !(this.target && (this.target === event.target || this.target.contains(event.target))) : true;
if (isOutsideContainer && isOutsideTarget) {
2022-09-06 12:03:37 +00:00
this.hide();
}
};
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;
}
},
bindResizeListener() {
if (!this.resizeListener) {
this.resizeListener = () => {
if (this.visible && !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;
}
},
bindDocumentContextMenuListener() {
if (!this.documentContextMenuListener) {
this.documentContextMenuListener = (event) => {
event.button === 2 && this.show(event);
2022-09-06 12:03:37 +00:00
};
document.addEventListener('contextmenu', this.documentContextMenuListener);
}
},
unbindDocumentContextMenuListener() {
2022-09-14 11:26:01 +00:00
if (this.documentContextMenuListener) {
2022-09-06 12:03:37 +00:00
document.removeEventListener('contextmenu', this.documentContextMenuListener);
this.documentContextMenuListener = null;
}
},
2022-12-08 11:04:25 +00:00
isItemMatched(processedItem) {
return this.isValidItem(processedItem) && this.getProccessedItemLabel(processedItem)?.toLocaleLowerCase().startsWith(this.searchValue.toLocaleLowerCase());
2022-12-08 11:04:25 +00:00
},
isValidItem(processedItem) {
return !!processedItem && !this.isItemDisabled(processedItem.item) && !this.isItemSeparator(processedItem.item) && this.isItemVisible(processedItem.item);
2022-12-08 11:04:25 +00:00
},
isValidSelectedItem(processedItem) {
return this.isValidItem(processedItem) && this.isSelected(processedItem);
},
isSelected(processedItem) {
return this.activeItemPath.some((p) => p.key === processedItem.key);
},
findFirstItemIndex() {
return this.visibleItems.findIndex((processedItem) => this.isValidItem(processedItem));
},
findLastItemIndex() {
return ObjectUtils.findLastIndex(this.visibleItems, (processedItem) => this.isValidItem(processedItem));
},
findNextItemIndex(index) {
const matchedItemIndex = index < this.visibleItems.length - 1 ? this.visibleItems.slice(index + 1).findIndex((processedItem) => this.isValidItem(processedItem)) : -1;
return matchedItemIndex > -1 ? matchedItemIndex + index + 1 : index;
},
findPrevItemIndex(index) {
const matchedItemIndex = index > 0 ? ObjectUtils.findLastIndex(this.visibleItems.slice(0, index), (processedItem) => this.isValidItem(processedItem)) : -1;
return matchedItemIndex > -1 ? matchedItemIndex : index;
},
findSelectedItemIndex() {
return this.visibleItems.findIndex((processedItem) => this.isValidSelectedItem(processedItem));
},
findFirstFocusedItemIndex() {
const selectedIndex = this.findSelectedItemIndex();
return selectedIndex < 0 ? this.findFirstItemIndex() : selectedIndex;
},
findLastFocusedItemIndex() {
const selectedIndex = this.findSelectedItemIndex();
return selectedIndex < 0 ? this.findLastItemIndex() : selectedIndex;
},
searchItems(event, char) {
this.searchValue = (this.searchValue || '') + char;
let itemIndex = -1;
let matched = false;
if (this.focusedItemInfo.index !== -1) {
itemIndex = this.visibleItems.slice(this.focusedItemInfo.index).findIndex((processedItem) => this.isItemMatched(processedItem));
itemIndex = itemIndex === -1 ? this.visibleItems.slice(0, this.focusedItemInfo.index).findIndex((processedItem) => this.isItemMatched(processedItem)) : itemIndex + this.focusedItemInfo.index;
} else {
itemIndex = this.visibleItems.findIndex((processedItem) => this.isItemMatched(processedItem));
}
if (itemIndex !== -1) {
matched = true;
}
if (itemIndex === -1 && this.focusedItemInfo.index === -1) {
itemIndex = this.findFirstFocusedItemIndex();
}
if (itemIndex !== -1) {
this.changeFocusedItemIndex(event, itemIndex);
}
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
this.searchTimeout = setTimeout(() => {
this.searchValue = '';
this.searchTimeout = null;
}, 500);
return matched;
},
changeFocusedItemIndex(event, index) {
if (this.focusedItemInfo.index !== index) {
this.focusedItemInfo.index = index;
this.scrollInView();
}
},
scrollInView(index = -1) {
const id = index !== -1 ? `${this.id}_${index}` : this.focusedItemIdx;
2022-12-08 11:04:25 +00:00
const element = DomHandler.findSingle(this.list, `li[id="${id}"]`);
if (element) {
element.scrollIntoView && element.scrollIntoView({ block: 'nearest', inline: 'start' });
}
},
createProcessedItems(items, level = 0, parent = {}, parentKey = '') {
const processedItems = [];
items &&
items.forEach((item, index) => {
const key = (parentKey !== '' ? parentKey + '_' : '') + index;
const newItem = {
item,
index,
level,
key,
parent,
parentKey
};
newItem['items'] = this.createProcessedItems(item.items, level + 1, newItem, key);
processedItems.push(newItem);
});
return processedItems;
},
2022-09-06 12:03:37 +00:00
containerRef(el) {
this.container = el;
2022-12-08 11:04:25 +00:00
},
listRef(el) {
this.list = el ? el.$el : undefined;
2022-09-06 12:03:37 +00:00
}
},
computed: {
2022-12-08 11:04:25 +00:00
processedItems() {
return this.createProcessedItems(this.model || []);
},
visibleItems() {
const processedItem = this.activeItemPath.find((p) => p.key === this.focusedItemInfo.parentKey);
return processedItem ? processedItem.items : this.processedItems;
},
focusedItemIdx() {
2022-12-08 11:04:25 +00:00
return this.focusedItemInfo.index !== -1 ? `${this.id}${ObjectUtils.isNotEmpty(this.focusedItemInfo.parentKey) ? '_' + this.focusedItemInfo.parentKey : ''}_${this.focusedItemInfo.index}` : null;
2022-09-06 12:03:37 +00:00
}
},
components: {
ContextMenuSub,
Portal
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>