151 lines
6.5 KiB
JavaScript
151 lines
6.5 KiB
JavaScript
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 = () => { };
|
|
}
|
|
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;
|
|
this.cd.markForCheck();
|
|
}
|
|
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
|
|
});
|
|
}
|
|
onBlur() {
|
|
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.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 || option.disabled,
|
|
'p-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" (blur)="onBlur()" [attr.tabindex]="disabled ? null : tabindex" [attr.aria-labelledby]="ariaLabelledBy" 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">{{option.label}}</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: [".ui-selectbutton{display:inline-block}.ui-selectbutton.ui-state-error{padding:0}.ui-selectbutton .ui-button.ui-state-focus{outline:none}", ".p-button{-moz-user-select:none;-ms-flex-align:center;-ms-user-select:none;-webkit-user-select:none;align-items:center;cursor:pointer;display:-ms-inline-flexbox;display:inline-flex;margin:0;overflow:hidden;position:relative;text-align:center;user-select:none;vertical-align:bottom}.p-button-label{-ms-flex:1 1 auto;flex:1 1 auto}.p-button-icon-right{-ms-flex-order:1;order:1}.p-button:disabled{cursor:default}.p-button-icon-only{-ms-flex-pack:center;justify-content:center}.p-button-icon-only .p-button-label{-ms-flex:0 0 auto;flex:0 0 auto;visibility:hidden;width:0}.p-button-vertical{-ms-flex-direction:column;flex-direction:column}.p-button-icon-bottom{-ms-flex-order:2;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}"]
|
|
},] }
|
|
];
|
|
SelectButton.ctorParameters = () => [
|
|
{ type: ChangeDetectorRef }
|
|
];
|
|
SelectButton.propDecorators = {
|
|
tabindex: [{ type: Input }],
|
|
multiple: [{ type: Input }],
|
|
style: [{ type: Input }],
|
|
styleClass: [{ type: Input }],
|
|
ariaLabelledBy: [{ type: Input }],
|
|
disabled: [{ type: Input }],
|
|
dataKey: [{ type: Input }],
|
|
optionLabel: [{ type: Input }],
|
|
onOptionClick: [{ type: Output }],
|
|
onChange: [{ type: Output }],
|
|
itemTemplate: [{ type: ContentChild, args: [TemplateRef,] }],
|
|
options: [{ type: Input }]
|
|
};
|
|
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
|