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

323 lines
12 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:09 +00:00
<div :class="cx('root')" v-bind="ptmi('root')">
2023-06-02 09:10:49 +00:00
<div :class="cx('controls')" v-bind="ptm('controls')">
2022-09-06 12:03:37 +00:00
<slot name="controlsstart"></slot>
2024-05-06 15:23:01 +00:00
<Button @click="moveUp" :aria-label="moveUpAriaLabel" :disabled="moveDisabled()" v-bind="{ ...buttonProps, ...moveUpButtonProps }" :pt="ptm('pcMoveUpButton')" :unstyled="unstyled">
<template #icon>
<slot name="moveupicon">
2024-05-06 15:23:01 +00:00
<AngleUpIcon v-bind="ptm('pcMoveUpButton')['icon']" data-pc-section="moveupicon" />
</slot>
</template>
2024-03-21 13:20:49 +00:00
</Button>
2024-05-06 15:23:01 +00:00
<Button @click="moveTop" :aria-label="moveTopAriaLabel" :disabled="moveDisabled()" v-bind="{ ...buttonProps, ...moveTopButtonProps }" :pt="ptm('pcMoveTopButton')" :unstyled="unstyled">
2023-05-09 16:03:01 +00:00
<template #icon>
<slot name="movetopicon">
2024-05-06 15:23:01 +00:00
<AngleDoubleUpIcon v-bind="ptm('pcMoveTopButton')['icon']" data-pc-section="movetopicon" />
</slot>
</template>
2024-03-21 13:20:49 +00:00
</Button>
2024-05-06 15:23:01 +00:00
<Button @click="moveDown" :aria-label="moveDownAriaLabel" :disabled="moveDisabled()" v-bind="{ ...buttonProps, ...moveDownButtonProps }" :pt="ptm('pcMoveDownButton')" :unstyled="unstyled">
<template #icon>
<slot name="movedownicon">
2024-05-06 15:23:01 +00:00
<AngleDownIcon v-bind="ptm('pcMoveDownButton')['icon']" data-pc-section="movedownicon" />
</slot>
</template>
2024-03-21 13:20:49 +00:00
</Button>
2024-05-06 15:23:01 +00:00
<Button @click="moveBottom" :aria-label="moveBottomAriaLabel" :disabled="moveDisabled()" v-bind="{ ...buttonProps, ...moveBottomButtonProps }" :pt="ptm('pcMoveBottomButton')" :unstyled="unstyled">
<template #icon>
<slot name="movebottomicon">
2024-05-06 15:23:01 +00:00
<AngleDoubleDownIcon v-bind="ptm('pcMoveBottomButton')['icon']" data-pc-section="movebottomicon" />
</slot>
</template>
2024-03-21 13:20:49 +00:00
</Button>
2022-09-06 12:03:37 +00:00
<slot name="controlsend"></slot>
</div>
2024-03-21 13:20:49 +00:00
<Listbox
ref="listbox"
:id="id"
:modelValue="d_selection"
:options="modelValue"
multiple
:metaKeySelection="metaKeySelection"
:listStyle="listStyle"
2024-03-26 14:21:39 +00:00
:scrollHeight="scrollHeight"
2024-03-21 13:20:49 +00:00
:tabindex="tabindex"
:dataKey="dataKey"
:autoOptionFocus="autoOptionFocus"
:focusOnHover="focusOnHover"
2024-05-27 06:39:11 +00:00
:striped="striped"
2024-03-21 13:52:26 +00:00
:disabled="disabled"
2024-03-21 13:20:49 +00:00
:ariaLabel="ariaLabel"
:ariaLabelledby="ariaLabelledby"
2024-05-06 15:23:01 +00:00
:pt="ptm('pcList')"
2024-03-21 13:20:49 +00:00
:unstyled="unstyled"
@focus="onListFocus"
@blur="onListBlur"
@change="onChangeSelection"
>
<template v-if="$slots.header" #header>
<slot name="header"></slot>
2024-03-21 13:20:49 +00:00
</template>
<template #option="{ option, selected, index }">
<slot name="item" :item="option" :selected="selected" :index="index" />
2024-03-21 13:20:49 +00:00
</template>
</Listbox>
2022-09-06 12:03:37 +00:00
</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';
2024-03-21 13:20:49 +00:00
import Listbox from 'primevue/listbox';
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,
2024-02-11 23:48:09 +00:00
inheritAttrs: false,
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,
2024-03-21 13:20:49 +00:00
d_selection: this.selection
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
2024-04-16 09:35:02 +00:00
'$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() {
2024-04-16 09:35:02 +00:00
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
if (this.responsive) {
this.createStyle();
}
},
methods: {
2024-03-21 13:20:49 +00:00
updateSelection(event) {
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
2023-05-09 16:03:01 +00:00
});
},
2024-03-21 13:20:49 +00:00
onChangeSelection(params) {
this.d_selection = params.value;
this.updateSelection(params.event);
2022-09-06 12:03:37 +00:00
},
2022-12-08 11:04:25 +00:00
onListFocus(event) {
this.$emit('focus', event);
2022-12-08 11:04:25 +00:00
},
onListBlur(event) {
this.$emit('blur', event);
},
2024-03-21 13:20:49 +00:00
onReorderUpdate(event, value) {
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection
});
2022-12-08 11:04:25 +00:00
},
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';
2024-03-21 13:20:49 +00:00
this.onReorderUpdate(event, value);
2022-09-06 12:03:37 +00:00
}
},
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';
2024-03-21 13:20:49 +00:00
this.onReorderUpdate(event, value);
2022-09-06 12:03:37 +00:00
}
},
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';
2024-03-21 13:20:49 +00:00
this.onReorderUpdate(event, value);
2022-09-06 12:03:37 +00:00
}
},
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';
2024-03-21 13:20:49 +00:00
this.onReorderUpdate(event, value);
2022-09-06 12:03:37 +00:00
}
},
updateListScroll() {
2024-03-21 13:20:49 +00:00
this.list = DomHandler.findSingle(this.$refs.listbox.$el, '[data-pc-section="list"]');
const listItems = DomHandler.find(this.list, '[data-pc-section="item"][data-p-selected="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';
DomHandler.setAttribute(this.styleElement, 'nonce', this.$primevue?.config?.csp?.nonce);
2022-09-14 11:26:01 +00:00
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 {
flex-direction: row;
}
}
`;
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() {
2024-03-21 13:52:26 +00:00
return this.disabled ? true : !this.d_selection || !this.d_selection.length ? true : false;
2022-09-06 12:03:37 +00:00
}
},
computed: {
attributeSelector() {
return UniqueComponentId();
2022-12-08 11:04:25 +00:00
},
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;
},
hasSelectedOption() {
return ObjectUtils.isNotEmpty(this.d_selection);
2022-09-06 12:03:37 +00:00
}
},
components: {
2024-03-21 13:20:49 +00:00
Listbox,
Button,
AngleUpIcon,
AngleDownIcon,
AngleDoubleUpIcon,
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>