primevue-mirror/components/lib/megamenu/MegaMenuSub.vue

233 lines
10 KiB
Vue
Raw Normal View History

2022-12-08 11:04:25 +00:00
<template>
<ul>
<li v-if="submenu" :class="getSubmenuHeaderClass(submenu)" :style="getItemProp(submenu, 'style')" role="presentation">{{ getItemLabel(submenu) }}</li>
<template v-for="(processedItem, index) of items" :key="getItemKey(processedItem)">
<li
v-if="isItemVisible(processedItem) && !getItemProp(processedItem, 'separator')"
:id="getItemId(processedItem)"
:style="getItemProp(processedItem, 'style')"
:class="getItemClass(processedItem)"
role="menuitem"
:aria-label="getItemLabel(processedItem)"
:aria-disabled="isItemDisabled(processedItem) || undefined"
:aria-expanded="isItemGroup(processedItem) ? isItemActive(processedItem) : undefined"
:aria-haspopup="isItemGroup(processedItem) && !getItemProp(processedItem, 'to') ? 'menu' : undefined"
:aria-level="level + 1"
:aria-setsize="getAriaSetSize()"
:aria-posinset="getAriaPosInset(index)"
>
<div class="p-menuitem-content" @click="onItemClick($event, processedItem)" @mouseenter="onItemMouseEnter($event, processedItem)">
<template v-if="!templates.item">
2022-12-08 11:04:25 +00:00
<router-link v-if="getItemProp(processedItem, 'to') && !isItemDisabled(processedItem)" v-slot="{ navigate, href, isActive, isExactActive }" :to="getItemProp(processedItem, 'to')" custom>
<a v-ripple :href="href" :class="getItemActionClass(processedItem, { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)">
<component :is="templates.itemicon || (getItemProp(processedItem, 'icon') ? 'span' : undefined)" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
2022-12-08 11:04:25 +00:00
<span class="p-menuitem-text">{{ getItemLabel(processedItem) }}</span>
</a>
</router-link>
<a v-else v-ripple :href="getItemProp(processedItem, 'url')" :class="getItemActionClass(processedItem)" :target="getItemProp(processedItem, 'target')" tabindex="-1" aria-hidden="true">
<component :is="templates.itemicon || (getItemProp(processedItem, 'icon') ? 'span' : undefined)" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
2022-12-08 11:04:25 +00:00
<span class="p-menuitem-text">{{ getItemLabel(processedItem) }}</span>
<component v-if="isItemGroup(processedItem)" :is="templates.submenuicon || (horizontal ? 'AngleDownIcon' : 'AngleRightIcon')" :active="isItemActive(processedItem)" class="p-submenu-icon" />
2022-12-08 11:04:25 +00:00
</a>
</template>
<component v-else :is="templates.item" :item="processedItem.item"></component>
2022-12-08 11:04:25 +00:00
</div>
<div v-if="isItemVisible(processedItem) && isItemGroup(processedItem)" class="p-megamenu-panel">
<div class="p-megamenu-grid">
<div v-for="col of processedItem.items" :key="getItemKey(col)" :class="getColumnClass(processedItem)">
<MegaMenuSub
v-for="submenu of col"
:key="getSubListKey(submenu)"
:id="getSubListId(submenu)"
role="menu"
class="p-submenu-list p-megamenu-submenu"
:menuId="menuId"
:focusedItemId="focusedItemId"
:submenu="submenu"
:items="submenu.items"
:templates="templates"
2022-12-08 11:04:25 +00:00
:exact="exact"
:level="level + 1"
@item-click="$emit('item-click', $event)"
@item-mouseenter="$emit('item-mouseenter', $event)"
/>
</div>
</div>
</div>
</li>
<li v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')" :id="getItemId(processedItem)" :style="getItemProp(processedItem, 'style')" :class="getSeparatorItemClass(processedItem)" role="separator"></li>
</template>
</ul>
</template>
<script>
import AngleDownIcon from 'primevue/icon/angledown';
import AngleRightIcon from 'primevue/icon/angleright';
2022-12-08 11:04:25 +00:00
import Ripple from 'primevue/ripple';
import { ObjectUtils } from 'primevue/utils';
export default {
name: 'MegaMenuSub',
emits: ['item-click', 'item-mouseenter'],
props: {
menuId: {
type: String,
default: null
},
focusedItemId: {
type: String,
default: null
},
horizontal: {
type: Boolean,
default: false
},
submenu: {
type: Object,
default: null
},
items: {
type: Array,
default: null
},
level: {
type: Number,
default: 0
},
templates: {
type: Object,
2022-12-08 11:04:25 +00:00
default: null
},
activeItem: {
type: Object,
default: null
},
exact: {
type: Boolean,
default: true
}
},
methods: {
getSubListId(processedItem) {
return `${this.getItemId(processedItem)}_list`;
},
getSubListKey(processedItem) {
return this.getSubListId(processedItem);
},
getItemId(processedItem) {
return `${this.menuId}_${processedItem.key}`;
},
getItemKey(processedItem) {
return this.getItemId(processedItem);
},
2023-01-05 15:12:19 +00:00
getItemProp(processedItem, name, params) {
return processedItem && processedItem.item ? ObjectUtils.getItemValue(processedItem.item[name], params) : undefined;
2022-12-08 11:04:25 +00:00
},
getItemLabel(processedItem) {
return this.getItemProp(processedItem, 'label');
},
isItemActive(processedItem) {
return ObjectUtils.isNotEmpty(this.activeItem) ? this.activeItem.key === processedItem.key : false;
},
isItemVisible(processedItem) {
return this.getItemProp(processedItem, 'visible') !== false;
},
isItemDisabled(processedItem) {
return this.getItemProp(processedItem, 'disabled');
},
isItemFocused(processedItem) {
return this.focusedItemId === this.getItemId(processedItem);
},
isItemGroup(processedItem) {
return ObjectUtils.isNotEmpty(processedItem.items);
},
onItemClick(event, processedItem) {
2023-01-05 15:12:19 +00:00
this.getItemProp(processedItem, 'command', { originalEvent: event, item: processedItem.item });
2022-12-08 11:04:25 +00:00
this.$emit('item-click', { originalEvent: event, processedItem, isFocus: true });
},
onItemMouseEnter(event, processedItem) {
this.$emit('item-mouseenter', { originalEvent: event, processedItem });
},
onItemActionClick(event, navigate) {
navigate && navigate(event);
},
getAriaSetSize() {
return this.items.filter((processedItem) => this.isItemVisible(processedItem) && !this.getItemProp(processedItem, 'separator')).length;
},
getAriaPosInset(index) {
return index - this.items.slice(0, index).filter((processedItem) => this.isItemVisible(processedItem) && this.getItemProp(processedItem, 'separator')).length + 1;
},
getSubmenuHeaderClass(processedItem) {
return [
'p-megamenu-submenu-header p-submenu-header',
this.getItemProp(processedItem, 'class'),
{
'p-disabled': this.isItemDisabled(processedItem)
}
];
},
getColumnClass(processedItem) {
let length = this.isItemGroup(processedItem) ? processedItem.items.length : 0;
let columnClass;
switch (length) {
case 2:
columnClass = 'p-megamenu-col-6';
break;
case 3:
columnClass = 'p-megamenu-col-4';
break;
case 4:
columnClass = 'p-megamenu-col-3';
break;
case 6:
columnClass = 'p-megamenu-col-2';
break;
default:
columnClass = 'p-megamenu-col-12';
break;
}
return columnClass;
},
getItemClass(processedItem) {
return [
'p-menuitem',
this.getItemProp(processedItem, 'class'),
{
'p-menuitem-active p-highlight': this.isItemActive(processedItem),
'p-focus': this.isItemFocused(processedItem),
'p-disabled': this.isItemDisabled(processedItem)
}
];
},
getItemActionClass(processedItem, routerProps) {
return [
'p-menuitem-link',
{
'router-link-active': routerProps && routerProps.isActive,
'router-link-active-exact': this.exact && routerProps && routerProps.isExactActive
}
];
},
getItemIconClass(processedItem) {
return ['p-menuitem-icon', this.getItemProp(processedItem, 'icon')];
},
getSeparatorItemClass(processedItem) {
return ['p-menuitem-separator', this.getItemProp(processedItem, 'class')];
}
},
components: {
AngleRightIcon: AngleRightIcon,
AngleDownIcon: AngleDownIcon
},
2022-12-08 11:04:25 +00:00
directives: {
ripple: Ripple
}
};
</script>