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

274 lines
12 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-30 08:25:54 +00:00
<div :id="id" :class="cx('root')" v-bind="ptm('root')" data-pc-name="panelmenu">
2023-01-03 09:14:46 +00:00
<template v-for="(item, index) of model" :key="getPanelKey(index)">
2023-05-30 08:25:54 +00:00
<div v-if="isItemVisible(item)" :style="getItemProp(item, 'style')" :class="[cx('panel'), getItemProp(item, 'class')]" v-bind="ptm('panel')">
2022-12-08 11:04:25 +00:00
<div
:id="getHeaderId(index)"
2023-05-30 08:25:54 +00:00
:class="[cx('header', { item }), getItemProp(item, 'headerClass')]"
2022-12-08 11:04:25 +00:00
: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)"
2023-07-24 10:48:47 +00:00
v-bind="getPTOptions('header', item, index)"
2023-05-30 08:25:54 +00:00
:data-p-highlight="isItemActive(item)"
:data-p-disabled="isItemDisabled(item)"
2022-12-08 11:04:25 +00:00
>
2023-07-24 10:48:47 +00:00
<div :class="cx('headerContent')" v-bind="getPTOptions('headerContent', item, index)">
2022-12-08 11:04:25 +00:00
<template v-if="!$slots.item">
2023-11-08 12:49:16 +00:00
<a :href="getItemProp(item, 'url')" :class="cx('headerAction')" :tabindex="-1" v-bind="getPTOptions('headerAction', item, index)">
<slot v-if="getItemProp(item, 'items')" name="submenuicon" :active="isItemActive(item)">
2023-07-24 10:48:47 +00:00
<component :is="isItemActive(item) ? 'ChevronDownIcon' : 'ChevronRightIcon'" :class="cx('submenuIcon')" v-bind="getPTOptions('submenuIcon', item, index)" />
</slot>
2023-05-30 08:25:54 +00:00
<component v-if="$slots.headericon" :is="$slots.headericon" :item="item" :class="[cx('headerIcon'), getItemProp(item, 'icon')]" />
2023-07-24 10:48:47 +00:00
<span v-else-if="getItemProp(item, 'icon')" :class="[cx('headerIcon'), getItemProp(item, 'icon')]" v-bind="getPTOptions('headerIcon', item, index)" />
<span :class="cx('headerLabel')" v-bind="getPTOptions('headerLabel', item, index)">{{ getItemLabel(item) }}</span>
2022-09-06 12:03:37 +00:00
</a>
2022-12-08 11:04:25 +00:00
</template>
2023-11-09 08:42:17 +00:00
<component v-else :is="$slots.item" :item="item" :root="true" :active="isItemActive(item)" :hasSubmenu="getItemProp(item, 'items')" :label="getItemLabel(item)" :props="getMenuItemProps(item, index)"></component>
2022-12-08 11:04:25 +00:00
</div>
2022-09-06 12:03:37 +00:00
</div>
2023-08-02 12:02:08 +00:00
<transition name="p-toggleable-content" v-bind="ptm('transition')">
2023-05-30 08:25:54 +00:00
<div v-show="isItemActive(item)" :id="getContentId(index)" :class="cx('toggleableContent')" role="region" :aria-labelledby="getHeaderId(index)" v-bind="ptm('toggleableContent')">
<div v-if="getItemProp(item, 'items')" :class="cx('menuContent')" v-bind="ptm('menuContent')">
2023-04-26 09:58:36 +00:00
<PanelMenuList
:panelId="getPanelId(index)"
:items="getItemProp(item, 'items')"
:templates="$slots"
:expandedKeys="expandedKeys"
@item-toggle="changeExpandedKeys"
@header-focus="updateFocusedHeader"
:pt="pt"
2023-08-30 07:27:49 +00:00
:unstyled="unstyled"
2023-04-26 09:58:36 +00:00
/>
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';
2023-10-17 09:55:48 +00:00
import { mergeProps } from 'vue';
2023-05-30 08:25:54 +00:00
import BasePanelMenu from './BasePanelMenu.vue';
2022-12-08 11:04:25 +00:00
import PanelMenuList from './PanelMenuList.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'PanelMenu',
2023-05-30 08:25:54 +00:00
extends: BasePanelMenu,
2022-12-08 11:04:25 +00:00
emits: ['update:expandedKeys', 'panel-open', 'panel-close'],
2022-09-06 12:03:37 +00:00
data() {
return {
id: this.$attrs.id,
activeItem: null,
activeItems: []
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();
}
},
2024-01-23 18:06:31 +00:00
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');
},
2023-07-24 10:48:47 +00:00
getPTOptions(key, item, index) {
return this.ptm(key, {
2023-07-24 10:48:47 +00:00
context: {
index,
active: this.isItemActive(item),
focused: this.isItemFocused(item),
2023-10-17 15:23:16 +00:00
disabled: this.isItemDisabled(item)
}
});
},
2022-12-08 11:04:25 +00:00
isItemActive(item) {
return this.expandedKeys ? this.expandedKeys[this.getItemProp(item, 'key')] : this.multiple ? this.activeItems.some((subItem) => ObjectUtils.equals(item, subItem)) : 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');
},
isItemFocused(item) {
return ObjectUtils.equals(item, this.activeItem);
},
2022-12-08 11:04:25 +00:00
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 'NumpadEnter':
2022-12-08 11:04:25 +00:00
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) {
2023-05-30 08:25:54 +00:00
const rootList = DomHandler.getAttribute(event.currentTarget, 'data-p-highlight') === true ? DomHandler.findSingle(event.currentTarget.nextElementSibling, '[data-pc-section="menu"]') : null;
2022-12-08 11:04:25 +00:00
rootList ? DomHandler.focus(rootList) : this.updateFocusedHeader({ originalEvent: event, focusOnNext: true });
event.preventDefault();
},
onHeaderArrowUpKey(event) {
const prevHeader = this.findPrevHeader(event.currentTarget.parentElement) || this.findLastHeader();
2023-05-30 08:25:54 +00:00
const rootList = DomHandler.getAttribute(prevHeader, 'data-p-highlight') === true ? DomHandler.findSingle(prevHeader.nextElementSibling, '[data-pc-section="menu"]') : null;
2022-12-08 11:04:25 +00:00
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) {
2023-05-30 08:25:54 +00:00
const headerAction = DomHandler.findSingle(event.currentTarget, '[data-pc-section="headeraction"]');
2022-12-08 11:04:25 +00:00
headerAction ? headerAction.click() : this.onHeaderClick(event, item);
event.preventDefault();
},
findNextHeader(panelElement, selfCheck = false) {
const nextPanelElement = selfCheck ? panelElement : panelElement.nextElementSibling;
2023-05-30 08:25:54 +00:00
const headerElement = DomHandler.findSingle(nextPanelElement, '[data-pc-section="header"]');
2022-12-08 11:04:25 +00:00
2023-05-30 08:25:54 +00:00
return headerElement ? (DomHandler.getAttribute(headerElement, 'data-p-disabled') ? this.findNextHeader(headerElement.parentElement) : headerElement) : null;
2022-12-08 11:04:25 +00:00
},
findPrevHeader(panelElement, selfCheck = false) {
const prevPanelElement = selfCheck ? panelElement : panelElement.previousElementSibling;
2023-05-30 08:25:54 +00:00
const headerElement = DomHandler.findSingle(prevPanelElement, '[data-pc-section="header"]');
2022-12-08 11:04:25 +00:00
2023-05-30 08:25:54 +00:00
return headerElement ? (DomHandler.getAttribute(headerElement, 'data-p-disabled') ? this.findPrevHeader(headerElement.parentElement) : headerElement) : null;
2022-12-08 11:04:25 +00:00
},
findFirstHeader() {
return this.findNextHeader(this.$el.firstElementChild, true);
},
findLastHeader() {
return this.findPrevHeader(this.$el.lastElementChild, true);
},
updateFocusedHeader(event) {
const { originalEvent, focusOnNext, selfCheck } = event;
2023-05-30 08:25:54 +00:00
const panelElement = originalEvent.currentTarget.closest('[data-pc-section="panel"]');
const header = selfCheck ? DomHandler.findSingle(panelElement, '[data-pc-section="header"]') : focusOnNext ? this.findNextHeader(panelElement) : this.findPrevHeader(panelElement);
2022-12-08 11:04:25 +00:00
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;
if (this.multiple) {
// activeItem and activeItems should be separated because it should be only one focused root item
if (this.activeItems.some((subItem) => ObjectUtils.equals(item, subItem))) {
this.activeItems = this.activeItems.filter((subItem) => !ObjectUtils.equals(item, subItem));
} else {
this.activeItems.push(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);
2023-10-17 09:55:48 +00:00
},
getMenuItemProps(item, index) {
return {
icon: mergeProps(
{
class: [this.cx('headerIcon'), this.getItemProp(item, 'icon')]
},
this.getPTOptions('headerIcon', item, index)
),
label: mergeProps(
{
class: this.cx('headerLabel')
},
this.getPTOptions('headerLabel', item, index)
2023-10-17 09:55:48 +00:00
)
};
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>