import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, Input, Output, ContentChild, TemplateRef, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ObjectUtils } from 'primeng/utils'; import { RippleModule } from 'primeng/ripple'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; const SELECTBUTTON_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SelectButton), multi: true }; class SelectButton { constructor(cd) { this.cd = cd; this.tabindex = 0; this.onOptionClick = new EventEmitter(); this.onChange = new EventEmitter(); this.onModelChange = () => { }; this.onModelTouched = () => { }; } 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 : 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(); } onItemClick(event, option, index) { if (this.disabled || this.isOptionDisabled(option)) { return; } if (this.multiple) { if (this.isSelected(option)) this.removeOption(option); else this.value = [...(this.value || []), this.getOptionValue(option)]; } else { this.value = this.getOptionValue(option); } this.onOptionClick.emit({ originalEvent: event, option: option, index: index }); this.onModelChange(this.value); this.onChange.emit({ originalEvent: event, value: this.value }); } onBlur() { this.onModelTouched(); } 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.getOptionValue(option), this.value, this.dataKey); } return selected; } } SelectButton.decorators = [ { type: Component, args: [{ selector: 'p-selectButton', template: `