primevue-mirror/components/menubar/MenubarSub.vue

324 lines
10 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<ul :class="containerClass" :role="root ? 'menubar' : 'menu'">
<template v-for="(item, i) of model" :key="label(item) + i.toString()">
2022-09-14 11:26:01 +00:00
<li v-if="visible(item) && !item.separator" role="none" :class="getItemClass(item)" :style="item.style" @mouseenter="onItemMouseEnter($event, item)">
2022-09-06 12:03:37 +00:00
<template v-if="!template">
2022-09-14 11:26:01 +00:00
<router-link v-if="item.to && !disabled(item)" v-slot="{ navigate, href, isActive, isExactActive }" :to="item.to" custom>
<a v-ripple :href="href" @click="onItemClick($event, item, navigate)" :class="linkClass(item, { isActive, isExactActive })" @keydown="onItemKeyDown($event, item)" role="menuitem">
<span v-if="item.icon" :class="['p-menuitem-icon', item.icon]"></span>
<span class="p-menuitem-text">{{ label(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
</router-link>
2022-09-14 11:26:01 +00:00
<a
v-else
v-ripple
:href="item.url"
:class="linkClass(item)"
:target="item.target"
:aria-haspopup="item.items != null"
:aria-expanded="item === activeItem"
@click="onItemClick($event, item)"
@keydown="onItemKeyDown($event, item)"
role="menuitem"
:tabindex="disabled(item) ? null : '0'"
>
<span v-if="item.icon" :class="['p-menuitem-icon', item.icon]"></span>
<span class="p-menuitem-text">{{ label(item) }}</span>
<span v-if="item.items" :class="getSubmenuIcon()"></span>
2022-09-06 12:03:37 +00:00
</a>
</template>
<component v-else :is="template" :item="item"></component>
2022-09-14 11:26:01 +00:00
<MenubarSub
v-if="visible(item) && item.items"
:key="label(item) + '_sub_'"
:model="item.items"
:mobileActive="mobileActive"
@leaf-click="onLeafClick"
@keydown-item="onChildItemKeyDown"
:parentActive="item === activeItem"
:template="template"
:exact="exact"
/>
2022-09-06 12:03:37 +00:00
</li>
2022-09-14 11:26:01 +00:00
<li v-if="visible(item) && item.separator" :key="'separator' + i.toString()" :class="['p-menu-separator', item.class]" :style="item.style" role="separator"></li>
2022-09-06 12:03:37 +00:00
</template>
</ul>
</template>
<script>
2022-09-14 11:26:01 +00:00
import { DomHandler } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
export default {
name: 'MenubarSub',
emits: ['keydown-item', 'leaf-click'],
props: {
model: {
type: Array,
default: null
},
root: {
type: Boolean,
default: false
},
popup: {
type: Boolean,
default: false
},
parentActive: {
type: Boolean,
default: false
},
mobileActive: {
type: Boolean,
default: false
},
template: {
type: Function,
default: null
},
exact: {
type: Boolean,
default: true
}
},
documentClickListener: null,
2022-09-14 11:26:01 +00:00
data() {
return {
activeItem: null
};
},
2022-09-06 12:03:37 +00:00
watch: {
parentActive(newValue) {
if (!newValue) {
this.activeItem = null;
}
}
},
updated() {
if (this.root && this.activeItem) {
this.bindDocumentClickListener();
}
},
beforeUnmount() {
this.unbindDocumentClickListener();
},
methods: {
onItemMouseEnter(event, item) {
if (this.disabled(item) || this.mobileActive) {
event.preventDefault();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return;
}
if (this.root) {
if (this.activeItem || this.popup) {
this.activeItem = item;
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.activeItem = item;
}
},
onItemClick(event, item, navigate) {
if (this.disabled(item)) {
event.preventDefault();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return;
}
if (item.command) {
item.command({
originalEvent: event,
item: item
});
}
if (item.items) {
2022-09-14 11:26:01 +00:00
if (this.activeItem && item === this.activeItem) this.activeItem = null;
else this.activeItem = item;
2022-09-06 12:03:37 +00:00
}
if (!item.items) {
this.onLeafClick();
}
if (item.to && navigate) {
navigate(event);
}
},
onLeafClick() {
this.activeItem = null;
this.$emit('leaf-click');
},
onItemKeyDown(event, item) {
let listItem = event.currentTarget.parentElement;
2022-09-14 11:26:01 +00:00
switch (event.which) {
2022-09-06 12:03:37 +00:00
//down
case 40:
if (this.root) {
if (item.items) {
this.expandSubmenu(item, listItem);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.navigateToNextItem(listItem);
}
event.preventDefault();
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
//up
case 38:
if (!this.root) {
this.navigateToPrevItem(listItem);
}
event.preventDefault();
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
//right
case 39:
if (this.root) {
var nextItem = this.findNextItem(listItem);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (nextItem) {
nextItem.children[0].focus();
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (item.items) {
this.expandSubmenu(item, listItem);
}
}
event.preventDefault();
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
//left
case 37:
if (this.root) {
this.navigateToPrevItem(listItem);
}
event.preventDefault();
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
}
this.$emit('keydown-item', {
originalEvent: event,
element: listItem
});
},
onChildItemKeyDown(event) {
if (this.root) {
//up
if (event.originalEvent.which === 38 && event.element.previousElementSibling == null) {
this.collapseMenu(event.element);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
//left
if (event.originalEvent.which === 37) {
this.collapseMenu(event.element);
}
}
},
findNextItem(item) {
let nextItem = item.nextElementSibling;
2022-09-14 11:26:01 +00:00
if (nextItem) return DomHandler.hasClass(nextItem, 'p-disabled') || !DomHandler.hasClass(nextItem, 'p-menuitem') ? this.findNextItem(nextItem) : nextItem;
else return null;
2022-09-06 12:03:37 +00:00
},
findPrevItem(item) {
let prevItem = item.previousElementSibling;
2022-09-14 11:26:01 +00:00
if (prevItem) return DomHandler.hasClass(prevItem, 'p-disabled') || !DomHandler.hasClass(prevItem, 'p-menuitem') ? this.findPrevItem(prevItem) : prevItem;
else return null;
2022-09-06 12:03:37 +00:00
},
expandSubmenu(item, listItem) {
this.activeItem = item;
setTimeout(() => {
listItem.children[1].children[0].children[0].focus();
}, 50);
},
collapseMenu(listItem) {
this.activeItem = null;
listItem.parentElement.previousElementSibling.focus();
},
navigateToNextItem(listItem) {
var nextItem = this.findNextItem(listItem);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (nextItem) {
nextItem.children[0].focus();
}
},
navigateToPrevItem(listItem) {
var prevItem = this.findPrevItem(listItem);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (prevItem) {
prevItem.children[0].focus();
}
},
getItemClass(item) {
return [
2022-09-14 11:26:01 +00:00
'p-menuitem',
item.class,
{
2022-09-06 12:03:37 +00:00
'p-menuitem-active': this.activeItem === item
}
2022-09-14 11:26:01 +00:00
];
2022-09-06 12:03:37 +00:00
},
linkClass(item, routerProps) {
2022-09-14 11:26:01 +00:00
return [
'p-menuitem-link',
{
'p-disabled': this.disabled(item),
'router-link-active': routerProps && routerProps.isActive,
'router-link-active-exact': this.exact && routerProps && routerProps.isExactActive
}
];
2022-09-06 12:03:37 +00:00
},
bindDocumentClickListener() {
if (!this.documentClickListener) {
this.documentClickListener = (event) => {
if (this.$el && !this.$el.contains(event.target)) {
this.activeItem = null;
this.unbindDocumentClickListener();
}
};
document.addEventListener('click', this.documentClickListener);
}
},
unbindDocumentClickListener() {
if (this.documentClickListener) {
document.removeEventListener('click', this.documentClickListener);
this.documentClickListener = null;
}
},
getSubmenuIcon() {
2022-09-14 11:26:01 +00:00
return ['p-submenu-icon pi', { 'pi-angle-right': !this.root, 'pi-angle-down': this.root }];
2022-09-06 12:03:37 +00:00
},
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
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return { 'p-submenu-list': !this.root, 'p-menubar-root-list': this.root };
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>