primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 18.4 kB
JavaScript
import { forwardRef, EventEmitter, ElementRef, ChangeDetectorRef, Input, Output, ViewChild, ContentChild, ContentChildren, Component, ChangeDetectionStrategy, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Header, Footer, PrimeTemplate, SharedModule } from 'primeng/api';
import { DomHandler } from 'primeng/dom';
import { ObjectUtils, FilterUtils } from 'primeng/utils';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const LISTBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Listbox),
multi: true
};
let Listbox = class Listbox {
constructor(el, cd) {
this.el = el;
this.cd = cd;
this.checkbox = false;
this.filter = false;
this.filterMode = 'contains';
this.metaKeySelection = true;
this.showToggleAll = true;
this.onChange = new EventEmitter();
this.onClick = new EventEmitter();
this.onDblClick = new EventEmitter();
this.onModelChange = () => { };
this.onModelTouched = () => { };
this.disabledSelectedOptions = [];
}
get options() {
return this._options;
}
set options(val) {
let opts = this.optionLabel ? ObjectUtils.generateSelectItems(val, this.optionLabel) : val;
this._options = opts;
}
get filterValue() {
return this._filterValue;
}
set filterValue(val) {
this._filterValue = val;
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'item':
this.itemTemplate = item.template;
break;
default:
this.itemTemplate = item.template;
break;
}
});
}
writeValue(value) {
this.value = value;
this.setDisabledSelectedOptions();
this.cd.markForCheck();
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
}
onOptionClick(event, option) {
if (this.disabled || option.disabled || this.readonly) {
return;
}
if (this.multiple) {
if (this.checkbox)
this.onOptionClickCheckbox(event, option);
else
this.onOptionClickMultiple(event, option);
}
else {
this.onOptionClickSingle(event, option);
}
this.onClick.emit({
originalEvent: event,
option: option,
value: this.value
});
this.optionTouched = false;
}
onOptionTouchEnd(event, option) {
if (this.disabled || option.disabled || this.readonly) {
return;
}
this.optionTouched = true;
}
onOptionDoubleClick(event, option) {
if (this.disabled || option.disabled || this.readonly) {
return;
}
this.onDblClick.emit({
originalEvent: event,
option: option,
value: this.value
});
}
onOptionClickSingle(event, option) {
let selected = this.isSelected(option);
let valueChanged = false;
let metaSelection = this.optionTouched ? false : this.metaKeySelection;
if (metaSelection) {
let metaKey = (event.metaKey || event.ctrlKey);
if (selected) {
if (metaKey) {
this.value = null;
valueChanged = true;
}
}
else {
this.value = option.value;
valueChanged = true;
}
}
else {
this.value = selected ? null : option.value;
valueChanged = true;
}
if (valueChanged) {
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
}
}
onOptionClickMultiple(event, option) {
let selected = this.isSelected(option);
let valueChanged = false;
let metaSelection = this.optionTouched ? false : this.metaKeySelection;
if (metaSelection) {
let metaKey = (event.metaKey || event.ctrlKey);
if (selected) {
if (metaKey) {
this.removeOption(option);
}
else {
this.value = [option.value];
}
valueChanged = true;
}
else {
this.value = (metaKey) ? this.value || [] : [];
this.value = [...this.value, option.value];
valueChanged = true;
}
}
else {
if (selected) {
this.removeOption(option);
}
else {
this.value = [...this.value || [], option.value];
}
valueChanged = true;
}
if (valueChanged) {
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
}
}
onOptionClickCheckbox(event, option) {
if (this.disabled || this.readonly) {
return;
}
let selected = this.isSelected(option);
if (selected) {
this.removeOption(option);
}
else {
this.value = this.value ? this.value : [];
this.value = [...this.value, option.value];
}
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
}
removeOption(option) {
this.value = this.value.filter(val => !ObjectUtils.equals(val, option.value, this.dataKey));
}
isSelected(option) {
let selected = false;
if (this.multiple) {
if (this.value) {
for (let val of this.value) {
if (ObjectUtils.equals(val, option.value, this.dataKey)) {
selected = true;
break;
}
}
}
}
else {
selected = ObjectUtils.equals(this.value, option.value, this.dataKey);
}
return selected;
}
get allChecked() {
if (this.filterValue) {
return this.allFilteredSelected();
}
else {
let optionCount = this.getEnabledOptionCount();
let disabledSelectedOptionCount = this.disabledSelectedOptions.length;
return this.value && this.options && (this.value.length > 0 && this.value.length == optionCount + disabledSelectedOptionCount);
}
}
getEnabledOptionCount() {
if (this.options) {
let count = 0;
for (let opt of this.options) {
if (!opt.disabled) {
count++;
}
}
return count;
}
else {
return 0;
}
}
allFilteredSelected() {
let allSelected;
let options = this.filterValue ? this.getFilteredOptions() : this.options;
if (this.value && options && options.length) {
allSelected = true;
for (let opt of this.options) {
if (this.isItemVisible(opt)) {
if (!this.isSelected(opt)) {
allSelected = false;
break;
}
}
}
}
return allSelected;
}
onFilter(event) {
this._filterValue = event.target.value;
}
toggleAll(event) {
if (this.disabled || this.readonly || !this.options || this.options.length === 0) {
return;
}
if (this.allChecked) {
if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) {
let value = [];
value = [...this.disabledSelectedOptions];
this.value = value;
}
else {
this.value = [];
}
}
else {
if (this.options) {
this.value = [];
if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) {
this.value = [...this.disabledSelectedOptions];
}
for (let i = 0; i < this.options.length; i++) {
let opt = this.options[i];
if (this.isItemVisible(opt) && !opt.disabled) {
this.value.push(opt.value);
}
}
}
}
this.onModelChange(this.value);
this.onChange.emit({ originalEvent: event, value: this.value });
event.preventDefault();
}
isItemVisible(option) {
if (this.filterValue) {
let visible;
let filterText = ObjectUtils.removeAccents(this.filterValue).toLowerCase();
if (this.filterMode) {
visible = FilterUtils[this.filterMode](option.label, this.filterValue);
}
else {
visible = true;
}
return visible;
}
else {
return true;
}
}
onInputFocus(event) {
this.focus = true;
}
onInputBlur(event) {
this.focus = false;
}
onOptionKeyDown(event, option) {
if (this.readonly) {
return;
}
let item = event.currentTarget;
switch (event.which) {
//down
case 40:
var nextItem = this.findNextItem(item);
if (nextItem) {
nextItem.focus();
}
event.preventDefault();
break;
//up
case 38:
var prevItem = this.findPrevItem(item);
if (prevItem) {
prevItem.focus();
}
event.preventDefault();
break;
//enter
case 13:
this.onOptionClick(event, option);
event.preventDefault();
break;
}
}
findNextItem(item) {
let nextItem = item.nextElementSibling;
if (nextItem)
return DomHandler.hasClass(nextItem, 'ui-state-disabled') || DomHandler.isHidden(nextItem) ? this.findNextItem(nextItem) : nextItem;
else
return null;
}
findPrevItem(item) {
let prevItem = item.previousElementSibling;
if (prevItem)
return DomHandler.hasClass(prevItem, 'ui-state-disabled') || DomHandler.isHidden(prevItem) ? this.findPrevItem(prevItem) : prevItem;
else
return null;
}
getFilteredOptions() {
let filteredOptions = [];
if (this.filterValue) {
for (let i = 0; i < this.options.length; i++) {
let opt = this.options[i];
if (this.isItemVisible(opt) && !opt.disabled) {
filteredOptions.push(opt);
}
}
return filteredOptions;
}
else {
return this.options;
}
}
onHeaderCheckboxFocus() {
this.headerCheckboxFocus = true;
}
onHeaderCheckboxBlur() {
this.headerCheckboxFocus = false;
}
setDisabledSelectedOptions() {
if (this.options) {
this.disabledSelectedOptions = [];
if (this.value) {
for (let opt of this.options) {
if (opt.disabled && this.isSelected(opt)) {
this.disabledSelectedOptions.push(opt.value);
}
}
}
}
}
};
Listbox.ctorParameters = () => [
{ type: ElementRef },
{ type: ChangeDetectorRef }
];
__decorate([
Input()
], Listbox.prototype, "multiple", void 0);
__decorate([
Input()
], Listbox.prototype, "style", void 0);
__decorate([
Input()
], Listbox.prototype, "styleClass", void 0);
__decorate([
Input()
], Listbox.prototype, "listStyle", void 0);
__decorate([
Input()
], Listbox.prototype, "readonly", void 0);
__decorate([
Input()
], Listbox.prototype, "disabled", void 0);
__decorate([
Input()
], Listbox.prototype, "checkbox", void 0);
__decorate([
Input()
], Listbox.prototype, "filter", void 0);
__decorate([
Input()
], Listbox.prototype, "filterMode", void 0);
__decorate([
Input()
], Listbox.prototype, "metaKeySelection", void 0);
__decorate([
Input()
], Listbox.prototype, "dataKey", void 0);
__decorate([
Input()
], Listbox.prototype, "showToggleAll", void 0);
__decorate([
Input()
], Listbox.prototype, "optionLabel", void 0);
__decorate([
Input()
], Listbox.prototype, "ariaFilterLabel", void 0);
__decorate([
Input()
], Listbox.prototype, "filterPlaceHolder", void 0);
__decorate([
Output()
], Listbox.prototype, "onChange", void 0);
__decorate([
Output()
], Listbox.prototype, "onClick", void 0);
__decorate([
Output()
], Listbox.prototype, "onDblClick", void 0);
__decorate([
ViewChild('headerchkbox')
], Listbox.prototype, "headerCheckboxViewChild", void 0);
__decorate([
ContentChild(Header)
], Listbox.prototype, "headerFacet", void 0);
__decorate([
ContentChild(Footer)
], Listbox.prototype, "footerFacet", void 0);
__decorate([
ContentChildren(PrimeTemplate)
], Listbox.prototype, "templates", void 0);
__decorate([
Input()
], Listbox.prototype, "options", null);
__decorate([
Input()
], Listbox.prototype, "filterValue", null);
Listbox = __decorate([
Component({
selector: 'p-listbox',
template: `
<div [ngClass]="{'ui-listbox ui-inputtext ui-widget ui-widget-content ui-corner-all':true,'ui-state-disabled':disabled,'ui-state-focus':focus}" [ngStyle]="style" [class]="styleClass">
<div class="ui-helper-hidden-accessible">
<input type="text" readonly="readonly" (focus)="onInputFocus($event)" (blur)="onInputBlur($event)">
</div>
<div class="ui-widget-header ui-corner-all ui-listbox-header ui-helper-clearfix" *ngIf="headerFacet">
<ng-content select="p-header"></ng-content>
</div>
<div class="ui-widget-header ui-corner-all ui-listbox-header ui-helper-clearfix" *ngIf="(checkbox && multiple && showToggleAll) || filter" [ngClass]="{'ui-listbox-header-w-checkbox': checkbox}">
<div class="ui-chkbox ui-widget" *ngIf="checkbox && multiple && showToggleAll">
<div class="ui-helper-hidden-accessible">
<input type="checkbox" readonly="readonly" [checked]="allChecked" (focus)="onHeaderCheckboxFocus()" (blur)="onHeaderCheckboxBlur()" (keydown.space)="toggleAll($event)">
</div>
<div #headerchkbox class="ui-chkbox-box ui-widget ui-corner-all ui-state-default" [ngClass]="{'ui-state-active': allChecked, 'ui-state-focus': headerCheckboxFocus}" (click)="toggleAll($event)">
<span class="ui-chkbox-icon ui-clickable" [ngClass]="{'pi pi-check':allChecked}"></span>
</div>
</div>
<div class="ui-listbox-filter-container" *ngIf="filter">
<input type="text" [value]="filterValue||''" (input)="onFilter($event)" class="ui-inputtext ui-widget ui-state-default ui-corner-all" [disabled]="disabled" [attr.placeholder]="filterPlaceHolder" [attr.aria-label]="ariaFilterLabel">
<span class="ui-listbox-filter-icon pi pi-search"></span>
</div>
</div>
<div class="ui-listbox-list-wrapper" [ngStyle]="listStyle">
<ul class="ui-listbox-list" role="listbox" aria-multiselectable="multiple">
<li *ngFor="let option of options; let i = index;" [style.display]="isItemVisible(option) ? 'block' : 'none'" [attr.tabindex]="option.disabled ? null : '0'"
[ngClass]="{'ui-listbox-item ui-corner-all':true,'ui-state-highlight':isSelected(option), 'ui-state-disabled': option.disabled}" role="option" [attr.aria-label]="option.label"
[attr.aria-selected]="isSelected(option)" (click)="onOptionClick($event,option)" (dblclick)="onOptionDoubleClick($event,option)" (touchend)="onOptionTouchEnd($event,option)" (keydown)="onOptionKeyDown($event,option)">
<div class="ui-chkbox ui-widget" *ngIf="checkbox && multiple">
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default" [ngClass]="{'ui-state-active':isSelected(option)}">
<span class="ui-chkbox-icon ui-clickable" [ngClass]="{'pi pi-check':isSelected(option)}"></span>
</div>
</div>
<span *ngIf="!itemTemplate">{{option.label}}</span>
<ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: option, index: i}"></ng-container>
</li>
</ul>
</div>
<div class="ui-listbox-footer ui-widget-header ui-corner-all" *ngIf="footerFacet">
<ng-content select="p-footer"></ng-content>
</div>
</div>
`,
providers: [LISTBOX_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.Default
})
], Listbox);
let ListboxModule = class ListboxModule {
};
ListboxModule = __decorate([
NgModule({
imports: [CommonModule, SharedModule],
exports: [Listbox, SharedModule],
declarations: [Listbox]
})
], ListboxModule);
/**
* Generated bundle index. Do not edit.
*/
export { LISTBOX_VALUE_ACCESSOR, Listbox, ListboxModule };
//# sourceMappingURL=primeng-listbox.js.map