primevue-mirror/components/lib/orderlist/OrderList.vue

559 lines
21 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-06-02 09:10:49 +00:00
<div :class="cx('root')" v-bind="ptm('root')">
<div :class="cx('controls')" v-bind="ptm('controls')">
2022-09-06 12:03:37 +00:00
<slot name="controlsstart"></slot>
<OLButton type="button" @click="moveUp" :aria-label="moveUpAriaLabel" :disabled="moveDisabled()" :pt="ptm('moveUpButton')" v-bind="moveUpButtonProps" :unstyled="unstyled">
<template #icon>
<slot name="moveupicon">
2023-05-09 16:03:01 +00:00
<AngleUpIcon v-bind="ptm('moveUpButton')['icon']" />
</slot>
</template>
</OLButton>
<OLButton type="button" @click="moveTop" :aria-label="moveTopAriaLabel" :disabled="moveDisabled()" :pt="ptm('moveTopButton')" v-bind="ptm('moveTopButton')" :unstyled="unstyled">
2023-05-09 16:03:01 +00:00
<template #icon>
<slot name="movetopicon">
<AngleDoubleUpIcon v-bind="ptm('moveTopButton')['icon']" />
</slot>
</template>
</OLButton>
<OLButton type="button" @click="moveDown" :aria-label="moveDownAriaLabel" :disabled="moveDisabled()" :pt="ptm('moveDownButton')" v-bind="moveDownButtonProps" :unstyled="unstyled">
<template #icon>
<slot name="movedownicon">
2023-05-09 16:03:01 +00:00
<AngleDownIcon v-bind="ptm('moveDownButton')['icon']" />
</slot>
</template>
</OLButton>
<OLButton type="button" @click="moveBottom" :aria-label="moveBottomAriaLabel" :disabled="moveDisabled()" :pt="ptm('moveBottomButton')" v-bind="moveBottomButtonProps" :unstyled="unstyled">
<template #icon>
<slot name="movebottomicon">
2023-05-09 16:03:01 +00:00
<AngleDoubleDownIcon v-bind="ptm('moveBottomButton')['icon']" />
</slot>
</template>
</OLButton>
2022-09-06 12:03:37 +00:00
<slot name="controlsend"></slot>
</div>
2023-06-02 09:10:49 +00:00
<div :class="cx('container')" v-bind="ptm('container')">
<div v-if="$slots.header" :class="cx('header')" v-bind="ptm('header')">
2022-09-06 12:03:37 +00:00
<slot name="header"></slot>
</div>
2022-12-08 11:04:25 +00:00
<transition-group
:ref="listRef"
:id="id + '_list'"
name="p-orderlist-flip"
tag="ul"
2023-06-02 09:10:49 +00:00
:class="cx('list')"
2022-12-08 11:04:25 +00:00
:style="listStyle"
role="listbox"
aria-multiselectable="true"
:tabindex="tabindex"
:aria-activedescendant="focused ? focusedOptionId : undefined"
:aria-label="ariaLabel"
:aria-labelledby="ariaLabelledby"
@focus="onListFocus"
@blur="onListBlur"
@keydown="onListKeyDown"
2023-05-09 16:03:01 +00:00
v-bind="{ ...listProps, ...ptm('list') }"
2022-12-08 11:04:25 +00:00
>
2022-09-06 12:03:37 +00:00
<template v-for="(item, i) of modelValue" :key="getItemKey(item, i)">
2023-05-09 16:03:01 +00:00
<li
:id="id + '_' + i"
v-ripple
role="option"
2023-06-06 13:20:55 +00:00
:class="cx('item', { item, id: `${id}_${i}` })"
2023-05-09 16:03:01 +00:00
@click="onItemClick($event, item, i)"
@touchend="onItemTouchEnd"
:aria-selected="isSelected(item)"
@mousedown="onOptionMouseDown(i)"
v-bind="getPTOptions(item, 'item', i)"
2023-06-06 13:20:55 +00:00
:data-p-highlight="isSelected(item)"
:data-p-focused="`${id}_${i}` === focusedOptionId"
2023-05-09 16:03:01 +00:00
>
2022-09-06 12:03:37 +00:00
<slot name="item" :item="item" :index="i"> </slot>
</li>
</template>
</transition-group>
</div>
</div>
</template>
<script>
import Button from 'primevue/button';
import AngleDoubleDownIcon from 'primevue/icons/angledoubledown';
import AngleDoubleUpIcon from 'primevue/icons/angledoubleup';
import AngleDownIcon from 'primevue/icons/angledown';
import AngleUpIcon from 'primevue/icons/angleup';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils, UniqueComponentId } from 'primevue/utils';
2023-06-05 10:18:57 +00:00
import BaseOrderList from './BaseOrderList.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'OrderList',
2023-06-02 09:10:49 +00:00
extends: BaseOrderList,
2022-12-08 11:04:25 +00:00
emits: ['update:modelValue', 'reorder', 'update:selection', 'selection-change', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
itemTouched: false,
reorderDirection: null,
styleElement: null,
2022-12-08 11:04:25 +00:00
list: null,
2022-09-06 12:03:37 +00:00
data() {
return {
id: this.$attrs.id,
2022-12-08 11:04:25 +00:00
d_selection: this.selection,
focused: false,
focusedOptionIndex: -1
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();
}
},
2022-09-06 12:03:37 +00:00
beforeUnmount() {
this.destroyStyle();
},
updated() {
if (this.reorderDirection) {
this.updateListScroll();
this.reorderDirection = null;
}
},
mounted() {
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
if (this.responsive) {
this.createStyle();
}
},
methods: {
getItemKey(item, index) {
2022-09-14 11:26:01 +00:00
return this.dataKey ? ObjectUtils.resolveFieldData(item, this.dataKey) : index;
2022-09-06 12:03:37 +00:00
},
getPTOptions(item, key, index) {
2023-05-09 16:03:01 +00:00
return this.ptm(key, {
context: {
active: this.isSelected(item),
focused: `${this.id}_${index}` === this.focusedOptionId
2023-05-09 16:03:01 +00:00
}
});
},
2022-09-06 12:03:37 +00:00
isSelected(item) {
return ObjectUtils.findIndexInList(item, this.d_selection) != -1;
},
2022-12-08 11:04:25 +00:00
onListFocus(event) {
2023-06-02 09:10:49 +00:00
const selectedFirstItem = DomHandler.findSingle(this.list, '[data-p-highlight="true"]');
const findIndex = ObjectUtils.findIndexInList(selectedFirstItem, this.list.children);
2022-12-08 11:04:25 +00:00
this.focused = true;
const index = this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : selectedFirstItem ? findIndex : -1;
2022-12-08 11:04:25 +00:00
this.changeFocusedOptionIndex(index);
this.$emit('focus', event);
},
onListBlur(event) {
this.focused = false;
this.focusedOptionIndex = -1;
this.$emit('blur', event);
},
onListKeyDown(event) {
switch (event.code) {
case 'ArrowDown':
this.onArrowDownKey(event);
break;
case 'ArrowUp':
this.onArrowUpKey(event);
break;
case 'Home':
this.onHomeKey(event);
break;
case 'End':
this.onEndKey(event);
break;
case 'Enter':
this.onEnterKey(event);
break;
case 'Space':
this.onSpaceKey(event);
break;
case 'KeyA':
if (event.ctrlKey) {
this.d_selection = [...this.modelValue];
this.$emit('update:selection', this.d_selection);
}
default:
break;
}
},
onOptionMouseDown(index) {
this.focused = true;
this.focusedOptionIndex = index;
},
2022-12-08 11:04:25 +00:00
onArrowDownKey(event) {
const optionIndex = this.findNextOptionIndex(this.focusedOptionIndex);
this.changeFocusedOptionIndex(optionIndex);
if (event.shiftKey) {
this.onEnterKey(event);
}
event.preventDefault();
},
onArrowUpKey(event) {
const optionIndex = this.findPrevOptionIndex(this.focusedOptionIndex);
this.changeFocusedOptionIndex(optionIndex);
if (event.shiftKey) {
this.onEnterKey(event);
}
event.preventDefault();
},
onHomeKey(event) {
if (event.ctrlKey && event.shiftKey) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
const focusedItem = DomHandler.findSingle(this.list, `[data-pc-section="item"][id=${this.focusedOptionIndex}]`);
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...items].findIndex((item) => item === focusedItem);
this.d_selection = [...this.modelValue].slice(0, matchedOptionIndex + 1);
this.$emit('update:selection', this.d_selection);
} else {
this.changeFocusedOptionIndex(0);
}
event.preventDefault();
},
onEndKey(event) {
if (event.ctrlKey && event.shiftKey) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
const focusedItem = DomHandler.findSingle(this.list, `[data-pc-section="item"][id=${this.focusedOptionIndex}]`);
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...items].findIndex((item) => item === focusedItem);
this.d_selection = [...this.modelValue].slice(matchedOptionIndex, items.length);
this.$emit('update:selection', this.d_selection);
} else {
2023-06-02 09:10:49 +00:00
this.changeFocusedOptionIndex(DomHandler.find(this.list, '[data-pc-section="item"]').length - 1);
2022-12-08 11:04:25 +00:00
}
event.preventDefault();
},
onEnterKey(event) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
const focusedItem = DomHandler.findSingle(this.list, `[data-pc-section="item"][id=${this.focusedOptionIndex}]`);
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...items].findIndex((item) => item === focusedItem);
this.onItemClick(event, this.modelValue[matchedOptionIndex], matchedOptionIndex);
event.preventDefault();
},
onSpaceKey(event) {
if (event.shiftKey) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const selectedItemIndex = ObjectUtils.findIndexInList(this.d_selection[0], [...this.modelValue]);
2023-06-02 09:10:49 +00:00
const focusedItem = DomHandler.findSingle(this.list, `[data-pc-section="item"][id=${this.focusedOptionIndex}]`);
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...items].findIndex((item) => item === focusedItem);
this.d_selection = [...this.modelValue].slice(Math.min(selectedItemIndex, matchedOptionIndex), Math.max(selectedItemIndex, matchedOptionIndex) + 1);
this.$emit('update:selection', this.d_selection);
} else {
this.onEnterKey(event);
}
},
findNextOptionIndex(index) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...items].findIndex((link) => link.id === index);
return matchedOptionIndex > -1 ? matchedOptionIndex + 1 : 0;
},
findPrevOptionIndex(index) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
const matchedOptionIndex = [...items].findIndex((link) => link.id === index);
return matchedOptionIndex > -1 ? matchedOptionIndex - 1 : 0;
},
changeFocusedOptionIndex(index) {
2023-06-02 09:10:49 +00:00
const items = DomHandler.find(this.list, '[data-pc-section="item"]');
2022-12-08 11:04:25 +00:00
let order = index >= items.length ? items.length - 1 : index < 0 ? 0 : index;
this.focusedOptionIndex = items[order] ? items[order].getAttribute('id') : -1;
this.scrollInView(this.focusedOptionIndex);
2022-12-08 11:04:25 +00:00
},
scrollInView(id) {
2023-06-02 09:10:49 +00:00
const element = DomHandler.findSingle(this.list, `[data-pc-section="item"][id="${id}"]`);
2022-12-08 11:04:25 +00:00
if (element) {
element.scrollIntoView && element.scrollIntoView({ block: 'nearest', inline: 'start' });
}
},
2022-09-06 12:03:37 +00:00
moveUp(event) {
if (this.d_selection) {
let value = [...this.modelValue];
for (let i = 0; i < this.d_selection.length; i++) {
let selectedItem = this.d_selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, value);
if (selectedItemIndex !== 0) {
let movedItem = value[selectedItemIndex];
let temp = value[selectedItemIndex - 1];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[selectedItemIndex - 1] = movedItem;
value[selectedItemIndex] = temp;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
this.reorderDirection = 'up';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection
});
}
},
moveTop(event) {
2022-09-14 11:26:01 +00:00
if (this.d_selection) {
2022-09-06 12:03:37 +00:00
let value = [...this.modelValue];
for (let i = 0; i < this.d_selection.length; i++) {
let selectedItem = this.d_selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, value);
if (selectedItemIndex !== 0) {
let movedItem = value.splice(selectedItemIndex, 1)[0];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value.unshift(movedItem);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
this.reorderDirection = 'top';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection
});
}
},
moveDown(event) {
2022-09-14 11:26:01 +00:00
if (this.d_selection) {
2022-09-06 12:03:37 +00:00
let value = [...this.modelValue];
for (let i = this.d_selection.length - 1; i >= 0; i--) {
let selectedItem = this.d_selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, value);
2022-09-14 11:26:01 +00:00
if (selectedItemIndex !== value.length - 1) {
2022-09-06 12:03:37 +00:00
let movedItem = value[selectedItemIndex];
let temp = value[selectedItemIndex + 1];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[selectedItemIndex + 1] = movedItem;
value[selectedItemIndex] = temp;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
this.reorderDirection = 'down';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection
});
}
},
moveBottom(event) {
if (this.d_selection) {
let value = [...this.modelValue];
for (let i = this.d_selection.length - 1; i >= 0; i--) {
let selectedItem = this.d_selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, value);
2022-09-14 11:26:01 +00:00
if (selectedItemIndex !== value.length - 1) {
2022-09-06 12:03:37 +00:00
let movedItem = value.splice(selectedItemIndex, 1)[0];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value.push(movedItem);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
this.reorderDirection = 'bottom';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection
});
}
},
onItemClick(event, item, index) {
this.itemTouched = false;
2022-12-08 11:04:25 +00:00
const selectedIndex = ObjectUtils.findIndexInList(item, this.d_selection);
const selected = selectedIndex != -1;
const metaSelection = this.itemTouched ? false : this.metaKeySelection;
2023-06-02 09:10:49 +00:00
const selectedId = DomHandler.find(this.list, '[data-pc-section="item"]')[index].getAttribute('id');
2022-12-08 11:04:25 +00:00
this.focusedOptionIndex = selectedId;
2022-09-06 12:03:37 +00:00
if (metaSelection) {
2022-12-08 11:04:25 +00:00
const metaKey = event.metaKey || event.ctrlKey;
2022-09-06 12:03:37 +00:00
if (selected && metaKey) {
this.d_selection = this.d_selection.filter((val, index) => index !== selectedIndex);
2022-09-14 11:26:01 +00:00
} else {
this.d_selection = metaKey ? (this.d_selection ? [...this.d_selection] : []) : [];
2022-09-06 12:03:37 +00:00
ObjectUtils.insertIntoOrderedArray(item, index, this.d_selection, this.modelValue);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (selected) {
this.d_selection = this.d_selection.filter((val, index) => index !== selectedIndex);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.d_selection = this.d_selection ? [...this.d_selection] : [];
ObjectUtils.insertIntoOrderedArray(item, index, this.d_selection, this.modelValue);
}
}
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
2022-09-14 11:26:01 +00:00
originalEvent: event,
2022-09-06 12:03:37 +00:00
value: this.d_selection
});
},
onItemTouchEnd() {
this.itemTouched = true;
},
findNextItem(item) {
let nextItem = item.nextElementSibling;
2023-06-06 13:20:55 +00:00
if (nextItem) return !(DomHandler.getAttribute(nextItem, 'data-pc-section') === 'item') ? this.findNextItem(nextItem) : nextItem;
2022-09-14 11:26:01 +00:00
else return null;
2022-09-06 12:03:37 +00:00
},
findPrevItem(item) {
let prevItem = item.previousElementSibling;
2023-06-06 13:20:55 +00:00
if (prevItem) return !(DomHandler.getAttribute(nextItem, 'data-pc-section') === 'item') ? this.findPrevItem(prevItem) : prevItem;
2022-09-14 11:26:01 +00:00
else return null;
2022-09-06 12:03:37 +00:00
},
updateListScroll() {
2023-06-02 09:10:49 +00:00
const listItems = DomHandler.find(this.list, '[data-pc-section="item"][data-p-highlight="true"]');
2022-09-06 12:03:37 +00:00
if (listItems && listItems.length) {
2022-09-14 11:26:01 +00:00
switch (this.reorderDirection) {
2022-09-06 12:03:37 +00:00
case 'up':
2022-12-08 11:04:25 +00:00
DomHandler.scrollInView(this.list, listItems[0]);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'top':
2022-12-08 11:04:25 +00:00
this.list.scrollTop = 0;
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'down':
2022-12-08 11:04:25 +00:00
DomHandler.scrollInView(this.list, listItems[listItems.length - 1]);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'bottom':
2022-12-08 11:04:25 +00:00
this.list.scrollTop = this.list.scrollHeight;
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
default:
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
}
}
},
createStyle() {
if (!this.styleElement && !this.isUnstyled) {
2022-09-06 12:03:37 +00:00
this.$el.setAttribute(this.attributeSelector, '');
2022-09-14 11:26:01 +00:00
this.styleElement = document.createElement('style');
this.styleElement.type = 'text/css';
document.head.appendChild(this.styleElement);
2022-09-06 12:03:37 +00:00
let innerHTML = `
@media screen and (max-width: ${this.breakpoint}) {
.p-orderlist[${this.attributeSelector}] {
flex-direction: column;
}
.p-orderlist[${this.attributeSelector}] .p-orderlist-controls {
padding: var(--content-padding);
flex-direction: row;
}
.p-orderlist[${this.attributeSelector}] .p-orderlist-controls .p-button {
margin-right: var(--inline-spacing);
margin-bottom: 0;
}
.p-orderlist[${this.attributeSelector}] .p-orderlist-controls .p-button:last-child {
margin-right: 0;
}
}
`;
this.styleElement.innerHTML = innerHTML;
2022-09-14 11:26:01 +00:00
}
},
2022-09-06 12:03:37 +00:00
destroyStyle() {
if (this.styleElement) {
document.head.removeChild(this.styleElement);
this.styleElement = null;
}
2022-12-08 11:04:25 +00:00
},
moveDisabled() {
if (!this.d_selection || !this.d_selection.length) {
return true;
}
},
listRef(el) {
this.list = el ? el.$el : undefined;
2022-09-06 12:03:37 +00:00
}
},
computed: {
attributeSelector() {
return UniqueComponentId();
2022-12-08 11:04:25 +00:00
},
focusedOptionId() {
return this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : null;
},
moveUpAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveUp : undefined;
},
moveTopAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveTop : undefined;
},
moveDownAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveDown : undefined;
},
moveBottomAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveBottom : undefined;
2022-09-06 12:03:37 +00:00
}
},
components: {
OLButton: Button,
AngleUpIcon: AngleUpIcon,
AngleDownIcon: AngleDownIcon,
AngleDoubleUpIcon: AngleDoubleUpIcon,
AngleDoubleDownIcon: AngleDoubleDownIcon
2022-09-06 12:03:37 +00:00
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
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>