primevue-mirror/components/lib/speeddial/SpeedDial.vue

468 lines
17 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:31 +00:00
<div :ref="containerRef" :class="containerClass" :style="[style, sx('root')]" v-bind="ptmi('root')">
2024-04-09 11:24:04 +00:00
<slot name="button" :visible="d_visible" :onClick="onClick" :toggleCallback="onClick">
2024-03-22 15:54:43 +00:00
<Button
2024-05-06 15:23:01 +00:00
:class="[cx('pcButton'), buttonClass]"
2022-12-08 11:04:25 +00:00
:disabled="disabled"
:aria-expanded="d_visible"
:aria-haspopup="true"
:aria-controls="id + '_list'"
:aria-label="ariaLabel"
:aria-labelledby="ariaLabelledby"
:unstyled="unstyled"
2024-04-09 11:24:04 +00:00
@click="onClick($event)"
@keydown="onTogglerKeydown"
v-bind="buttonProps"
2024-05-06 15:23:01 +00:00
:pt="ptm('pcButton')"
>
2024-04-09 11:24:04 +00:00
<template #icon="slotProps">
<slot name="icon" :visible="d_visible">
2024-05-06 15:23:01 +00:00
<component v-if="d_visible && !!hideIcon" :is="hideIcon ? 'span' : 'PlusIcon'" :class="[hideIcon, slotProps.class]" v-bind="ptm('pcButton')['icon']" data-pc-section="icon" />
<component v-else :is="showIcon ? 'span' : 'PlusIcon'" :class="[d_visible && !!hideIcon ? hideIcon : showIcon, slotProps.class]" v-bind="ptm('pcButton')['icon']" data-pc-section="icon" />
</slot>
</template>
2024-03-22 15:54:43 +00:00
</Button>
2022-09-06 12:03:37 +00:00
</slot>
2024-05-09 09:17:58 +00:00
<ul :ref="listRef" :id="id + '_list'" :class="cx('list')" :style="sx('list')" role="menu" tabindex="-1" @focus="onFocus" @blur="onBlur" @keydown="onKeyDown" v-bind="ptm('menu')">
2022-12-08 11:04:25 +00:00
<template v-for="(item, index) of model" :key="index">
2024-04-09 11:24:04 +00:00
<li
v-if="isItemVisible(item)"
:id="`${id}_${index}`"
:aria-controls="`${id}_item`"
2024-05-06 13:10:13 +00:00
:class="cx('item', { id: `${id}_${index}` })"
2024-04-09 11:24:04 +00:00
:style="getItemStyle(index)"
role="none"
:data-p-active="isItemActive(`${id}_${index}`)"
2024-05-06 13:10:13 +00:00
v-bind="getPTOptions(`${id}_${index}`, 'item')"
2024-04-09 11:24:04 +00:00
>
2022-12-08 11:04:25 +00:00
<template v-if="!$slots.item">
2024-04-09 11:24:04 +00:00
<Button
v-tooltip:[tooltipOptions]="{ value: item.label, disabled: !tooltipOptions }"
2022-12-08 11:04:25 +00:00
:tabindex="-1"
role="menuitem"
2024-05-06 15:23:01 +00:00
:class="cx('pcAction', { item })"
2022-12-08 11:04:25 +00:00
:aria-label="item.label"
2024-04-09 11:24:04 +00:00
:disabled="disabled"
:unstyled="unstyled"
@click="onItemClick($event, item)"
v-bind="actionButtonProps"
2024-05-06 15:23:01 +00:00
:pt="getPTOptions(`${id}_${index}`, 'pcAction')"
2022-12-08 11:04:25 +00:00
>
2024-04-09 11:24:04 +00:00
<template v-if="item.icon" #icon="slotProps">
2024-05-06 15:23:01 +00:00
<span :class="[item.icon, slotProps.class]" v-bind="getPTOptions(`${id}_${index}`, 'actionIcon')"></span>
2024-04-09 11:24:04 +00:00
</template>
</Button>
2022-12-08 11:04:25 +00:00
</template>
2024-04-09 11:24:04 +00:00
<component v-else :is="$slots.item" :item="item" :onClick="(event) => onItemClick(event, item)" :toggleCallback="(event) => onItemClick(event, item)"></component>
2022-12-08 11:04:25 +00:00
</li>
</template>
2022-09-06 12:03:37 +00:00
</ul>
</div>
<template v-if="mask">
2023-05-30 14:55:13 +00:00
<div :class="[cx('mask'), maskClass]" :style="maskStyle" v-bind="ptm('mask')"></div>
2022-09-06 12:03:37 +00:00
</template>
</template>
<script>
import Button from 'primevue/button';
import PlusIcon from 'primevue/icons/plus';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
import { $dt } from 'primevue/themes';
2022-09-06 12:03:37 +00:00
import Tooltip from 'primevue/tooltip';
2022-12-08 11:04:25 +00:00
import { DomHandler, UniqueComponentId } from 'primevue/utils';
2023-05-30 14:55:13 +00:00
import BaseSpeedDial from './BaseSpeedDial.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'SpeedDial',
2023-05-30 14:55:13 +00:00
extends: BaseSpeedDial,
2024-02-11 23:48:31 +00:00
inheritAttrs: false,
2022-12-08 11:04:25 +00:00
emits: ['click', 'show', 'hide', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
documentClickListener: null,
container: null,
list: null,
data() {
return {
id: this.$attrs.id,
2022-09-06 12:03:37 +00:00
d_visible: this.visible,
2022-12-08 11:04:25 +00:00
isItemClicked: false,
focused: false,
focusedOptionIndex: -1
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
2024-04-09 11:24:04 +00:00
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
},
2022-09-06 12:03:37 +00:00
visible(newValue) {
this.d_visible = newValue;
}
},
mounted() {
2024-04-09 11:24:04 +00:00
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
if (this.type !== 'linear') {
2024-05-13 09:19:03 +00:00
const button = DomHandler.findSingle(this.container, '[data-pc-name="pcbutton"]');
2024-05-06 13:10:13 +00:00
const firstItem = DomHandler.findSingle(this.list, '[data-pc-section="item"]');
2022-09-06 12:03:37 +00:00
if (button && firstItem) {
const wDiff = Math.abs(button.offsetWidth - firstItem.offsetWidth);
const hDiff = Math.abs(button.offsetHeight - firstItem.offsetHeight);
2022-09-14 11:26:01 +00:00
this.list.style.setProperty($dt('item.diff.x').name, `${wDiff / 2}px`);
this.list.style.setProperty($dt('item.diff.y').name, `${hDiff / 2}px`);
2022-09-06 12:03:37 +00:00
}
}
if (this.hideOnClickOutside) {
this.bindDocumentClickListener();
}
},
2024-01-23 18:06:31 +00:00
beforeUnmount() {
2022-09-06 12:03:37 +00:00
this.unbindDocumentClickListener();
},
methods: {
2023-04-28 09:45:36 +00:00
getPTOptions(id, key) {
return this.ptm(key, {
context: {
2023-07-27 14:32:30 +00:00
active: this.isItemActive(id),
hidden: !this.d_visible
2023-04-28 09:45:36 +00:00
}
});
},
2022-12-08 11:04:25 +00:00
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event) {
this.focusedOptionIndex = -1;
this.$emit('blur', event);
},
2022-09-06 12:03:37 +00:00
onItemClick(e, item) {
if (item.command) {
item.command({ originalEvent: e, item });
}
this.hide();
this.isItemClicked = true;
e.preventDefault();
},
onClick(event) {
this.d_visible ? this.hide() : this.show();
this.isItemClicked = true;
this.$emit('click', event);
},
show() {
this.d_visible = true;
this.$emit('show');
},
hide() {
this.d_visible = false;
this.$emit('hide');
},
calculateTransitionDelay(index) {
const length = this.model.length;
const visible = this.d_visible;
return (visible ? index : length - index - 1) * this.transitionDelay;
},
2022-12-08 11:04:25 +00:00
onTogglerKeydown(event) {
switch (event.code) {
case 'ArrowDown':
case 'ArrowLeft':
this.onTogglerArrowDown(event);
break;
case 'ArrowUp':
case 'ArrowRight':
this.onTogglerArrowUp(event);
break;
case 'Escape':
this.onEscapeKey();
break;
default:
break;
}
},
onKeyDown(event) {
switch (event.code) {
case 'ArrowDown':
this.onArrowDown(event);
break;
case 'ArrowUp':
this.onArrowUp(event);
break;
case 'ArrowLeft':
this.onArrowLeft(event);
break;
case 'ArrowRight':
this.onArrowRight(event);
break;
case 'Enter':
case 'NumpadEnter':
2022-12-08 11:04:25 +00:00
case 'Space':
this.onEnterKey(event);
break;
case 'Escape':
this.onEscapeKey(event);
break;
case 'Home':
this.onHomeKey(event);
break;
case 'End':
this.onEndKey(event);
break;
default:
break;
}
},
onTogglerArrowUp(event) {
this.show();
this.navigatePrevItem(event);
event.preventDefault();
},
onTogglerArrowDown(event) {
this.show();
this.navigateNextItem(event);
event.preventDefault();
},
onEnterKey(event) {
2024-05-06 13:10:13 +00:00
const items = DomHandler.find(this.container, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const itemIndex = [...items].findIndex((item) => item.id === this.focusedOptionIndex);
2024-04-09 11:24:04 +00:00
const buttonEl = DomHandler.findSingle(this.container, 'button');
2022-12-08 11:04:25 +00:00
this.onItemClick(event, this.model[itemIndex]);
this.onBlur(event);
buttonEl && DomHandler.focus(buttonEl);
},
onEscapeKey() {
this.hide();
const buttonEl = DomHandler.findSingle(this.container, 'button');
buttonEl && DomHandler.focus(buttonEl);
},
onArrowUp(event) {
2024-04-09 11:24:04 +00:00
if (this.direction === 'down') {
2022-12-08 11:04:25 +00:00
this.navigatePrevItem(event);
} else {
this.navigateNextItem(event);
}
},
onArrowDown(event) {
2024-04-09 11:24:04 +00:00
if (this.direction === 'down') {
2022-12-08 11:04:25 +00:00
this.navigateNextItem(event);
} else {
this.navigatePrevItem(event);
}
},
onArrowLeft(event) {
const leftValidDirections = ['left', 'up-right', 'down-left'];
const rightValidDirections = ['right', 'up-left', 'down-right'];
if (leftValidDirections.includes(this.direction)) {
this.navigateNextItem(event);
} else if (rightValidDirections.includes(this.direction)) {
this.navigatePrevItem(event);
} else {
this.navigatePrevItem(event);
}
},
onArrowRight(event) {
const leftValidDirections = ['left', 'up-right', 'down-left'];
const rightValidDirections = ['right', 'up-left', 'down-right'];
if (leftValidDirections.includes(this.direction)) {
this.navigatePrevItem(event);
} else if (rightValidDirections.includes(this.direction)) {
this.navigateNextItem(event);
} else {
this.navigateNextItem(event);
}
},
onEndKey(event) {
event.preventDefault();
this.focusedOptionIndex = -1;
this.navigatePrevItem(event);
},
onHomeKey(event) {
event.preventDefault();
this.focusedOptionIndex = -1;
this.navigateNextItem(event);
},
navigateNextItem(event) {
const optionIndex = this.findNextOptionIndex(this.focusedOptionIndex);
this.changeFocusedOptionIndex(optionIndex);
event.preventDefault();
},
navigatePrevItem(event) {
const optionIndex = this.findPrevOptionIndex(this.focusedOptionIndex);
this.changeFocusedOptionIndex(optionIndex);
event.preventDefault();
},
changeFocusedOptionIndex(index) {
2024-05-06 13:10:13 +00:00
const items = DomHandler.find(this.container, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const filteredItems = [...items].filter((item) => !DomHandler.hasClass(DomHandler.findSingle(item, 'a'), 'p-disabled'));
if (filteredItems[index]) {
this.focusedOptionIndex = filteredItems[index].getAttribute('id');
2024-04-09 11:24:04 +00:00
const buttonEl = DomHandler.findSingle(filteredItems[index], '[type="button"]');
buttonEl && DomHandler.focus(buttonEl);
2022-12-08 11:04:25 +00:00
}
},
findPrevOptionIndex(index) {
2024-05-06 13:10:13 +00:00
const items = DomHandler.find(this.container, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const filteredItems = [...items].filter((item) => !DomHandler.hasClass(DomHandler.findSingle(item, 'a'), 'p-disabled'));
const newIndex = index === -1 ? filteredItems[filteredItems.length - 1].id : index;
let matchedOptionIndex = filteredItems.findIndex((link) => link.getAttribute('id') === newIndex);
matchedOptionIndex = index === -1 ? filteredItems.length - 1 : matchedOptionIndex - 1;
return matchedOptionIndex;
},
findNextOptionIndex(index) {
2024-05-06 13:10:13 +00:00
const items = DomHandler.find(this.container, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const filteredItems = [...items].filter((item) => !DomHandler.hasClass(DomHandler.findSingle(item, 'a'), 'p-disabled'));
const newIndex = index === -1 ? filteredItems[0].id : index;
let matchedOptionIndex = filteredItems.findIndex((link) => link.getAttribute('id') === newIndex);
matchedOptionIndex = index === -1 ? 0 : matchedOptionIndex + 1;
return matchedOptionIndex;
},
2022-09-06 12:03:37 +00:00
calculatePointStyle(index) {
const type = this.type;
if (type !== 'linear') {
const length = this.model.length;
2022-09-14 11:26:01 +00:00
const radius = this.radius || length * 20;
2022-09-06 12:03:37 +00:00
if (type === 'circle') {
2022-09-14 11:26:01 +00:00
const step = (2 * Math.PI) / length;
2022-09-06 12:03:37 +00:00
return {
2024-05-13 09:19:03 +00:00
left: `calc(${radius * Math.cos(step * index)}px + ${$dt('item.diff.x', '0px').variable})`,
top: `calc(${radius * Math.sin(step * index)}px + ${$dt('item.diff.y', '0px').variable})`
2022-09-14 11:26:01 +00:00
};
} else if (type === 'semi-circle') {
2022-09-06 12:03:37 +00:00
const direction = this.direction;
const step = Math.PI / (length - 1);
2024-05-13 09:19:03 +00:00
const x = `calc(${radius * Math.cos(step * index)}px + ${$dt('item.diff.x', '0px').variable})`;
const y = `calc(${radius * Math.sin(step * index)}px + ${$dt('item.diff.y', '0px').variable})`;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (direction === 'up') {
return { left: x, bottom: y };
2022-09-14 11:26:01 +00:00
} else if (direction === 'down') {
2022-09-06 12:03:37 +00:00
return { left: x, top: y };
2022-09-14 11:26:01 +00:00
} else if (direction === 'left') {
2022-09-06 12:03:37 +00:00
return { right: y, top: x };
2022-09-14 11:26:01 +00:00
} else if (direction === 'right') {
2022-09-06 12:03:37 +00:00
return { left: y, top: x };
}
2022-09-14 11:26:01 +00:00
} else if (type === 'quarter-circle') {
2022-09-06 12:03:37 +00:00
const direction = this.direction;
const step = Math.PI / (2 * (length - 1));
2024-05-13 09:19:03 +00:00
const x = `calc(${radius * Math.cos(step * index)}px + ${$dt('item.diff.x', '0px').variable})`;
const y = `calc(${radius * Math.sin(step * index)}px + ${$dt('item.diff.y', '0px').variable})`;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (direction === 'up-left') {
return { right: x, bottom: y };
2022-09-14 11:26:01 +00:00
} else if (direction === 'up-right') {
2022-09-06 12:03:37 +00:00
return { left: x, bottom: y };
2022-09-14 11:26:01 +00:00
} else if (direction === 'down-left') {
2022-09-06 12:03:37 +00:00
return { right: y, top: x };
2022-09-14 11:26:01 +00:00
} else if (direction === 'down-right') {
2022-09-06 12:03:37 +00:00
return { left: y, top: x };
}
}
}
return {};
},
getItemStyle(index) {
const transitionDelay = this.calculateTransitionDelay(index);
const pointStyle = this.calculatePointStyle(index);
return {
transitionDelay: `${transitionDelay}ms`,
...pointStyle
};
},
bindDocumentClickListener() {
if (!this.documentClickListener) {
this.documentClickListener = (event) => {
if (this.d_visible && this.isOutsideClicked(event)) {
this.hide();
}
this.isItemClicked = false;
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
document.addEventListener('click', this.documentClickListener);
}
},
unbindDocumentClickListener() {
if (this.documentClickListener) {
document.removeEventListener('click', this.documentClickListener);
this.documentClickListener = null;
}
},
isOutsideClicked(event) {
return this.container && !(this.container.isSameNode(event.target) || this.container.contains(event.target) || this.isItemClicked);
},
2022-12-08 11:04:25 +00:00
isItemVisible(item) {
return typeof item.visible === 'function' ? item.visible() : item.visible !== false;
},
2023-04-28 09:45:36 +00:00
isItemActive(id) {
return id === this.focusedOptionId;
},
2022-09-06 12:03:37 +00:00
containerRef(el) {
this.container = el;
},
listRef(el) {
this.list = el;
}
},
computed: {
containerClass() {
2023-05-30 14:55:13 +00:00
return [this.cx('root'), this.class];
2022-12-08 11:04:25 +00:00
},
focusedOptionId() {
return this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : null;
2022-09-06 12:03:37 +00:00
}
},
components: {
2024-03-22 15:54:43 +00:00
Button,
PlusIcon
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>