primevue-mirror/components/lib/picklist/PickList.vue

969 lines
40 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-06-02 15:43:30 +00:00
<div :class="cx('root')" v-bind="ptm('root')">
2023-08-17 23:36:06 +00:00
<div v-if="showSourceControls" :class="cx('sourceControls')" v-bind="ptm('sourceControls')" data-pc-group-section="controls">
2022-09-06 12:03:37 +00:00
<slot name="sourcecontrolsstart"></slot>
<PLButton :aria-label="moveUpAriaLabel" :disabled="moveDisabled(0)" type="button" @click="moveUp($event, 0)" v-bind="moveUpButtonProps" :pt="ptm('sourceMoveUpButton')" :unstyled="unstyled" data-pc-section="sourcemoveupbutton">
<template #icon>
<slot name="moveupicon">
2023-09-06 07:13:47 +00:00
<AngleUpIcon v-bind="ptm('sourceMoveUpButton')['icon']" data-pc-section="moveupicon" />
</slot>
</template>
</PLButton>
<PLButton :aria-label="moveTopAriaLabel" :disabled="moveDisabled(0)" type="button" @click="moveTop($event, 0)" v-bind="moveTopButtonProps" :pt="ptm('sourceMoveTopButton')" :unstyled="unstyled" data-pc-section="sourcemovetopbutton">
<template #icon>
<slot name="movetopicon">
2023-09-06 07:13:47 +00:00
<AngleDoubleUpIcon v-bind="ptm('sourceMoveTopButton')['icon']" data-pc-section="movetopicon" />
</slot>
</template>
</PLButton>
<PLButton :aria-label="moveDownAriaLabel" :disabled="moveDisabled(0)" type="button" @click="moveDown($event, 0)" v-bind="moveDownButtonProps" :pt="ptm('sourceMoveDownButton')" :unstyled="unstyled" data-pc-section="sourcemovedownbutton">
<template #icon>
<slot name="movedownicon">
2023-09-06 07:13:47 +00:00
<AngleDownIcon v-bind="ptm('sourceMoveDownButton')['icon']" data-pc-section="movedownicon" />
</slot>
</template>
</PLButton>
<PLButton
:aria-label="moveBottomAriaLabel"
:disabled="moveDisabled(0)"
type="button"
@click="moveBottom($event, 0)"
v-bind="moveBottomButtonProps"
:pt="ptm('sourceMoveBottomButton')"
:unstyled="unstyled"
data-pc-section="sourcemovebottombutton"
>
<template #icon>
<slot name="movebottomicon">
2023-09-06 07:13:47 +00:00
<AngleDoubleDownIcon v-bind="ptm('sourceMoveBottomButton')['icon']" data-pc-section="movebottomicon" />
</slot>
</template>
</PLButton>
2022-09-06 12:03:37 +00:00
<slot name="sourcecontrolsend"></slot>
</div>
2023-08-17 23:36:06 +00:00
<div :class="cx('sourceWrapper')" v-bind="ptm('sourceWrapper')" data-pc-group-section="listwrapper">
<div v-if="$slots.sourceheader" :class="cx('sourceHeader')" v-bind="ptm('sourceHeader')" data-pc-group-section="header">
2022-09-06 12:03:37 +00:00
<slot name="sourceheader"></slot>
</div>
2022-12-08 11:04:25 +00:00
<transition-group
ref="sourceList"
:id="idSource + '_list'"
name="p-picklist-flip"
tag="ul"
2023-06-02 15:43:30 +00:00
:class="cx('sourceList')"
2022-12-08 11:04:25 +00:00
:style="listStyle"
role="listbox"
aria-multiselectable="true"
:aria-activedescendant="focused['sourceList'] ? focusedOptionId : undefined"
:tabindex="sourceList && sourceList.length > 0 ? tabindex : -1"
@focus="onListFocus($event, 'sourceList')"
@blur="onListBlur($event, 'sourceList')"
@keydown="onItemKeyDown($event, 'sourceList')"
2023-08-02 12:02:28 +00:00
v-bind="{ ...sourceListProps, ...ptm('sourceList'), ...ptm('transition') }"
2023-08-17 23:36:06 +00:00
data-pc-group-section="list"
2022-12-08 11:04:25 +00:00
>
2022-09-06 12:03:37 +00:00
<template v-for="(item, i) of sourceList" :key="getItemKey(item, i)">
2022-09-14 11:26:01 +00:00
<li
2022-12-08 11:04:25 +00:00
:id="idSource + '_' + i"
2022-09-14 11:26:01 +00:00
v-ripple
:class="cx('item', { item, id: `${idSource}_${i}`, listIndex: 0 })"
2022-12-08 11:04:25 +00:00
@click="onItemClick($event, item, i, 0)"
2022-09-14 11:26:01 +00:00
@dblclick="onItemDblClick($event, item, 0)"
@touchend="onItemTouchEnd"
@mousedown="onOptionMouseDown(i, 'sourceList')"
2022-09-14 11:26:01 +00:00
role="option"
:aria-selected="isSelected(item, 0)"
v-bind="getPTOptions(item, 'item', `${idSource}_${i}`, 0)"
2023-06-06 13:30:04 +00:00
:data-p-highlight="isSelected(item, 0)"
:data-p-focused="`${idSource}_${i}` === focusedOptionId"
2022-09-14 11:26:01 +00:00
>
2022-09-06 12:03:37 +00:00
<slot name="item" :item="item" :index="i"> </slot>
</li>
</template>
</transition-group>
</div>
2023-08-17 23:36:06 +00:00
<div :class="cx('buttons')" v-bind="ptm('buttons')" data-pc-group-section="controls">
2022-09-06 12:03:37 +00:00
<slot name="movecontrolsstart"></slot>
<PLButton :aria-label="moveToTargetAriaLabel" type="button" @click="moveToTarget" :disabled="moveDisabled(0)" v-bind="moveToTargetProps" :pt="ptm('moveToTargetButton')" :unstyled="unstyled" data-pc-section="movetotargetbuton">
<template #icon>
<slot name="movetotargeticon" :viewChanged="viewChanged">
2023-09-06 07:13:47 +00:00
<component :is="viewChanged ? 'AngleDownIcon' : 'AngleRightIcon'" v-bind="ptm('moveToTargetButton')['icon']" data-pc-section="movetotargeticon" />
</slot>
</template>
</PLButton>
<PLButton
:aria-label="moveAllToTargetAriaLabel"
type="button"
@click="moveAllToTarget"
:disabled="moveAllDisabled('sourceList')"
v-bind="moveAllToTargetProps"
:pt="ptm('moveAllToTargetButton')"
:unstyled="unstyled"
data-pc-section="movealltotargetbuton"
>
<template #icon>
<slot name="movealltotargeticon" :viewChanged="viewChanged">
2023-09-06 07:13:47 +00:00
<component :is="viewChanged ? 'AngleDoubleDownIcon' : 'AngleDoubleRightIcon'" v-bind="ptm('moveAllToTargetButton')['icon']" data-pc-section="movealltotargeticon" />
</slot>
</template>
</PLButton>
<PLButton :aria-label="moveToSourceAriaLabel" type="button" @click="moveToSource" :disabled="moveDisabled(1)" v-bind="moveToSourceProps" :pt="ptm('moveToSourceButton')" :unstyled="unstyled" data-pc-section="movetosourcebuton">
<template #icon>
<slot name="movetosourceicon" :viewChanged="viewChanged">
2023-09-06 07:13:47 +00:00
<component :is="viewChanged ? 'AngleUpIcon' : 'AngleLeftIcon'" v-bind="ptm('moveToSourceButton')['icon']" data-pc-section="movetosourceicon" />
</slot>
</template>
</PLButton>
<PLButton
:aria-label="moveAllToSourceAriaLabel"
type="button"
@click="moveAllToSource"
:disabled="moveAllDisabled('targetList')"
v-bind="moveAllToSourceProps"
:pt="ptm('moveAllToSourceButton')"
data-pc-section="movealltosourcebuton"
:unstyled="unstyled"
>
<template #icon>
<slot name="movealltosourceicon" :viewChanged="viewChanged">
2023-09-06 07:13:47 +00:00
<component :is="viewChanged ? 'AngleDoubleUpIcon' : 'AngleDoubleLeftIcon'" v-bind="ptm('moveAllToSourceButton')['icon']" data-pc-section="movealltosourceicon" />
</slot>
</template>
</PLButton>
2022-09-06 12:03:37 +00:00
<slot name="movecontrolsend"></slot>
</div>
2023-08-17 23:36:06 +00:00
<div :class="cx('targetWrapper')" v-bind="ptm('targetWrapper')" data-pc-group-section="listwrapper">
<div v-if="$slots.targetheader" :class="cx('targetHeader')" v-bind="ptm('targetHeader')" data-pc-group-section="header">
2022-09-06 12:03:37 +00:00
<slot name="targetheader"></slot>
</div>
2022-12-08 11:04:25 +00:00
<transition-group
ref="targetList"
:id="idTarget + '_list'"
name="p-picklist-flip"
tag="ul"
2023-06-02 15:43:30 +00:00
:class="cx('targetList')"
2022-12-08 11:04:25 +00:00
:style="listStyle"
role="listbox"
aria-multiselectable="true"
:aria-activedescendant="focused['targetList'] ? focusedOptionId : undefined"
:tabindex="targetList && targetList.length > 0 ? tabindex : -1"
@focus="onListFocus($event, 'targetList')"
@blur="onListBlur($event, 'targetList')"
@keydown="onItemKeyDown($event, 'targetList')"
2023-08-02 12:02:28 +00:00
v-bind="{ ...targetListProps, ...ptm('targetList'), ...ptm('transition') }"
2023-08-17 23:36:06 +00:00
data-pc-group-section="list"
2022-12-08 11:04:25 +00:00
>
2022-09-06 12:03:37 +00:00
<template v-for="(item, i) of targetList" :key="getItemKey(item, i)">
2022-09-14 11:26:01 +00:00
<li
2022-12-08 11:04:25 +00:00
:id="idTarget + '_' + i"
2022-09-14 11:26:01 +00:00
v-ripple
:class="cx('item', { item, id: `${idTarget}_${i}`, listIndex: 1 })"
2022-12-08 11:04:25 +00:00
@click="onItemClick($event, item, i, 1)"
2022-09-14 11:26:01 +00:00
@dblclick="onItemDblClick($event, item, 1)"
2022-12-08 11:04:25 +00:00
@keydown="onItemKeyDown($event, 'targetList')"
@mousedown="onOptionMouseDown(i, 'targetList')"
2022-09-14 11:26:01 +00:00
@touchend="onItemTouchEnd"
role="option"
:aria-selected="isSelected(item, 1)"
v-bind="getPTOptions(item, 'item', `${idTarget}_${i}`, 1)"
2023-06-06 13:30:04 +00:00
:data-p-highlight="isSelected(item, 1)"
:data-p-focused="`${idTarget}_${i}` === focusedOptionId"
2022-09-14 11:26:01 +00:00
>
2022-09-06 12:03:37 +00:00
<slot name="item" :item="item" :index="i"> </slot>
</li>
</template>
</transition-group>
</div>
2023-08-17 23:36:06 +00:00
<div v-if="showTargetControls" :class="cx('targetControls')" v-bind="ptm('targetControls')" data-pc-group-section="controls">
2022-09-06 12:03:37 +00:00
<slot name="targetcontrolsstart"></slot>
<PLButton :aria-label="moveUpAriaLabel" :disabled="moveDisabled(1)" type="button" @click="moveUp($event, 1)" v-bind="moveUpButtonProps" :pt="ptm('targetMoveUpButton')" :unstyled="unstyled" data-pc-section="targetmoveupbutton">
<template #icon>
<slot name="moveupicon">
2023-09-06 07:13:47 +00:00
<AngleUpIcon v-bind="ptm('targetMoveUpButton')['icon']" data-pc-section="moveupicon" />
</slot>
</template>
</PLButton>
<PLButton :aria-label="moveTopAriaLabel" :disabled="moveDisabled(1)" type="button" @click="moveTop($event, 1)" v-bind="moveTopButtonProps" :pt="ptm('targetMoveTopButton')" :unstyled="unstyled" data-pc-section="targetmovetopbutton">
<template #icon>
<slot name="movetopicon">
2023-09-06 07:13:47 +00:00
<AngleDoubleUpIcon v-bind="ptm('targetMoveTopButton')['icon']" data-pc-section="movetopicon" />
</slot>
</template>
</PLButton>
<PLButton :aria-label="moveDownAriaLabel" :disabled="moveDisabled(1)" type="button" @click="moveDown($event, 1)" v-bind="moveDownButtonProps" :pt="ptm('targetMoveDownButton')" :unstyled="unstyled" data-pc-section="targetmovedownbutton">
<template #icon>
<slot name="movedownicon">
2023-09-06 07:13:47 +00:00
<AngleDownIcon v-bind="ptm('targetMoveDownButton')['icon']" data-pc-section="movedownicon" />
</slot>
</template>
</PLButton>
<PLButton
:aria-label="moveBottomAriaLabel"
:disabled="moveDisabled(1)"
type="button"
@click="moveBottom($event, 1)"
v-bind="moveBottomButtonProps"
:pt="ptm('targetMoveBottomButton')"
:unstyled="unstyled"
data-pc-section="targetmovebottombutton"
>
<template #icon>
<slot name="movebottomicon">
2023-09-06 07:13:47 +00:00
<AngleDoubleDownIcon v-bind="ptm('targetMoveBottomButton')['icon']" data-pc-section="movebottomicon" />
</slot>
</template>
</PLButton>
2022-09-06 12:03:37 +00:00
<slot name="targetcontrolsend"></slot>
</div>
</div>
</template>
<script>
import Button from 'primevue/button';
import AngleDoubleDownIcon from 'primevue/icons/angledoubledown';
import AngleDoubleLeftIcon from 'primevue/icons/angledoubleleft';
import AngleDoubleRightIcon from 'primevue/icons/angledoubleright';
import AngleDoubleUpIcon from 'primevue/icons/angledoubleup';
import AngleDownIcon from 'primevue/icons/angledown';
import AngleLeftIcon from 'primevue/icons/angleleft';
import AngleRightIcon from 'primevue/icons/angleright';
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:19:03 +00:00
import BasePickList from './BasePickList.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'PickList',
2023-06-02 15:43:30 +00:00
extends: BasePickList,
2022-12-08 11:04:25 +00:00
emits: ['update:modelValue', 'reorder', 'update:selection', 'selection-change', 'move-to-target', 'move-to-source', 'move-all-to-target', 'move-all-to-source', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
itemTouched: false,
reorderDirection: null,
styleElement: null,
media: null,
mediaChangeListener: 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: {
sourceList: false,
targetList: false
},
focusedOptionIndex: -1,
viewChanged: false
2022-09-14 11:26:01 +00:00
};
},
watch: {
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
},
2022-09-14 11:26:01 +00:00
selection(newValue) {
this.d_selection = newValue;
},
breakpoint() {
this.destroyMedia();
this.initMedia();
2022-09-06 12:03:37 +00:00
}
},
updated() {
if (this.reorderDirection) {
this.updateListScroll(this.$refs.sourceList.$el);
this.updateListScroll(this.$refs.targetList.$el);
this.reorderDirection = null;
}
},
beforeUnmount() {
this.destroyStyle();
this.destroyMedia();
2022-09-06 12:03:37 +00:00
},
beforeMount() {},
mounted() {
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
if (this.responsive) {
this.createStyle();
this.initMedia();
2022-09-06 12:03:37 +00:00
}
},
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, id, listIndex) {
2023-05-09 22:03:24 +00:00
return this.ptm(key, {
context: {
active: this.isSelected(item, listIndex),
focused: id === this.focusedOptionId
2023-05-09 22:03:24 +00:00
}
});
},
2022-09-06 12:03:37 +00:00
isSelected(item, listIndex) {
return ObjectUtils.findIndexInList(item, this.d_selection[listIndex]) != -1;
},
2022-12-08 11:04:25 +00:00
onListFocus(event, listType) {
this.focused[listType] = true;
this.findCurrentFocusedIndex(listType);
this.$emit('focus', event);
2022-12-08 11:04:25 +00:00
},
onListBlur(event, listType) {
this.focused[listType] = false;
this.focusedOptionIndex = -1;
this.$emit('blur', event);
},
onOptionMouseDown(index, listType) {
this.focused[listType] = true;
this.focusedOptionIndex = index;
},
2022-09-06 12:03:37 +00:00
moveUp(event, listIndex) {
if (this.d_selection && this.d_selection[listIndex]) {
let valueList = [...this.modelValue[listIndex]];
let selectionList = this.d_selection[listIndex];
for (let i = 0; i < selectionList.length; i++) {
let selectedItem = selectionList[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, valueList);
if (selectedItemIndex !== 0) {
let movedItem = valueList[selectedItemIndex];
let temp = valueList[selectedItemIndex - 1];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
valueList[selectedItemIndex - 1] = movedItem;
valueList[selectedItemIndex] = temp;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[listIndex] = valueList;
this.reorderDirection = 'up';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection,
listIndex: listIndex
});
}
},
moveTop(event, listIndex) {
2022-09-14 11:26:01 +00:00
if (this.d_selection) {
2022-09-06 12:03:37 +00:00
let valueList = [...this.modelValue[listIndex]];
let selectionList = this.d_selection[listIndex];
for (let i = 0; i < selectionList.length; i++) {
let selectedItem = selectionList[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, valueList);
if (selectedItemIndex !== 0) {
let movedItem = valueList.splice(selectedItemIndex, 1)[0];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
valueList.unshift(movedItem);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[listIndex] = valueList;
this.reorderDirection = 'top';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection,
listIndex: listIndex
});
}
},
moveDown(event, listIndex) {
2022-09-14 11:26:01 +00:00
if (this.d_selection) {
2022-09-06 12:03:37 +00:00
let valueList = [...this.modelValue[listIndex]];
let selectionList = this.d_selection[listIndex];
for (let i = selectionList.length - 1; i >= 0; i--) {
let selectedItem = selectionList[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, valueList);
2022-09-14 11:26:01 +00:00
if (selectedItemIndex !== valueList.length - 1) {
2022-09-06 12:03:37 +00:00
let movedItem = valueList[selectedItemIndex];
let temp = valueList[selectedItemIndex + 1];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
valueList[selectedItemIndex + 1] = movedItem;
valueList[selectedItemIndex] = temp;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[listIndex] = valueList;
this.reorderDirection = 'down';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection,
listIndex: listIndex
});
}
},
moveBottom(event, listIndex) {
if (this.d_selection) {
let valueList = [...this.modelValue[listIndex]];
let selectionList = this.d_selection[listIndex];
for (let i = selectionList.length - 1; i >= 0; i--) {
let selectedItem = selectionList[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, valueList);
2022-09-14 11:26:01 +00:00
if (selectedItemIndex !== valueList.length - 1) {
2022-09-06 12:03:37 +00:00
let movedItem = valueList.splice(selectedItemIndex, 1)[0];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
valueList.push(movedItem);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
break;
}
}
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[listIndex] = valueList;
this.reorderDirection = 'bottom';
this.$emit('update:modelValue', value);
this.$emit('reorder', {
originalEvent: event,
value: value,
direction: this.reorderDirection,
listIndex: listIndex
});
}
},
moveToTarget(event) {
let selection = this.d_selection && this.d_selection[0] ? this.d_selection[0] : null;
let sourceList = [...this.modelValue[0]];
let targetList = [...this.modelValue[1]];
if (selection) {
for (let i = 0; i < selection.length; i++) {
let selectedItem = selection[i];
if (ObjectUtils.findIndexInList(selectedItem, targetList) == -1) {
2022-09-14 11:26:01 +00:00
targetList.push(sourceList.splice(ObjectUtils.findIndexInList(selectedItem, sourceList), 1)[0]);
2022-09-06 12:03:37 +00:00
}
}
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[0] = sourceList;
value[1] = targetList;
this.$emit('update:modelValue', value);
this.$emit('move-to-target', {
originalEvent: event,
items: [...new Set(selection)]
2022-09-06 12:03:37 +00:00
});
this.d_selection[0] = [];
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
}
},
moveAllToTarget(event) {
if (this.modelValue[0]) {
let sourceList = [...this.modelValue[0]];
let targetList = [...this.modelValue[1]];
this.$emit('move-all-to-target', {
originalEvent: event,
items: sourceList
});
targetList = [...targetList, ...sourceList];
sourceList = [];
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[0] = sourceList;
value[1] = targetList;
this.$emit('update:modelValue', value);
this.d_selection[0] = [];
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
}
},
moveToSource(event) {
let selection = this.d_selection && this.d_selection[1] ? this.d_selection[1] : null;
let sourceList = [...this.modelValue[0]];
let targetList = [...this.modelValue[1]];
if (selection) {
for (let i = 0; i < selection.length; i++) {
let selectedItem = selection[i];
if (ObjectUtils.findIndexInList(selectedItem, sourceList) == -1) {
2022-09-14 11:26:01 +00:00
sourceList.push(targetList.splice(ObjectUtils.findIndexInList(selectedItem, targetList), 1)[0]);
2022-09-06 12:03:37 +00:00
}
}
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[0] = sourceList;
value[1] = targetList;
this.$emit('update:modelValue', value);
this.$emit('move-to-source', {
originalEvent: event,
items: [...new Set(selection)]
2022-09-06 12:03:37 +00:00
});
this.d_selection[1] = [];
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
}
},
moveAllToSource(event) {
if (this.modelValue[1]) {
let sourceList = [...this.modelValue[0]];
let targetList = [...this.modelValue[1]];
this.$emit('move-all-to-source', {
originalEvent: event,
items: targetList
});
sourceList = [...sourceList, ...targetList];
targetList = [];
let value = [...this.modelValue];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
value[0] = sourceList;
value[1] = targetList;
this.$emit('update:modelValue', value);
this.d_selection[1] = [];
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
}
},
2022-12-08 11:04:25 +00:00
onItemClick(event, item, index, listIndex) {
const listType = listIndex === 0 ? 'sourceList' : 'targetList';
2022-09-06 12:03:37 +00:00
this.itemTouched = false;
const selectionList = this.d_selection[listIndex];
const selectedIndex = ObjectUtils.findIndexInList(item, selectionList);
2022-09-14 11:26:01 +00:00
const selected = selectedIndex != -1;
2022-09-06 12:03:37 +00:00
const metaSelection = this.itemTouched ? false : this.metaKeySelection;
2023-06-02 15:43:30 +00:00
const selectedId = DomHandler.find(this.$refs[listType].$el, '[data-pc-section="item"]')[index].getAttribute('id');
2022-12-08 11:04:25 +00:00
this.focusedOptionIndex = event?.type === 'click' ? -1 : selectedId;
2022-09-06 12:03:37 +00:00
let _selection;
if (metaSelection) {
2022-09-14 11:26:01 +00:00
let metaKey = event.metaKey || event.ctrlKey;
2022-09-06 12:03:37 +00:00
if (selected && metaKey) {
_selection = selectionList.filter((val, index) => index !== selectedIndex);
2022-09-14 11:26:01 +00:00
} else {
_selection = metaKey ? (selectionList ? [...selectionList] : []) : [];
2022-09-06 12:03:37 +00:00
_selection.push(item);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (selected) {
_selection = selectionList.filter((val, index) => index !== selectedIndex);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
_selection = selectionList ? [...selectionList] : [];
_selection.push(item);
}
}
let newSelection = [...this.d_selection];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
newSelection[listIndex] = _selection;
this.d_selection = newSelection;
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
});
},
onItemDblClick(event, item, listIndex) {
2022-09-14 11:26:01 +00:00
if (listIndex === 0) this.moveToTarget(event);
else if (listIndex === 1) this.moveToSource(event);
2022-09-06 12:03:37 +00:00
},
onItemTouchEnd() {
this.itemTouched = true;
},
2022-12-08 11:04:25 +00:00
onItemKeyDown(event, listType) {
switch (event.code) {
case 'ArrowDown':
this.onArrowDownKey(event, listType);
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'ArrowUp':
this.onArrowUpKey(event, listType);
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 'Home':
this.onHomeKey(event, listType);
break;
2022-09-14 11:26:01 +00:00
2022-12-08 11:04:25 +00:00
case 'End':
this.onEndKey(event, listType);
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Enter':
case 'NumpadEnter':
2022-12-08 11:04:25 +00:00
this.onEnterKey(event, listType);
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 'Space':
this.onSpaceKey(event, listType);
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 'KeyA':
if (event.ctrlKey) {
this.d_selection = [...this.modelValue];
this.$emit('update:selection', this.d_selection);
event.preventDefault();
2022-12-08 11:04:25 +00:00
}
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
}
},
2022-12-08 11:04:25 +00:00
onArrowDownKey(event, listType) {
const optionIndex = this.focusedOptionIndex !== -1 ? this.findNextOptionIndex(listType) : this.findFirstSelectedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
this.changeFocusedOptionIndex(optionIndex, listType);
if (event.shiftKey) {
this.onEnterKey(event, listType);
}
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
onArrowUpKey(event, listType) {
const optionIndex = this.focusedOptionIndex !== -1 ? this.findPrevOptionIndex(listType) : this.findLastSelectedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
this.changeFocusedOptionIndex(optionIndex, listType);
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
if (event.shiftKey) {
this.onEnterKey(event, listType);
}
event.preventDefault();
},
onEnterKey(event, listType) {
const listId = listType === 'sourceList' ? 0 : 1;
const matchedOptionIndex = this.findMatchedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
this.onItemClick(event, this.modelValue[listId][matchedOptionIndex], matchedOptionIndex, listId);
event.preventDefault();
},
onSpaceKey(event, listType) {
event.preventDefault();
if (event.shiftKey && this.d_selection && this.d_selection.length > 0) {
2022-12-08 11:04:25 +00:00
const listId = listType === 'sourceList' ? 0 : 1;
const selectedItemIndex = ObjectUtils.findIndexInList(this.d_selection[listId][0], [...this.modelValue[listId]]);
const matchedOptionIndex = this.findMatchedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
this.d_selection[listId] = [...this.modelValue[listId]].slice(Math.min(selectedItemIndex, matchedOptionIndex), Math.max(selectedItemIndex, matchedOptionIndex) + 1);
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
2022-12-08 11:04:25 +00:00
} else {
this.onEnterKey(event, listType);
}
},
onHomeKey(event, listType) {
if (event.ctrlKey && event.shiftKey) {
const listId = listType === 'sourceList' ? 0 : 1;
const matchedOptionIndex = this.findMatchedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
this.d_selection[listId] = [...this.modelValue[listId]].slice(0, matchedOptionIndex + 1);
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
2022-12-08 11:04:25 +00:00
} else {
this.changeFocusedOptionIndex(0, listType);
}
event.preventDefault();
},
onEndKey(event, listType) {
const items = this.findAllItems(listType);
2022-12-08 11:04:25 +00:00
if (event.ctrlKey && event.shiftKey) {
const listId = listType === 'sourceList' ? 0 : 1;
const matchedOptionIndex = this.findMatchedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
this.d_selection[listId] = [...this.modelValue[listId]].slice(matchedOptionIndex, items.length);
this.$emit('update:selection', this.d_selection);
this.$emit('selection-change', {
originalEvent: event,
value: this.d_selection
});
2022-12-08 11:04:25 +00:00
} else {
this.changeFocusedOptionIndex(items.length - 1, listType);
}
event.preventDefault();
},
findAllItems(listType) {
return DomHandler.find(this.$refs[listType].$el, '[data-pc-section="item"]');
},
findFocusedItem(listType) {
return DomHandler.findSingle(this.$refs[listType].$el, `[data-pc-section="item"][id=${this.focusedOptionIndex}]`);
},
findCurrentFocusedIndex(listType) {
this.focusedOptionIndex = this.findFirstSelectedOptionIndex(listType);
if (this.autoOptionFocus && this.focusedOptionIndex === -1) {
this.focusedOptionIndex = this.findFirstFocusedOptionIndex(listType);
}
this.scrollInView(this.focusedOptionIndex, listType);
},
findFirstFocusedOptionIndex(listType) {
const firstFocusableItem = DomHandler.findSingle(this.$refs[listType].$el, '[data-pc-section="item"]');
return DomHandler.getAttribute(firstFocusableItem, 'id');
},
findFirstSelectedOptionIndex(listType) {
if (this.hasSelectedOption(listType)) {
const selectedFirstItem = DomHandler.findSingle(this.$refs[listType].$el, '[data-p-highlight="true"]');
2022-12-08 11:04:25 +00:00
return DomHandler.getAttribute(selectedFirstItem, 'id');
}
return -1;
},
findLastSelectedOptionIndex(listType) {
if (this.hasSelectedOption(listType)) {
const selectedItems = DomHandler.find(this.$refs[listType].$el, '[data-p-highlight="true"]');
return ObjectUtils.findIndexInList(selectedItems[selectedItems.length - 1], this.list.children);
}
return -1;
},
findMatchedOptionIndex(listType, id = this.focusedOptionIndex) {
const items = this.findAllItems(listType);
return [...items].findIndex((link) => link.id === id);
},
findNextOptionIndex(listType) {
const matchedOptionIndex = this.findMatchedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
return matchedOptionIndex > -1 ? matchedOptionIndex + 1 : 0;
},
findPrevOptionIndex(listType) {
const matchedOptionIndex = this.findMatchedOptionIndex(listType);
2022-12-08 11:04:25 +00:00
return matchedOptionIndex > -1 ? matchedOptionIndex - 1 : 0;
},
changeFocusedOptionIndex(index, listType) {
const items = this.findAllItems(listType);
2022-12-08 11:04:25 +00:00
let order = index >= items.length ? items.length - 1 : index < 0 ? 0 : index;
this.focusedOptionIndex = items[order].getAttribute('id');
this.scrollInView(items[order].getAttribute('id'), listType);
},
scrollInView(id, listType) {
2023-06-02 15:43:30 +00:00
const element = DomHandler.findSingle(this.$refs[listType].$el, `[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
},
updateListScroll(listElement) {
2023-06-02 15:43:30 +00:00
const listItems = DomHandler.find(listElement, '[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':
DomHandler.scrollInView(listElement, listItems[0]);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'top':
listElement.scrollTop = 0;
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'down':
DomHandler.scrollInView(listElement, listItems[listItems.length - 1]);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
case 'bottom':
listElement.scrollTop = listElement.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
}
}
},
initMedia() {
this.media = window.matchMedia(`(max-width: ${this.breakpoint})`);
this.viewChanged = this.media.matches;
this.bindMediaChangeListener();
},
destroyMedia() {
this.unbindMediaChangeListener();
},
bindMediaChangeListener() {
if (this.media && !this.mediaChangeListener) {
this.mediaChangeListener = (event) => {
this.viewChanged = event.matches;
};
this.media.addEventListener('change', this.mediaChangeListener);
}
},
unbindMediaChangeListener() {
if (this.media && this.mediaChangeListener) {
this.media.removeEventListener('change', this.mediaChangeListener);
this.mediaChangeListener = null;
}
},
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-picklist[${this.attributeSelector}] {
flex-direction: column;
}
.p-picklist[${this.attributeSelector}] .p-picklist-buttons {
padding: var(--content-padding);
flex-direction: row;
}
.p-picklist[${this.attributeSelector}] .p-picklist-buttons .p-button {
margin-right: var(--inline-spacing);
margin-bottom: 0;
}
.p-picklist[${this.attributeSelector}] .p-picklist-buttons .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(index) {
if (!this.d_selection[index] || !this.d_selection[index].length) {
return true;
}
},
moveAllDisabled(list) {
return ObjectUtils.isEmpty(this[list]);
},
hasSelectedOption(listType) {
return listType === 'sourceList' ? ObjectUtils.isNotEmpty(this.d_selection[0]) : ObjectUtils.isNotEmpty(this.d_selection[0]);
2022-09-06 12:03:37 +00:00
}
},
computed: {
2022-12-08 11:04:25 +00:00
idSource() {
return `${this.id}_source`;
2022-12-08 11:04:25 +00:00
},
idTarget() {
return `${this.id}_target`;
2022-12-08 11:04:25 +00:00
},
focusedOptionId() {
return this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : null;
},
2022-09-06 12:03:37 +00:00
sourceList() {
return this.modelValue && this.modelValue[0] ? this.modelValue[0] : null;
},
targetList() {
return this.modelValue && this.modelValue[1] ? this.modelValue[1] : null;
},
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;
},
moveToTargetAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveToTarget : undefined;
},
moveAllToTargetAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveAllToTarget : undefined;
},
moveToSourceAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveToSource : undefined;
},
moveAllToSourceAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.moveAllToSource : undefined;
2022-09-06 12:03:37 +00:00
}
},
components: {
PLButton: Button,
AngleRightIcon: AngleRightIcon,
AngleLeftIcon: AngleLeftIcon,
AngleDownIcon: AngleDownIcon,
AngleUpIcon: AngleUpIcon,
AngleDoubleRightIcon: AngleDoubleRightIcon,
AngleDoubleLeftIcon: AngleDoubleLeftIcon,
AngleDoubleDownIcon: AngleDoubleDownIcon,
AngleDoubleUpIcon: AngleDoubleUpIcon
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>