primevue-mirror/components/lib/panelmenu/PanelMenuSub.vue

199 lines
9.2 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-04-26 09:58:36 +00:00
<ul class="p-submenu-list" v-bind="ptm('menu')">
2022-12-08 11:04:25 +00:00
<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="treeitem"
:aria-label="getItemLabel(processedItem)"
:aria-expanded="isItemGroup(processedItem) ? isItemActive(processedItem) : undefined"
:aria-level="level + 1"
:aria-setsize="getAriaSetSize()"
:aria-posinset="getAriaPosInset(index)"
2023-04-27 14:16:09 +00:00
v-bind="getPTOptions(processedItem, 'menuitem')"
2022-12-08 11:04:25 +00:00
>
2023-04-27 14:16:09 +00:00
<div class="p-menuitem-content" @click="onItemClick($event, processedItem)" v-bind="getPTOptions(processedItem, 'content')">
<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>
2023-04-27 14:16:09 +00:00
<a v-ripple :href="href" :class="getItemActionClass(processedItem, { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)" v-bind="getPTOptions(processedItem, 'action')">
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
2023-04-27 14:16:09 +00:00
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" v-bind="getPTOptions(processedItem, 'icon')" />
<span class="p-menuitem-text" v-bind="getPTOptions(processedItem, 'label')">{{ getItemLabel(processedItem) }}</span>
2022-12-08 11:04:25 +00:00
</a>
</router-link>
2023-04-27 14:16:09 +00:00
<a
v-else
v-ripple
:href="getItemProp(processedItem, 'url')"
:class="getItemActionClass(processedItem)"
:target="getItemProp(processedItem, 'target')"
tabindex="-1"
aria-hidden="true"
v-bind="getPTOptions(processedItem, 'action')"
>
<template v-if="isItemGroup(processedItem)">
2023-04-27 14:16:09 +00:00
<component v-if="templates.submenuicon" :is="templates.submenuicon" class="p-submenu-icon" :active="isItemActive(processedItem)" v-bind="getPTOptions(processedItem, 'submenuIcon')" />
<component v-else :is="isItemActive(processedItem) ? 'ChevronDownIcon' : 'ChevronRightIcon'" class="p-submenu-icon" v-bind="getPTOptions(processedItem, 'submenuIcon')" />
</template>
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
2023-04-27 14:16:09 +00:00
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" v-bind="getPTOptions(processedItem, 'icon')" />
<span class="p-menuitem-text" v-bind="getPTOptions(processedItem, 'label')">{{ getItemLabel(processedItem) }}</span>
2022-09-06 12:03:37 +00:00
</a>
2022-12-08 11:04:25 +00:00
</template>
<component v-else :is="templates.item" :item="processedItem.item"></component>
2022-12-08 11:04:25 +00:00
</div>
2022-09-06 12:03:37 +00:00
<transition name="p-toggleable-content">
2023-04-26 09:58:36 +00:00
<div v-show="isItemActive(processedItem)" class="p-toggleable-content" v-bind="ptm('toggleableContent')">
2022-12-08 11:04:25 +00:00
<PanelMenuSub
v-if="isItemVisible(processedItem) && isItemGroup(processedItem)"
:id="getItemId(processedItem) + '_list'"
role="group"
:panelId="panelId"
:focusedItemId="focusedItemId"
:items="processedItem.items"
:level="level + 1"
:templates="templates"
2022-12-08 11:04:25 +00:00
:activeItemPath="activeItemPath"
:exact="exact"
@item-toggle="onItemToggle"
2023-04-26 09:58:36 +00:00
:pt="pt"
2022-12-08 11:04:25 +00:00
/>
2022-09-06 12:03:37 +00:00
</div>
</transition>
</li>
2023-04-26 09:58:36 +00:00
<li v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')" :style="getItemProp(processedItem, 'style')" :class="getSeparatorItemClass(processedItem)" role="separator" v-bind="ptm('separator')"></li>
2022-09-06 12:03:37 +00:00
</template>
</ul>
</template>
<script>
2023-04-26 09:58:36 +00:00
import BaseComponent from 'primevue/basecomponent';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import ChevronRightIcon from 'primevue/icons/chevronright';
2022-12-08 11:04:25 +00:00
import Ripple from 'primevue/ripple';
import { ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'PanelMenuSub',
2023-04-26 09:58:36 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
emits: ['item-toggle'],
props: {
2022-12-08 11:04:25 +00:00
panelId: {
type: String,
default: null
},
focusedItemId: {
type: String,
default: null
},
items: {
type: Array,
2022-09-06 12:03:37 +00:00
default: null
},
2022-12-08 11:04:25 +00:00
level: {
type: Number,
default: 0
},
templates: {
type: Object,
2022-09-06 12:03:37 +00:00
default: null
},
2022-12-08 11:04:25 +00:00
activeItemPath: {
type: Object,
2022-09-06 12:03:37 +00:00
default: null
},
exact: {
type: Boolean,
default: true
}
},
methods: {
2022-12-08 11:04:25 +00:00
getItemId(processedItem) {
return `${this.panelId}_${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');
},
2023-04-27 14:16:09 +00:00
getPTOptions(processedItem, key) {
return this.ptm(key, {
context: {
2023-04-27 14:16:09 +00:00
active: this.isItemActive(processedItem),
focused: this.isItemFocused(processedItem)
}
});
},
2022-12-08 11:04:25 +00:00
isItemActive(processedItem) {
return this.activeItemPath.some((path) => path.key === processedItem.key);
},
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-toggle', { processedItem, expanded: !this.isItemActive(processedItem) });
},
onItemToggle(event) {
this.$emit('item-toggle', event);
},
onItemActionClick(event, navigate) {
navigate && navigate(event);
},
getAriaSetSize() {
return this.items.filter((processedItem) => this.isItemVisible(processedItem) && !this.getItemProp(processedItem, 'separator')).length;
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getAriaPosInset(index) {
return index - this.items.slice(0, index).filter((processedItem) => this.isItemVisible(processedItem) && this.getItemProp(processedItem, 'separator')).length + 1;
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getItemClass(processedItem) {
return [
'p-menuitem',
this.getItemProp(processedItem, 'class'),
{
'p-focus': this.isItemFocused(processedItem),
'p-disabled': this.isItemDisabled(processedItem)
}
];
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getItemActionClass(processedItem, routerProps) {
2022-09-14 11:26:01 +00:00
return [
'p-menuitem-link',
{
'router-link-active': routerProps && routerProps.isActive,
'router-link-active-exact': this.exact && routerProps && routerProps.isExactActive
}
];
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getItemIconClass(processedItem) {
return ['p-menuitem-icon', this.getItemProp(processedItem, 'icon')];
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getSeparatorItemClass(processedItem) {
return ['p-menuitem-separator', this.getItemProp(processedItem, 'class')];
2022-09-06 12:03:37 +00:00
}
2022-12-08 11:04:25 +00:00
},
components: {
ChevronRightIcon: ChevronRightIcon,
ChevronDownIcon: ChevronDownIcon
},
2022-12-08 11:04:25 +00:00
directives: {
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>