primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 4.56 kB
JavaScript
import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ChangeDetectorRef, Input, Output, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RippleModule } from 'primeng/ripple';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
const TOGGLEBUTTON_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ToggleButton),
multi: true
};
class ToggleButton {
constructor(cd) {
this.cd = cd;
this.iconPos = 'left';
this.onChange = new EventEmitter();
this.checked = false;
this.onModelChange = () => { };
this.onModelTouched = () => { };
}
toggle(event) {
if (!this.disabled) {
this.checked = !this.checked;
this.onModelChange(this.checked);
this.onModelTouched();
this.onChange.emit({
originalEvent: event,
checked: this.checked
});
this.cd.markForCheck();
}
}
onBlur() {
this.onModelTouched();
}
writeValue(value) {
this.checked = value;
this.cd.markForCheck();
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
this.cd.markForCheck();
}
get hasOnLabel() {
return this.onLabel && this.onLabel.length > 0;
}
get hasOffLabel() {
return this.onLabel && this.onLabel.length > 0;
}
}
ToggleButton.decorators = [
{ type: Component, args: [{
selector: 'p-toggleButton',
template: `
<div [ngClass]="{'p-button p-togglebutton p-component': true, 'p-button-icon-only': (onIcon && offIcon && !hasOnLabel && !hasOffLabel),'p-highlight': checked,'p-disabled':disabled}"
[ngStyle]="style" [class]="styleClass" (click)="toggle($event)" (keydown.enter)="toggle($event)"
[attr.tabindex]="disabled ? null : '0'" role="checkbox" [attr.aria-checked]="checked" pRipple>
<span *ngIf="onIcon||offIcon" [class]="checked ? this.onIcon : this.offIcon"
[ngClass]="{'p-button-icon': true, 'p-button-icon-left': (iconPos === 'left'), 'p-button-icon-right': (iconPos === 'right')}"></span>
<span class="p-button-label">{{checked ? hasOnLabel ? onLabel : '' : hasOffLabel ? offLabel : ''}}</span>
</div>
`,
providers: [TOGGLEBUTTON_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
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}"]
},] }
];
ToggleButton.ctorParameters = () => [
{ type: ChangeDetectorRef }
];
ToggleButton.propDecorators = {
onLabel: [{ type: Input }],
offLabel: [{ type: Input }],
onIcon: [{ type: Input }],
offIcon: [{ type: Input }],
ariaLabelledBy: [{ type: Input }],
disabled: [{ type: Input }],
style: [{ type: Input }],
styleClass: [{ type: Input }],
inputId: [{ type: Input }],
tabindex: [{ type: Input }],
iconPos: [{ type: Input }],
onChange: [{ type: Output }]
};
class ToggleButtonModule {
}
ToggleButtonModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, RippleModule],
exports: [ToggleButton],
declarations: [ToggleButton]
},] }
];
/**
* Generated bundle index. Do not edit.
*/
export { TOGGLEBUTTON_VALUE_ACCESSOR, ToggleButton, ToggleButtonModule };
//# sourceMappingURL=primeng-togglebutton.js.map