2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2024-02-11 23:47:53 +00:00
|
|
|
<div :ref="containerRef" :id="id" :class="cx('root')" v-bind="ptmi('root')">
|
2023-05-30 06:56:47 +00:00
|
|
|
<div v-if="$slots.start" :class="cx('start')" v-bind="ptm('start')">
|
2022-09-06 12:03:37 +00:00
|
|
|
<slot name="start"></slot>
|
|
|
|
</div>
|
2024-04-30 13:05:25 +00:00
|
|
|
<!--TODO: menubutton deprecated since v4.0. Use button-->
|
2024-05-06 10:52:41 +00:00
|
|
|
<slot :id="id" :name="$slots.button ? 'button' : 'menubutton'" :class="cx('button')" :toggleCallback="(event) => menuButtonClick(event)">
|
2023-11-22 10:36:31 +00:00
|
|
|
<a
|
|
|
|
v-if="model && model.length > 0"
|
|
|
|
ref="menubutton"
|
|
|
|
role="button"
|
|
|
|
tabindex="0"
|
2024-04-30 13:02:03 +00:00
|
|
|
:class="cx('button')"
|
2023-11-22 10:36:31 +00:00
|
|
|
:aria-haspopup="model.length && model.length > 0 ? true : false"
|
|
|
|
:aria-expanded="mobileActive"
|
|
|
|
:aria-controls="id"
|
|
|
|
:aria-label="$primevue.config.locale.aria?.navigation"
|
|
|
|
@click="menuButtonClick($event)"
|
|
|
|
@keydown="menuButtonKeydown($event)"
|
2024-04-30 13:02:03 +00:00
|
|
|
v-bind="ptm('button')"
|
2023-11-22 10:36:31 +00:00
|
|
|
>
|
2024-04-30 13:05:25 +00:00
|
|
|
<!--TODO: menubuttonicon deprecated since v4.0. Use buttonicon-->
|
2024-05-06 10:52:41 +00:00
|
|
|
<slot :name="$slots.buttonicon ? 'buttonicon' : 'menubuttonicon'">
|
2024-04-30 13:02:03 +00:00
|
|
|
<BarsIcon v-bind="ptm('buttonicon')" />
|
2023-11-22 10:36:31 +00:00
|
|
|
</slot>
|
|
|
|
</a>
|
|
|
|
</slot>
|
2022-12-08 11:04:25 +00:00
|
|
|
<MegaMenuSub
|
|
|
|
:ref="menubarRef"
|
|
|
|
:id="id + '_list'"
|
|
|
|
:tabindex="!disabled ? tabindex : -1"
|
|
|
|
role="menubar"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
:aria-disabled="disabled || undefined"
|
|
|
|
:aria-orientation="orientation"
|
|
|
|
:aria-activedescendant="focused ? focusedItemId : undefined"
|
|
|
|
:menuId="id"
|
|
|
|
:focusedItemId="focused ? focusedItemId : undefined"
|
|
|
|
:items="processedItems"
|
|
|
|
:horizontal="horizontal"
|
2023-04-17 06:14:45 +00:00
|
|
|
:templates="$slots"
|
2022-12-08 11:04:25 +00:00
|
|
|
:activeItem="activeItem"
|
2023-11-15 11:30:23 +00:00
|
|
|
:mobileActive="mobileActive"
|
2022-12-08 11:04:25 +00:00
|
|
|
:level="0"
|
2023-04-26 09:57:55 +00:00
|
|
|
:pt="pt"
|
2023-08-30 07:27:41 +00:00
|
|
|
:unstyled="unstyled"
|
2022-12-08 11:04:25 +00:00
|
|
|
@focus="onFocus"
|
|
|
|
@blur="onBlur"
|
|
|
|
@keydown="onKeyDown"
|
|
|
|
@item-click="onItemClick"
|
|
|
|
@item-mouseenter="onItemMouseEnter"
|
|
|
|
/>
|
2023-05-30 06:56:47 +00:00
|
|
|
<div v-if="$slots.end" :class="cx('end')" v-bind="ptm('end')">
|
2022-09-06 12:03:37 +00:00
|
|
|
<slot name="end"></slot>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-11-15 11:30:23 +00:00
|
|
|
import BarsIcon from 'primevue/icons/bars';
|
|
|
|
import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
|
2023-05-30 06:56:47 +00:00
|
|
|
import BaseMegaMenu from './BaseMegaMenu.vue';
|
2022-12-08 11:04:25 +00:00
|
|
|
import MegaMenuSub from './MegaMenuSub.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'MegaMenu',
|
2023-05-30 06:56:47 +00:00
|
|
|
extends: BaseMegaMenu,
|
2024-02-11 23:47:53 +00:00
|
|
|
inheritAttrs: false,
|
2022-12-08 11:04:25 +00:00
|
|
|
emits: ['focus', 'blur'],
|
|
|
|
outsideClickListener: null,
|
|
|
|
resizeListener: null,
|
2023-11-21 17:28:04 +00:00
|
|
|
matchMediaListener: null,
|
2022-12-08 11:04:25 +00:00
|
|
|
container: null,
|
|
|
|
menubar: null,
|
|
|
|
searchTimeout: null,
|
|
|
|
searchValue: null,
|
2022-09-06 12:03:37 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2023-01-19 04:01:03 +00:00
|
|
|
id: this.$attrs.id,
|
2023-11-15 11:30:23 +00:00
|
|
|
mobileActive: false,
|
2022-12-08 11:04:25 +00:00
|
|
|
focused: false,
|
|
|
|
focusedItemInfo: { index: -1, key: '', parentKey: '' },
|
|
|
|
activeItem: null,
|
2023-11-15 10:16:57 +00:00
|
|
|
dirty: false,
|
2023-11-21 17:28:04 +00:00
|
|
|
query: null,
|
2023-11-15 10:16:57 +00:00
|
|
|
queryMatches: false
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
watch: {
|
2024-04-16 09:35:02 +00:00
|
|
|
'$attrs.id': function (newValue) {
|
|
|
|
this.id = newValue || UniqueComponentId();
|
2023-01-19 04:01:03 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
activeItem(newItem) {
|
|
|
|
if (ObjectUtils.isNotEmpty(newItem)) {
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
this.bindResizeListener();
|
|
|
|
} else {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
this.unbindResizeListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-01-16 11:28:03 +00:00
|
|
|
mounted() {
|
2024-04-16 09:35:02 +00:00
|
|
|
this.id = this.id || UniqueComponentId();
|
2023-11-21 17:28:04 +00:00
|
|
|
this.bindMatchMediaListener();
|
2023-01-19 04:01:03 +00:00
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
beforeUnmount() {
|
2023-11-15 11:30:23 +00:00
|
|
|
this.mobileActive = false;
|
2022-12-08 11:04:25 +00:00
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
this.unbindResizeListener();
|
2023-11-21 17:28:04 +00:00
|
|
|
this.unbindMatchMediaListener();
|
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');
|
|
|
|
},
|
|
|
|
isItemDisabled(item) {
|
|
|
|
return this.getItemProp(item, 'disabled');
|
|
|
|
},
|
2024-04-03 14:06:06 +00:00
|
|
|
isItemVisible(item) {
|
|
|
|
return this.getItemProp(item, 'visible') !== false;
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
isItemGroup(item) {
|
|
|
|
return ObjectUtils.isNotEmpty(this.getItemProp(item, 'items'));
|
|
|
|
},
|
|
|
|
isItemSeparator(item) {
|
|
|
|
return this.getItemProp(item, 'separator');
|
|
|
|
},
|
|
|
|
getProccessedItemLabel(processedItem) {
|
|
|
|
return processedItem ? this.getItemLabel(processedItem.item) : undefined;
|
|
|
|
},
|
|
|
|
isProccessedItemGroup(processedItem) {
|
|
|
|
return processedItem && ObjectUtils.isNotEmpty(processedItem.items);
|
|
|
|
},
|
2023-11-15 11:30:23 +00:00
|
|
|
toggle(event) {
|
|
|
|
if (this.mobileActive) {
|
|
|
|
this.mobileActive = false;
|
|
|
|
ZIndexUtils.clear(this.menubar);
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.mobileActive = true;
|
|
|
|
ZIndexUtils.set('menu', this.menubar, this.$primevue.config.zIndex.menu);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.show();
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
show() {
|
|
|
|
this.focusedItemInfo = { index: this.findFirstFocusedItemIndex(), level: 0, parentKey: '' };
|
|
|
|
|
|
|
|
DomHandler.focus(this.menubar);
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
hide(event, isFocus) {
|
2023-11-15 11:30:23 +00:00
|
|
|
if (this.mobileActive) {
|
|
|
|
this.mobileActive = false;
|
|
|
|
setTimeout(() => {
|
|
|
|
DomHandler.focus(this.$refs.menubutton);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.activeItem = null;
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focusedItemInfo = { index: -1, key: '', parentKey: '' };
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
isFocus && DomHandler.focus(this.menubar);
|
|
|
|
this.dirty = false;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onFocus(event) {
|
|
|
|
this.focused = true;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
if (this.focusedItemInfo.index === -1) {
|
|
|
|
const index = this.findFirstFocusedItemIndex();
|
|
|
|
const processedItem = this.findVisibleItem(index);
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focusedItemInfo = { index, key: processedItem.key, parentKey: processedItem.parentKey };
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.focused = false;
|
|
|
|
this.focusedItemInfo = { index: -1, key: '', parentKey: '' };
|
|
|
|
this.searchValue = '';
|
|
|
|
this.dirty = false;
|
|
|
|
this.$emit('blur', event);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onKeyDown(event) {
|
|
|
|
if (this.disabled) {
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
const metaKey = event.metaKey || event.ctrlKey;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
switch (event.code) {
|
|
|
|
case 'ArrowDown':
|
|
|
|
this.onArrowDownKey(event);
|
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'ArrowUp':
|
|
|
|
this.onArrowUpKey(event);
|
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'ArrowLeft':
|
|
|
|
this.onArrowLeftKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowRight':
|
|
|
|
this.onArrowRightKey(event);
|
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'Home':
|
|
|
|
this.onHomeKey(event);
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'End':
|
|
|
|
this.onEndKey(event);
|
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'Space':
|
|
|
|
this.onSpaceKey(event);
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'Enter':
|
2023-12-20 10:45:43 +00:00
|
|
|
case 'NumpadEnter':
|
2022-12-08 11:04:25 +00:00
|
|
|
this.onEnterKey(event);
|
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'Escape':
|
|
|
|
this.onEscapeKey(event);
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'Tab':
|
|
|
|
this.onTabKey(event);
|
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
case 'PageDown':
|
|
|
|
case 'PageUp':
|
|
|
|
case 'Backspace':
|
|
|
|
case 'ShiftLeft':
|
|
|
|
case 'ShiftRight':
|
|
|
|
//NOOP
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
default:
|
2022-12-08 11:04:25 +00:00
|
|
|
if (!metaKey && ObjectUtils.isPrintableCharacter(event.key)) {
|
|
|
|
this.searchItems(event, event.key);
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onItemChange(event) {
|
|
|
|
const { processedItem, isFocus } = event;
|
|
|
|
|
|
|
|
if (ObjectUtils.isEmpty(processedItem)) return;
|
|
|
|
|
|
|
|
const { index, key, parentKey, items } = processedItem;
|
|
|
|
const grouped = ObjectUtils.isNotEmpty(items);
|
|
|
|
|
|
|
|
grouped && (this.activeItem = processedItem);
|
|
|
|
this.focusedItemInfo = { index, key, parentKey };
|
|
|
|
|
|
|
|
grouped && (this.dirty = true);
|
|
|
|
isFocus && DomHandler.focus(this.menubar);
|
|
|
|
},
|
|
|
|
onItemClick(event) {
|
|
|
|
const { originalEvent, processedItem } = event;
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
|
|
|
const root = ObjectUtils.isEmpty(processedItem.parent);
|
|
|
|
const selected = this.isSelected(processedItem);
|
|
|
|
|
|
|
|
if (selected) {
|
|
|
|
const { index, key, parentKey } = processedItem;
|
|
|
|
|
|
|
|
this.activeItem = null;
|
|
|
|
this.focusedItemInfo = { index, key, parentKey };
|
|
|
|
|
|
|
|
this.dirty = !root;
|
|
|
|
DomHandler.focus(this.menubar);
|
|
|
|
} else {
|
|
|
|
if (grouped) {
|
|
|
|
this.onItemChange(event);
|
|
|
|
} else {
|
|
|
|
const rootProcessedItem = root ? processedItem : this.activeItem;
|
|
|
|
|
|
|
|
this.hide(originalEvent);
|
|
|
|
this.changeFocusedItemInfo(originalEvent, rootProcessedItem ? rootProcessedItem.index : -1);
|
2023-11-15 11:30:23 +00:00
|
|
|
this.mobileActive = false;
|
2022-12-08 11:04:25 +00:00
|
|
|
DomHandler.focus(this.menubar);
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onItemMouseEnter(event) {
|
2023-11-15 11:30:23 +00:00
|
|
|
if (!this.mobileActive && this.dirty) {
|
2022-12-08 11:04:25 +00:00
|
|
|
this.onItemChange(event);
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2023-11-15 11:30:23 +00:00
|
|
|
menuButtonClick(event) {
|
|
|
|
this.toggle(event);
|
|
|
|
},
|
|
|
|
menuButtonKeydown(event) {
|
2023-12-20 10:45:43 +00:00
|
|
|
(event.code === 'Enter' || event.code === 'NumpadEnter' || event.code === 'Space') && this.menuButtonClick(event);
|
2023-11-15 11:30:23 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onArrowDownKey(event) {
|
|
|
|
if (this.horizontal) {
|
|
|
|
if (ObjectUtils.isNotEmpty(this.activeItem) && this.activeItem.key === this.focusedItemInfo.key) {
|
|
|
|
this.focusedItemInfo = { index: -1, key: '', parentKey: this.activeItem.key };
|
|
|
|
} else {
|
|
|
|
const processedItem = this.findVisibleItem(this.focusedItemInfo.index);
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
if (grouped) {
|
|
|
|
this.onItemChange({ originalEvent: event, processedItem });
|
|
|
|
this.focusedItemInfo = { index: -1, key: processedItem.key, parentKey: processedItem.parentKey };
|
|
|
|
this.searchValue = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const itemIndex = this.focusedItemInfo.index !== -1 ? this.findNextItemIndex(this.focusedItemInfo.index) : this.findFirstFocusedItemIndex();
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
this.changeFocusedItemInfo(event, itemIndex);
|
|
|
|
event.preventDefault();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onArrowUpKey(event) {
|
|
|
|
if (event.altKey && this.horizontal) {
|
|
|
|
if (this.focusedItemInfo.index !== -1) {
|
|
|
|
const processedItem = this.findVisibleItem(this.focusedItemInfo.index);
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
|
|
|
|
|
|
|
if (!grouped && ObjectUtils.isNotEmpty(this.activeItem)) {
|
|
|
|
if (this.focusedItemInfo.index === 0) {
|
|
|
|
this.focusedItemInfo = { index: this.activeItem.index, key: this.activeItem.key, parentKey: this.activeItem.parentKey };
|
|
|
|
this.activeItem = null;
|
|
|
|
} else {
|
|
|
|
this.changeFocusedItemInfo(event, this.findFirstItemIndex());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
event.preventDefault();
|
|
|
|
} else {
|
|
|
|
const itemIndex = this.focusedItemInfo.index !== -1 ? this.findPrevItemIndex(this.focusedItemInfo.index) : this.findLastFocusedItemIndex();
|
|
|
|
|
|
|
|
this.changeFocusedItemInfo(event, itemIndex);
|
|
|
|
event.preventDefault();
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onArrowLeftKey(event) {
|
|
|
|
const processedItem = this.findVisibleItem(this.focusedItemInfo.index);
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
|
|
|
|
|
|
|
if (grouped) {
|
|
|
|
if (this.horizontal) {
|
|
|
|
const itemIndex = this.focusedItemInfo.index !== -1 ? this.findPrevItemIndex(this.focusedItemInfo.index) : this.findLastFocusedItemIndex();
|
|
|
|
|
|
|
|
this.changeFocusedItemInfo(event, itemIndex);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.vertical && ObjectUtils.isNotEmpty(this.activeItem)) {
|
|
|
|
if (processedItem.columnIndex === 0) {
|
|
|
|
this.focusedItemInfo = { index: this.activeItem.index, key: this.activeItem.key, parentKey: this.activeItem.parentKey };
|
|
|
|
this.activeItem = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const columnIndex = processedItem.columnIndex - 1;
|
|
|
|
const itemIndex = this.visibleItems.findIndex((item) => item.columnIndex === columnIndex);
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
itemIndex !== -1 && this.changeFocusedItemInfo(event, itemIndex);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onArrowRightKey(event) {
|
|
|
|
const processedItem = this.findVisibleItem(this.focusedItemInfo.index);
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
|
|
|
|
|
|
|
if (grouped) {
|
|
|
|
if (this.vertical) {
|
|
|
|
if (ObjectUtils.isNotEmpty(this.activeItem) && this.activeItem.key === processedItem.key) {
|
|
|
|
this.focusedItemInfo = { index: -1, key: '', parentKey: this.activeItem.key };
|
|
|
|
} else {
|
|
|
|
const processedItem = this.findVisibleItem(this.focusedItemInfo.index);
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
|
|
|
|
|
|
|
if (grouped) {
|
|
|
|
this.onItemChange({ originalEvent: event, processedItem });
|
|
|
|
this.focusedItemInfo = { index: -1, key: processedItem.key, parentKey: processedItem.parentKey };
|
|
|
|
this.searchValue = '';
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
}
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
const itemIndex = this.focusedItemInfo.index !== -1 ? this.findNextItemIndex(this.focusedItemInfo.index) : this.findFirstFocusedItemIndex();
|
|
|
|
|
|
|
|
this.changeFocusedItemInfo(event, itemIndex);
|
|
|
|
} else {
|
|
|
|
const columnIndex = processedItem.columnIndex + 1;
|
|
|
|
const itemIndex = this.visibleItems.findIndex((item) => item.columnIndex === columnIndex);
|
|
|
|
|
|
|
|
itemIndex !== -1 && this.changeFocusedItemInfo(event, itemIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onHomeKey(event) {
|
|
|
|
this.changeFocusedItemInfo(event, this.findFirstItemIndex());
|
|
|
|
event.preventDefault();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onEndKey(event) {
|
|
|
|
this.changeFocusedItemInfo(event, this.findLastItemIndex());
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onEnterKey(event) {
|
|
|
|
if (this.focusedItemInfo.index !== -1) {
|
|
|
|
const element = DomHandler.findSingle(this.menubar, `li[id="${`${this.focusedItemId}`}"]`);
|
2024-04-30 13:02:03 +00:00
|
|
|
const anchorElement = element && DomHandler.findSingle(element, 'a[data-pc-section="itemlink"]');
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
anchorElement ? anchorElement.click() : element && element.click();
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
const processedItem = this.visibleItems[this.focusedItemInfo.index];
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
!grouped && this.changeFocusedItemInfo(event, this.findFirstFocusedItemIndex());
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onSpaceKey(event) {
|
|
|
|
this.onEnterKey(event);
|
|
|
|
},
|
|
|
|
onEscapeKey(event) {
|
|
|
|
if (ObjectUtils.isNotEmpty(this.activeItem)) {
|
|
|
|
this.focusedItemInfo = { index: this.activeItem.index, key: this.activeItem.key };
|
|
|
|
this.activeItem = null;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
event.preventDefault();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onTabKey(event) {
|
|
|
|
if (this.focusedItemInfo.index !== -1) {
|
|
|
|
const processedItem = this.findVisibleItem(this.focusedItemInfo.index);
|
|
|
|
const grouped = this.isProccessedItemGroup(processedItem);
|
|
|
|
|
|
|
|
!grouped && this.onItemChange({ originalEvent: event, processedItem });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hide();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
bindOutsideClickListener() {
|
|
|
|
if (!this.outsideClickListener) {
|
|
|
|
this.outsideClickListener = (event) => {
|
|
|
|
const isOutsideContainer = this.container && !this.container.contains(event.target);
|
2023-11-15 11:30:23 +00:00
|
|
|
const isOutsideTarget = !(this.target && (this.target === event.target || this.target.contains(event.target)));
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
if (isOutsideContainer && isOutsideTarget) {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('click', this.outsideClickListener);
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
unbindOutsideClickListener() {
|
|
|
|
if (this.outsideClickListener) {
|
|
|
|
document.removeEventListener('click', this.outsideClickListener);
|
|
|
|
this.outsideClickListener = null;
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
bindResizeListener() {
|
|
|
|
if (!this.resizeListener) {
|
|
|
|
this.resizeListener = (event) => {
|
|
|
|
if (!DomHandler.isTouchDevice()) {
|
|
|
|
this.hide(event, true);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2023-11-15 11:30:23 +00:00
|
|
|
|
|
|
|
this.mobileActive = false;
|
2022-09-06 12:03:37 +00:00
|
|
|
};
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
window.addEventListener('resize', this.resizeListener);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
unbindResizeListener() {
|
|
|
|
if (this.resizeListener) {
|
|
|
|
window.removeEventListener('resize', this.resizeListener);
|
|
|
|
this.resizeListener = null;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2023-11-21 17:28:04 +00:00
|
|
|
bindMatchMediaListener() {
|
|
|
|
if (!this.matchMediaListener) {
|
|
|
|
const query = matchMedia(`(max-width: ${this.breakpoint})`);
|
|
|
|
|
|
|
|
this.query = query;
|
|
|
|
this.queryMatches = query.matches;
|
|
|
|
|
|
|
|
this.matchMediaListener = () => {
|
|
|
|
this.queryMatches = query.matches;
|
|
|
|
this.mobileActive = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.query.addEventListener('change', this.matchMediaListener);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbindMatchMediaListener() {
|
|
|
|
if (this.matchMediaListener) {
|
|
|
|
this.query.removeEventListener('change', this.matchMediaListener);
|
|
|
|
this.matchMediaListener = null;
|
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
isItemMatched(processedItem) {
|
2024-01-24 11:54:14 +00:00
|
|
|
return this.isValidItem(processedItem) && this.getProccessedItemLabel(processedItem)?.toLocaleLowerCase().startsWith(this.searchValue.toLocaleLowerCase());
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
isValidItem(processedItem) {
|
2024-04-03 14:06:06 +00:00
|
|
|
return !!processedItem && !this.isItemDisabled(processedItem.item) && !this.isItemSeparator(processedItem.item) && this.isItemVisible(processedItem.item);
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
isValidSelectedItem(processedItem) {
|
|
|
|
return this.isValidItem(processedItem) && this.isSelected(processedItem);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
isSelected(processedItem) {
|
|
|
|
return ObjectUtils.isNotEmpty(this.activeItem) ? this.activeItem.key === processedItem.key : false;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
findFirstItemIndex() {
|
|
|
|
return this.visibleItems.findIndex((processedItem) => this.isValidItem(processedItem));
|
|
|
|
},
|
|
|
|
findLastItemIndex() {
|
|
|
|
return ObjectUtils.findLastIndex(this.visibleItems, (processedItem) => this.isValidItem(processedItem));
|
|
|
|
},
|
|
|
|
findNextItemIndex(index) {
|
|
|
|
const matchedItemIndex = index < this.visibleItems.length - 1 ? this.visibleItems.slice(index + 1).findIndex((processedItem) => this.isValidItem(processedItem)) : -1;
|
|
|
|
|
|
|
|
return matchedItemIndex > -1 ? matchedItemIndex + index + 1 : index;
|
|
|
|
},
|
|
|
|
findPrevItemIndex(index) {
|
|
|
|
const matchedItemIndex = index > 0 ? ObjectUtils.findLastIndex(this.visibleItems.slice(0, index), (processedItem) => this.isValidItem(processedItem)) : -1;
|
|
|
|
|
|
|
|
return matchedItemIndex > -1 ? matchedItemIndex : index;
|
|
|
|
},
|
|
|
|
findSelectedItemIndex() {
|
|
|
|
return this.visibleItems.findIndex((processedItem) => this.isValidSelectedItem(processedItem));
|
|
|
|
},
|
|
|
|
findFirstFocusedItemIndex() {
|
|
|
|
const selectedIndex = this.findSelectedItemIndex();
|
|
|
|
|
|
|
|
return selectedIndex < 0 ? this.findFirstItemIndex() : selectedIndex;
|
|
|
|
},
|
|
|
|
findLastFocusedItemIndex() {
|
|
|
|
const selectedIndex = this.findSelectedItemIndex();
|
|
|
|
|
|
|
|
return selectedIndex < 0 ? this.findLastItemIndex() : selectedIndex;
|
|
|
|
},
|
|
|
|
findVisibleItem(index) {
|
|
|
|
return ObjectUtils.isNotEmpty(this.visibleItems) ? this.visibleItems[index] : null;
|
|
|
|
},
|
|
|
|
searchItems(event, char) {
|
|
|
|
this.searchValue = (this.searchValue || '') + char;
|
|
|
|
|
|
|
|
let itemIndex = -1;
|
|
|
|
let matched = false;
|
|
|
|
|
|
|
|
if (this.focusedItemInfo.index !== -1) {
|
|
|
|
itemIndex = this.visibleItems.slice(this.focusedItemInfo.index).findIndex((processedItem) => this.isItemMatched(processedItem));
|
|
|
|
itemIndex = itemIndex === -1 ? this.visibleItems.slice(0, this.focusedItemInfo.index).findIndex((processedItem) => this.isItemMatched(processedItem)) : itemIndex + this.focusedItemInfo.index;
|
|
|
|
} else {
|
|
|
|
itemIndex = this.visibleItems.findIndex((processedItem) => this.isItemMatched(processedItem));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemIndex !== -1) {
|
|
|
|
matched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemIndex === -1 && this.focusedItemInfo.index === -1) {
|
|
|
|
itemIndex = this.findFirstFocusedItemIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemIndex !== -1) {
|
|
|
|
this.changeFocusedItemInfo(event, itemIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.searchTimeout) {
|
|
|
|
clearTimeout(this.searchTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.searchTimeout = setTimeout(() => {
|
|
|
|
this.searchValue = '';
|
|
|
|
this.searchTimeout = null;
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
return matched;
|
|
|
|
},
|
|
|
|
changeFocusedItemInfo(event, index) {
|
|
|
|
const processedItem = this.findVisibleItem(index);
|
|
|
|
|
|
|
|
this.focusedItemInfo.index = index;
|
|
|
|
this.focusedItemInfo.key = ObjectUtils.isNotEmpty(processedItem) ? processedItem.key : '';
|
|
|
|
this.scrollInView();
|
|
|
|
},
|
|
|
|
scrollInView(index = -1) {
|
|
|
|
const id = index !== -1 ? `${this.id}_${index}` : this.focusedItemId;
|
|
|
|
const element = DomHandler.findSingle(this.menubar, `li[id="${id}"]`);
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
element.scrollIntoView && element.scrollIntoView({ block: 'nearest', inline: 'start' });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
createProcessedItems(items, level = 0, parent = {}, parentKey = '', columnIndex) {
|
|
|
|
const processedItems = [];
|
|
|
|
|
|
|
|
items &&
|
|
|
|
items.forEach((item, index) => {
|
|
|
|
const key = (parentKey !== '' ? parentKey + '_' : '') + (columnIndex !== undefined ? columnIndex + '_' : '') + index;
|
|
|
|
const newItem = {
|
|
|
|
item,
|
|
|
|
index,
|
|
|
|
level,
|
|
|
|
key,
|
|
|
|
parent,
|
|
|
|
parentKey,
|
|
|
|
columnIndex: columnIndex !== undefined ? columnIndex : parent.columnIndex !== undefined ? parent.columnIndex : index
|
|
|
|
};
|
|
|
|
|
|
|
|
newItem['items'] =
|
|
|
|
level === 0 && item.items && item.items.length > 0 ? item.items.map((_items, _index) => this.createProcessedItems(_items, level + 1, newItem, key, _index)) : this.createProcessedItems(item.items, level + 1, newItem, key);
|
|
|
|
processedItems.push(newItem);
|
|
|
|
});
|
|
|
|
|
|
|
|
return processedItems;
|
|
|
|
},
|
|
|
|
containerRef(el) {
|
|
|
|
this.container = el;
|
|
|
|
},
|
|
|
|
menubarRef(el) {
|
|
|
|
this.menubar = el ? el.$el : undefined;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-12-08 11:04:25 +00:00
|
|
|
processedItems() {
|
|
|
|
return this.createProcessedItems(this.model || []);
|
|
|
|
},
|
|
|
|
visibleItems() {
|
|
|
|
const processedItem = ObjectUtils.isNotEmpty(this.activeItem) ? this.activeItem : null;
|
|
|
|
|
|
|
|
return processedItem && processedItem.key === this.focusedItemInfo.parentKey
|
|
|
|
? processedItem.items.reduce((items, col) => {
|
|
|
|
col.forEach((submenu) => {
|
|
|
|
submenu.items.forEach((a) => {
|
|
|
|
items.push(a);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}, [])
|
|
|
|
: this.processedItems;
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
horizontal() {
|
|
|
|
return this.orientation === 'horizontal';
|
|
|
|
},
|
|
|
|
vertical() {
|
|
|
|
return this.orientation === 'vertical';
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
focusedItemId() {
|
|
|
|
return ObjectUtils.isNotEmpty(this.focusedItemInfo.key) ? `${this.id}_${this.focusedItemInfo.key}` : null;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
components: {
|
2023-11-15 11:30:23 +00:00
|
|
|
MegaMenuSub: MegaMenuSub,
|
|
|
|
BarsIcon: BarsIcon
|
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>
|