UNPKG

primeng

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![npm downloads](https://img.shields.io/npm/dm/primeng.sv

155 lines (151 loc) 6.91 kB
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.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(); } 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: ` <div [ngClass]="'p-selectbutton p-buttonset p-component'" [ngStyle]="style" [class]="styleClass" role="group"> <div *ngFor="let option of options; let i = index" #btn class="p-button p-component" [class]="option.styleClass" role="button" [attr.aria-pressed]="isSelected(option)" [ngClass]="{'p-highlight':isSelected(option), 'p-disabled': disabled || isOptionDisabled(option), 'p-button-icon-only': (option.icon && !getOptionLabel(option))}" (click)="onItemClick($event,option,i)" (keydown.enter)="onItemClick($event,option,i)" [attr.title]="option.title" [attr.aria-label]="option.label" (blur)="onBlur()" [attr.tabindex]="disabled ? null : tabindex" [attr.aria-labelledby]="this.getOptionLabel(option)" pRipple> <ng-container *ngIf="!itemTemplate else customcontent"> <span [ngClass]="'p-button-icon p-button-icon-left'" [class]="option.icon" *ngIf="option.icon"></span> <span class="p-button-label">{{getOptionLabel(option)}}</span> </ng-container> <ng-template #customcontent> <ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: option, index: i}"></ng-container> </ng-template> </div> </div> `, providers: [SELECTBUTTON_VALUE_ACCESSOR], changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, styles: [".p-button{-ms-user-select:none;-webkit-user-select:none;align-items:center;cursor:pointer;display:inline-flex;margin:0;overflow:hidden;position:relative;text-align:center;user-select:none;vertical-align:bottom}.p-button-label{flex:1 1 auto}.p-button-icon-right{order:1}.p-button:disabled{cursor:default}.p-button-icon-only{justify-content:center}.p-button-icon-only .p-button-label{flex:0 0 auto;visibility:hidden;width:0}.p-button-vertical{flex-direction:column}.p-button-icon-bottom{order:2}.p-buttonset .p-button{margin:0}.p-buttonset .p-button:not(:last-child){border-right:0}.p-buttonset .p-button:not(:first-of-type):not(:last-of-type){border-radius:0}.p-buttonset .p-button:first-of-type{border-bottom-right-radius:0;border-top-right-radius:0}.p-buttonset .p-button:last-of-type{border-bottom-left-radius:0;border-top-left-radius:0}.p-buttonset .p-button:focus{position:relative;z-index:1}.p-button-label{transition:all .2s}"] },] } ]; SelectButton.ctorParameters = () => [ { type: ChangeDetectorRef } ]; SelectButton.propDecorators = { options: [{ type: Input }], optionLabel: [{ type: Input }], optionValue: [{ type: Input }], optionDisabled: [{ type: Input }], tabindex: [{ type: Input }], multiple: [{ type: Input }], style: [{ type: Input }], styleClass: [{ type: Input }], ariaLabelledBy: [{ type: Input }], disabled: [{ type: Input }], dataKey: [{ type: Input }], onOptionClick: [{ type: Output }], onChange: [{ type: Output }], itemTemplate: [{ type: ContentChild, args: [TemplateRef,] }] }; class SelectButtonModule { } SelectButtonModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule, RippleModule], exports: [SelectButton], declarations: [SelectButton] },] } ]; /** * Generated bundle index. Do not edit. */ export { SELECTBUTTON_VALUE_ACCESSOR, SelectButton, SelectButtonModule }; //# sourceMappingURL=primeng-selectbutton.js.map