import * as i0 from '@angular/core'; import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Input, Output, ViewChild, ContentChildren, NgModule } from '@angular/core'; import * as i3 from '@angular/common'; import { CommonModule } from '@angular/common'; import * as i4 from 'primeng/button'; import { ButtonModule } from 'primeng/button'; import * as i1 from 'primeng/api'; import { PrimeTemplate, SharedModule } from 'primeng/api'; import { DomHandler } from 'primeng/dom'; import * as i5 from 'primeng/ripple'; import { RippleModule } from 'primeng/ripple'; import * as i2 from '@angular/cdk/drag-drop'; import { transferArrayItem, moveItemInArray, DragDropModule } from '@angular/cdk/drag-drop'; import { UniqueComponentId, ObjectUtils } from 'primeng/utils'; class PickList { constructor(el, cd, filterService) { this.el = el; this.cd = cd; this.filterService = filterService; this.trackBy = (index, item) => item; this.showSourceFilter = true; this.showTargetFilter = true; this.metaKeySelection = true; this.dragdrop = false; this.showSourceControls = true; this.showTargetControls = true; this.disabled = false; this.filterMatchMode = "contains"; this.breakpoint = "960px"; this.onMoveToSource = new EventEmitter(); this.onMoveAllToSource = new EventEmitter(); this.onMoveAllToTarget = new EventEmitter(); this.onMoveToTarget = new EventEmitter(); this.onSourceReorder = new EventEmitter(); this.onTargetReorder = new EventEmitter(); this.onSourceSelect = new EventEmitter(); this.onTargetSelect = new EventEmitter(); this.onSourceFilter = new EventEmitter(); this.onTargetFilter = new EventEmitter(); this.selectedItemsSource = []; this.selectedItemsTarget = []; this.id = UniqueComponentId(); this.SOURCE_LIST = -1; this.TARGET_LIST = 1; } ngOnInit() { if (this.responsive) { this.createStyle(); } } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'item': this.itemTemplate = item.template; break; case 'sourceHeader': this.sourceHeaderTemplate = item.template; break; case 'targetHeader': this.targetHeaderTemplate = item.template; break; case 'emptymessagesource': this.emptyMessageSourceTemplate = item.template; break; case 'emptyfiltermessagesource': this.emptyFilterMessageSourceTemplate = item.template; break; case 'emptymessagetarget': this.emptyMessageTargetTemplate = item.template; break; case 'emptyfiltermessagetarget': this.emptyFilterMessageTargetTemplate = item.template; break; default: this.itemTemplate = item.template; break; } }); } ngAfterViewChecked() { if (this.movedUp || this.movedDown) { let listItems = DomHandler.find(this.reorderedListElement, 'li.p-highlight'); let listItem; if (this.movedUp) listItem = listItems[0]; else listItem = listItems[listItems.length - 1]; DomHandler.scrollInView(this.reorderedListElement, listItem); this.movedUp = false; this.movedDown = false; this.reorderedListElement = null; } } onItemClick(event, item, selectedItems, callback) { if (this.disabled) { return; } let index = this.findIndexInSelection(item, selectedItems); let selected = (index != -1); let metaSelection = this.itemTouched ? false : this.metaKeySelection; if (metaSelection) { let metaKey = (event.metaKey || event.ctrlKey || event.shiftKey); if (selected && metaKey) { selectedItems.splice(index, 1); } else { if (!metaKey) { selectedItems.length = 0; } selectedItems.push(item); } } else { if (selected) selectedItems.splice(index, 1); else selectedItems.push(item); } callback.emit({ originalEvent: event, items: selectedItems }); this.itemTouched = false; } onSourceItemDblClick() { if (this.disabled) { return; } this.moveRight(); } onTargetItemDblClick() { if (this.disabled) { return; } this.moveLeft(); } onFilter(event, data, listType) { let query = event.target.value.trim().toLocaleLowerCase(this.filterLocale); if (listType === this.SOURCE_LIST) this.filterValueSource = query; else if (listType === this.TARGET_LIST) this.filterValueTarget = query; this.filter(data, listType); } filter(data, listType) { let searchFields = this.filterBy.split(','); if (listType === this.SOURCE_LIST) { this.visibleOptionsSource = this.filterService.filter(data, searchFields, this.filterValueSource, this.filterMatchMode, this.filterLocale); this.onSourceFilter.emit({ query: this.filterValueSource, value: this.visibleOptionsSource }); } else if (listType === this.TARGET_LIST) { this.visibleOptionsTarget = this.filterService.filter(data, searchFields, this.filterValueTarget, this.filterMatchMode, this.filterLocale); this.onTargetFilter.emit({ query: this.filterValueTarget, value: this.visibleOptionsTarget }); } } isItemVisible(item, listType) { if (listType == this.SOURCE_LIST) return this.isVisibleInList(this.visibleOptionsSource, item, this.filterValueSource); else return this.isVisibleInList(this.visibleOptionsTarget, item, this.filterValueTarget); } isEmpty(listType) { if (listType == this.SOURCE_LIST) return this.filterValueSource ? (!this.visibleOptionsSource || this.visibleOptionsSource.length === 0) : (!this.source || this.source.length === 0); else return this.filterValueTarget ? (!this.visibleOptionsTarget || this.visibleOptionsTarget.length === 0) : (!this.target || this.target.length === 0); } isVisibleInList(data, item, filterValue) { if (filterValue && filterValue.trim().length) { for (let i = 0; i < data.length; i++) { if (item == data[i]) { return true; } } } else { return true; } } onItemTouchEnd() { if (this.disabled) { return; } this.itemTouched = true; } sortByIndexInList(items, list) { return items.sort((item1, item2) => ObjectUtils.findIndexInList(item1, list) - ObjectUtils.findIndexInList(item2, list)); } moveUp(listElement, list, selectedItems, callback, listType) { if (selectedItems && selectedItems.length) { selectedItems = this.sortByIndexInList(selectedItems, list); for (let i = 0; i < selectedItems.length; i++) { let selectedItem = selectedItems[i]; let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, list); if (selectedItemIndex != 0) { let movedItem = list[selectedItemIndex]; let temp = list[selectedItemIndex - 1]; list[selectedItemIndex - 1] = movedItem; list[selectedItemIndex] = temp; } else { break; } } if (this.dragdrop && ((this.filterValueSource && listType === this.SOURCE_LIST) || (this.filterValueTarget && listType === this.TARGET_LIST))) this.filter(list, listType); this.movedUp = true; this.reorderedListElement = listElement; callback.emit({ items: selectedItems }); } } moveTop(listElement, list, selectedItems, callback, listType) { if (selectedItems && selectedItems.length) { selectedItems = this.sortByIndexInList(selectedItems, list); for (let i = 0; i < selectedItems.length; i++) { let selectedItem = selectedItems[i]; let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, list); if (selectedItemIndex != 0) { let movedItem = list.splice(selectedItemIndex, 1)[0]; list.unshift(movedItem); } else { break; } } if (this.dragdrop && ((this.filterValueSource && listType === this.SOURCE_LIST) || (this.filterValueTarget && listType === this.TARGET_LIST))) this.filter(list, listType); listElement.scrollTop = 0; callback.emit({ items: selectedItems }); } } moveDown(listElement, list, selectedItems, callback, listType) { if (selectedItems && selectedItems.length) { selectedItems = this.sortByIndexInList(selectedItems, list); for (let i = selectedItems.length - 1; i >= 0; i--) { let selectedItem = selectedItems[i]; let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, list); if (selectedItemIndex != (list.length - 1)) { let movedItem = list[selectedItemIndex]; let temp = list[selectedItemIndex + 1]; list[selectedItemIndex + 1] = movedItem; list[selectedItemIndex] = temp; } else { break; } } if (this.dragdrop && ((this.filterValueSource && listType === this.SOURCE_LIST) || (this.filterValueTarget && listType === this.TARGET_LIST))) this.filter(list, listType); this.movedDown = true; this.reorderedListElement = listElement; callback.emit({ items: selectedItems }); } } moveBottom(listElement, list, selectedItems, callback, listType) { if (selectedItems && selectedItems.length) { selectedItems = this.sortByIndexInList(selectedItems, list); for (let i = selectedItems.length - 1; i >= 0; i--) { let selectedItem = selectedItems[i]; let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, list); if (selectedItemIndex != (list.length - 1)) { let movedItem = list.splice(selectedItemIndex, 1)[0]; list.push(movedItem); } else { break; } } if (this.dragdrop && ((this.filterValueSource && listType === this.SOURCE_LIST) || (this.filterValueTarget && listType === this.TARGET_LIST))) this.filter(list, listType); listElement.scrollTop = listElement.scrollHeight; callback.emit({ items: selectedItems }); } } moveRight() { if (this.selectedItemsSource && this.selectedItemsSource.length) { for (let i = 0; i < this.selectedItemsSource.length; i++) { let selectedItem = this.selectedItemsSource[i]; if (ObjectUtils.findIndexInList(selectedItem, this.target) == -1) { this.target.push(this.source.splice(ObjectUtils.findIndexInList(selectedItem, this.source), 1)[0]); if (this.visibleOptionsSource) this.visibleOptionsSource.splice(ObjectUtils.findIndexInList(selectedItem, this.visibleOptionsSource), 1); } } this.onMoveToTarget.emit({ items: this.selectedItemsSource }); this.selectedItemsSource = []; if (this.filterValueTarget) { this.filter(this.target, this.TARGET_LIST); } } } moveAllRight() { if (this.source) { let movedItems = []; for (let i = 0; i < this.source.length; i++) { if (this.isItemVisible(this.source[i], this.SOURCE_LIST)) { let removedItem = this.source.splice(i, 1)[0]; this.target.push(removedItem); movedItems.push(removedItem); i--; } } this.onMoveAllToTarget.emit({ items: movedItems }); this.selectedItemsSource = []; if (this.filterValueTarget) { this.filter(this.target, this.TARGET_LIST); } this.visibleOptionsSource = []; } } moveLeft() { if (this.selectedItemsTarget && this.selectedItemsTarget.length) { for (let i = 0; i < this.selectedItemsTarget.length; i++) { let selectedItem = this.selectedItemsTarget[i]; if (ObjectUtils.findIndexInList(selectedItem, this.source) == -1) { this.source.push(this.target.splice(ObjectUtils.findIndexInList(selectedItem, this.target), 1)[0]); if (this.visibleOptionsTarget) this.visibleOptionsTarget.splice(ObjectUtils.findIndexInList(selectedItem, this.visibleOptionsTarget), 1)[0]; } } this.onMoveToSource.emit({ items: this.selectedItemsTarget }); this.selectedItemsTarget = []; if (this.filterValueSource) { this.filter(this.source, this.SOURCE_LIST); } } } moveAllLeft() { if (this.target) { let movedItems = []; for (let i = 0; i < this.target.length; i++) { if (this.isItemVisible(this.target[i], this.TARGET_LIST)) { let removedItem = this.target.splice(i, 1)[0]; this.source.push(removedItem); movedItems.push(removedItem); i--; } } this.onMoveAllToSource.emit({ items: movedItems }); this.selectedItemsTarget = []; if (this.filterValueSource) { this.filter(this.source, this.SOURCE_LIST); } this.visibleOptionsTarget = []; } } isSelected(item, selectedItems) { return this.findIndexInSelection(item, selectedItems) != -1; } findIndexInSelection(item, selectedItems) { return ObjectUtils.findIndexInList(item, selectedItems); } onDrop(event, listType) { let isTransfer = event.previousContainer !== event.container; let dropIndexes = this.getDropIndexes(event.previousIndex, event.currentIndex, listType, isTransfer, event.item.data); if (listType === this.SOURCE_LIST) { if (isTransfer) { transferArrayItem(event.previousContainer.data, event.container.data, dropIndexes.previousIndex, dropIndexes.currentIndex); if (this.visibleOptionsTarget) this.visibleOptionsTarget.splice(event.previousIndex, 1); this.onMoveToSource.emit({ items: [event.item.data] }); } else { moveItemInArray(event.container.data, dropIndexes.previousIndex, dropIndexes.currentIndex); this.onSourceReorder.emit({ items: [event.item.data] }); } if (this.filterValueSource) { this.filter(this.source, this.SOURCE_LIST); } } else { if (isTransfer) { transferArrayItem(event.previousContainer.data, event.container.data, dropIndexes.previousIndex, dropIndexes.currentIndex); if (this.visibleOptionsSource) this.visibleOptionsSource.splice(event.previousIndex, 1); this.onMoveToTarget.emit({ items: [event.item.data] }); } else { moveItemInArray(event.container.data, dropIndexes.previousIndex, dropIndexes.currentIndex); this.onTargetReorder.emit({ items: [event.item.data] }); } if (this.filterValueTarget) { this.filter(this.target, this.TARGET_LIST); } } } getDropIndexes(fromIndex, toIndex, droppedList, isTransfer, data) { let previousIndex, currentIndex; if (droppedList === this.SOURCE_LIST) { previousIndex = isTransfer ? this.filterValueTarget ? ObjectUtils.findIndexInList(data, this.target) : fromIndex : this.filterValueSource ? ObjectUtils.findIndexInList(data, this.source) : fromIndex; currentIndex = this.filterValueSource ? this.findFilteredCurrentIndex(this.visibleOptionsSource, toIndex, this.source) : toIndex; } else { previousIndex = isTransfer ? this.filterValueSource ? ObjectUtils.findIndexInList(data, this.source) : fromIndex : this.filterValueTarget ? ObjectUtils.findIndexInList(data, this.target) : fromIndex; currentIndex = this.filterValueTarget ? this.findFilteredCurrentIndex(this.visibleOptionsTarget, toIndex, this.target) : toIndex; } return { previousIndex, currentIndex }; } findFilteredCurrentIndex(visibleOptions, index, options) { if (visibleOptions.length === index) { let toIndex = ObjectUtils.findIndexInList(visibleOptions[index - 1], options); return toIndex + 1; } else { return ObjectUtils.findIndexInList(visibleOptions[index], options); } } resetFilter() { this.visibleOptionsSource = null; this.filterValueSource = null; this.visibleOptionsTarget = null; this.filterValueTarget = null; this.sourceFilterViewChild.nativeElement.value = ''; this.targetFilterViewChild.nativeElement.value = ''; } onItemKeydown(event, item, selectedItems, callback) { let listItem = event.currentTarget; switch (event.which) { //down case 40: var nextItem = this.findNextItem(listItem); if (nextItem) { nextItem.focus(); } event.preventDefault(); break; //up case 38: var prevItem = this.findPrevItem(listItem); if (prevItem) { prevItem.focus(); } event.preventDefault(); break; //enter case 13: this.onItemClick(event, item, selectedItems, callback); event.preventDefault(); break; } } findNextItem(item) { let nextItem = item.nextElementSibling; if (nextItem) return !DomHandler.hasClass(nextItem, 'p-picklist-item') || DomHandler.isHidden(nextItem) ? this.findNextItem(nextItem) : nextItem; else return null; } findPrevItem(item) { let prevItem = item.previousElementSibling; if (prevItem) return !DomHandler.hasClass(prevItem, 'p-picklist-item') || DomHandler.isHidden(prevItem) ? this.findPrevItem(prevItem) : prevItem; else return null; } createStyle() { if (!this.styleElement) { this.el.nativeElement.children[0].setAttribute(this.id, ''); this.styleElement = document.createElement('style'); this.styleElement.type = 'text/css'; document.head.appendChild(this.styleElement); let innerHTML = ` @media screen and (max-width: ${this.breakpoint}) { .p-picklist[${this.id}] { flex-direction: column; } .p-picklist[${this.id}] .p-picklist-buttons { padding: var(--content-padding); flex-direction: row; } .p-picklist[${this.id}] .p-picklist-buttons .p-button { margin-right: var(--inline-spacing); margin-bottom: 0; } .p-picklist[${this.id}] .p-picklist-buttons .p-button:last-child { margin-right: 0; } .p-picklist[${this.id}] .pi-angle-right:before { content: "\\e930" } .p-picklist[${this.id}] .pi-angle-double-right:before { content: "\\e92c" } .p-picklist[${this.id}] .pi-angle-left:before { content: "\\e933" } .p-picklist[${this.id}] .pi-angle-double-left:before { content: "\\e92f" } } `; this.styleElement.innerHTML = innerHTML; } } destroyStyle() { if (this.styleElement) { document.head.removeChild(this.styleElement); this.styleElement = null; ``; } } ngOnDestroy() { this.destroyStyle(); } } PickList.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: PickList, deps: [{ token: i0.ElementRef }, { token: i0.ChangeDetectorRef }, { token: i1.FilterService }], target: i0.ɵɵFactoryTarget.Component }); PickList.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.0.0", type: PickList, selector: "p-pickList", inputs: { source: "source", target: "target", sourceHeader: "sourceHeader", rightButtonAriaLabel: "rightButtonAriaLabel", leftButtonAriaLabel: "leftButtonAriaLabel", allRightButtonAriaLabel: "allRightButtonAriaLabel", allLeftButtonAriaLabel: "allLeftButtonAriaLabel", upButtonAriaLabel: "upButtonAriaLabel", downButtonAriaLabel: "downButtonAriaLabel", topButtonAriaLabel: "topButtonAriaLabel", bottomButtonAriaLabel: "bottomButtonAriaLabel", targetHeader: "targetHeader", responsive: "responsive", filterBy: "filterBy", filterLocale: "filterLocale", trackBy: "trackBy", sourceTrackBy: "sourceTrackBy", targetTrackBy: "targetTrackBy", showSourceFilter: "showSourceFilter", showTargetFilter: "showTargetFilter", metaKeySelection: "metaKeySelection", dragdrop: "dragdrop", style: "style", styleClass: "styleClass", sourceStyle: "sourceStyle", targetStyle: "targetStyle", showSourceControls: "showSourceControls", showTargetControls: "showTargetControls", sourceFilterPlaceholder: "sourceFilterPlaceholder", targetFilterPlaceholder: "targetFilterPlaceholder", disabled: "disabled", ariaSourceFilterLabel: "ariaSourceFilterLabel", ariaTargetFilterLabel: "ariaTargetFilterLabel", filterMatchMode: "filterMatchMode", breakpoint: "breakpoint" }, outputs: { onMoveToSource: "onMoveToSource", onMoveAllToSource: "onMoveAllToSource", onMoveAllToTarget: "onMoveAllToTarget", onMoveToTarget: "onMoveToTarget", onSourceReorder: "onSourceReorder", onTargetReorder: "onTargetReorder", onSourceSelect: "onSourceSelect", onTargetSelect: "onTargetSelect", onSourceFilter: "onSourceFilter", onTargetFilter: "onTargetFilter" }, host: { classAttribute: "p-element" }, queries: [{ propertyName: "templates", predicate: PrimeTemplate }], viewQueries: [{ propertyName: "listViewSourceChild", first: true, predicate: ["sourcelist"], descendants: true }, { propertyName: "listViewTargetChild", first: true, predicate: ["targetlist"], descendants: true }, { propertyName: "sourceFilterViewChild", first: true, predicate: ["sourceFilter"], descendants: true }, { propertyName: "targetFilterViewChild", first: true, predicate: ["targetFilter"], descendants: true }], ngImport: i0, template: `