156 lines
5.6 KiB
JavaScript
156 lines
5.6 KiB
JavaScript
import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, Input, ViewChild, Output, NgModule } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
|
const CHECKBOX_VALUE_ACCESSOR = {
|
|
provide: NG_VALUE_ACCESSOR,
|
|
useExisting: forwardRef(() => Checkbox),
|
|
multi: true
|
|
};
|
|
class Checkbox {
|
|
constructor(cd) {
|
|
this.cd = cd;
|
|
this.checkboxIcon = 'pi pi-check';
|
|
this.onChange = new EventEmitter();
|
|
this.onModelChange = () => { };
|
|
this.onModelTouched = () => { };
|
|
this.focused = false;
|
|
this.checked = false;
|
|
}
|
|
onClick(event, checkbox, focus) {
|
|
event.preventDefault();
|
|
if (this.disabled || this.readonly) {
|
|
return;
|
|
}
|
|
this.checked = !this.checked;
|
|
this.updateModel(event);
|
|
if (focus) {
|
|
checkbox.focus();
|
|
}
|
|
}
|
|
updateModel(event) {
|
|
if (!this.binary) {
|
|
if (this.checked)
|
|
this.addValue();
|
|
else
|
|
this.removeValue();
|
|
this.onModelChange(this.model);
|
|
if (this.formControl) {
|
|
this.formControl.setValue(this.model);
|
|
}
|
|
}
|
|
else {
|
|
this.onModelChange(this.checked);
|
|
}
|
|
this.onChange.emit({ checked: this.checked, originalEvent: event });
|
|
}
|
|
handleChange(event) {
|
|
if (!this.readonly) {
|
|
this.checked = event.target.checked;
|
|
this.updateModel(event);
|
|
}
|
|
}
|
|
isChecked() {
|
|
if (this.binary)
|
|
return this.model;
|
|
else
|
|
return this.model && this.model.indexOf(this.value) > -1;
|
|
}
|
|
removeValue() {
|
|
this.model = this.model.filter(val => val !== this.value);
|
|
}
|
|
addValue() {
|
|
if (this.model)
|
|
this.model = [...this.model, this.value];
|
|
else
|
|
this.model = [this.value];
|
|
}
|
|
onFocus() {
|
|
this.focused = true;
|
|
}
|
|
onBlur() {
|
|
this.focused = false;
|
|
this.onModelTouched();
|
|
}
|
|
focus() {
|
|
this.inputViewChild.nativeElement.focus();
|
|
}
|
|
writeValue(model) {
|
|
this.model = model;
|
|
this.checked = this.isChecked();
|
|
this.cd.markForCheck();
|
|
}
|
|
registerOnChange(fn) {
|
|
this.onModelChange = fn;
|
|
}
|
|
registerOnTouched(fn) {
|
|
this.onModelTouched = fn;
|
|
}
|
|
setDisabledState(val) {
|
|
this.disabled = val;
|
|
this.cd.markForCheck();
|
|
}
|
|
}
|
|
Checkbox.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-checkbox',
|
|
template: `
|
|
<div [ngStyle]="style" [ngClass]="{'p-checkbox p-component': true, 'p-checkbox-checked': checked, 'p-checkbox-disabled': disabled, 'p-checkbox-focused': focused}" [class]="styleClass">
|
|
<div class="p-hidden-accessible">
|
|
<input #cb type="checkbox" [attr.id]="inputId" [attr.name]="name" [readonly]="readonly" [value]="value" [checked]="checked" (focus)="onFocus()" (blur)="onBlur()"
|
|
(change)="handleChange($event)" [disabled]="disabled" [attr.tabindex]="tabindex" [attr.aria-labelledby]="ariaLabelledBy" [attr.required]="required">
|
|
</div>
|
|
<div class="p-checkbox-box" (click)="onClick($event,cb,true)"
|
|
[ngClass]="{'p-highlight': checked, 'p-disabled': disabled, 'p-focus': focused}" role="checkbox" [attr.aria-checked]="checked">
|
|
<span class="p-checkbox-icon" [ngClass]="checked ? checkboxIcon : null"></span>
|
|
</div>
|
|
</div>
|
|
<label (click)="onClick($event,cb,true)" [class]="labelStyleClass"
|
|
[ngClass]="{'p-checkbox-label': true, 'p-checkbox-label-active':checked, 'p-disabled':disabled, 'p-checkbox-label-focus':focused}"
|
|
*ngIf="label" [attr.for]="inputId">{{label}}</label>
|
|
`,
|
|
providers: [CHECKBOX_VALUE_ACCESSOR],
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
encapsulation: ViewEncapsulation.None,
|
|
styles: [".p-checkbox{-moz-user-select:none;-ms-user-select:none;-webkit-user-select:none;cursor:pointer;display:-ms-inline-flexbox;display:inline-flex;user-select:none;vertical-align:bottom}.p-checkbox-box{-ms-flex-pack:center;display:-ms-flexbox;display:flex;justify-content:center}.p-checkbox-box,p-checkbox{-ms-flex-align:center;align-items:center}p-checkbox{display:-ms-inline-flexbox;display:inline-flex;vertical-align:bottom}.p-checkbox-label{line-height:1}"]
|
|
},] }
|
|
];
|
|
Checkbox.ctorParameters = () => [
|
|
{ type: ChangeDetectorRef }
|
|
];
|
|
Checkbox.propDecorators = {
|
|
value: [{ type: Input }],
|
|
name: [{ type: Input }],
|
|
disabled: [{ type: Input }],
|
|
binary: [{ type: Input }],
|
|
label: [{ type: Input }],
|
|
ariaLabelledBy: [{ type: Input }],
|
|
tabindex: [{ type: Input }],
|
|
inputId: [{ type: Input }],
|
|
style: [{ type: Input }],
|
|
styleClass: [{ type: Input }],
|
|
labelStyleClass: [{ type: Input }],
|
|
formControl: [{ type: Input }],
|
|
checkboxIcon: [{ type: Input }],
|
|
readonly: [{ type: Input }],
|
|
required: [{ type: Input }],
|
|
inputViewChild: [{ type: ViewChild, args: ['cb',] }],
|
|
onChange: [{ type: Output }]
|
|
};
|
|
class CheckboxModule {
|
|
}
|
|
CheckboxModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule],
|
|
exports: [Checkbox],
|
|
declarations: [Checkbox]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { CHECKBOX_VALUE_ACCESSOR, Checkbox, CheckboxModule };
|
|
//# sourceMappingURL=primeng-checkbox.js.map
|