primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 61.3 kB
JavaScript
import * as i4 from '@angular/cdk/scrolling';
import { CdkVirtualScrollViewport, ScrollingModule } from '@angular/cdk/scrolling';
import * as i0 from '@angular/core';
import { forwardRef, EventEmitter, Component, Input, Output, ChangeDetectionStrategy, ViewEncapsulation, ViewChild, ContentChildren, NgModule } from '@angular/core';
import { trigger, transition, style, animate } from '@angular/animations';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 from 'primeng/api';
import { TranslationKeys, PrimeTemplate, SharedModule } from 'primeng/api';
import { DomHandler, ConnectedOverlayScrollHandler } from 'primeng/dom';
import { ObjectUtils } from 'primeng/utils';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import * as i5 from 'primeng/tooltip';
import { TooltipModule } from 'primeng/tooltip';
import * as i1 from 'primeng/ripple';
import { RippleModule } from 'primeng/ripple';
const DROPDOWN_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Dropdown),
multi: true
};
class DropdownItem {
constructor() {
this.onClick = new EventEmitter();
}
onOptionClick(event) {
this.onClick.emit({
originalEvent: event,
option: this.option
});
}
}
DropdownItem.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: DropdownItem, deps: [], target: i0.ɵɵFactoryTarget.Component });
DropdownItem.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.0.4", type: DropdownItem, selector: "p-dropdownItem", inputs: { option: "option", selected: "selected", label: "label", disabled: "disabled", visible: "visible", itemSize: "itemSize", template: "template" }, outputs: { onClick: "onClick" }, ngImport: i0, template: `
<li (click)="onOptionClick($event)" role="option" pRipple
[attr.aria-label]="label" [attr.aria-selected]="selected"
[ngStyle]="{'height': itemSize + 'px'}"
[ngClass]="{'p-dropdown-item':true, 'p-highlight': selected, 'p-disabled': disabled}">
<span *ngIf="!template">{{label||'empty'}}</span>
<ng-container *ngTemplateOutlet="template; context: {$implicit: option}"></ng-container>
</li>
`, isInline: true, directives: [{ type: i1.Ripple, selector: "[pRipple]" }, { type: i2.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i2.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: DropdownItem, decorators: [{
type: Component,
args: [{
selector: 'p-dropdownItem',
template: `
<li (click)="onOptionClick($event)" role="option" pRipple
[attr.aria-label]="label" [attr.aria-selected]="selected"
[ngStyle]="{'height': itemSize + 'px'}"
[ngClass]="{'p-dropdown-item':true, 'p-highlight': selected, 'p-disabled': disabled}">
<span *ngIf="!template">{{label||'empty'}}</span>
<ng-container *ngTemplateOutlet="template; context: {$implicit: option}"></ng-container>
</li>
`
}]
}], propDecorators: { option: [{
type: Input
}], selected: [{
type: Input
}], label: [{
type: Input
}], disabled: [{
type: Input
}], visible: [{
type: Input
}], itemSize: [{
type: Input
}], template: [{
type: Input
}], onClick: [{
type: Output
}] } });
class Dropdown {
constructor(el, renderer, cd, zone, filterService, config) {
this.el = el;
this.renderer = renderer;
this.cd = cd;
this.zone = zone;
this.filterService = filterService;
this.config = config;
this.scrollHeight = '200px';
this.resetFilterOnHide = false;
this.dropdownIcon = 'pi pi-chevron-down';
this.optionGroupChildren = "items";
this.autoDisplayFirst = true;
this.emptyFilterMessage = '';
this.emptyMessage = '';
this.autoZIndex = true;
this.baseZIndex = 0;
this.showTransitionOptions = '.12s cubic-bezier(0, 0, 0.2, 1)';
this.hideTransitionOptions = '.1s linear';
this.filterMatchMode = "contains";
this.tooltip = '';
this.tooltipPosition = 'right';
this.tooltipPositionStyle = 'absolute';
this.autofocusFilter = true;
this.onChange = new EventEmitter();
this.onFilter = new EventEmitter();
this.onFocus = new EventEmitter();
this.onBlur = new EventEmitter();
this.onClick = new EventEmitter();
this.onShow = new EventEmitter();
this.onHide = new EventEmitter();
this.onModelChange = () => { };
this.onModelTouched = () => { };
this.viewPortOffsetTop = 0;
}
get disabled() {
return this._disabled;
}
;
set disabled(_disabled) {
if (_disabled) {
this.focused = false;
if (this.overlayVisible)
this.hide();
}
this._disabled = _disabled;
if (!this.cd.destroyed) {
this.cd.detectChanges();
}
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'item':
this.itemTemplate = item.template;
break;
case 'selectedItem':
this.selectedItemTemplate = item.template;
break;
case 'header':
this.headerTemplate = item.template;
break;
case 'footer':
this.footerTemplate = item.template;
break;
case 'emptyfilter':
this.emptyFilterTemplate = item.template;
break;
case 'empty':
this.emptyTemplate = item.template;
break;
case 'group':
this.groupTemplate = item.template;
break;
default:
this.itemTemplate = item.template;
break;
}
});
}
ngOnInit() {
this.optionsToDisplay = this.options;
this.updateSelectedOption(null);
}
get options() {
return this._options;
}
set options(val) {
this._options = val;
this.optionsToDisplay = this._options;
this.updateSelectedOption(this.value);
this.optionsChanged = true;
if (this._filterValue && this._filterValue.length) {
this.activateFilter();
}
}
get filterValue() {
return this._filterValue;
}
set filterValue(val) {
this._filterValue = val;
this.activateFilter();
}
ngAfterViewInit() {
if (this.editable) {
this.updateEditableLabel();
}
}
get label() {
return this.selectedOption ? this.getOptionLabel(this.selectedOption) : null;
}
get emptyMessageLabel() {
return this.emptyMessage || this.config.getTranslation(TranslationKeys.EMPTY_MESSAGE);
}
get emptyFilterMessageLabel() {
return this.emptyFilterMessage || this.config.getTranslation(TranslationKeys.EMPTY_FILTER_MESSAGE);
}
get filled() {
return this.value || this.value != null || this.value != undefined;
}
updateEditableLabel() {
if (this.editableInputViewChild && this.editableInputViewChild.nativeElement) {
this.editableInputViewChild.nativeElement.value = (this.selectedOption ? this.getOptionLabel(this.selectedOption) : this.value || '');
}
}
getOptionLabel(option) {
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : (option.label != undefined ? option.label : option);
}
getOptionValue(option) {
return this.optionValue ? ObjectUtils.resolveFieldData(option, this.optionValue) : (this.optionLabel || option.value === undefined ? option : option.value);
}
isOptionDisabled(option) {
return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : (option.disabled !== undefined ? option.disabled : false);
}
getOptionGroupLabel(optionGroup) {
return this.optionGroupLabel ? ObjectUtils.resolveFieldData(optionGroup, this.optionGroupLabel) : (optionGroup.label != undefined ? optionGroup.label : optionGroup);
}
getOptionGroupChildren(optionGroup) {
return this.optionGroupChildren ? ObjectUtils.resolveFieldData(optionGroup, this.optionGroupChildren) : optionGroup.items;
}
onItemClick(event) {
const option = event.option;
if (!this.isOptionDisabled(option)) {
this.selectItem(event, option);
this.accessibleViewChild.nativeElement.focus();
}
setTimeout(() => {
this.hide();
}, 150);
}
selectItem(event, option) {
if (this.selectedOption != option) {
this.selectedOption = option;
this.value = this.getOptionValue(option);
this.onModelChange(this.value);
this.updateEditableLabel();
this.onChange.emit({
originalEvent: event.originalEvent,
value: this.value
});
if (this.virtualScroll) {
setTimeout(() => {
this.viewPortOffsetTop = this.viewPort ? this.viewPort.measureScrollOffset() : 0;
}, 1);
}
}
}
ngAfterViewChecked() {
if (this.optionsChanged && this.overlayVisible) {
this.optionsChanged = false;
if (this.virtualScroll) {
this.updateVirtualScrollSelectedIndex(true);
}
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.alignOverlay();
}, 1);
});
}
if (this.selectedOptionUpdated && this.itemsWrapper) {
if (this.virtualScroll && this.viewPort) {
let range = this.viewPort.getRenderedRange();
this.updateVirtualScrollSelectedIndex(false);
if (range.start > this.virtualScrollSelectedIndex || range.end < this.virtualScrollSelectedIndex) {
this.viewPort.scrollToIndex(this.virtualScrollSelectedIndex);
}
}
let selectedItem = DomHandler.findSingle(this.overlay, 'li.p-highlight');
if (selectedItem) {
DomHandler.scrollInView(this.itemsWrapper, DomHandler.findSingle(this.overlay, 'li.p-highlight'));
}
this.selectedOptionUpdated = false;
}
}
writeValue(value) {
if (this.filter) {
this.resetFilter();
}
this.value = value;
this.updateSelectedOption(value);
this.updateEditableLabel();
this.cd.markForCheck();
}
resetFilter() {
this._filterValue = null;
if (this.filterViewChild && this.filterViewChild.nativeElement) {
this.filterViewChild.nativeElement.value = '';
}
this.optionsToDisplay = this.options;
}
updateSelectedOption(val) {
this.selectedOption = this.findOption(val, this.optionsToDisplay);
if (this.autoDisplayFirst && !this.placeholder && !this.selectedOption && this.optionsToDisplay && this.optionsToDisplay.length && !this.editable) {
this.selectedOption = this.optionsToDisplay[0];
}
this.selectedOptionUpdated = true;
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
this.cd.markForCheck();
}
onMouseclick(event) {
if (this.disabled || this.readonly || this.isInputClick(event)) {
return;
}
this.onClick.emit(event);
this.accessibleViewChild.nativeElement.focus();
if (this.overlayVisible)
this.hide();
else
this.show();
this.cd.detectChanges();
}
isInputClick(event) {
return DomHandler.hasClass(event.target, 'p-dropdown-clear-icon') ||
event.target.isSameNode(this.accessibleViewChild.nativeElement) ||
(this.editableInputViewChild && event.target.isSameNode(this.editableInputViewChild.nativeElement));
}
isOutsideClicked(event) {
return !(this.el.nativeElement.isSameNode(event.target) || this.el.nativeElement.contains(event.target) || (this.overlay && this.overlay.contains(event.target)));
}
isEmpty() {
return !this.optionsToDisplay || (this.optionsToDisplay && this.optionsToDisplay.length === 0);
}
onEditableInputClick() {
this.bindDocumentClickListener();
}
onEditableInputFocus(event) {
this.focused = true;
this.hide();
this.onFocus.emit(event);
}
onEditableInputChange(event) {
this.value = event.target.value;
this.updateSelectedOption(this.value);
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
}
show() {
this.overlayVisible = true;
}
onOverlayAnimationStart(event) {
switch (event.toState) {
case 'visible':
this.overlay = event.element;
let itemsWrapperSelector = this.virtualScroll ? '.cdk-virtual-scroll-viewport' : '.p-dropdown-items-wrapper';
this.itemsWrapper = DomHandler.findSingle(this.overlay, itemsWrapperSelector);
this.appendOverlay();
if (this.autoZIndex) {
this.overlay.style.zIndex = String(this.baseZIndex + (++DomHandler.zindex));
}
this.alignOverlay();
this.bindDocumentClickListener();
this.bindDocumentResizeListener();
this.bindScrollListener();
if (this.options && this.options.length) {
if (!this.virtualScroll) {
let selectedListItem = DomHandler.findSingle(this.itemsWrapper, '.p-dropdown-item.p-highlight');
if (selectedListItem) {
DomHandler.scrollInView(this.itemsWrapper, selectedListItem);
}
}
}
if (this.filterViewChild && this.filterViewChild.nativeElement) {
this.preventModelTouched = true;
if (this.autofocusFilter) {
this.filterViewChild.nativeElement.focus();
}
}
this.onShow.emit(event);
break;
case 'void':
this.onOverlayHide();
this.onHide.emit(event);
break;
}
}
scrollToSelectedVirtualScrollElement() {
if (!this.virtualAutoScrolled) {
if (this.viewPortOffsetTop) {
this.viewPort.scrollToOffset(this.viewPortOffsetTop);
}
else if (this.virtualScrollSelectedIndex > -1) {
this.viewPort.scrollToIndex(this.virtualScrollSelectedIndex);
}
}
this.virtualAutoScrolled = true;
}
updateVirtualScrollSelectedIndex(resetOffset) {
if (this.selectedOption && this.optionsToDisplay && this.optionsToDisplay.length) {
if (resetOffset) {
this.viewPortOffsetTop = 0;
}
this.virtualScrollSelectedIndex = this.findOptionIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay);
}
}
appendOverlay() {
if (this.appendTo) {
if (this.appendTo === 'body')
document.body.appendChild(this.overlay);
else
DomHandler.appendChild(this.overlay, this.appendTo);
if (!this.overlay.style.minWidth) {
this.overlay.style.minWidth = DomHandler.getWidth(this.containerViewChild.nativeElement) + 'px';
}
}
}
restoreOverlayAppend() {
if (this.overlay && this.appendTo) {
this.el.nativeElement.appendChild(this.overlay);
}
}
hide() {
this.overlayVisible = false;
if (this.filter && this.resetFilterOnHide) {
this.resetFilter();
}
if (this.virtualScroll) {
this.virtualAutoScrolled = false;
}
this.cd.markForCheck();
}
alignOverlay() {
if (this.overlay) {
if (this.appendTo)
DomHandler.absolutePosition(this.overlay, this.containerViewChild.nativeElement);
else
DomHandler.relativePosition(this.overlay, this.containerViewChild.nativeElement);
}
}
onInputFocus(event) {
this.focused = true;
this.onFocus.emit(event);
}
onInputBlur(event) {
this.focused = false;
this.onBlur.emit(event);
if (!this.preventModelTouched) {
this.onModelTouched();
}
this.preventModelTouched = false;
}
findPrevEnabledOption(index) {
let prevEnabledOption;
if (this.optionsToDisplay && this.optionsToDisplay.length) {
for (let i = (index - 1); 0 <= i; i--) {
let option = this.optionsToDisplay[i];
if (option.disabled) {
continue;
}
else {
prevEnabledOption = option;
break;
}
}
if (!prevEnabledOption) {
for (let i = this.optionsToDisplay.length - 1; i >= index; i--) {
let option = this.optionsToDisplay[i];
if (this.isOptionDisabled(option)) {
continue;
}
else {
prevEnabledOption = option;
break;
}
}
}
}
return prevEnabledOption;
}
findNextEnabledOption(index) {
let nextEnabledOption;
if (this.optionsToDisplay && this.optionsToDisplay.length) {
for (let i = (index + 1); i < (this.optionsToDisplay.length - 1); i++) {
let option = this.optionsToDisplay[i];
if (this.isOptionDisabled(option)) {
continue;
}
else {
nextEnabledOption = option;
break;
}
}
if (!nextEnabledOption) {
for (let i = 0; i < index; i++) {
let option = this.optionsToDisplay[i];
if (this.isOptionDisabled(option)) {
continue;
}
else {
nextEnabledOption = option;
break;
}
}
}
}
return nextEnabledOption;
}
onKeydown(event, search) {
if (this.readonly || !this.optionsToDisplay || this.optionsToDisplay.length === null) {
return;
}
switch (event.which) {
//down
case 40:
if (!this.overlayVisible && event.altKey) {
this.show();
}
else {
if (this.group) {
let selectedItemIndex = this.selectedOption ? this.findOptionGroupIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay) : -1;
if (selectedItemIndex !== -1) {
let nextItemIndex = selectedItemIndex.itemIndex + 1;
if (nextItemIndex < (this.getOptionGroupChildren(this.optionsToDisplay[selectedItemIndex.groupIndex]).length)) {
this.selectItem(event, this.getOptionGroupChildren(this.optionsToDisplay[selectedItemIndex.groupIndex])[nextItemIndex]);
this.selectedOptionUpdated = true;
}
else if (this.optionsToDisplay[selectedItemIndex.groupIndex + 1]) {
this.selectItem(event, this.getOptionGroupChildren(this.optionsToDisplay[selectedItemIndex.groupIndex + 1])[0]);
this.selectedOptionUpdated = true;
}
}
else {
if (this.optionsToDisplay && this.optionsToDisplay.length > 0) {
this.selectItem(event, this.getOptionGroupChildren(this.optionsToDisplay[0])[0]);
}
}
}
else {
let selectedItemIndex = this.selectedOption ? this.findOptionIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay) : -1;
let nextEnabledOption = this.findNextEnabledOption(selectedItemIndex);
if (nextEnabledOption) {
this.selectItem(event, nextEnabledOption);
this.selectedOptionUpdated = true;
}
}
}
event.preventDefault();
break;
//up
case 38:
if (this.group) {
let selectedItemIndex = this.selectedOption ? this.findOptionGroupIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay) : -1;
if (selectedItemIndex !== -1) {
let prevItemIndex = selectedItemIndex.itemIndex - 1;
if (prevItemIndex >= 0) {
this.selectItem(event, this.getOptionGroupChildren(this.optionsToDisplay[selectedItemIndex.groupIndex])[prevItemIndex]);
this.selectedOptionUpdated = true;
}
else if (prevItemIndex < 0) {
let prevGroup = this.optionsToDisplay[selectedItemIndex.groupIndex - 1];
if (prevGroup) {
this.selectItem(event, this.getOptionGroupChildren(prevGroup)[this.getOptionGroupChildren(prevGroup).length - 1]);
this.selectedOptionUpdated = true;
}
}
}
}
else {
let selectedItemIndex = this.selectedOption ? this.findOptionIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay) : -1;
let prevEnabledOption = this.findPrevEnabledOption(selectedItemIndex);
if (prevEnabledOption) {
this.selectItem(event, prevEnabledOption);
this.selectedOptionUpdated = true;
}
}
event.preventDefault();
break;
//space
case 32:
case 32:
if (!this.overlayVisible) {
this.show();
event.preventDefault();
}
break;
//enter
case 13:
if (!this.filter || (this.optionsToDisplay && this.optionsToDisplay.length > 0)) {
this.hide();
}
event.preventDefault();
break;
//escape and tab
case 27:
case 9:
this.hide();
break;
//search item based on keyboard input
default:
if (search && !event.metaKey) {
this.search(event);
}
break;
}
}
search(event) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
const char = event.key;
this.previousSearchChar = this.currentSearchChar;
this.currentSearchChar = char;
if (this.previousSearchChar === this.currentSearchChar)
this.searchValue = this.currentSearchChar;
else
this.searchValue = this.searchValue ? this.searchValue + char : char;
let newOption;
if (this.group) {
let searchIndex = this.selectedOption ? this.findOptionGroupIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay) : { groupIndex: 0, itemIndex: 0 };
newOption = this.searchOptionWithinGroup(searchIndex);
}
else {
let searchIndex = this.selectedOption ? this.findOptionIndex(this.getOptionValue(this.selectedOption), this.optionsToDisplay) : -1;
newOption = this.searchOption(++searchIndex);
}
if (newOption && !this.isOptionDisabled(newOption)) {
this.selectItem(event, newOption);
this.selectedOptionUpdated = true;
}
this.searchTimeout = setTimeout(() => {
this.searchValue = null;
}, 250);
}
searchOption(index) {
let option;
if (this.searchValue) {
option = this.searchOptionInRange(index, this.optionsToDisplay.length);
if (!option) {
option = this.searchOptionInRange(0, index);
}
}
return option;
}
searchOptionInRange(start, end) {
for (let i = start; i < end; i++) {
let opt = this.optionsToDisplay[i];
if (this.getOptionLabel(opt).toLocaleLowerCase(this.filterLocale).startsWith(this.searchValue.toLocaleLowerCase(this.filterLocale)) && !this.isOptionDisabled(opt)) {
return opt;
}
}
return null;
}
searchOptionWithinGroup(index) {
let option;
if (this.searchValue) {
for (let i = index.groupIndex; i < this.optionsToDisplay.length; i++) {
for (let j = (index.groupIndex === i) ? (index.itemIndex + 1) : 0; j < this.getOptionGroupChildren(this.optionsToDisplay[i]).length; j++) {
let opt = this.getOptionGroupChildren(this.optionsToDisplay[i])[j];
if (this.getOptionLabel(opt).toLocaleLowerCase(this.filterLocale).startsWith(this.searchValue.toLocaleLowerCase(this.filterLocale)) && !this.isOptionDisabled(opt)) {
return opt;
}
}
}
if (!option) {
for (let i = 0; i <= index.groupIndex; i++) {
for (let j = 0; j < ((index.groupIndex === i) ? index.itemIndex : this.getOptionGroupChildren(this.optionsToDisplay[i]).length); j++) {
let opt = this.getOptionGroupChildren(this.optionsToDisplay[i])[j];
if (this.getOptionLabel(opt).toLocaleLowerCase(this.filterLocale).startsWith(this.searchValue.toLocaleLowerCase(this.filterLocale)) && !this.isOptionDisabled(opt)) {
return opt;
}
}
}
}
}
return null;
}
findOptionIndex(val, opts) {
let index = -1;
if (opts) {
for (let i = 0; i < opts.length; i++) {
if ((val == null && this.getOptionValue(opts[i]) == null) || ObjectUtils.equals(val, this.getOptionValue(opts[i]), this.dataKey)) {
index = i;
break;
}
}
}
return index;
}
findOptionGroupIndex(val, opts) {
let groupIndex, itemIndex;
if (opts) {
for (let i = 0; i < opts.length; i++) {
groupIndex = i;
itemIndex = this.findOptionIndex(val, this.getOptionGroupChildren(opts[i]));
if (itemIndex !== -1) {
break;
}
}
}
if (itemIndex !== -1) {
return { groupIndex: groupIndex, itemIndex: itemIndex };
}
else {
return -1;
}
}
findOption(val, opts, inGroup) {
if (this.group && !inGroup) {
let opt;
if (opts && opts.length) {
for (let optgroup of opts) {
opt = this.findOption(val, this.getOptionGroupChildren(optgroup), true);
if (opt) {
break;
}
}
}
return opt;
}
else {
let index = this.findOptionIndex(val, opts);
return (index != -1) ? opts[index] : null;
}
}
onFilterInputChange(event) {
let inputValue = event.target.value;
if (inputValue && inputValue.length) {
this._filterValue = inputValue;
this.activateFilter();
}
else {
this._filterValue = null;
this.optionsToDisplay = this.options;
}
this.optionsChanged = true;
this.onFilter.emit({ originalEvent: event, filter: this._filterValue });
}
activateFilter() {
let searchFields = (this.filterBy || this.optionLabel || 'label').split(',');
if (this.options && this.options.length) {
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(Object.assign(Object.assign({}, optgroup), { [this.optionGroupChildren]: filteredSubOptions }));
}
}
this.optionsToDisplay = filteredGroups;
}
else {
this.optionsToDisplay = this.filterService.filter(this.options, searchFields, this.filterValue, this.filterMatchMode, this.filterLocale);
}
this.optionsChanged = true;
}
}
applyFocus() {
if (this.editable)
DomHandler.findSingle(this.el.nativeElement, '.p-dropdown-label.p-inputtext').focus();
else
DomHandler.findSingle(this.el.nativeElement, 'input[readonly]').focus();
}
focus() {
this.applyFocus();
}
bindDocumentClickListener() {
if (!this.documentClickListener) {
const documentTarget = this.el ? this.el.nativeElement.ownerDocument : 'document';
this.documentClickListener = this.renderer.listen(documentTarget, 'click', (event) => {
if (this.isOutsideClicked(event)) {
this.hide();
this.unbindDocumentClickListener();
}
this.cd.markForCheck();
});
}
}
unbindDocumentClickListener() {
if (this.documentClickListener) {
this.documentClickListener();
this.documentClickListener = null;
}
}
bindDocumentResizeListener() {
this.documentResizeListener = this.onWindowResize.bind(this);
window.addEventListener('resize', this.documentResizeListener);
}
unbindDocumentResizeListener() {
if (this.documentResizeListener) {
window.removeEventListener('resize', this.documentResizeListener);
this.documentResizeListener = null;
}
}
onWindowResize() {
if (!DomHandler.isAndroid()) {
this.hide();
}
}
bindScrollListener() {
if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.containerViewChild.nativeElement, (event) => {
if (this.overlayVisible) {
this.hide();
}
});
}
this.scrollHandler.bindScrollListener();
}
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
}
}
clear(event) {
this.value = null;
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
this.updateSelectedOption(this.value);
this.updateEditableLabel();
}
onOverlayHide() {
this.unbindDocumentClickListener();
this.unbindDocumentResizeListener();
this.unbindScrollListener();
this.overlay = null;
this.itemsWrapper = null;
this.onModelTouched();
}
ngOnDestroy() {
if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
this.restoreOverlayAppend();
this.onOverlayHide();
}
}
Dropdown.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: Dropdown, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: i0.ChangeDetectorRef }, { token: i0.NgZone }, { token: i3.FilterService }, { token: i3.PrimeNGConfig }], target: i0.ɵɵFactoryTarget.Component });
Dropdown.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.0.4", type: Dropdown, selector: "p-dropdown", inputs: { scrollHeight: "scrollHeight", filter: "filter", name: "name", style: "style", panelStyle: "panelStyle", styleClass: "styleClass", panelStyleClass: "panelStyleClass", readonly: "readonly", required: "required", editable: "editable", appendTo: "appendTo", tabindex: "tabindex", placeholder: "placeholder", filterPlaceholder: "filterPlaceholder", filterLocale: "filterLocale", inputId: "inputId", selectId: "selectId", dataKey: "dataKey", filterBy: "filterBy", autofocus: "autofocus", resetFilterOnHide: "resetFilterOnHide", dropdownIcon: "dropdownIcon", optionLabel: "optionLabel", optionValue: "optionValue", optionDisabled: "optionDisabled", optionGroupLabel: "optionGroupLabel", optionGroupChildren: "optionGroupChildren", autoDisplayFirst: "autoDisplayFirst", group: "group", showClear: "showClear", emptyFilterMessage: "emptyFilterMessage", emptyMessage: "emptyMessage", virtualScroll: "virtualScroll", itemSize: "itemSize", autoZIndex: "autoZIndex", baseZIndex: "baseZIndex", showTransitionOptions: "showTransitionOptions", hideTransitionOptions: "hideTransitionOptions", ariaFilterLabel: "ariaFilterLabel", ariaLabelledBy: "ariaLabelledBy", filterMatchMode: "filterMatchMode", maxlength: "maxlength", tooltip: "tooltip", tooltipPosition: "tooltipPosition", tooltipPositionStyle: "tooltipPositionStyle", tooltipStyleClass: "tooltipStyleClass", autofocusFilter: "autofocusFilter", disabled: "disabled", options: "options", filterValue: "filterValue" }, outputs: { onChange: "onChange", onFilter: "onFilter", onFocus: "onFocus", onBlur: "onBlur", onClick: "onClick", onShow: "onShow", onHide: "onHide" }, host: { properties: { "class.p-inputwrapper-filled": "filled", "class.p-inputwrapper-focus": "focused || overlayVisible" } }, providers: [DROPDOWN_VALUE_ACCESSOR], queries: [{ propertyName: "templates", predicate: PrimeTemplate }], viewQueries: [{ propertyName: "containerViewChild", first: true, predicate: ["container"], descendants: true }, { propertyName: "filterViewChild", first: true, predicate: ["filter"], descendants: true }, { propertyName: "accessibleViewChild", first: true, predicate: ["in"], descendants: true }, { propertyName: "viewPort", first: true, predicate: CdkVirtualScrollViewport, descendants: true }, { propertyName: "editableInputViewChild", first: true, predicate: ["editableInput"], descendants: true }], ngImport: i0, template: `
<div #container [ngClass]="{'p-dropdown p-component':true,
'p-disabled':disabled, 'p-dropdown-open':overlayVisible, 'p-focus':focused, 'p-dropdown-clearable': showClear && !disabled}"
(click)="onMouseclick($event)" [ngStyle]="style" [class]="styleClass">
<div class="p-hidden-accessible">
<input #in [attr.id]="inputId" type="text" readonly (focus)="onInputFocus($event)" aria-haspopup="listbox"
aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible" [attr.aria-labelledby]="ariaLabelledBy" (blur)="onInputBlur($event)" (keydown)="onKeydown($event, true)"
[disabled]="disabled" [attr.tabindex]="tabindex" [attr.autofocus]="autofocus">
</div>
<span [ngClass]="{'p-dropdown-label p-inputtext':true,'p-dropdown-label-empty':(label == null || label.length === 0)}" *ngIf="!editable && (label != null)" [pTooltip]="tooltip" [tooltipPosition]="tooltipPosition" [positionStyle]="tooltipPositionStyle" [tooltipStyleClass]="tooltipStyleClass">
<ng-container *ngIf="!selectedItemTemplate">{{label||'empty'}}</ng-container>
<ng-container *ngTemplateOutlet="selectedItemTemplate; context: {$implicit: selectedOption}"></ng-container>
</span>
<span [ngClass]="{'p-dropdown-label p-inputtext p-placeholder':true,'p-dropdown-label-empty': (placeholder == null || placeholder.length === 0)}" *ngIf="!editable && (label == null)">{{placeholder||'empty'}}</span>
<input #editableInput type="text" [attr.maxlength]="maxlength" class="p-dropdown-label p-inputtext" *ngIf="editable" [disabled]="disabled" [attr.placeholder]="placeholder"
aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible" (click)="onEditableInputClick()" (input)="onEditableInputChange($event)" (focus)="onEditableInputFocus($event)" (blur)="onInputBlur($event)">
<i class="p-dropdown-clear-icon pi pi-times" (click)="clear($event)" *ngIf="value != null && showClear && !disabled"></i>
<div class="p-dropdown-trigger" role="button" aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible">
<span class="p-dropdown-trigger-icon" [ngClass]="dropdownIcon"></span>
</div>
<div *ngIf="overlayVisible" [ngClass]="'p-dropdown-panel p-component'" [@overlayAnimation]="{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}" (@overlayAnimation.start)="onOverlayAnimationStart($event)" [ngStyle]="panelStyle" [class]="panelStyleClass">
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
<div class="p-dropdown-header" *ngIf="filter" >
<div class="p-dropdown-filter-container" (click)="$event.stopPropagation()">
<input #filter type="text" autocomplete="off" [value]="filterValue||''" class="p-dropdown-filter p-inputtext p-component" [attr.placeholder]="filterPlaceholder"
(keydown.enter)="$event.preventDefault()" (keydown)="onKeydown($event, false)" (input)="onFilterInputChange($event)" [attr.aria-label]="ariaFilterLabel">
<span class="p-dropdown-filter-icon pi pi-search"></span>
</div>
</div>
<div class="p-dropdown-items-wrapper" [style.max-height]="virtualScroll ? 'auto' : (scrollHeight||'auto')">
<ul class="p-dropdown-items" [ngClass]="{'p-dropdown-virtualscroll': virtualScroll}" role="listbox">
<ng-container *ngIf="group">
<ng-template ngFor let-optgroup [ngForOf]="optionsToDisplay">
<li class="p-dropdown-item-group">
<span *ngIf="!groupTemplate">{{getOptionGroupLabel(optgroup)||'empty'}}</span>
<ng-container *ngTemplateOutlet="groupTemplate; context: {$implicit: optgroup}"></ng-container>
</li>
<ng-container *ngTemplateOutlet="itemslist; context: {$implicit: getOptionGroupChildren(optgroup), selectedOption: selectedOption}"></ng-container>
</ng-template>
</ng-container>
<ng-container *ngIf="!group">
<ng-container *ngTemplateOutlet="itemslist; context: {$implicit: optionsToDisplay, selectedOption: selectedOption}"></ng-container>
</ng-container>
<ng-template #itemslist let-options let-selectedOption="selectedOption">
<ng-container *ngIf="!virtualScroll; else virtualScrollList">
<ng-template ngFor let-option let-i="index" [ngForOf]="options">
<p-dropdownItem [option]="option" [selected]="selectedOption == option" [label]="getOptionLabel(option)" [disabled]="isOptionDisabled(option)"
(onClick)="onItemClick($event)"
[template]="itemTemplate"></p-dropdownItem>
</ng-template>
</ng-container>
<ng-template #virtualScrollList>
<cdk-virtual-scroll-viewport (scrolledIndexChange)="scrollToSelectedVirtualScrollElement()" #viewport [ngStyle]="{'height': scrollHeight}" [itemSize]="itemSize" *ngIf="virtualScroll && optionsToDisplay && optionsToDisplay.length">
<ng-container *cdkVirtualFor="let option of options; let i = index; let c = count; let f = first; let l = last; let e = even; let o = odd">
<p-dropdownItem [option]="option" [selected]="selectedOption == option" [label]="getOptionLabel(option)" [disabled]="isOptionDisabled(option)"
(onClick)="onItemClick($event)"
[template]="itemTemplate"></p-dropdownItem>
</ng-container>
</cdk-virtual-scroll-viewport>
</ng-template>
</ng-template>
<li *ngIf="filterValue && isEmpty()" class="p-dropdown-empty-message">
<ng-container *ngIf="!emptyFilterTemplate && !emptyTemplate; else emptyFilter">
{{emptyFilterMessageLabel}}
</ng-container>
<ng-container #emptyFilter *ngTemplateOutlet="emptyFilterTemplate || emptyTemplate"></ng-container>
</li>
<li *ngIf="!filterValue && isEmpty()" class="p-dropdown-empty-message">
<ng-container *ngIf="!emptyTemplate; else empty">
{{emptyMessageLabel}}
</ng-container>
<ng-container #empty *ngTemplateOutlet="emptyTemplate"></ng-container>
</li>
</ul>
</div>
<ng-container *ngTemplateOutlet="footerTemplate"></ng-container>
</div>
</div>
`, isInline: true, styles: [".p-dropdown{display:inline-flex;cursor:pointer;position:relative;-webkit-user-select:none;-ms-user-select:none;user-select:none}.p-dropdown-clear-icon{position:absolute;top:50%;margin-top:-.5rem}.p-dropdown-trigger{display:flex;align-items:center;justify-content:center;flex-shrink:0}.p-dropdown-label{display:block;white-space:nowrap;overflow:hidden;flex:1 1 auto;width:1%;text-overflow:ellipsis;cursor:pointer}.p-dropdown-label-empty{overflow:hidden;visibility:hidden}input.p-dropdown-label{cursor:default}.p-dropdown .p-dropdown-panel{min-width:100%}.p-dropdown-panel{position:absolute}.p-dropdown-items-wrapper{overflow:auto}.p-dropdown-item{cursor:pointer;font-weight:400;white-space:nowrap;position:relative;overflow:hidden}.p-dropdown-items{margin:0;padding:0;list-style-type:none}.p-dropdown-filter{width:100%}.p-dropdown-filter-container{position:relative}.p-dropdown-filter-icon{position:absolute;top:50%;margin-top:-.5rem}.p-fluid .p-dropdown{display:flex}.p-fluid .p-dropdown .p-dropdown-label{width:1%}"], components: [{ type: DropdownItem, selector: "p-dropdownItem", inputs: ["option", "selected", "label", "disabled", "visible", "itemSize", "template"], outputs: ["onClick"] }, { type: i4.CdkVirtualScrollViewport, selector: "cdk-virtual-scroll-viewport", inputs: ["orientation"], outputs: ["scrolledIndexChange"] }], directives: [{ type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { type: i2.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i5.Tooltip, selector: "[pTooltip]", inputs: ["tooltipPosition", "tooltipEvent", "appendTo", "positionStyle", "tooltipStyleClass", "tooltipZIndex", "escape", "showDelay", "hideDelay", "life", "tooltipDisabled", "pTooltip"] }, { type: i2.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet"] }, { type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i4.CdkFixedSizeVirtualScroll, selector: "cdk-virtual-scroll-viewport[itemSize]", inputs: ["itemSize", "minBufferPx", "maxBufferPx"] }, { type: i4.CdkVirtualForOf, selector: "[cdkVirtualFor][cdkVirtualForOf]", inputs: ["cdkVirtualForOf", "cdkVirtualForTrackBy", "cdkVirtualForTemplate", "cdkVirtualForTemplateCacheSize"] }], animations: [
trigger('overlayAnimation', [
transition(':enter', [
style({ opacity: 0, transform: 'scaleY(0.8)' }),
animate('{{showTransitionParams}}')
]),
transition(':leave', [
animate('{{hideTransitionParams}}', style({ opacity: 0 }))
])
])
], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.4", ngImport: i0, type: Dropdown, decorators: [{
type: Component,
args: [{
selector: 'p-dropdown',
template: `
<div #container [ngClass]="{'p-dropdown p-component':true,
'p-disabled':disabled, 'p-dropdown-open':overlayVisible, 'p-focus':focused, 'p-dropdown-clearable': showClear && !disabled}"
(click)="onMouseclick($event)" [ngStyle]="style" [class]="styleClass">
<div class="p-hidden-accessible">
<input #in [attr.id]="inputId" type="text" readonly (focus)="onInputFocus($event)" aria-haspopup="listbox"
aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible" [attr.aria-labelledby]="ariaLabelledBy" (blur)="onInputBlur($event)" (keydown)="onKeydown($event, true)"
[disabled]="disabled" [attr.tabindex]="tabindex" [attr.autofocus]="autofocus">
</div>
<span [ngClass]="{'p-dropdown-label p-inputtext':true,'p-dropdown-label-empty':(label == null || label.length === 0)}" *ngIf="!editable && (label != null)" [pTooltip]="tooltip" [tooltipPosition]="tooltipPosition" [positionStyle]="tooltipPositionStyle" [tooltipStyleClass]="tooltipStyleClass">
<ng-container *ngIf="!selectedItemTemplate">{{label||'empty'}}</ng-container>
<ng-container *ngTemplateOutlet="selectedItemTemplate; context: {$implicit: selectedOption}"></ng-container>
</span>
<span [ngClass]="{'p-dropdown-label p-inputtext p-placeholder':true,'p-dropdown-label-empty': (placeholder == null || placeholder.length === 0)}" *ngIf="!editable && (label == null)">{{placeholder||'empty'}}</span>
<input #editableInput type="text" [attr.maxlength]="maxlength" class="p-dropdown-label p-inputtext" *ngIf="editable" [disabled]="disabled" [attr.placeholder]="placeholder"
aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible" (click)="onEditableInputClick()" (input)="onEditableInputChange($event)" (focus)="onEditableInputFocus($event)" (blur)="onInputBlur($event)">
<i class="p-dropdown-clear-icon pi pi-times" (click)="clear($event)" *ngIf="value != null && showClear && !disabled"></i>
<div class="p-dropdown-trigger" role="button" aria-haspopup="listbox" [attr.aria-expanded]="overlayVisible">
<span class="p-dropdown-trigger-icon" [ngClass]="dropdownIcon"></span>
</div>
<div *ngIf="overlayVisible" [ngClass]="'p-dropdown-panel p-component'" [@overlayAnimation]="{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}" (@overlayAnimation.start)="onOverlayAnimationStart($event)" [ngStyle]="panelStyle" [class]="panelStyleClass">
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
<div class="p-dropdown-header" *ngIf="filter" >
<div class="p-dropdown-filter-container" (click)="$event.