primevue-mirror/components/lib/dock/DockSub.vue

286 lines
10 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-30 12:56:18 +00:00
<div :class="cx('container')" v-bind="ptm('container')">
2022-12-08 11:04:25 +00:00
<ul
ref="list"
:id="id"
2023-05-30 12:56:18 +00:00
:class="cx('menu')"
2022-12-08 11:04:25 +00:00
role="menu"
:aria-orientation="position === 'bottom' || position === 'top' ? 'horizontal' : 'vertical'"
:aria-activedescendant="focused ? focusedOptionId : undefined"
:tabindex="tabindex"
:aria-label="ariaLabel"
:aria-labelledby="ariaLabelledby"
@focus="onListFocus"
@blur="onListBlur"
@keydown="onListKeyDown"
@mouseleave="onListMouseLeave"
2023-04-26 10:49:14 +00:00
v-bind="ptm('menu')"
2022-12-08 11:04:25 +00:00
>
<template v-for="(processedItem, index) of model" :key="index">
<li
:id="getItemId(index)"
2024-03-18 12:17:22 +00:00
:class="cx('menuitem', { processedItem, id: getItemId(index) })"
2022-12-08 11:04:25 +00:00
role="menuitem"
:aria-label="processedItem.label"
:aria-disabled="disabled(processedItem)"
@click="onItemClick($event, processedItem)"
@mouseenter="onItemMouseEnter(index)"
2023-07-25 09:11:01 +00:00
v-bind="getPTOptions('menuitem', processedItem, index)"
2023-05-30 12:56:18 +00:00
:data-p-focused="isItemActive(getItemId(index))"
:data-p-disabled="disabled(processedItem) || false"
2022-12-08 11:04:25 +00:00
>
2023-07-25 09:11:01 +00:00
<div :class="cx('content')" v-bind="getPTOptions('content', processedItem, index)">
2022-12-08 11:04:25 +00:00
<template v-if="!templates['item']">
2023-04-26 10:49:14 +00:00
<a
v-tooltip:[tooltipOptions]="{ value: processedItem.label, disabled: !tooltipOptions }"
:href="processedItem.url"
2023-05-30 12:56:18 +00:00
:class="cx('action')"
2023-04-26 10:49:14 +00:00
:target="processedItem.target"
tabindex="-1"
aria-hidden="true"
2023-07-25 09:11:01 +00:00
v-bind="getPTOptions('action', processedItem, index)"
2023-04-26 10:49:14 +00:00
>
2022-12-08 11:04:25 +00:00
<template v-if="!templates['icon']">
2023-07-25 09:11:01 +00:00
<span v-ripple :class="[cx('icon'), processedItem.icon]" v-bind="getPTOptions('icon', processedItem, index)"></span>
2022-12-08 11:04:25 +00:00
</template>
2023-05-30 12:56:18 +00:00
<component v-else :is="templates['icon']" :item="processedItem" :class="cx('icon')"></component>
2022-12-08 11:04:25 +00:00
</a>
2022-09-06 12:03:37 +00:00
</template>
2023-08-30 06:40:37 +00:00
<component v-else :is="templates['item']" :item="processedItem" :index="index" :label="processedItem.label" :props="getMenuItemProps(processedItem, index)"></component>
2022-12-08 11:04:25 +00:00
</div>
</li>
</template>
2022-09-06 12:03:37 +00:00
</ul>
</div>
</template>
<script>
2023-04-26 10:49:14 +00:00
import BaseComponent from 'primevue/basecomponent';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
import Tooltip from 'primevue/tooltip';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils, UniqueComponentId } 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: 'DockSub',
2023-07-04 06:29:36 +00:00
hostName: 'Dock',
2023-04-26 10:49:14 +00:00
extends: BaseComponent,
2022-12-08 11:04:25 +00:00
emits: ['focus', 'blur'],
2022-09-06 12:03:37 +00:00
props: {
2022-12-08 11:04:25 +00:00
position: {
type: String,
default: 'bottom'
},
2022-09-06 12:03:37 +00:00
model: {
type: Array,
default: null
},
templates: {
type: null,
default: null
},
2022-12-08 11:04:25 +00:00
tooltipOptions: null,
menuId: {
type: String,
default: null
},
tabindex: {
type: Number,
default: 0
},
ariaLabel: {
2022-12-08 11:04:25 +00:00
type: String,
default: null
},
ariaLabelledby: {
2022-12-08 11:04:25 +00:00
type: String,
default: null
}
2022-09-06 12:03:37 +00:00
},
data() {
return {
id: this.menuId,
2022-12-08 11:04:25 +00:00
currentIndex: -3,
focused: false,
focusedOptionIndex: -1
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
menuId(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
getItemId(index) {
return `${this.id}_${index}`;
},
getItemProp(processedItem, name) {
return processedItem && processedItem.item ? ObjectUtils.getItemValue(processedItem.item[name]) : undefined;
},
2023-07-25 09:11:01 +00:00
getPTOptions(key, item, index) {
return this.ptm(key, {
context: {
2023-07-19 12:03:52 +00:00
index,
2023-07-25 09:11:01 +00:00
item,
2023-07-19 12:03:52 +00:00
active: this.isItemActive(this.getItemId(index))
}
});
},
2022-12-08 11:04:25 +00:00
isSameMenuItem(event) {
2023-05-30 12:56:18 +00:00
return event.currentTarget && (event.currentTarget.isSameNode(event.target) || event.currentTarget.isSameNode(event.target.closest('[data-pc-section="menuitem"]')));
2022-12-08 11:04:25 +00:00
},
isItemActive(id) {
return id === this.focusedOptionIndex;
},
2022-09-06 12:03:37 +00:00
onListMouseLeave() {
this.currentIndex = -3;
},
onItemMouseEnter(index) {
this.currentIndex = index;
},
2022-12-08 11:04:25 +00:00
onItemClick(event, processedItem) {
if (this.isSameMenuItem(event)) {
const command = this.getItemProp(processedItem, 'command');
2022-09-14 11:26:01 +00:00
2022-12-08 11:04:25 +00:00
command && command({ originalEvent: event, item: processedItem.item });
2022-09-06 12:03:37 +00:00
}
2022-12-08 11:04:25 +00:00
},
onListFocus(event) {
this.focused = true;
this.changeFocusedOptionIndex(0);
this.$emit('focus', event);
},
onListBlur(event) {
this.focused = false;
this.focusedOptionIndex = -1;
this.$emit('blur', event);
},
onListKeyDown(event) {
switch (event.code) {
case 'ArrowDown': {
if (this.position === 'left' || this.position === 'right') this.onArrowDownKey();
event.preventDefault();
break;
}
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'ArrowUp': {
if (this.position === 'left' || this.position === 'right') this.onArrowUpKey();
event.preventDefault();
break;
}
case 'ArrowRight': {
if (this.position === 'top' || this.position === 'bottom') this.onArrowDownKey();
event.preventDefault();
break;
}
case 'ArrowLeft': {
if (this.position === 'top' || this.position === 'bottom') this.onArrowUpKey();
event.preventDefault();
break;
}
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Home': {
this.onHomeKey();
event.preventDefault();
break;
}
case 'End': {
this.onEndKey();
event.preventDefault();
break;
}
case 'Enter':
case 'NumpadEnter':
2022-12-08 11:04:25 +00:00
case 'Space': {
this.onSpaceKey(event);
event.preventDefault();
break;
}
default:
break;
2022-09-06 12:03:37 +00:00
}
},
2022-12-08 11:04:25 +00:00
onArrowDownKey() {
const optionIndex = this.findNextOptionIndex(this.focusedOptionIndex);
this.changeFocusedOptionIndex(optionIndex);
},
onArrowUpKey() {
const optionIndex = this.findPrevOptionIndex(this.focusedOptionIndex);
this.changeFocusedOptionIndex(optionIndex);
},
onHomeKey() {
this.changeFocusedOptionIndex(0);
},
onEndKey() {
2023-05-30 12:56:18 +00:00
this.changeFocusedOptionIndex(DomHandler.find(this.$refs.list, 'li[data-pc-section="menuitem"][data-p-disabled="false"]').length - 1);
2022-12-08 11:04:25 +00:00
},
onSpaceKey() {
const element = DomHandler.findSingle(this.$refs.list, `li[id="${`${this.focusedOptionIndex}`}"]`);
2023-05-30 12:56:18 +00:00
const anchorElement = element && DomHandler.findSingle(element, '[data-pc-section="action"]');
2022-12-08 11:04:25 +00:00
anchorElement ? anchorElement.click() : element && element.click();
},
findNextOptionIndex(index) {
2023-05-30 12:56:18 +00:00
const menuitems = DomHandler.find(this.$refs.list, 'li[data-pc-section="menuitem"][data-p-disabled="false"]');
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...menuitems].findIndex((link) => link.id === index);
return matchedOptionIndex > -1 ? matchedOptionIndex + 1 : 0;
},
findPrevOptionIndex(index) {
2023-05-30 12:56:18 +00:00
const menuitems = DomHandler.find(this.$refs.list, 'li[data-pc-section="menuitem"][data-p-disabled="false"]');
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...menuitems].findIndex((link) => link.id === index);
return matchedOptionIndex > -1 ? matchedOptionIndex - 1 : 0;
},
changeFocusedOptionIndex(index) {
2023-05-30 12:56:18 +00:00
const menuitems = DomHandler.find(this.$refs.list, 'li[data-pc-section="menuitem"][data-p-disabled="false"]');
2022-12-08 11:04:25 +00:00
let order = index >= menuitems.length ? menuitems.length - 1 : index < 0 ? 0 : index;
this.focusedOptionIndex = menuitems[order].getAttribute('id');
},
2022-09-06 12:03:37 +00:00
disabled(item) {
2022-09-14 11:26:01 +00:00
return typeof item.disabled === 'function' ? item.disabled() : item.disabled;
2023-08-30 06:40:37 +00:00
},
getMenuItemProps(item, index) {
return {
action: mergeProps(
{
tabindex: -1,
'aria-hidden': true,
class: this.cx('action')
},
this.getPTOptions('action', item, index)
),
icon: mergeProps(
{
class: [this.cx('icon'), item.icon]
},
this.getPTOptions('icon', item, index)
)
};
2022-09-06 12:03:37 +00:00
}
},
2022-12-08 11:04:25 +00:00
computed: {
focusedOptionId() {
return this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : null;
}
},
2022-09-06 12:03:37 +00:00
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple,
tooltip: Tooltip
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>