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

242 lines
11 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-08-02 12:00:28 +00:00
<transition name="p-contextmenusub" @enter="onEnter" v-bind="ptm('menu.transition')">
2023-05-30 07:02:33 +00:00
<ul v-if="root ? true : visible" ref="container" :tabindex="tabindex" 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="[cx('menuitem', { processedItem }), getItemProp(processedItem, 'class')]"
2022-12-08 11:04:25 +00:00
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)"
2023-07-24 14:23:12 +00:00
v-bind="getPTOptions('menuitem', processedItem, index)"
2023-05-29 11:12:29 +00:00
:data-p-highlight="isItemActive(processedItem)"
:data-p-focused="isItemFocused(processedItem)"
:data-p-disabled="isItemDisabled(processedItem)"
2022-12-08 11:04:25 +00:00
>
2023-07-24 14:23:12 +00:00
<div :class="cx('content')" @click="onItemClick($event, processedItem)" @mouseenter="onItemMouseEnter($event, processedItem)" v-bind="getPTOptions('content', processedItem, index)">
<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-07-24 14:23:12 +00:00
<a v-ripple :href="href" :class="cx('action', { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)" v-bind="getPTOptions('action', processedItem, index)">
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="[cx('icon'), getItemProp(processedItem, 'icon')]" />
2023-07-24 14:23:12 +00:00
<span v-else-if="getItemProp(processedItem, 'icon')" :class="[cx('icon'), getItemProp(processedItem, 'icon')]" v-bind="getPTOptions('icon', processedItem, index)" />
<span :class="cx('label')" v-bind="getPTOptions('label', processedItem, index)">{{ getItemLabel(processedItem) }}</span>
2022-12-08 11:04:25 +00:00
</a>
</router-link>
2023-07-24 14:23:12 +00:00
<a v-else v-ripple :href="getItemProp(processedItem, 'url')" :class="cx('action')" :target="getItemProp(processedItem, 'target')" tabindex="-1" aria-hidden="true" v-bind="getPTOptions('action', processedItem, index)">
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="[cx('icon'), getItemProp(processedItem, 'icon')]" />
2023-07-24 14:23:12 +00:00
<span v-else-if="getItemProp(processedItem, 'icon')" :class="[cx('icon'), getItemProp(processedItem, 'icon')]" v-bind="getPTOptions('icon', processedItem, index)" />
<span :class="cx('label')" v-bind="getPTOptions('label', processedItem, index)">{{ getItemLabel(processedItem) }}</span>
<template v-if="getItemProp(processedItem, 'items')">
<component v-if="templates.submenuicon" :is="templates.submenuicon" :active="isItemActive(processedItem)" :class="cx('submenuIcon')" />
2023-07-24 14:23:12 +00:00
<AngleRightIcon v-else :class="cx('submenuIcon')" v-bind="getPTOptions('submenuicon', processedItem, index)" />
</template>
2022-09-06 12:03:37 +00:00
</a>
2022-12-08 11:04:25 +00:00
</template>
2023-08-30 13:28:00 +00:00
<component v-else :is="templates.item" :item="processedItem.item" :label="getItemLabel(processedItem)" :props="getMenuItemProps(processedItem, index)"></component>
2022-12-08 11:04:25 +00:00
</div>
<ContextMenuSub
v-if="isItemVisible(processedItem) && isItemGroup(processedItem)"
:id="getItemId(processedItem) + '_list'"
role="menu"
:class="cx('submenu')"
2022-12-08 11:04:25 +00:00
:menuId="menuId"
:focusedItemId="focusedItemId"
:items="processedItem.items"
:templates="templates"
2022-12-08 11:04:25 +00:00
:activeItemPath="activeItemPath"
:exact="exact"
:level="level + 1"
:visible="isItemActive(processedItem) && isItemGroup(processedItem)"
2023-08-30 06:27:36 +00:00
:pt="pt"
:unstyled="unstyled"
2022-12-08 11:04:25 +00:00
@item-click="$emit('item-click', $event)"
@item-mouseenter="$emit('item-mouseenter', $event)"
2023-05-29 11:12:29 +00:00
v-bind="ptm('submenu')"
2022-12-08 11:04:25 +00:00
/>
2022-09-06 12:03:37 +00:00
</li>
2023-04-26 09:57:43 +00:00
<li
v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')"
:id="getItemId(processedItem)"
:style="getItemProp(processedItem, 'style')"
:class="[cx('separator'), getItemProp(processedItem, 'class')]"
2023-04-26 09:57:43 +00:00
role="separator"
v-bind="ptm('separator')"
></li>
2022-09-06 12:03:37 +00:00
</template>
</ul>
</transition>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import AngleRightIcon from 'primevue/icons/angleright';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
2023-08-30 13:28:00 +00:00
import { mergeProps } from 'vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'ContextMenuSub',
2023-07-04 06:29:36 +00:00
hostName: 'ContextMenu',
extends: BaseComponent,
2022-12-08 11:04:25 +00:00
emits: ['item-click', 'item-mouseenter'],
2022-09-06 12:03:37 +00:00
props: {
2022-12-08 11:04:25 +00:00
items: {
2022-09-06 12:03:37 +00:00
type: Array,
default: null
},
2022-12-08 11:04:25 +00:00
menuId: {
type: String,
default: null
},
focusedItemId: {
type: String,
default: null
},
2022-09-06 12:03:37 +00:00
root: {
type: Boolean,
default: false
},
2022-12-08 11:04:25 +00:00
visible: {
2022-09-06 12:03:37 +00:00
type: Boolean,
default: false
},
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
},
exact: {
type: Boolean,
default: true
2022-12-08 11:04:25 +00:00
},
activeItemPath: {
type: Object,
default: null
2023-05-30 07:02:33 +00:00
},
tabindex: {
type: Number,
default: 0
2022-09-06 12:03:37 +00:00
}
},
methods: {
2022-12-08 11:04:25 +00:00
getItemId(processedItem) {
return `${this.menuId}_${processedItem.key}`;
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
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-07-24 14:23:12 +00:00
getPTOptions(key, processedItem, index) {
2023-04-27 14:16:09 +00:00
return this.ptm(key, {
context: {
2023-07-24 14:23:12 +00:00
item: processedItem,
2023-04-27 14:16:09 +00:00
active: this.isItemActive(processedItem),
2023-07-24 14:23:12 +00:00
focused: this.isItemFocused(processedItem),
index
2023-04-27 14:16:09 +00:00
}
});
},
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-click', { originalEvent: event, processedItem, isFocus: true });
},
onItemMouseEnter(event, processedItem) {
this.$emit('item-mouseenter', { originalEvent: event, processedItem });
},
onItemActionClick(event, navigate) {
navigate && navigate(event);
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
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;
2022-09-06 12:03:37 +00:00
},
onEnter() {
this.position();
},
position() {
const parentItem = this.$refs.container.parentElement;
2022-09-14 11:26:01 +00:00
const containerOffset = DomHandler.getOffset(this.$refs.container.parentElement);
2022-09-06 12:03:37 +00:00
const viewport = DomHandler.getViewport();
const sublistWidth = this.$refs.container.offsetParent ? this.$refs.container.offsetWidth : DomHandler.getHiddenElementOuterWidth(this.$refs.container);
const itemOuterWidth = DomHandler.getOuterWidth(parentItem.children[0]);
this.$refs.container.style.top = '0px';
2022-09-14 11:26:01 +00:00
if (parseInt(containerOffset.left, 10) + itemOuterWidth + sublistWidth > viewport.width - DomHandler.calculateScrollbarWidth()) {
2022-09-06 12:03:37 +00:00
this.$refs.container.style.left = -1 * sublistWidth + 'px';
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.$refs.container.style.left = itemOuterWidth + 'px';
}
2023-08-30 06:27:36 +00:00
},
getMenuItemProps(processedItem, index) {
return {
action: mergeProps(
{
class: this.cx('action'),
tabindex: -1,
'aria-hidden': true
},
this.getPTOptions('action', processedItem, index)
),
icon: mergeProps(
{
class: [this.cx('icon'), this.getItemProp(processedItem, 'icon')]
},
this.getPTOptions('icon', processedItem, index)
),
label: mergeProps(
{
class: this.cx('label')
},
this.getPTOptions('label', processedItem, index)
),
submenuicon: mergeProps(
{
class: this.cx('submenuIcon')
},
this.getPTOptions('submenuicon', processedItem, index)
)
};
2022-09-06 12:03:37 +00:00
}
},
components: {
AngleRightIcon: AngleRightIcon
},
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>