ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
345 lines (337 loc) • 11.9 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { FocusMonitor, A11yModule } from '@angular/cdk/a11y';
import { Directionality, BidiModule } from '@angular/cdk/bidi';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Renderer2, ElementRef, Output, forwardRef, Optional, ChangeDetectorRef, ViewChild, Input, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzCheckboxWrapperComponent {
constructor(renderer, elementRef) {
this.nzOnChange = new EventEmitter();
this.checkboxList = [];
renderer.addClass(elementRef.nativeElement, 'ant-checkbox-group');
}
addCheckbox(value) {
this.checkboxList.push(value);
}
removeCheckbox(value) {
this.checkboxList.splice(this.checkboxList.indexOf(value), 1);
}
onChange() {
const listOfCheckedValue = this.checkboxList.filter(item => item.nzChecked).map(item => item.nzValue);
this.nzOnChange.emit(listOfCheckedValue);
}
}
NzCheckboxWrapperComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-checkbox-wrapper',
exportAs: 'nzCheckboxWrapper',
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
<ng-content></ng-content>
`
},] }
];
NzCheckboxWrapperComponent.ctorParameters = () => [
{ type: Renderer2 },
{ type: ElementRef }
];
NzCheckboxWrapperComponent.propDecorators = {
nzOnChange: [{ type: Output }]
};
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzCheckboxComponent {
constructor(elementRef, nzCheckboxWrapperComponent, cdr, focusMonitor, directionality) {
this.elementRef = elementRef;
this.nzCheckboxWrapperComponent = nzCheckboxWrapperComponent;
this.cdr = cdr;
this.focusMonitor = focusMonitor;
this.directionality = directionality;
this.dir = 'ltr';
this.destroy$ = new Subject();
this.onChange = () => { };
this.onTouched = () => { };
this.nzCheckedChange = new EventEmitter();
this.nzValue = null;
this.nzAutoFocus = false;
this.nzDisabled = false;
this.nzIndeterminate = false;
this.nzChecked = false;
// TODO: move to host after View Engine deprecation
this.elementRef.nativeElement.classList.add('ant-checkbox-wrapper');
}
hostClick(e) {
e.preventDefault();
this.focus();
this.innerCheckedChange(!this.nzChecked);
}
innerCheckedChange(checked) {
if (!this.nzDisabled) {
this.nzChecked = checked;
this.onChange(this.nzChecked);
this.nzCheckedChange.emit(this.nzChecked);
if (this.nzCheckboxWrapperComponent) {
this.nzCheckboxWrapperComponent.onChange();
}
}
}
writeValue(value) {
this.nzChecked = value;
this.cdr.markForCheck();
}
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
setDisabledState(disabled) {
this.nzDisabled = disabled;
this.cdr.markForCheck();
}
focus() {
this.focusMonitor.focusVia(this.inputElement, 'keyboard');
}
blur() {
this.inputElement.nativeElement.blur();
}
ngOnInit() {
var _a;
this.focusMonitor.monitor(this.elementRef, true).subscribe(focusOrigin => {
if (!focusOrigin) {
Promise.resolve().then(() => this.onTouched());
}
});
if (this.nzCheckboxWrapperComponent) {
this.nzCheckboxWrapperComponent.addCheckbox(this);
}
(_a = this.directionality.change) === null || _a === void 0 ? void 0 : _a.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngAfterViewInit() {
if (this.nzAutoFocus) {
this.focus();
}
}
ngOnDestroy() {
this.focusMonitor.stopMonitoring(this.elementRef);
if (this.nzCheckboxWrapperComponent) {
this.nzCheckboxWrapperComponent.removeCheckbox(this);
}
this.destroy$.next();
this.destroy$.complete();
}
}
NzCheckboxComponent.decorators = [
{ type: Component, args: [{
selector: '[nz-checkbox]',
exportAs: 'nzCheckbox',
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
<span
class="ant-checkbox"
[class.ant-checkbox-checked]="nzChecked && !nzIndeterminate"
[class.ant-checkbox-disabled]="nzDisabled"
[class.ant-checkbox-indeterminate]="nzIndeterminate"
>
<input
#inputElement
type="checkbox"
class="ant-checkbox-input"
[attr.autofocus]="nzAutoFocus ? 'autofocus' : null"
[checked]="nzChecked"
[ngModel]="nzChecked"
[disabled]="nzDisabled"
(ngModelChange)="innerCheckedChange($event)"
(click)="$event.stopPropagation()"
/>
<span class="ant-checkbox-inner"></span>
</span>
<span><ng-content></ng-content></span>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NzCheckboxComponent),
multi: true
}
],
host: {
'[class.ant-checkbox-wrapper-checked]': 'nzChecked',
'[class.ant-checkbox-rtl]': `dir === 'rtl'`,
'(click)': 'hostClick($event)'
}
},] }
];
NzCheckboxComponent.ctorParameters = () => [
{ type: ElementRef },
{ type: NzCheckboxWrapperComponent, decorators: [{ type: Optional }] },
{ type: ChangeDetectorRef },
{ type: FocusMonitor },
{ type: Directionality, decorators: [{ type: Optional }] }
];
NzCheckboxComponent.propDecorators = {
inputElement: [{ type: ViewChild, args: ['inputElement', { static: true },] }],
nzCheckedChange: [{ type: Output }],
nzValue: [{ type: Input }],
nzAutoFocus: [{ type: Input }],
nzDisabled: [{ type: Input }],
nzIndeterminate: [{ type: Input }],
nzChecked: [{ type: Input }]
};
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzCheckboxComponent.prototype, "nzAutoFocus", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzCheckboxComponent.prototype, "nzDisabled", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzCheckboxComponent.prototype, "nzIndeterminate", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzCheckboxComponent.prototype, "nzChecked", void 0);
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzCheckboxGroupComponent {
constructor(elementRef, focusMonitor, cdr, directionality) {
this.elementRef = elementRef;
this.focusMonitor = focusMonitor;
this.cdr = cdr;
this.directionality = directionality;
this.onChange = () => { };
this.onTouched = () => { };
this.options = [];
this.nzDisabled = false;
this.dir = 'ltr';
this.destroy$ = new Subject();
// TODO: move to host after View Engine deprecation
this.elementRef.nativeElement.classList.add('ant-checkbox-group');
}
trackByOption(_, option) {
return option.value;
}
onCheckedChange(option, checked) {
option.checked = checked;
this.onChange(this.options);
}
ngOnInit() {
var _a;
this.focusMonitor.monitor(this.elementRef, true).subscribe(focusOrigin => {
if (!focusOrigin) {
Promise.resolve().then(() => this.onTouched());
}
});
(_a = this.directionality.change) === null || _a === void 0 ? void 0 : _a.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngOnDestroy() {
this.focusMonitor.stopMonitoring(this.elementRef);
this.destroy$.next();
this.destroy$.complete();
}
writeValue(value) {
this.options = value;
this.cdr.markForCheck();
}
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
setDisabledState(disabled) {
this.nzDisabled = disabled;
this.cdr.markForCheck();
}
}
NzCheckboxGroupComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-checkbox-group',
exportAs: 'nzCheckboxGroup',
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None,
template: `
<label
nz-checkbox
class="ant-checkbox-group-item"
*ngFor="let o of options; trackBy: trackByOption"
[nzDisabled]="o.disabled || nzDisabled"
[nzChecked]="o.checked!"
(nzCheckedChange)="onCheckedChange(o, $event)"
>
<span>{{ o.label }}</span>
</label>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NzCheckboxGroupComponent),
multi: true
}
],
host: {
'[class.ant-checkbox-group-rtl]': `dir === 'rtl'`
}
},] }
];
NzCheckboxGroupComponent.ctorParameters = () => [
{ type: ElementRef },
{ type: FocusMonitor },
{ type: ChangeDetectorRef },
{ type: Directionality, decorators: [{ type: Optional }] }
];
NzCheckboxGroupComponent.propDecorators = {
nzDisabled: [{ type: Input }]
};
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzCheckboxGroupComponent.prototype, "nzDisabled", void 0);
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzCheckboxModule {
}
NzCheckboxModule.decorators = [
{ type: NgModule, args: [{
imports: [BidiModule, CommonModule, FormsModule, A11yModule],
declarations: [NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxWrapperComponent],
exports: [NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxWrapperComponent]
},] }
];
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
/**
* Generated bundle index. Do not edit.
*/
export { NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxModule, NzCheckboxWrapperComponent };
//# sourceMappingURL=ng-zorro-antd-checkbox.js.map