import * as i0 from '@angular/core'; import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Input, Output, ViewChild, ContentChild, ContentChildren, NgModule } from '@angular/core'; import * as i2 from '@angular/common'; import { CommonModule } from '@angular/common'; import * as i1 from 'primeng/api'; import { TranslationKeys, Header, Footer, PrimeTemplate, SharedModule } from 'primeng/api'; import { DomHandler } from 'primeng/dom'; import { ObjectUtils } from 'primeng/utils'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import * as i3 from 'primeng/ripple'; import { RippleModule } from 'primeng/ripple'; const LISTBOX_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Listbox), multi: true }; class Listbox { constructor(el, cd, filterService, config) { this.el = el; this.cd = cd; this.filterService = filterService; this.config = config; this.checkbox = false; this.filter = false; this.filterMatchMode = 'contains'; this.metaKeySelection = true; this.showToggleAll = true; this.optionGroupChildren = "items"; this.onChange = new EventEmitter(); this.onClick = new EventEmitter(); this.onDblClick = new EventEmitter(); this.onModelChange = () => { }; this.onModelTouched = () => { }; } get options() { return this._options; } set options(val) { this._options = val; if (this.hasFilter()) this.activateFilter(); } get filterValue() { return this._filterValue; } set filterValue(val) { this._filterValue = val; this.activateFilter(); } ngOnInit() { this.translationSubscription = this.config.translationObserver.subscribe(() => { this.cd.markForCheck(); }); } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'item': this.itemTemplate = item.template; break; case 'group': this.groupTemplate = item.template; break; case 'header': this.headerTemplate = item.template; break; case 'footer': this.footerTemplate = item.template; break; case 'empty': this.emptyTemplate = item.template; break; case 'emptyfilter': this.emptyFilterTemplate = item.template; break; default: this.itemTemplate = item.template; break; } }); } getOptionLabel(option) { return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : (option.label != undefined ? option.label : option); } getOptionGroupChildren(optionGroup) { return this.optionGroupChildren ? ObjectUtils.resolveFieldData(optionGroup, this.optionGroupChildren) : optionGroup.items; } getOptionGroupLabel(optionGroup) { return this.optionGroupLabel ? ObjectUtils.resolveFieldData(optionGroup, this.optionGroupLabel) : (optionGroup.label != undefined ? optionGroup.label : optionGroup); } 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); } writeValue(value) { this.value = value; this.cd.markForCheck(); } registerOnChange(fn) { this.onModelChange = fn; } registerOnTouched(fn) { this.onModelTouched = fn; } setDisabledState(val) { this.disabled = val; this.cd.markForCheck(); } onOptionClick(event, option) { if (this.disabled || this.isOptionDisabled(option) || 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(option) { if (this.disabled || this.isOptionDisabled(option) || this.readonly) { return; } this.optionTouched = true; } onOptionDoubleClick(event, option) { if (this.disabled || this.isOptionDisabled(option) || 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 = this.getOptionValue(option); valueChanged = true; } } else { this.value = selected ? null : this.getOptionValue(option); 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 = [this.getOptionValue(option)]; } valueChanged = true; } else { this.value = (metaKey) ? this.value || [] : []; this.value = [...this.value, this.getOptionValue(option)]; valueChanged = true; } } else { if (selected) { this.removeOption(option); } else { this.value = [...this.value || [], this.getOptionValue(option)]; } 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, this.getOptionValue(option)]; } this.onModelChange(this.value); this.onChange.emit({ originalEvent: event, value: this.value }); } removeOption(option) { this.value = this.value.filter(val => !ObjectUtils.equals(val, this.getOptionValue(option), this.dataKey)); } isSelected(option) { let selected = false; let optionValue = this.getOptionValue(option); if (this.multiple) { if (this.value) { for (let val of this.value) { if (ObjectUtils.equals(val, optionValue, this.dataKey)) { selected = true; break; } } } } else { selected = ObjectUtils.equals(this.value, optionValue, this.dataKey); } return selected; } 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(optionsToDisplay) { return !optionsToDisplay || (optionsToDisplay && optionsToDisplay.length === 0); } onFilter(event) { this._filterValue = event.target.value; this.activateFilter(); } activateFilter() { if (this.hasFilter() && this._options) { if (this.group) { let searchFields = (this.optionLabel || 'label').split(','); 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._options.filter(option => this.filterService.filters[this.filterMatchMode](this.getOptionLabel(option), this._filterValue, this.filterLocale)); } } else { this._filteredOptions = null; } } 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 }); event.preventDefault(); } 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; } 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, 'p-disabled') || DomHandler.isHidden(nextItem) || DomHandler.hasClass(nextItem, 'p-listbox-item-group') ? this.findNextItem(nextItem) : nextItem; else return null; } findPrevItem(item) { let prevItem = item.previousElementSibling; if (prevItem) return DomHandler.hasClass(prevItem, 'p-disabled') || DomHandler.isHidden(prevItem) || DomHandler.hasClass(prevItem, 'p-listbox-item-group') ? this.findPrevItem(prevItem) : prevItem; else return null; } onHeaderCheckboxFocus() { this.headerCheckboxFocus = true; } onHeaderCheckboxBlur() { this.headerCheckboxFocus = false; } ngOnDestroy() { if (this.translationSubscription) { this.translationSubscription.unsubscribe(); } } } Listbox.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: Listbox, deps: [{ token: i0.ElementRef }, { token: i0.ChangeDetectorRef }, { token: i1.FilterService }, { token: i1.PrimeNGConfig }], target: i0.ɵɵFactoryTarget.Component }); Listbox.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.0.3", type: Listbox, selector: "p-listbox", inputs: { multiple: "multiple", style: "style", styleClass: "styleClass", listStyle: "listStyle", listStyleClass: "listStyleClass", readonly: "readonly", disabled: "disabled", checkbox: "checkbox", filter: "filter", filterMatchMode: "filterMatchMode", filterLocale: "filterLocale", metaKeySelection: "metaKeySelection", dataKey: "dataKey", showToggleAll: "showToggleAll", optionLabel: "optionLabel", optionValue: "optionValue", optionGroupChildren: "optionGroupChildren", optionGroupLabel: "optionGroupLabel", optionDisabled: "optionDisabled", ariaFilterLabel: "ariaFilterLabel", filterPlaceHolder: "filterPlaceHolder", emptyFilterMessage: "emptyFilterMessage", emptyMessage: "emptyMessage", group: "group", options: "options", filterValue: "filterValue" }, outputs: { onChange: "onChange", onClick: "onClick", onDblClick: "onDblClick" }, host: { classAttribute: "p-element" }, providers: [LISTBOX_VALUE_ACCESSOR], queries: [{ propertyName: "headerFacet", first: true, predicate: Header, descendants: true }, { propertyName: "footerFacet", first: true, predicate: Footer, descendants: true }, { propertyName: "templates", predicate: PrimeTemplate }], viewQueries: [{ propertyName: "headerCheckboxViewChild", first: true, predicate: ["headerchkbox"], descendants: true }], ngImport: i0, template: `