primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 25.8 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Input, Output, ViewChild, ContentChildren, NgModule } from '@angular/core';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 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 { UniqueComponentId, ObjectUtils } from 'primeng/utils';
import * as i4 from 'primeng/ripple';
import { RippleModule } from 'primeng/ripple';
import * as i5 from '@angular/cdk/drag-drop';
import { moveItemInArray, DragDropModule } from '@angular/cdk/drag-drop';
class OrderList {
constructor(el, cd, filterService) {
this.el = el;
this.cd = cd;
this.filterService = filterService;
this.metaKeySelection = true;
this.dragdrop = false;
this.controlsPosition = 'left';
this.filterMatchMode = "contains";
this.breakpoint = "960px";
this.selectionChange = new EventEmitter();
this.trackBy = (index, item) => item;
this.onReorder = new EventEmitter();
this.onSelectionChange = new EventEmitter();
this.onFilterEvent = new EventEmitter();
this.id = UniqueComponentId();
}
get selection() {
return this._selection;
}
set selection(val) {
this._selection = val;
}
ngOnInit() {
if (this.responsive) {
this.createStyle();
}
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'item':
this.itemTemplate = item.template;
break;
case 'empty':
this.emptyMessageTemplate = item.template;
break;
case 'emptyfilter':
this.emptyFilterMessageTemplate = item.template;
break;
case 'header':
this.headerTemplate = item.template;
break;
default:
this.itemTemplate = item.template;
break;
}
});
}
ngAfterViewChecked() {
if (this.movedUp || this.movedDown) {
let listItems = DomHandler.find(this.listViewChild.nativeElement, 'li.p-highlight');
let listItem;
if (listItems.length > 0) {
if (this.movedUp)
listItem = listItems[0];
else
listItem = listItems[listItems.length - 1];
DomHandler.scrollInView(this.listViewChild.nativeElement, listItem);
}
this.movedUp = false;
this.movedDown = false;
}
}
get value() {
return this._value;
}
set value(val) {
this._value = val;
if (this.filterValue) {
this.filter();
}
}
onItemClick(event, item, index) {
this.itemTouched = false;
let selectedIndex = ObjectUtils.findIndexInList(item, this.selection);
let selected = (selectedIndex != -1);
let metaSelection = this.itemTouched ? false : this.metaKeySelection;
if (metaSelection) {
let metaKey = (event.metaKey || event.ctrlKey || event.shiftKey);
if (selected && metaKey) {
this._selection = this._selection.filter((val, index) => index !== selectedIndex);
}
else {
this._selection = (metaKey) ? this._selection ? [...this._selection] : [] : [];
ObjectUtils.insertIntoOrderedArray(item, index, this._selection, this.value);
}
}
else {
if (selected) {
this._selection = this._selection.filter((val, index) => index !== selectedIndex);
}
else {
this._selection = this._selection ? [...this._selection] : [];
ObjectUtils.insertIntoOrderedArray(item, index, this._selection, this.value);
}
}
//binding
this.selectionChange.emit(this._selection);
//event
this.onSelectionChange.emit({ originalEvent: event, value: this._selection });
}
onFilterKeyup(event) {
this.filterValue = event.target.value.trim().toLocaleLowerCase(this.filterLocale);
this.filter();
this.onFilterEvent.emit({
originalEvent: event,
value: this.visibleOptions
});
}
filter() {
let searchFields = this.filterBy.split(',');
this.visibleOptions = this.filterService.filter(this.value, searchFields, this.filterValue, this.filterMatchMode, this.filterLocale);
}
isItemVisible(item) {
if (this.filterValue && this.filterValue.trim().length) {
for (let i = 0; i < this.visibleOptions.length; i++) {
if (item == this.visibleOptions[i]) {
return true;
}
}
}
else {
return true;
}
}
onItemTouchEnd() {
this.itemTouched = true;
}
isSelected(item) {
return ObjectUtils.findIndexInList(item, this.selection) != -1;
}
isEmpty() {
return this.filterValue ? (!this.visibleOptions || this.visibleOptions.length === 0) : (!this.value || this.value.length === 0);
}
moveUp() {
if (this.selection) {
for (let i = 0; i < this.selection.length; i++) {
let selectedItem = this.selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, this.value);
if (selectedItemIndex != 0) {
let movedItem = this.value[selectedItemIndex];
let temp = this.value[selectedItemIndex - 1];
this.value[selectedItemIndex - 1] = movedItem;
this.value[selectedItemIndex] = temp;
}
else {
break;
}
}
if (this.dragdrop && this.filterValue)
this.filter();
this.movedUp = true;
this.onReorder.emit(this.selection);
}
}
moveTop() {
if (this.selection) {
for (let i = this.selection.length - 1; i >= 0; i--) {
let selectedItem = this.selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, this.value);
if (selectedItemIndex != 0) {
let movedItem = this.value.splice(selectedItemIndex, 1)[0];
this.value.unshift(movedItem);
}
else {
break;
}
}
if (this.dragdrop && this.filterValue)
this.filter();
this.onReorder.emit(this.selection);
this.listViewChild.nativeElement.scrollTop = 0;
}
}
moveDown() {
if (this.selection) {
for (let i = this.selection.length - 1; i >= 0; i--) {
let selectedItem = this.selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, this.value);
if (selectedItemIndex != (this.value.length - 1)) {
let movedItem = this.value[selectedItemIndex];
let temp = this.value[selectedItemIndex + 1];
this.value[selectedItemIndex + 1] = movedItem;
this.value[selectedItemIndex] = temp;
}
else {
break;
}
}
if (this.dragdrop && this.filterValue)
this.filter();
this.movedDown = true;
this.onReorder.emit(this.selection);
}
}
moveBottom() {
if (this.selection) {
for (let i = 0; i < this.selection.length; i++) {
let selectedItem = this.selection[i];
let selectedItemIndex = ObjectUtils.findIndexInList(selectedItem, this.value);
if (selectedItemIndex != (this.value.length - 1)) {
let movedItem = this.value.splice(selectedItemIndex, 1)[0];
this.value.push(movedItem);
}
else {
break;
}
}
if (this.dragdrop && this.filterValue)
this.filter();
this.onReorder.emit(this.selection);
this.listViewChild.nativeElement.scrollTop = this.listViewChild.nativeElement.scrollHeight;
}
}
onDrop(event) {
let previousIndex = event.previousIndex;
let currentIndex = event.currentIndex;
if (previousIndex !== currentIndex) {
if (this.visibleOptions) {
if (this.filterValue) {
previousIndex = ObjectUtils.findIndexInList(event.item.data, this.value);
currentIndex = ObjectUtils.findIndexInList(this.visibleOptions[currentIndex], this.value);
}
moveItemInArray(this.visibleOptions, event.previousIndex, event.currentIndex);
}
moveItemInArray(this.value, previousIndex, currentIndex);
this.onReorder.emit([event.item.data]);
}
}
onItemKeydown(event, item, index) {
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, index);
event.preventDefault();
break;
}
}
findNextItem(item) {
let nextItem = item.nextElementSibling;
if (nextItem)
return !DomHandler.hasClass(nextItem, 'p-orderlist-item') || DomHandler.isHidden(nextItem) ? this.findNextItem(nextItem) : nextItem;
else
return null;
}
findPrevItem(item) {
let prevItem = item.previousElementSibling;
if (prevItem)
return !DomHandler.hasClass(prevItem, 'p-orderlist-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-orderlist[${this.id}] {
flex-direction: column;
}
.p-orderlist[${this.id}] .p-orderlist-controls {
padding: var(--content-padding);
flex-direction: row;
}
.p-orderlist[${this.id}] .p-orderlist-controls .p-button {
margin-right: var(--inline-spacing);
margin-bottom: 0;
}
.p-orderlist[${this.id}] .p-orderlist-controls .p-button:last-child {
margin-right: 0;
}
}
`;
this.styleElement.innerHTML = innerHTML;
}
}
destroyStyle() {
if (this.styleElement) {
document.head.removeChild(this.styleElement);
this.styleElement = null;
``;
}
}
ngOnDestroy() {
this.destroyStyle();
}
}
OrderList.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: OrderList, deps: [{ token: i0.ElementRef }, { token: i0.ChangeDetectorRef }, { token: i1.FilterService }], target: i0.ɵɵFactoryTarget.Component });
OrderList.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.0.4", type: OrderList, selector: "p-orderList", inputs: { header: "header", style: "style", styleClass: "styleClass", listStyle: "listStyle", responsive: "responsive", filterBy: "filterBy", filterPlaceholder: "filterPlaceholder", filterLocale: "filterLocale", metaKeySelection: "metaKeySelection", dragdrop: "dragdrop", controlsPosition: "controlsPosition", ariaFilterLabel: "ariaFilterLabel", filterMatchMode: "filterMatchMode", breakpoint: "breakpoint", trackBy: "trackBy", selection: "selection", value: "value" }, outputs: { selectionChange: "selectionChange", onReorder: "onReorder", onSelectionChange: "onSelectionChange", onFilterEvent: "onFilterEvent" }, queries: [{ propertyName: "templates", predicate: PrimeTemplate }], viewQueries: [{ propertyName: "listViewChild", first: true, predicate: ["listelement"], descendants: true }], ngImport: i0, template: `
<div [ngClass]="{'p-orderlist p-component': true, 'p-orderlist-controls-left': controlsPosition === 'left',
'p-orderlist-controls-right': controlsPosition === 'right'}" [ngStyle]="style" [class]="styleClass">
<div class="p-orderlist-controls">
<button type="button" pButton pRipple icon="pi pi-angle-up" (click)="moveUp()"></button>
<button type="button" pButton pRipple icon="pi pi-angle-double-up" (click)="moveTop()"></button>
<button type="button" pButton pRipple icon="pi pi-angle-down" (click)="moveDown()"></button>
<button type="button" pButton pRipple icon="pi pi-angle-double-down" (click)="moveBottom()"></button>
</div>
<div class="p-orderlist-list-container">
<div class="p-orderlist-header" *ngIf="header || headerTemplate">
<div class="p-orderlist-title" *ngIf="!headerTemplate">{{header}}</div>
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
</div>
<div class="p-orderlist-filter-container" *ngIf="filterBy">
<div class="p-orderlist-filter">
<input type="text" role="textbox" (keyup)="onFilterKeyup($event)" class="p-orderlist-filter-input p-inputtext p-component" [attr.placeholder]="filterPlaceholder" [attr.aria-label]="ariaFilterLabel">
<span class="p-orderlist-filter-icon pi pi-search"></span>
</div>
</div>
<ul #listelement cdkDropList (cdkDropListDropped)="onDrop($event)" class="p-orderlist-list" [ngStyle]="listStyle">
<ng-template ngFor [ngForTrackBy]="trackBy" let-item [ngForOf]="value" let-i="index" let-l="last">
<li class="p-orderlist-item" tabindex="0" [ngClass]="{'p-highlight':isSelected(item)}" cdkDrag pRipple [cdkDragData]="item" [cdkDragDisabled]="!dragdrop"
(click)="onItemClick($event,item,i)" (touchend)="onItemTouchEnd()" (keydown)="onItemKeydown($event,item,i)"
*ngIf="isItemVisible(item)" role="option" [attr.aria-selected]="isSelected(item)">
<ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item, index: i}"></ng-container>
</li>
</ng-template>
<ng-container *ngIf="isEmpty() && (emptyMessageTemplate || emptyFilterMessageTemplate)">
<li *ngIf="!filterValue || !emptyFilterMessageTemplate" class="p-orderlist-empty-message">
<ng-container *ngTemplateOutlet="emptyMessageTemplate"></ng-container>
</li>
<li *ngIf="filterValue" class="p-orderlist-empty-message">
<ng-container *ngTemplateOutlet="emptyFilterMessageTemplate"></ng-container>
</li>
</ng-container>
</ul>
</div>
</div>
`, isInline: true, styles: [".p-orderlist{display:flex}.p-orderlist-controls{display:flex;flex-direction:column;justify-content:center}.p-orderlist-list-container{flex:1 1 auto}.p-orderlist-list{list-style-type:none;margin:0;padding:0;overflow:auto;min-height:12rem}.p-orderlist-item{display:block;cursor:pointer;overflow:hidden;position:relative}.p-orderlist-item:not(.cdk-drag-disabled){cursor:move}.p-orderlist-item.cdk-drag-placeholder{opacity:0}.p-orderlist-item.cdk-drag-animating{transition:transform .25s cubic-bezier(0,0,.2,1)}.p-orderlist.p-state-disabled .p-button,.p-orderlist.p-state-disabled .p-orderlist-item{cursor:default}.p-orderlist.p-state-disabled .p-orderlist-list{overflow:hidden}.p-orderlist-filter{position:relative}.p-orderlist-filter-icon{position:absolute;top:50%;margin-top:-.5rem}.p-orderlist-filter-input{width:100%}.p-orderlist-controls-right .p-orderlist-controls{order:2}.p-orderlist-controls-right .p-orderlist-list-container{order:1}.p-orderlist-list.cdk-drop-list-dragging .p-orderlist-item:not(.cdk-drag-placeholder){transition:transform .25s cubic-bezier(0,0,.2,1)}"], directives: [{ type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { type: i2.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { type: i3.ButtonDirective, selector: "[pButton]", inputs: ["iconPos", "loadingIcon", "label", "icon", "loading"] }, { type: i4.Ripple, selector: "[pRipple]" }, { type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i2.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet"] }, { type: i5.CdkDropList, selector: "[cdkDropList], cdk-drop-list", inputs: ["cdkDropListConnectedTo", "id", "cdkDropListEnterPredicate", "cdkDropListSortPredicate", "cdkDropListDisabled", "cdkDropListSortingDisabled", "cdkDropListAutoScrollDisabled", "cdkDropListOrientation", "cdkDropListLockAxis", "cdkDropListData", "cdkDropListAutoScrollStep"], outputs: ["cdkDropListDropped", "cdkDropListEntered", "cdkDropListExited", "cdkDropListSorted"], exportAs: ["cdkDropList"] }, { type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i5.CdkDrag, selector: "[cdkDrag]", inputs: ["cdkDragDisabled", "cdkDragStartDelay", "cdkDragLockAxis", "cdkDragConstrainPosition", "cdkDragPreviewClass", "cdkDragBoundary", "cdkDragRootElement", "cdkDragPreviewContainer", "cdkDragData", "cdkDragFreeDragPosition"], outputs: ["cdkDragStarted", "cdkDragReleased", "cdkDragEnded", "cdkDragEntered", "cdkDragExited", "cdkDragDropped", "cdkDragMoved"], exportAs: ["cdkDrag"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: OrderList, decorators: [{
type: Component,
args: [{
selector: 'p-orderList',
template: `
<div [ngClass]="{'p-orderlist p-component': true, 'p-orderlist-controls-left': controlsPosition === 'left',
'p-orderlist-controls-right': controlsPosition === 'right'}" [ngStyle]="style" [class]="styleClass">
<div class="p-orderlist-controls">
<button type="button" pButton pRipple icon="pi pi-angle-up" (click)="moveUp()"></button>
<button type="button" pButton pRipple icon="pi pi-angle-double-up" (click)="moveTop()"></button>
<button type="button" pButton pRipple icon="pi pi-angle-down" (click)="moveDown()"></button>
<button type="button" pButton pRipple icon="pi pi-angle-double-down" (click)="moveBottom()"></button>
</div>
<div class="p-orderlist-list-container">
<div class="p-orderlist-header" *ngIf="header || headerTemplate">
<div class="p-orderlist-title" *ngIf="!headerTemplate">{{header}}</div>
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
</div>
<div class="p-orderlist-filter-container" *ngIf="filterBy">
<div class="p-orderlist-filter">
<input type="text" role="textbox" (keyup)="onFilterKeyup($event)" class="p-orderlist-filter-input p-inputtext p-component" [attr.placeholder]="filterPlaceholder" [attr.aria-label]="ariaFilterLabel">
<span class="p-orderlist-filter-icon pi pi-search"></span>
</div>
</div>
<ul #listelement cdkDropList (cdkDropListDropped)="onDrop($event)" class="p-orderlist-list" [ngStyle]="listStyle">
<ng-template ngFor [ngForTrackBy]="trackBy" let-item [ngForOf]="value" let-i="index" let-l="last">
<li class="p-orderlist-item" tabindex="0" [ngClass]="{'p-highlight':isSelected(item)}" cdkDrag pRipple [cdkDragData]="item" [cdkDragDisabled]="!dragdrop"
(click)="onItemClick($event,item,i)" (touchend)="onItemTouchEnd()" (keydown)="onItemKeydown($event,item,i)"
*ngIf="isItemVisible(item)" role="option" [attr.aria-selected]="isSelected(item)">
<ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item, index: i}"></ng-container>
</li>
</ng-template>
<ng-container *ngIf="isEmpty() && (emptyMessageTemplate || emptyFilterMessageTemplate)">
<li *ngIf="!filterValue || !emptyFilterMessageTemplate" class="p-orderlist-empty-message">
<ng-container *ngTemplateOutlet="emptyMessageTemplate"></ng-container>
</li>
<li *ngIf="filterValue" class="p-orderlist-empty-message">
<ng-container *ngTemplateOutlet="emptyFilterMessageTemplate"></ng-container>
</li>
</ng-container>
</ul>
</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styleUrls: ['./orderlist.css']
}]
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.ChangeDetectorRef }, { type: i1.FilterService }]; }, propDecorators: { header: [{
type: Input
}], style: [{
type: Input
}], styleClass: [{
type: Input
}], listStyle: [{
type: Input
}], responsive: [{
type: Input
}], filterBy: [{
type: Input
}], filterPlaceholder: [{
type: Input
}], filterLocale: [{
type: Input
}], metaKeySelection: [{
type: Input
}], dragdrop: [{
type: Input
}], controlsPosition: [{
type: Input
}], ariaFilterLabel: [{
type: Input
}], filterMatchMode: [{
type: Input
}], breakpoint: [{
type: Input
}], selectionChange: [{
type: Output
}], trackBy: [{
type: Input
}], onReorder: [{
type: Output
}], onSelectionChange: [{
type: Output
}], onFilterEvent: [{
type: Output
}], listViewChild: [{
type: ViewChild,
args: ['listelement']
}], templates: [{
type: ContentChildren,
args: [PrimeTemplate]
}], selection: [{
type: Input
}], value: [{
type: Input
}] } });
class OrderListModule {
}
OrderListModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: OrderListModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
OrderListModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: OrderListModule, declarations: [OrderList], imports: [CommonModule, ButtonModule, SharedModule, RippleModule, DragDropModule], exports: [OrderList, SharedModule, DragDropModule] });
OrderListModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: OrderListModule, imports: [[CommonModule, ButtonModule, SharedModule, RippleModule, DragDropModule], SharedModule, DragDropModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: OrderListModule, decorators: [{
type: NgModule,
args: [{
imports: [CommonModule, ButtonModule, SharedModule, RippleModule, DragDropModule],
exports: [OrderList, SharedModule, DragDropModule],
declarations: [OrderList]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { OrderList, OrderListModule };
//# sourceMappingURL=primeng-orderlist.js.map