primeng
Version:
[](https://badge.fury.io/js/primeng) [](https://www.npmjs.com/package/primeng) [ • 206 kB
JavaScript
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, ContentChild, ContentChildren, EventEmitter, forwardRef, Input, NgModule, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { Footer, Header, PrimeTemplate, SharedModule, TranslationKeys } from 'primeng/api';
import { DomHandler } from 'primeng/dom';
import { OverlayModule } from 'primeng/overlay';
import { RippleModule } from 'primeng/ripple';
import { ScrollerModule } from 'primeng/scroller';
import { TooltipModule } from 'primeng/tooltip';
import { ObjectUtils } from 'primeng/utils';
import { CheckIcon } from 'primeng/icons/check';
import { SearchIcon } from 'primeng/icons/search';
import { TimesCircleIcon } from 'primeng/icons/timescircle';
import { TimesIcon } from 'primeng/icons/times';
import { ChevronDownIcon } from 'primeng/icons/chevrondown';
import * as i0 from "@angular/core";
import * as i1 from "@angular/common";
import * as i2 from "primeng/ripple";
import * as i3 from "primeng/api";
import * as i4 from "primeng/overlay";
import * as i5 from "primeng/tooltip";
import * as i6 from "primeng/scroller";
export const MULTISELECT_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MultiSelect),
multi: true
};
class MultiSelectItem {
option;
selected;
label;
disabled;
itemSize;
template;
checkIconTemplate;
onClick = new EventEmitter();
onKeydown = new EventEmitter();
onOptionClick(event) {
this.onClick.emit({
originalEvent: event,
option: this.option,
selected: this.selected
});
}
onOptionKeydown(event) {
this.onKeydown.emit({
originalEvent: event,
option: this.option
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.0", ngImport: i0, type: MultiSelectItem, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.0", type: MultiSelectItem, selector: "p-multiSelectItem", inputs: { option: "option", selected: "selected", label: "label", disabled: "disabled", itemSize: "itemSize", template: "template", checkIconTemplate: "checkIconTemplate" }, outputs: { onClick: "onClick", onKeydown: "onKeydown" }, host: { classAttribute: "p-element" }, ngImport: i0, template: `
<li
class="p-multiselect-item"
(click)="onOptionClick($event)"
(keydown)="onOptionKeydown($event)"
[attr.aria-label]="label"
[attr.tabindex]="disabled ? null : '0'"
[ngStyle]="{ height: itemSize + 'px' }"
[ngClass]="{ 'p-highlight': selected, 'p-disabled': disabled }"
pRipple
>
<div class="p-checkbox p-component">
<div class="p-checkbox-box" [ngClass]="{ 'p-highlight': selected }">
<ng-container *ngIf="selected">
<CheckIcon *ngIf="!checkIconTemplate" [styleClass]="'p-checkbox-icon'" />
<span *ngIf="checkIconTemplate" class="p-checkbox-icon">
<ng-template *ngTemplateOutlet="checkIconTemplate"></ng-template>
</span>
</ng-container>
</div>
</div>
<span *ngIf="!template">{{ label }}</span>
<ng-container *ngTemplateOutlet="template; context: { $implicit: option }"></ng-container>
</li>
`, isInline: true, dependencies: [{ kind: "directive", type: i0.forwardRef(function () { return i1.NgClass; }), selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i0.forwardRef(function () { return i1.NgIf; }), selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i0.forwardRef(function () { return i1.NgTemplateOutlet; }), selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i0.forwardRef(function () { return i1.NgStyle; }), selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "directive", type: i0.forwardRef(function () { return i2.Ripple; }), selector: "[pRipple]" }, { kind: "component", type: i0.forwardRef(function () { return CheckIcon; }), selector: "CheckIcon" }], encapsulation: i0.ViewEncapsulation.None });
}
export { MultiSelectItem };
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.0", ngImport: i0, type: MultiSelectItem, decorators: [{
type: Component,
args: [{
selector: 'p-multiSelectItem',
template: `
<li
class="p-multiselect-item"
(click)="onOptionClick($event)"
(keydown)="onOptionKeydown($event)"
[attr.aria-label]="label"
[attr.tabindex]="disabled ? null : '0'"
[ngStyle]="{ height: itemSize + 'px' }"
[ngClass]="{ 'p-highlight': selected, 'p-disabled': disabled }"
pRipple
>
<div class="p-checkbox p-component">
<div class="p-checkbox-box" [ngClass]="{ 'p-highlight': selected }">
<ng-container *ngIf="selected">
<CheckIcon *ngIf="!checkIconTemplate" [styleClass]="'p-checkbox-icon'" />
<span *ngIf="checkIconTemplate" class="p-checkbox-icon">
<ng-template *ngTemplateOutlet="checkIconTemplate"></ng-template>
</span>
</ng-container>
</div>
</div>
<span *ngIf="!template">{{ label }}</span>
<ng-container *ngTemplateOutlet="template; context: { $implicit: option }"></ng-container>
</li>
`,
encapsulation: ViewEncapsulation.None,
host: {
class: 'p-element'
}
}]
}], propDecorators: { option: [{
type: Input
}], selected: [{
type: Input
}], label: [{
type: Input
}], disabled: [{
type: Input
}], itemSize: [{
type: Input
}], template: [{
type: Input
}], checkIconTemplate: [{
type: Input
}], onClick: [{
type: Output
}], onKeydown: [{
type: Output
}] } });
/**
* MultiSelect is used to select multiple items from a collection.
* @group Components
*/
class MultiSelect {
el;
renderer;
cd;
zone;
filterService;
config;
overlayService;
/**
* Inline style of the element.
* @group Props
*/
style;
/**
* Style class of the element.
* @group Props
*/
styleClass;
/**
* Inline style of the overlay panel.
* @group Props
*/
panelStyle;
/**
* Style class of the overlay panel element.
* @group Props
*/
panelStyleClass;
/**
* Identifier of the focus input to match a label defined for the component.
* @group Props
*/
inputId;
/**
* When present, it specifies that the element should be disabled.
* @group Props
*/
disabled;
/**
* When present, it specifies that the component cannot be edited.
* @group Props
*/
readonly;
/**
* Whether to display options as grouped when nested options are provided.
* @group Props
*/
group;
/**
* When specified, displays an input field to filter the items on keyup.
* @group Props
*/
filter = true;
/**
* Defines placeholder of the filter input.
* @group Props
*/
filterPlaceHolder;
/**
* Locale to use in filtering. The default locale is the host environment's current locale.
* @group Props
*/
filterLocale;
/**
* Specifies the visibility of the options panel.
* @group Props
*/
overlayVisible;
/**
* Index of the element in tabbing order.
* @group Props
*/
tabindex;
/**
* Target element to attach the overlay, valid values are "body" or a local ng-template variable of another element (note: use binding with brackets for template variables, e.g. [appendTo]="mydiv" for a div element having #mydiv as variable name).
* @group Props
*/
appendTo;
/**
* A property to uniquely identify a value in options.
* @group Props
*/
dataKey;
/**
* Name of the input element.
* @group Props
*/
name;
/**
* Label of the input for accessibility.
* @group Props
*/
label;
/**
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
* @group Props
*/
ariaLabelledBy;
/**
* Whether to show labels of selected item labels or use default label.
* @group Props
*/
displaySelectedLabel = true;
/**
* Decides how many selected item labels to show at most.
* @group Props
*/
maxSelectedLabels = 3;
/**
* Label to display after exceeding max selected labels e.g. ({0} items selected), defaults "ellipsis" keyword to indicate a text-overflow.
* @group Props
*/
selectedItemsLabel = 'ellipsis';
/**
* Whether to show the checkbox at header to toggle all items at once.
* @group Props
*/
showToggleAll = true;
/**
* Text to display when filtering does not return any results.
* @group Props
*/
emptyFilterMessage = '';
/**
* Text to display when there is no data. Defaults to global value in i18n translation configuration.
* @group Props
*/
emptyMessage = '';
/**
* Clears the filter value when hiding the dropdown.
* @group Props
*/
resetFilterOnHide = false;
/**
* Icon class of the dropdown icon.
* @group Props
*/
dropdownIcon;
/**
* Name of the label field of an option.
* @group Props
*/
optionLabel;
/**
* Name of the value field of an option.
* @group Props
*/
optionValue;
/**
* Name of the disabled field of an option.
* @group Props
*/
optionDisabled;
/**
* Name of the label field of an option group.
* @group Props
*/
optionGroupLabel;
/**
* Name of the options field of an option group.
* @group Props
*/
optionGroupChildren = 'items';
/**
* Whether to show the header.
* @group Props
*/
showHeader = true;
/**
* When filtering is enabled, filterBy decides which field or fields (comma separated) to search against.
* @group Props
*/
filterBy;
/**
* Height of the viewport in pixels, a scrollbar is defined if height of list exceeds this value.
* @group Props
*/
scrollHeight = '200px';
/**
* Defines if data is loaded and interacted with in lazy manner.
* @group Props
*/
lazy = false;
/**
* Whether the data should be loaded on demand during scroll.
* @group Props
*/
virtualScroll;
/**
* Height of an item in the list for VirtualScrolling.
* @group Props
*/
virtualScrollItemSize;
/**
* Whether to use the scroller feature. The properties of scroller component can be used like an object in it.
* @group Props
*/
virtualScrollOptions;
/**
* Whether to use overlay API feature. The properties of overlay API can be used like an object in it.
* @group Props
*/
overlayOptions;
/**
* Defines a string that labels the filter input.
* @group Props
*/
ariaFilterLabel;
/**
* Defines how the items are filtered.
* @group Props
*/
filterMatchMode = 'contains';
/**
* Advisory information to display in a tooltip on hover.
* @group Props
*/
tooltip = '';
/**
* Position of the tooltip.
* @group Props
*/
tooltipPosition = 'right';
/**
* Type of CSS position.
* @group Props
*/
tooltipPositionStyle = 'absolute';
/**
* Style class of the tooltip.
* @group Props
*/
tooltipStyleClass;
/**
* Applies focus to the filter element when the overlay is shown.
* @group Props
*/
autofocusFilter = true;
/**
* Defines how the selected items are displayed.
* @group Props
*/
display = 'comma';
/**
* Defines the autocomplete is active.
* @group Props
*/
autocomplete = 'on';
/**
* When enabled, a clear icon is displayed to clear the value.
* @group Props
*/
showClear = false;
/**
* @deprecated since v14.2.0, use overlayOptions property instead.
* Whether to automatically manage layering.
* @group Props
*/
get autoZIndex() {
return this._autoZIndex;
}
set autoZIndex(val) {
this._autoZIndex = val;
console.warn('The autoZIndex property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* @deprecated since v14.2.0, use overlayOptions property instead.
* Base zIndex value to use in layering.
* @group Props
*/
get baseZIndex() {
return this._baseZIndex;
}
set baseZIndex(val) {
this._baseZIndex = val;
console.warn('The baseZIndex property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* Transition options of the show animation.
* @group Props
* @deprecated since v14.2.0, use overlayOptions property instead.
*/
get showTransitionOptions() {
return this._showTransitionOptions;
}
set showTransitionOptions(val) {
this._showTransitionOptions = val;
console.warn('The showTransitionOptions property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* Transition options of the hide animation.
* @group Props
* @deprecated since v14.2.0, use overlayOptions property instead.
*/
get hideTransitionOptions() {
return this._hideTransitionOptions;
}
set hideTransitionOptions(val) {
this._hideTransitionOptions = val;
console.warn('The hideTransitionOptions property is deprecated since v14.2.0, use overlayOptions property instead.');
}
/**
* Label to display when there are no selections.
* @group Props
* @deprecated Use placeholder instead.
*/
set defaultLabel(val) {
this._defaultLabel = val;
this.updateLabel();
}
get defaultLabel() {
return this._defaultLabel;
}
/**
* Label to display when there are no selections.
* @group Props
*/
set placeholder(val) {
this._placeholder = val;
this.updateLabel();
}
get placeholder() {
return this._placeholder;
}
/**
* An array of objects to display as the available options.
* @group Props
*/
get options() {
return this._options;
}
set options(val) {
this._options = val;
this.updateLabel();
}
/**
* When specified, filter displays with this value.
* @group Props
*/
get filterValue() {
return this._filterValue;
}
set filterValue(val) {
this._filterValue = val;
this.activateFilter();
}
/**
* Item size of item to be virtual scrolled.
* @group Props
* @deprecated use virtualScrollItemSize property instead.
*/
get itemSize() {
return this._itemSize;
}
set itemSize(val) {
this._itemSize = val;
console.warn('The itemSize property is deprecated, use virtualScrollItemSize property instead.');
}
/**
* Number of maximum options that can be selected.
* @group Props
*/
get selectionLimit() {
return this._selectionLimit;
}
set selectionLimit(val) {
this._selectionLimit = val;
this.checkSelectionLimit();
}
containerViewChild;
overlayViewChild;
filterInputChild;
accessibleViewChild;
itemsViewChild;
scroller;
footerFacet;
headerFacet;
templates;
/**
* Callback to invoke when value changes.
* @param {MultiSelectChangeEvent} event - Custom change event.
* @group Emits
*/
onChange = new EventEmitter();
/**
* Callback to invoke when data is filtered.
* @param {MultiSelectFilterEvent} event - Custom filter event.
* @group Emits
*/
onFilter = new EventEmitter();
/**
* Callback to invoke when multiselect receives focus.
* @param {MultiSelectFocusEvent} event - Custom focus event.
* @group Emits
*/
onFocus = new EventEmitter();
/**
* Callback to invoke when multiselect loses focus.
* @param {MultiSelectBlurEvent} event - Custom blur event.
* @group Emits
*/
onBlur = new EventEmitter();
/**
* Callback to invoke when component is clicked.
* @param {Event} event - Browser event.
* @group Emits
*/
onClick = new EventEmitter();
/**
* Callback to invoke when input field is cleared.
* @group Emits
*/
onClear = new EventEmitter();
/**
* Callback to invoke when overlay panel becomes visible.
* @group Emits
*/
onPanelShow = new EventEmitter();
/**
* Callback to invoke when overlay panel becomes hidden.
* @group Emits
*/
onPanelHide = new EventEmitter();
/**
* Callback to invoke in lazy mode to load new data.
* @param {MultiSelectLazyLoadEvent} event - Lazy load event.
* @group Emits
*/
onLazyLoad = new EventEmitter();
/**
* Callback to invoke in lazy mode to load new data.
* @param {MultiSelectRemoveEvent} event - Remove event.
* @group Emits
*/
onRemove = new EventEmitter();
_autoZIndex;
_baseZIndex;
_showTransitionOptions;
_hideTransitionOptions;
_defaultLabel;
_placeholder;
_itemSize;
_selectionLimit;
value;
_filteredOptions;
onModelChange = () => { };
onModelTouched = () => { };
valuesAsString;
focus;
filled;
_filterValue;
filtered;
itemTemplate;
groupTemplate;
loaderTemplate;
headerTemplate;
filterTemplate;
footerTemplate;
emptyFilterTemplate;
emptyTemplate;
selectedItemsTemplate;
checkIconTemplate;
filterIconTemplate;
removeTokenIconTemplate;
closeIconTemplate;
clearIconTemplate;
dropdownIconTemplate;
headerCheckboxFocus;
filterOptions;
_options;
maxSelectionLimitReached;
preventModelTouched;
preventDocumentDefault;
constructor(el, renderer, cd, zone, filterService, config, overlayService) {
this.el = el;
this.renderer = renderer;
this.cd = cd;
this.zone = zone;
this.filterService = filterService;
this.config = config;
this.overlayService = overlayService;
}
ngOnInit() {
this.updateLabel();
if (this.filterBy) {
this.filterOptions = {
filter: (value) => this.onFilterInputChange(value),
reset: () => this.resetFilter()
};
}
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'item':
this.itemTemplate = item.template;
break;
case 'group':
this.groupTemplate = item.template;
break;
case 'selectedItems':
this.selectedItemsTemplate = item.template;
break;
case 'header':
this.headerTemplate = item.template;
break;
case 'filter':
this.filterTemplate = item.template;
break;
case 'emptyfilter':
this.emptyFilterTemplate = item.template;
break;
case 'empty':
this.emptyTemplate = item.template;
break;
case 'footer':
this.footerTemplate = item.template;
break;
case 'loader':
this.loaderTemplate = item.template;
break;
case 'checkicon':
this.checkIconTemplate = item.template;
break;
case 'filtericon':
this.filterIconTemplate = item.template;
break;
case 'removetokenicon':
this.removeTokenIconTemplate = item.template;
break;
case 'closeicon':
this.closeIconTemplate = item.template;
break;
case 'clearicon':
this.clearIconTemplate = item.template;
break;
case 'dropdownicon':
this.dropdownIconTemplate = item.template;
break;
default:
this.itemTemplate = item.template;
break;
}
});
}
ngAfterViewInit() {
if (this.overlayVisible) {
this.show();
}
}
ngAfterViewChecked() {
if (this.filtered) {
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.overlayViewChild?.alignOverlay();
}, 1);
});
this.filtered = false;
}
}
getOptionLabel(option) {
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : option && option.label != undefined ? option.label : option;
}
getOptionValue(option) {
return this.optionValue ? ObjectUtils.resolveFieldData(option, this.optionValue) : !this.optionLabel && option && option.value !== undefined ? option.value : option;
}
getOptionGroupLabel(optionGroup) {
return this.optionGroupLabel ? ObjectUtils.resolveFieldData(optionGroup, this.optionGroupLabel) : optionGroup && optionGroup.label != undefined ? optionGroup.label : optionGroup;
}
getOptionGroupChildren(optionGroup) {
return this.optionGroupChildren ? ObjectUtils.resolveFieldData(optionGroup, this.optionGroupChildren) : optionGroup.items;
}
isOptionDisabled(option) {
let disabled = this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : option && option.disabled !== undefined ? option.disabled : false;
return disabled || (this.maxSelectionLimitReached && !this.isSelected(option));
}
writeValue(value) {
this.value = value;
this.updateLabel();
this.updateFilledState();
this.checkSelectionLimit();
this.cd.markForCheck();
}
checkSelectionLimit() {
if (this.selectionLimit && this.value && this.value.length === this.selectionLimit) {
this.maxSelectionLimitReached = true;
}
else {
this.maxSelectionLimitReached = false;
}
}
updateFilledState() {
this.filled = this.value && this.value.length > 0;
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
this.cd.markForCheck();
}
onOptionClick(event) {
let option = event.option;
if (this.isOptionDisabled(option)) {
return;
}
let optionValue = this.getOptionValue(option);
let selectionIndex = this.findSelectionIndex(optionValue);
if (selectionIndex != -1) {
this.value = this.value.filter((val, i) => i != selectionIndex);
this.onRemove.emit({ newValue: this.value, removed: optionValue });
if (this.selectionLimit) {
this.maxSelectionLimitReached = false;
}
}
else {
if (!this.selectionLimit || !this.value || this.value.length < this.selectionLimit) {
this.value = [...(this.value || []), optionValue];
}
this.checkSelectionLimit();
}
this.onModelChange(this.value);
this.onChange.emit({ originalEvent: event.originalEvent, value: this.value, itemValue: optionValue });
this.updateLabel();
this.updateFilledState();
}
isSelected(option) {
return this.findSelectionIndex(this.getOptionValue(option)) != -1;
}
findSelectionIndex(val) {
let index = -1;
if (this.value) {
for (let i = 0; i < this.value.length; i++) {
if (ObjectUtils.equals(this.value[i], val, this.dataKey)) {
index = i;
break;
}
}
}
return index;
}
get toggleAllDisabled() {
let optionsToRender = this.optionsToRender;
if (!optionsToRender || optionsToRender.length === 0) {
return true;
}
else {
for (let option of optionsToRender) {
if (!this.isOptionDisabled(option))
return false;
}
return true;
}
}
toggleAll(event) {
if (this.disabled || this.toggleAllDisabled || this.readonly) {
return;
}
let allChecked = this.allChecked;
if (allChecked)
this.uncheckAll();
else
this.checkAll();
this.onModelChange(this.value);
this.onChange.emit({ originalEvent: event, value: this.value });
this.updateFilledState();
this.updateLabel();
event.preventDefault();
event.stopPropagation();
}
checkAll() {
let optionsToRender = this.optionsToRender;
let val = [];
optionsToRender.forEach((opt) => {
if (!this.group) {
let optionDisabled = this.isOptionDisabled(opt);
if (!optionDisabled || (optionDisabled && this.isSelected(opt))) {
val.push(this.getOptionValue(opt));
}
}
else {
let subOptions = this.getOptionGroupChildren(opt);
if (subOptions) {
subOptions.forEach((option) => {
let optionDisabled = this.isOptionDisabled(option);
if (!optionDisabled || (optionDisabled && this.isSelected(option))) {
val.push(this.getOptionValue(option));
}
});
}
}
});
this.value = val;
}
uncheckAll() {
let optionsToRender = this.optionsToRender;
let val = [];
optionsToRender.forEach((opt) => {
if (!this.group) {
let optionDisabled = this.isOptionDisabled(opt);
if (optionDisabled && this.isSelected(opt)) {
val.push(this.getOptionValue(opt));
}
}
else {
if (opt.items) {
opt.items.forEach((option) => {
let optionDisabled = this.isOptionDisabled(option);
if (optionDisabled && this.isSelected(option)) {
val.push(this.getOptionValue(option));
}
});
}
}
});
this.value = val;
}
show() {
if (!this.overlayVisible) {
this.overlayVisible = true;
this.preventDocumentDefault = true;
this.cd.markForCheck();
}
}
onOverlayAnimationStart(event) {
switch (event.toState) {
case 'visible':
this.virtualScroll && this.scroller?.setContentEl(this.itemsViewChild?.nativeElement);
if (this.filterInputChild && this.filterInputChild.nativeElement) {
this.preventModelTouched = true;
if (this.autofocusFilter) {
this.filterInputChild.nativeElement.focus();
}
}
this.onPanelShow.emit();
break;
case 'void':
this.onModelTouched();
break;
}
}
hide() {
this.overlayVisible = false;
if (this.resetFilterOnHide) {
this.resetFilter();
}
this.onPanelHide.emit();
this.cd.markForCheck();
}
resetFilter() {
if (this.filterInputChild && this.filterInputChild.nativeElement) {
this.filterInputChild.nativeElement.value = '';
}
this._filterValue = null;
this._filteredOptions = null;
}
close(event) {
this.hide();
event.preventDefault();
event.stopPropagation();
}
clear(event) {
this.value = null;
this.updateLabel();
this.updateFilledState();
this.checkSelectionLimit();
this.onClear.emit();
this.onModelChange(this.value);
event.stopPropagation();
}
onMouseclick(event, input) {
if (this.disabled || this.readonly || event.target.isSameNode(this.accessibleViewChild?.nativeElement)) {
return;
}
this.onClick.emit(event);
if (!this.overlayViewChild?.el?.nativeElement?.contains(event.target) && !DomHandler.hasClass(event.target, 'p-multiselect-token-icon')) {
if (this.overlayVisible) {
this.hide();
}
else {
this.show();
}
input.focus();
}
}
removeChip(chip, event) {
this.value = this.value.filter((val) => !ObjectUtils.equals(val, chip, this.dataKey));
this.onModelChange(this.value);
this.checkSelectionLimit();
this.onChange.emit({ originalEvent: event, value: this.value, itemValue: chip });
this.updateLabel();
this.updateFilledState();
}
onInputFocus(event) {
this.focus = true;
this.onFocus.emit({ originalEvent: event });
}
onInputBlur(event) {
this.focus = false;
this.onBlur.emit({ originalEvent: event });
if (!this.preventModelTouched) {
this.onModelTouched();
}
this.preventModelTouched = false;
}
onOptionKeydown(event) {
if (this.readonly) {
return;
}
switch (event.originalEvent.which) {
//down
case 40:
var nextItem = this.findNextItem(event.originalEvent.target.parentElement);
if (nextItem) {
nextItem.focus();
}
event.originalEvent.preventDefault();
break;
//up
case 38:
var prevItem = this.findPrevItem(event.originalEvent.target.parentElement);
if (prevItem) {
prevItem.focus();
}
event.originalEvent.preventDefault();
break;
//enter
case 13:
this.onOptionClick(event);
event.originalEvent.preventDefault();
break;
case 27:
case 9:
this.hide();
break;
}
}
findNextItem(item) {
let nextItem = item.nextElementSibling;
if (nextItem)
return DomHandler.hasClass(nextItem.children[0], 'p-disabled') || DomHandler.isHidden(nextItem.children[0]) || DomHandler.hasClass(nextItem, 'p-multiselect-item-group') ? this.findNextItem(nextItem) : nextItem.children[0];
else
return null;
}
findPrevItem(item) {
let prevItem = item.previousElementSibling;
if (prevItem)
return DomHandler.hasClass(prevItem.children[0], 'p-disabled') || DomHandler.isHidden(prevItem.children[0]) || DomHandler.hasClass(prevItem, 'p-multiselect-item-group') ? this.findPrevItem(prevItem) : prevItem.children[0];
else
return null;
}
onKeydown(event) {
switch (event.which) {
//down
case 40:
if (!this.overlayVisible && event.altKey) {
this.show();
event.preventDefault();
}
break;
//space
case 32:
if (!this.overlayVisible) {
this.show();
event.preventDefault();
}
break;
//escape
case 27:
this.hide();
break;
}
}
updateLabel() {
if (this.value && this.options && this.value.length && this.displaySelectedLabel) {
let label = '';
for (let i = 0; i < this.value.length; i++) {
let itemLabel = this.findLabelByValue(this.value[i]);
if (itemLabel) {
if (label.length > 0) {
label = label + ', ';
}
label = label + itemLabel;
}
}
if (this.value.length <= this.maxSelectedLabels || this.selectedItemsLabel === 'ellipsis') {
this.valuesAsString = label;
}
else {
let pattern = /{(.*?)}/;
if (pattern.test(this.selectedItemsLabel)) {
this.valuesAsString = this.selectedItemsLabel.replace(this.selectedItemsLabel.match(pattern)[0], this.value.length + '');
}
else {
this.valuesAsString = this.selectedItemsLabel;
}
}
}
else {
this.valuesAsString = this.placeholder || this.defaultLabel;
}
}
findLabelByValue(val) {
if (this.group) {
let label = null;
for (let i = 0; i < this.options.length; i++) {
let subOptions = this.getOptionGroupChildren(this.options[i]);
if (subOptions) {
label = this.searchLabelByValue(val, subOptions);
if (label) {
break;
}
}
}
return label;
}
else {
return this.searchLabelByValue(val, this.options);
}
}
searchLabelByValue(val, options) {
let label = null;
for (let i = 0; i < options.length; i++) {
let option = options[i];
let optionValue = this.getOptionValue(option);
if ((val == null && optionValue == null) || ObjectUtils.equals(val, optionValue, this.dataKey)) {
label = this.getOptionLabel(option);
break;
}
}
return label;
}
get allChecked() {
let optionsToRender = this.optionsToRender;
if (!optionsToRender || optionsToRender.length === 0) {
return false;
}
else {
let selectedDisabledItemsLength = 0;
let unselectedDisabledItemsLength = 0;
let selectedEnabledItemsLength = 0;
let visibleOptionsLength = this.group ? 0 : this.optionsToRender.length;
for (let option of optionsToRender) {
if (!this.group) {
let disabled = this.isOptionDisabled(option);
let selected = this.isSelected(option);
if (disabled) {
if (selected)
selectedDisabledItemsLength++;
else
unselectedDisabledItemsLength++;
}
else {
if (selected)
selectedEnabledItemsLength++;
else
return false;
}
}
else {
for (let opt of this.getOptionGroupChildren(option)) {
let disabled = this.isOptionDisabled(opt);
let selected = this.isSelected(opt);
if (disabled) {
if (selected)
selectedDisabledItemsLength++;
else
unselectedDisabledItemsLength++;
}
else {
if (selected)
selectedEnabledItemsLength++;
else {
return false;
}
}
visibleOptionsLength++;
}
}
}
return (visibleOptionsLength === selectedDisabledItemsLength ||
visibleOptionsLength === selectedEnabledItemsLength ||
(selectedEnabledItemsLength && visibleOptionsLength === selectedEnabledItemsLength + unselectedDisabledItemsLength + selectedDisabledItemsLength));
}
}
get optionsToRender() {
return (this._filteredOptions || this.options);
}
get emptyMessageLabel() {
return this.emptyMessage || this.config.getTranslation(TranslationKeys.EMPTY_MESSAGE);
}
get emptyFilterMessageLabel() {
return this.emptyFilterMessage || this.config.getTranslation(TranslationKeys.EMPTY_FILTER_MESSAGE);
}
hasFilter() {
return this._filterValue && this._filterValue.trim().length > 0;
}
isEmpty() {
return !this.optionsToRender || (this.optionsToRender && this.optionsToRender.length === 0);
}
onFilterInputChange(event) {
this._filterValue = event.target.value;
this.activateFilter();
this.filtered = true;
this.onFilter.emit({ originalEvent: event, filter: this._filterValue });
this.cd.detectChanges();
}
activateFilter() {
if (this.hasFilter() && this._options) {
let searchFields = (this.filterBy || this.optionLabel || 'label').split(',');
if (this.group) {
let filteredGroups = [];
for (let optgroup of this.options) {
let filteredSubOptions = this.filterService.filter(this.getOptionGroupChildren(optgroup), searchFields, this.filterValue, this.filterMatchMode, this.filterLocale);
if (filteredSubOptions && filteredSubOptions.length) {
filteredGroups.push({ ...optgroup, ...{ [this.optionGroupChildren]: filteredSubOptions } });
}
}
this._filteredOptions = filteredGroups;
}
else {
this._filteredOptions = this.filterService.filter(this.options, searchFields, this._filterValue, this.filterMatchMode, this.filterLocale);
}
}
else {
this._filteredOptions = null;
}
}
onHeaderCheckboxFocus() {
this.headerCheckboxFocus = true;
}
onHeaderCheckboxBlur() {
this.headerCheckboxFocus = false;
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.0", ngImport: i0, type: MultiSelect, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: i0.ChangeDetectorRef }, { token: i0.NgZone }, { token: i3.FilterService }, { token: i3.PrimeNGConfig }, { token: i3.OverlayService }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.0", type: MultiSelect, selector: "p-multiSelect", inputs: { style: "style", styleClass: "styleClass", panelStyle: "panelStyle", panelStyleClass: "panelStyleClass", inputId: "inputId", disabled: "disabled", readonly: "readonly", group: "group", filter: "filter", filterPlaceHolder: "filterPlaceHolder", filterLocale: "filterLocale", overlayVisible: "overlayVisible", tabindex: "tabindex", appendTo: "appendTo", dataKey: "dataKey", name: "name", label: "label", ariaLabelledBy: "ariaLabelledBy", displaySelectedLabel: "displaySelectedLabel", maxSelectedLabels: "maxSelectedLabels", selectedItemsLabel: "selectedItemsLabel", showToggleAll: "showToggleAll", emptyFilterMessage: "emptyFilterMessage", emptyMessage: "emptyMessage", resetFilterOnHide: "resetFilterOnHide", dropdownIcon: "dropdownIcon", optionLabel: "optionLabel", optionValue: "optionValue", optionDisabled: "optionDisabled", optionGroupLabel: "optionGroupLabel", optionGroupChildren: "optionGroupChildren", showHeader: "showHeader", filterBy: "filterBy", scrollHeight: "scrollHeight", lazy: "lazy", virtualScroll: "virtualScroll", virtualScrollItemSize: "virtualScrollItemSize", virtualScrollOptions: "virtualScrollOptions", overlayOptions: "overlayOptions", ariaFilterLabel: "ariaFilterLabel", filterMatchMode: "filterMatchMode", tooltip: "tooltip", tooltipPosition: "tooltipPosition", tooltipPositionStyle: "tooltipPositionStyle", tooltipStyleClass: "tooltipStyleClass", autofocusFilter: "autofocusFilter", display: "display", autocomplete: "autocomplete", showClear: "showClear", autoZIndex: "autoZIndex", baseZIndex: "baseZIndex", showTransitionOptions: "showTransitionOptions", hideTransitionOptions: "hideTransitionOptions", defaultLabel: "defaultLabel", placeholder: "placeholder", options: "options", filterValue: "filterValue", itemSize: "itemSize", selectionLimit: "selectionLimit" }, outputs: { onChange: "onChange", onFilter: "onFilter", onFocus: "onFocus", onBlur: "onBlur", onClick: "onClick", onClear: "onClear", onPanelShow: "onPanelShow", onPanelHide: "onPanelHide", onLazyLoad: "onLazyLoad", onRemove: "onRemove" }, host: { properties: { "class.p-inputwrapper-filled": "filled", "class.p-inputwrapper-focus": "focus || overlayVisible", "class.p-multiselect-clearable": "showClear && !disabled" }, classAttribute: "p-element p-inputwrapper" }, providers: [MULTISELECT_VALUE_ACCESSOR], queries: [{ propertyName: "footerFacet", first: true, predicate: Footer, descendants: true }, { propertyName: "headerFacet", first: true, predicate: Header, descendants: true }, { propertyName: "templates", predicate: PrimeTemplate }], viewQueries: [{ propertyName: "containerViewChild", first: true, predicate: ["container"], descendants: true }, { propertyName: "overlayViewChild", first: true, predicate: ["overlay"], descendants: true }, { propertyName: "filterInputChild", first: true, predicate: ["filterInput"], descendants: true }, { propertyName: "accessibleViewChild", first: true, predicate: ["in"], descendants: true }, { propertyName: "itemsViewChild", first: true, predicate: ["items"], descendants: true }, { propertyName: "scroller", first: true, predicate: ["scroller"], descendants: true }], ngImport: i0, template: `
<div
#container
[ngClass]="{ 'p-multiselect p-component': true, 'p-multiselect-open': overlayVisible, 'p-multiselect-chip': display === 'chip', 'p-focus': focus, 'p-disabled': disabled }"
[ngStyle]="style"
[class]="styleClass"
(click)="onMouseclick($event, in)"
>
<div class="p-hidden-accessible">
<input
#in
type="text"
[attr.label]="label"
readonly="readonly"
[attr.id]="inputId"
[attr.name]="name"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
[disabled]="disabled"
[attr.tabindex]="tabindex"
(keydown)="onKeydown($event)"
aria-haspopup="listbox"
[attr.aria-expanded]="overlayVisible"
[attr.aria-labelledby]="ariaLabelledBy"
role="listbox"
/>
</div>
<div class="p-multiselect-label-container" [pTooltip]="tooltip" [tooltipPosition]="tooltipPosition" [positionStyle]="tooltipPositionStyle" [tooltipStyleClass]="tooltipStyleClass">
<div
class="p-multiselect-label"
[ngClass]="{ 'p-placeholder': valuesAsString === (defaultLabel || placeholder), 'p-multiselect-label-empty': (valuesAsString == null || valuesAsString.length === 0) && (placeholder == null || placeholder.length === 0) }"
>
<ng-container *ngIf="!selectedItemsTemplate">
<ng-container *ngIf="display === 'comma'">{{ valuesAsString || 'empty' }}</ng-container>
<ng-container *ngIf="display === 'chip'">
<div #token *ngFor="let item of value; let i = index" class="p-multiselect-token">
<span class="p-multiselect-token-label">{{ findLabelByValue(item) }}</span>
<ng-container *ngIf="!disabled">
<TimesCircleIcon *ngIf="!removeTokenIconTemplate" [styleClass]="'p-multiselect-token-icon'" (click)="removeChip(item, event)" />
<span *ngIf="removeTokenIconTemplate" class="p-multiselect-token-icon" (click)="removeChip(item, event)">
<ng-container *ngTemplateOutlet="removeTokenIconTemplate"></ng-container>
</span>
</ng-container>
</div>
<ng-container *ngIf="!value || value.length === 0">{{ placeholder || defaultLabel || 'empty' }}</ng-container>
</ng-container>
</ng-container>
<ng-container *ngTemplateOutlet="selectedItemsTemplate; context: { $implicit: value, removeChip: removeChip.bind(this) }"></ng-container>
</div>
<ng-container *ngIf="value != null && filled && !disabled && showClear">
<TimesIcon *ngIf="!clearIconTemplate" [styleClass]="'p-multiselect-clear-icon'" (click)="clear($event)" />
<span *ngIf="clearIconTemplate" class="p-multiselect-clear-icon" (click)="clear($event)">
<ng-template *ngTemplateOutlet="clearIconTemplate"></ng-template>
</span>
</ng-container>
</div>
<div [ngClass]="{ 'p-multiselect-trigger': true }">
<ng-container *ngIf="!dropdownIconTemplate">
<span *ngIf="dropdownIcon" class="p-multiselect-trigger-icon" [ngClass]="dropdownIcon"></span>
<ChevronDownIcon *ngIf="!dropdownIcon" [styleClass]="'p-multiselect-trigger-icon'" />
</ng-container>
<span *ngIf="dropdownIconTemplate" class="p-multiselect-trigger-icon">
<ng-template *ngTemplateOutlet="dropdownIconTemplate"></ng-template>
</span>
</div>
<p-overlay
#overlay
[(visible)]="overlayVisible"
[options]="overlayOptions"
[target]="'@parent'"
[appendTo]="appendTo"
[autoZIndex]="autoZIndex"
[baseZIndex]="baseZIndex"
[showTransitionOptions]="showTransitionOptions"
[hideTransitionOptions]="hideTransitionOptions"
(onAnimationStart)="onOverlayAnimationStart($event)"
(onHide)="hide()"
>
<ng-template pTemplate="content">
<div [ngClass]="['p-multiselect-panel p-component']" [ngStyle]="panelStyle" [class]="panelStyleClass" (keydown)="onKeydown($event)">
<div class="p-multiselect-header" *ngIf="showHeader">
<ng-content select="p-header"></ng-content>
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
<ng-container *ngIf="filterTemplate; else builtInFilterElement">
<ng-container *ngTemplateOutlet="filterTemplate; context: { options: filterOptions }"></ng-container>
</ng-container>
<ng-template #builtInFilterElement>
<div class="p-checkbox p-component" *ngIf="showToggleAll && !selectionLimit" [ngClass]="{ 'p-checkbox-disabled': disabled || toggleAllDisabled }">
<div class="p-hidden-accessible">
<input
type="checkbox"
readonly="readonly"
[checked]="allChecked"
(focus)="onHeaderCheckboxFocus()"
(blur)="onHeaderCheckboxBlur()"
(keydown.space)="toggleAll($event)"
[disabled]="disabled || toggleAllDisabled"
/>
</div>
<div
class="p-checkbox-box"