primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 6.53 kB
JavaScript
import { forwardRef, EventEmitter, ChangeDetectorRef, Input, Output, ContentChild, TemplateRef, Component, ChangeDetectionStrategy, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ObjectUtils } from 'primeng/utils';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const SELECTBUTTON_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectButton),
multi: true
};
let SelectButton = class SelectButton {
constructor(cd) {
this.cd = cd;
this.tabindex = 0;
this.onOptionClick = new EventEmitter();
this.onChange = new EventEmitter();
this.onModelChange = () => { };
this.onModelTouched = () => { };
}
get options() {
return this._options;
}
set options(val) {
//NoOp
}
ngOnChanges(simpleChange) {
if (simpleChange.options) {
this._options = this.optionLabel ? ObjectUtils.generateSelectItems(simpleChange.options.currentValue, this.optionLabel) : simpleChange.options.currentValue;
}
}
writeValue(value) {
this.value = value;
this.cd.markForCheck();
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
}
onItemClick(event, option, index) {
if (this.disabled || option.disabled) {
return;
}
if (this.multiple) {
let itemIndex = this.findItemIndex(option);
if (itemIndex != -1)
this.value = this.value.filter((val, i) => i != itemIndex);
else
this.value = [...this.value || [], option.value];
}
else {
this.value = option.value;
}
this.onOptionClick.emit({
originalEvent: event,
option: option,
index: index
});
this.onModelChange(this.value);
this.onChange.emit({
originalEvent: event,
value: this.value
});
}
onFocus(event) {
this.focusedItem = event.target;
}
onBlur(event) {
this.focusedItem = null;
this.onModelTouched();
}
isSelected(option) {
if (this.multiple)
return this.findItemIndex(option) != -1;
else
return ObjectUtils.equals(option.value, this.value, this.dataKey);
}
findItemIndex(option) {
let index = -1;
if (this.value) {
for (let i = 0; i < this.value.length; i++) {
if (this.value[i] == option.value) {
index = i;
break;
}
}
}
return index;
}
};
SelectButton.ctorParameters = () => [
{ type: ChangeDetectorRef }
];
__decorate([
Input()
], SelectButton.prototype, "tabindex", void 0);
__decorate([
Input()
], SelectButton.prototype, "multiple", void 0);
__decorate([
Input()
], SelectButton.prototype, "style", void 0);
__decorate([
Input()
], SelectButton.prototype, "styleClass", void 0);
__decorate([
Input()
], SelectButton.prototype, "ariaLabelledBy", void 0);
__decorate([
Input()
], SelectButton.prototype, "disabled", void 0);
__decorate([
Input()
], SelectButton.prototype, "dataKey", void 0);
__decorate([
Input()
], SelectButton.prototype, "optionLabel", void 0);
__decorate([
Output()
], SelectButton.prototype, "onOptionClick", void 0);
__decorate([
Output()
], SelectButton.prototype, "onChange", void 0);
__decorate([
ContentChild(TemplateRef)
], SelectButton.prototype, "itemTemplate", void 0);
__decorate([
Input()
], SelectButton.prototype, "options", null);
SelectButton = __decorate([
Component({
selector: 'p-selectButton',
template: `
<div [ngClass]="'ui-selectbutton ui-buttonset ui-widget ui-corner-all ui-buttonset-' + (options ? options.length : 0)" [ngStyle]="style" [class]="styleClass" role="group">
<div *ngFor="let option of options; let i = index" #btn class="ui-button ui-widget ui-state-default ui-button-text-only {{option.styleClass}}" role="button" [attr.aria-pressed]="isSelected(option)"
[ngClass]="{'ui-state-active':isSelected(option), 'ui-state-disabled': disabled || option.disabled, 'ui-state-focus': btn == focusedItem,
'ui-button-text-icon-left': (option.icon != null), 'ui-button-icon-only': (option.icon && !option.label)}" (click)="onItemClick($event,option,i)" (keydown.enter)="onItemClick($event,option,i)"
[attr.title]="option.title" [attr.aria-label]="option.label" (focus)="onFocus($event)" (blur)="onBlur($event)" [attr.tabindex]="tabindex" [attr.aria-labelledby]="ariaLabelledBy">
<ng-container *ngIf="!itemTemplate else customcontent">
<span [ngClass]="['ui-clickable', 'ui-button-icon-left']" [class]="option.icon" *ngIf="option.icon"></span>
<span class="ui-button-text ui-clickable">{{option.label||'ui-btn'}}</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.Default
})
], SelectButton);
let SelectButtonModule = class SelectButtonModule {
};
SelectButtonModule = __decorate([
NgModule({
imports: [CommonModule],
exports: [SelectButton],
declarations: [SelectButton]
})
], SelectButtonModule);
/**
* Generated bundle index. Do not edit.
*/
export { SELECTBUTTON_VALUE_ACCESSOR, SelectButton, SelectButtonModule };
//# sourceMappingURL=primeng-selectbutton.js.map