2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<Portal :appendTo="appendTo" :disabled="!popup">
|
2023-08-02 12:01:18 +00:00
|
|
|
<transition name="p-connected-overlay" @enter="onEnter" @leave="onLeave" @after-leave="onAfterLeave" v-bind="ptm('transition')">
|
2024-02-11 23:47:53 +00:00
|
|
|
<div v-if="popup ? overlayVisible : true" :ref="containerRef" :id="id" :class="cx('root')" @click="onOverlayClick" v-bind="ptmi('root')">
|
2023-05-29 09:09:48 +00:00
|
|
|
<div v-if="$slots.start" :class="cx('start')" v-bind="ptm('start')">
|
2023-01-27 13:25:28 +00:00
|
|
|
<slot name="start"></slot>
|
|
|
|
</div>
|
2022-12-08 11:04:25 +00:00
|
|
|
<ul
|
|
|
|
:ref="listRef"
|
|
|
|
:id="id + '_list'"
|
2024-04-30 13:02:03 +00:00
|
|
|
:class="cx('list')"
|
2022-12-08 11:04:25 +00:00
|
|
|
role="menu"
|
|
|
|
:tabindex="tabindex"
|
|
|
|
:aria-activedescendant="focused ? focusedOptionId : undefined"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
@focus="onListFocus"
|
|
|
|
@blur="onListBlur"
|
|
|
|
@keydown="onListKeyDown"
|
2024-04-30 13:02:03 +00:00
|
|
|
v-bind="ptm('list')"
|
2022-12-08 11:04:25 +00:00
|
|
|
>
|
2022-09-06 12:03:37 +00:00
|
|
|
<template v-for="(item, i) of model" :key="label(item) + i.toString()">
|
|
|
|
<template v-if="item.items && visible(item) && !item.separator">
|
2024-04-30 13:02:03 +00:00
|
|
|
<li v-if="item.items" :id="id + '_' + i" :class="[cx('submenuItem'), item.class]" role="none" v-bind="ptm('submenuItem')">
|
|
|
|
<!--TODO: submenuheader deprecated since v4.0. Use submenuitem-->
|
2024-05-06 10:52:41 +00:00
|
|
|
<slot :name="$slots.submenuitem ? 'submenuitem' : 'submenuheader'" :item="item">{{ label(item) }}</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</li>
|
2022-12-08 11:04:25 +00:00
|
|
|
<template v-for="(child, j) of item.items" :key="child.label + i + '_' + j">
|
2024-02-15 15:57:40 +00:00
|
|
|
<PVMenuitem
|
|
|
|
v-if="visible(child) && !child.separator"
|
|
|
|
:id="id + '_' + i + '_' + j"
|
|
|
|
:item="child"
|
|
|
|
:templates="$slots"
|
|
|
|
:focusedOptionId="focusedOptionId"
|
2024-04-19 12:28:49 +00:00
|
|
|
:unstyled="unstyled"
|
2024-02-15 15:57:40 +00:00
|
|
|
@item-click="itemClick"
|
|
|
|
@item-mousemove="itemMouseMove"
|
|
|
|
:pt="pt"
|
|
|
|
/>
|
2023-05-30 05:51:28 +00:00
|
|
|
<li v-else-if="visible(child) && child.separator" :key="'separator' + i + j" :class="[cx('separator'), item.class]" :style="child.style" role="separator" v-bind="ptm('separator')"></li>
|
2022-09-06 12:03:37 +00:00
|
|
|
</template>
|
|
|
|
</template>
|
2023-05-30 05:51:28 +00:00
|
|
|
<li v-else-if="visible(item) && item.separator" :key="'separator' + i.toString()" :class="[cx('separator'), item.class]" :style="item.style" role="separator" v-bind="ptm('separator')"></li>
|
2024-04-19 12:28:49 +00:00
|
|
|
<PVMenuitem
|
|
|
|
v-else
|
|
|
|
:key="label(item) + i.toString()"
|
|
|
|
:id="id + '_' + i"
|
|
|
|
:item="item"
|
|
|
|
:index="i"
|
|
|
|
:templates="$slots"
|
|
|
|
:focusedOptionId="focusedOptionId"
|
|
|
|
:unstyled="unstyled"
|
|
|
|
@item-click="itemClick"
|
|
|
|
@item-mousemove="itemMouseMove"
|
|
|
|
:pt="pt"
|
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</template>
|
|
|
|
</ul>
|
2023-05-29 09:09:48 +00:00
|
|
|
<div v-if="$slots.end" :class="cx('end')" v-bind="ptm('end')">
|
2023-01-27 13:25:28 +00:00
|
|
|
<slot name="end"></slot>
|
|
|
|
</div>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</Portal>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import OverlayEventBus from 'primevue/overlayeventbus';
|
|
|
|
import Portal from 'primevue/portal';
|
2022-12-08 11:04:25 +00:00
|
|
|
import { ConnectedOverlayScrollHandler, DomHandler, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
|
2023-05-29 09:09:48 +00:00
|
|
|
import BaseMenu from './BaseMenu.vue';
|
2022-12-08 11:04:25 +00:00
|
|
|
import Menuitem from './Menuitem.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Menu',
|
2023-05-29 09:09:48 +00:00
|
|
|
extends: BaseMenu,
|
2022-09-06 12:03:37 +00:00
|
|
|
inheritAttrs: false,
|
2022-12-08 11:04:25 +00:00
|
|
|
emits: ['show', 'hide', 'focus', 'blur'],
|
2022-09-06 12:03:37 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2023-01-19 04:01:03 +00:00
|
|
|
id: this.$attrs.id,
|
2022-12-08 11:04:25 +00:00
|
|
|
overlayVisible: false,
|
|
|
|
focused: false,
|
|
|
|
focusedOptionIndex: -1,
|
|
|
|
selectedOptionIndex: -1
|
2022-09-06 12:03:37 +00:00
|
|
|
};
|
|
|
|
},
|
2023-01-19 04:01:03 +00:00
|
|
|
watch: {
|
2024-04-16 09:35:02 +00:00
|
|
|
'$attrs.id': function (newValue) {
|
|
|
|
this.id = newValue || UniqueComponentId();
|
2024-03-27 23:27:46 +00:00
|
|
|
}
|
2023-01-19 04:01:03 +00:00
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
target: null,
|
|
|
|
outsideClickListener: null,
|
|
|
|
scrollHandler: null,
|
|
|
|
resizeListener: null,
|
|
|
|
container: null,
|
2022-12-08 11:04:25 +00:00
|
|
|
list: null,
|
2024-01-16 11:28:03 +00:00
|
|
|
mounted() {
|
2024-04-16 09:35:02 +00:00
|
|
|
this.id = this.id || UniqueComponentId();
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
if (!this.popup) {
|
|
|
|
this.bindResizeListener();
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
}
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
beforeUnmount() {
|
|
|
|
this.unbindResizeListener();
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
|
|
|
|
if (this.scrollHandler) {
|
|
|
|
this.scrollHandler.destroy();
|
|
|
|
this.scrollHandler = null;
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.target = null;
|
|
|
|
|
|
|
|
if (this.container && this.autoZIndex) {
|
|
|
|
ZIndexUtils.clear(this.container);
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.container = null;
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
itemClick(event) {
|
|
|
|
const item = event.item;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
if (this.disabled(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.command) {
|
|
|
|
item.command(event);
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
if (this.overlayVisible) this.hide();
|
|
|
|
|
|
|
|
if (!this.popup && this.focusedOptionIndex !== event.id) {
|
|
|
|
this.focusedOptionIndex = event.id;
|
|
|
|
}
|
|
|
|
},
|
2024-02-15 15:57:40 +00:00
|
|
|
itemMouseMove(event) {
|
|
|
|
if (this.focused) {
|
|
|
|
this.focusedOptionIndex = event.id;
|
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onListFocus(event) {
|
|
|
|
this.focused = true;
|
2024-02-15 15:57:40 +00:00
|
|
|
!this.popup && this.changeFocusedOptionIndex(0);
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onListBlur(event) {
|
|
|
|
this.focused = false;
|
|
|
|
this.focusedOptionIndex = -1;
|
|
|
|
this.$emit('blur', event);
|
|
|
|
},
|
|
|
|
onListKeyDown(event) {
|
|
|
|
switch (event.code) {
|
|
|
|
case 'ArrowDown':
|
|
|
|
this.onArrowDownKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowUp':
|
|
|
|
this.onArrowUpKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Home':
|
|
|
|
this.onHomeKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'End':
|
|
|
|
this.onEndKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Enter':
|
2023-12-20 10:45:43 +00:00
|
|
|
case 'NumpadEnter':
|
2022-12-08 11:04:25 +00:00
|
|
|
this.onEnterKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Space':
|
|
|
|
this.onSpaceKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Escape':
|
|
|
|
if (this.popup) {
|
|
|
|
DomHandler.focus(this.target);
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'Tab':
|
|
|
|
this.overlayVisible && this.hide();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onArrowDownKey(event) {
|
|
|
|
const optionIndex = this.findNextOptionIndex(this.focusedOptionIndex);
|
|
|
|
|
|
|
|
this.changeFocusedOptionIndex(optionIndex);
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onArrowUpKey(event) {
|
|
|
|
if (event.altKey && this.popup) {
|
|
|
|
DomHandler.focus(this.target);
|
|
|
|
this.hide();
|
|
|
|
event.preventDefault();
|
|
|
|
} else {
|
|
|
|
const optionIndex = this.findPrevOptionIndex(this.focusedOptionIndex);
|
|
|
|
|
|
|
|
this.changeFocusedOptionIndex(optionIndex);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onHomeKey(event) {
|
|
|
|
this.changeFocusedOptionIndex(0);
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onEndKey(event) {
|
2024-04-30 13:02:03 +00:00
|
|
|
this.changeFocusedOptionIndex(DomHandler.find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]').length - 1);
|
2022-12-08 11:04:25 +00:00
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onEnterKey(event) {
|
|
|
|
const element = DomHandler.findSingle(this.list, `li[id="${`${this.focusedOptionIndex}`}"]`);
|
2024-04-30 13:02:03 +00:00
|
|
|
const anchorElement = element && DomHandler.findSingle(element, 'a[data-pc-section="itemlink"]');
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
this.popup && DomHandler.focus(this.target);
|
|
|
|
anchorElement ? anchorElement.click() : element && element.click();
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onSpaceKey(event) {
|
|
|
|
this.onEnterKey(event);
|
|
|
|
},
|
|
|
|
findNextOptionIndex(index) {
|
2024-04-30 13:02:03 +00:00
|
|
|
const links = DomHandler.find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]');
|
2022-12-08 11:04:25 +00:00
|
|
|
const matchedOptionIndex = [...links].findIndex((link) => link.id === index);
|
|
|
|
|
|
|
|
return matchedOptionIndex > -1 ? matchedOptionIndex + 1 : 0;
|
|
|
|
},
|
|
|
|
findPrevOptionIndex(index) {
|
2024-04-30 13:02:03 +00:00
|
|
|
const links = DomHandler.find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]');
|
2022-12-08 11:04:25 +00:00
|
|
|
const matchedOptionIndex = [...links].findIndex((link) => link.id === index);
|
|
|
|
|
|
|
|
return matchedOptionIndex > -1 ? matchedOptionIndex - 1 : 0;
|
|
|
|
},
|
|
|
|
changeFocusedOptionIndex(index) {
|
2024-04-30 13:02:03 +00:00
|
|
|
const links = DomHandler.find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]');
|
2022-12-08 11:04:25 +00:00
|
|
|
let order = index >= links.length ? links.length - 1 : index < 0 ? 0 : index;
|
|
|
|
|
2023-01-16 10:07:17 +00:00
|
|
|
order > -1 && (this.focusedOptionIndex = links[order].getAttribute('id'));
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
toggle(event) {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.overlayVisible) this.hide();
|
|
|
|
else this.show(event);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
show(event) {
|
|
|
|
this.overlayVisible = true;
|
|
|
|
this.target = event.currentTarget;
|
|
|
|
},
|
|
|
|
hide() {
|
|
|
|
this.overlayVisible = false;
|
|
|
|
this.target = null;
|
|
|
|
},
|
|
|
|
onEnter(el) {
|
2023-05-29 09:09:48 +00:00
|
|
|
DomHandler.addStyles(el, { position: 'absolute', top: '0', left: '0' });
|
2022-09-06 12:03:37 +00:00
|
|
|
this.alignOverlay();
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
this.bindResizeListener();
|
|
|
|
this.bindScrollListener();
|
|
|
|
|
|
|
|
if (this.autoZIndex) {
|
|
|
|
ZIndexUtils.set('menu', el, this.baseZIndex + this.$primevue.config.zIndex.menu);
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
if (this.popup) {
|
|
|
|
DomHandler.focus(this.list);
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.$emit('show');
|
|
|
|
},
|
|
|
|
onLeave() {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
this.unbindResizeListener();
|
|
|
|
this.unbindScrollListener();
|
|
|
|
this.$emit('hide');
|
|
|
|
},
|
|
|
|
onAfterLeave(el) {
|
|
|
|
if (this.autoZIndex) {
|
|
|
|
ZIndexUtils.clear(el);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
alignOverlay() {
|
|
|
|
DomHandler.absolutePosition(this.container, this.target);
|
2023-11-23 07:25:58 +00:00
|
|
|
const targetWidth = DomHandler.getOuterWidth(this.target);
|
|
|
|
|
|
|
|
if (targetWidth > DomHandler.getOuterWidth(this.container)) {
|
|
|
|
this.container.style.minWidth = DomHandler.getOuterWidth(this.target) + 'px';
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
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.target && (this.target === event.target || this.target.contains(event.target)));
|
|
|
|
|
|
|
|
if (this.overlayVisible && isOutsideContainer && isOutsideTarget) {
|
2022-09-06 12:03:37 +00:00
|
|
|
this.hide();
|
2022-12-08 11:04:25 +00:00
|
|
|
} else if (!this.popup && isOutsideContainer && isOutsideTarget) {
|
|
|
|
this.focusedOptionIndex = -1;
|
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.target, () => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visible(item) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return typeof item.visible === 'function' ? item.visible() : item.visible !== false;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
disabled(item) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return typeof item.disabled === 'function' ? item.disabled() : item.disabled;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
label(item) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return typeof item.label === 'function' ? item.label() : item.label;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
onOverlayClick(event) {
|
|
|
|
OverlayEventBus.emit('overlay-click', {
|
|
|
|
originalEvent: event,
|
|
|
|
target: this.target
|
|
|
|
});
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
containerRef(el) {
|
|
|
|
this.container = el;
|
|
|
|
},
|
|
|
|
listRef(el) {
|
|
|
|
this.list = el;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-12-08 11:04:25 +00:00
|
|
|
focusedOptionId() {
|
|
|
|
return this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : null;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
2022-12-08 11:04:25 +00:00
|
|
|
PVMenuitem: Menuitem,
|
2022-09-14 11:26:01 +00:00
|
|
|
Portal: 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>
|