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

306 lines
12 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2022-12-08 11:04:25 +00:00
<div :id="id" class="p-panelmenu p-component">
2023-01-03 09:14:46 +00:00
<template v-for="(item, index) of model" :key="getPanelKey(index)">
2022-12-08 11:04:25 +00:00
<div v-if="isItemVisible(item)" :style="getItemProp(item, 'style')" :class="getPanelClass(item)">
<div
:id="getHeaderId(index)"
:class="getHeaderClass(item)"
:tabindex="isItemDisabled(item) ? -1 : tabindex"
role="button"
:aria-label="getItemLabel(item)"
:aria-expanded="isItemActive(item)"
:aria-controls="getContentId(index)"
:aria-disabled="isItemDisabled(item)"
@click="onHeaderClick($event, item)"
@keydown="onHeaderKeyDown($event, item)"
>
<div class="p-panelmenu-header-content">
<template v-if="!$slots.item">
<router-link v-if="getItemProp(item, 'to') && !isItemDisabled(item)" v-slot="{ navigate, href, isActive, isExactActive }" :to="getItemProp(item, 'to')" custom>
<a :href="href" :class="getHeaderActionClass(item, { isActive, isExactActive })" :tabindex="-1" @click="onHeaderActionClick($event, navigate)">
<component v-if="$slots.headericon" :is="$slots.headericon" :item="item" :class="getHeaderIconClass(item)" />
<span v-else-if="getItemProp(item, 'icon')" :class="getHeaderIconClass(item)" />
2022-12-08 11:04:25 +00:00
<span class="p-menuitem-text">{{ getItemLabel(item) }}</span>
</a>
</router-link>
<a v-else :href="getItemProp(item, 'url')" :class="getHeaderActionClass(item)" :tabindex="-1">
<slot v-if="getItemProp(item, 'items')" name="submenuicon" :active="isItemActive(item)">
<component :is="isItemActive(item) ? 'ChevronDownIcon' : 'ChevronRightIcon'" class="p-submenu-icon" />
</slot>
<component v-if="$slots.headericon" :is="$slots.headericon" :item="item" :class="getHeaderIconClass(item)" />
<span v-else-if="getItemProp(item, 'icon')" :class="getHeaderIconClass(item)" />
2022-12-08 11:04:25 +00:00
<span class="p-menuitem-text">{{ getItemLabel(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
2022-12-08 11:04:25 +00:00
</template>
<component v-else :is="$slots.item" :item="item"></component>
</div>
2022-09-06 12:03:37 +00:00
</div>
<transition name="p-toggleable-content">
2022-12-08 11:04:25 +00:00
<div v-show="isItemActive(item)" :id="getContentId(index)" class="p-toggleable-content" role="region" :aria-labelledby="getHeaderId(index)">
<div v-if="getItemProp(item, 'items')" class="p-panelmenu-content">
<PanelMenuList :panelId="getPanelId(index)" :items="getItemProp(item, 'items')" :templates="$slots" :expandedKeys="expandedKeys" @item-toggle="changeExpandedKeys" @header-focus="updateFocusedHeader" :exact="exact" />
2022-09-06 12:03:37 +00:00
</div>
</div>
</transition>
</div>
</template>
</div>
</template>
<script>
import ChevronDownIcon from 'primevue/icons/chevrondown';
import ChevronRightIcon from 'primevue/icons/chevronright';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils, UniqueComponentId } from 'primevue/utils';
import PanelMenuList from './PanelMenuList.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'PanelMenu',
2022-12-08 11:04:25 +00:00
emits: ['update:expandedKeys', 'panel-open', 'panel-close'],
2022-09-06 12:03:37 +00:00
props: {
2022-09-14 11:26:01 +00:00
model: {
2022-09-06 12:03:37 +00:00
type: Array,
default: null
},
expandedKeys: {
2022-12-08 11:04:25 +00:00
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
},
tabindex: {
type: Number,
default: 0
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
id: this.$attrs.id,
2023-01-09 18:52:00 +00:00
activeItem: null
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
}
},
mounted() {
this.id = this.id || UniqueComponentId();
},
2022-09-06 12:03:37 +00:00
methods: {
2022-12-08 11:04:25 +00:00
getItemProp(item, name) {
return item ? ObjectUtils.getItemValue(item[name]) : undefined;
},
getItemLabel(item) {
return this.getItemProp(item, 'label');
},
isItemActive(item) {
2023-01-09 18:31:18 +00:00
return this.expandedKeys ? this.expandedKeys[this.getItemProp(item, 'key')] : ObjectUtils.equals(item, this.activeItem);
2022-12-08 11:04:25 +00:00
},
isItemVisible(item) {
return this.getItemProp(item, 'visible') !== false;
},
isItemDisabled(item) {
return this.getItemProp(item, 'disabled');
},
getPanelId(index) {
return `${this.id}_${index}`;
},
getPanelKey(index) {
return this.getPanelId(index);
},
getHeaderId(index) {
return `${this.getPanelId(index)}_header`;
},
getContentId(index) {
return `${this.getPanelId(index)}_content`;
},
onHeaderClick(event, item) {
if (this.isItemDisabled(item)) {
2022-09-06 12:03:37 +00:00
event.preventDefault();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return;
}
if (item.command) {
2022-12-08 11:04:25 +00:00
item.command({ originalEvent: event, item });
2022-09-06 12:03:37 +00:00
}
2022-12-08 11:04:25 +00:00
this.changeActiveItem(event, item);
DomHandler.focus(event.currentTarget);
},
onHeaderKeyDown(event, item) {
switch (event.code) {
case 'ArrowDown':
this.onHeaderArrowDownKey(event);
break;
case 'ArrowUp':
this.onHeaderArrowUpKey(event);
break;
case 'Home':
this.onHeaderHomeKey(event);
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'End':
this.onHeaderEndKey(event);
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Enter':
case 'Space':
this.onHeaderEnterKey(event, item);
break;
default:
break;
2022-09-06 12:03:37 +00:00
}
},
2022-12-08 11:04:25 +00:00
onHeaderArrowDownKey(event) {
const rootList = DomHandler.hasClass(event.currentTarget, 'p-highlight') ? DomHandler.findSingle(event.currentTarget.nextElementSibling, '.p-panelmenu-root-list') : null;
rootList ? DomHandler.focus(rootList) : this.updateFocusedHeader({ originalEvent: event, focusOnNext: true });
event.preventDefault();
},
onHeaderArrowUpKey(event) {
const prevHeader = this.findPrevHeader(event.currentTarget.parentElement) || this.findLastHeader();
const rootList = DomHandler.hasClass(prevHeader, 'p-highlight') ? DomHandler.findSingle(prevHeader.nextElementSibling, '.p-panelmenu-root-list') : null;
rootList ? DomHandler.focus(rootList) : this.updateFocusedHeader({ originalEvent: event, focusOnNext: false });
event.preventDefault();
},
onHeaderHomeKey(event) {
this.changeFocusedHeader(event, this.findFirstHeader());
event.preventDefault();
},
onHeaderEndKey(event) {
this.changeFocusedHeader(event, this.findLastHeader());
event.preventDefault();
},
onHeaderEnterKey(event, item) {
const headerAction = DomHandler.findSingle(event.currentTarget, '.p-panelmenu-header-action');
headerAction ? headerAction.click() : this.onHeaderClick(event, item);
event.preventDefault();
},
onHeaderActionClick(event, navigate) {
navigate && navigate(event);
},
findNextHeader(panelElement, selfCheck = false) {
const nextPanelElement = selfCheck ? panelElement : panelElement.nextElementSibling;
const headerElement = DomHandler.findSingle(nextPanelElement, '.p-panelmenu-header');
return headerElement ? (DomHandler.hasClass(headerElement, 'p-disabled') ? this.findNextHeader(headerElement.parentElement) : headerElement) : null;
},
findPrevHeader(panelElement, selfCheck = false) {
const prevPanelElement = selfCheck ? panelElement : panelElement.previousElementSibling;
const headerElement = DomHandler.findSingle(prevPanelElement, '.p-panelmenu-header');
return headerElement ? (DomHandler.hasClass(headerElement, 'p-disabled') ? this.findPrevHeader(headerElement.parentElement) : headerElement) : null;
},
findFirstHeader() {
return this.findNextHeader(this.$el.firstElementChild, true);
},
findLastHeader() {
return this.findPrevHeader(this.$el.lastElementChild, true);
},
updateFocusedHeader(event) {
const { originalEvent, focusOnNext, selfCheck } = event;
const panelElement = originalEvent.currentTarget.closest('.p-panelmenu-panel');
const header = selfCheck ? DomHandler.findSingle(panelElement, '.p-panelmenu-header') : focusOnNext ? this.findNextHeader(panelElement) : this.findPrevHeader(panelElement);
header ? this.changeFocusedHeader(originalEvent, header) : focusOnNext ? this.onHeaderHomeKey(originalEvent) : this.onHeaderEndKey(originalEvent);
},
changeActiveItem(event, item, selfActive = false) {
if (!this.isItemDisabled(item)) {
const active = this.isItemActive(item);
const eventName = !active ? 'panel-open' : 'panel-close';
2022-12-08 11:04:25 +00:00
2023-01-09 18:36:40 +00:00
this.activeItem = selfActive ? item : this.activeItem && ObjectUtils.equals(item, this.activeItem) ? null : item;
2022-12-08 11:04:25 +00:00
this.changeExpandedKeys({ item, expanded: !active });
this.$emit(eventName, { originalEvent: event, item });
2022-09-06 12:03:37 +00:00
}
},
2022-12-08 11:04:25 +00:00
changeExpandedKeys({ item, expanded = false }) {
2022-09-06 12:03:37 +00:00
if (this.expandedKeys) {
2022-09-14 11:26:01 +00:00
let _keys = { ...this.expandedKeys };
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
if (expanded) _keys[item.key] = true;
2022-09-14 11:26:01 +00:00
else delete _keys[item.key];
2022-09-06 12:03:37 +00:00
this.$emit('update:expandedKeys', _keys);
}
},
2022-12-08 11:04:25 +00:00
changeFocusedHeader(event, element) {
element && DomHandler.focus(element);
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getPanelClass(item) {
return ['p-panelmenu-panel', this.getItemProp(item, 'class')];
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getHeaderClass(item) {
return [
'p-panelmenu-header',
this.getItemProp(item, 'headerClass'),
{
'p-highlight': this.isItemActive(item),
'p-disabled': this.isItemDisabled(item)
}
];
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
getHeaderActionClass(item, routerProps) {
2022-09-14 11:26:01 +00:00
return [
2022-12-08 11:04:25 +00:00
'p-panelmenu-header-action',
2022-09-14 11:26:01 +00:00
{
'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
getHeaderIconClass(item) {
return ['p-menuitem-icon', this.getItemProp(item, 'icon')];
2022-09-06 12:03:37 +00:00
}
},
2022-09-14 11:26:01 +00:00
components: {
PanelMenuList: PanelMenuList,
ChevronRightIcon: ChevronRightIcon,
ChevronDownIcon: ChevronDownIcon
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>
<style>
2022-12-08 11:04:25 +00:00
.p-panelmenu .p-panelmenu-header-action {
2022-09-06 12:03:37 +00:00
display: flex;
align-items: center;
user-select: none;
cursor: pointer;
position: relative;
text-decoration: none;
}
2022-12-08 11:04:25 +00:00
.p-panelmenu .p-panelmenu-header-action:focus {
2022-09-06 12:03:37 +00:00
z-index: 1;
}
.p-panelmenu .p-submenu-list {
margin: 0;
padding: 0;
list-style: none;
}
.p-panelmenu .p-menuitem-link {
display: flex;
align-items: center;
user-select: none;
cursor: pointer;
text-decoration: none;
2022-12-08 11:04:25 +00:00
position: relative;
overflow: hidden;
2022-09-06 12:03:37 +00:00
}
.p-panelmenu .p-menuitem-text {
line-height: 1;
}
</style>