UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

217 lines (211 loc) 7.7 kB
import { __decorate, __metadata } from 'tslib'; import { FocusMonitor } from '@angular/cdk/a11y'; import { LEFT_ARROW, RIGHT_ARROW, SPACE, ENTER } from '@angular/cdk/keycodes'; import { Component, ChangeDetectionStrategy, ViewEncapsulation, forwardRef, ChangeDetectorRef, Optional, ViewChild, Input, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { Directionality, BidiModule } from '@angular/cdk/bidi'; import { NzConfigService, WithConfig } from 'ng-zorro-antd/core/config'; import { InputBoolean } from 'ng-zorro-antd/core/util'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { CommonModule } from '@angular/common'; import { NzOutletModule } from 'ng-zorro-antd/core/outlet'; import { NzWaveModule } from 'ng-zorro-antd/core/wave'; import { NzIconModule } from 'ng-zorro-antd/icon'; /** * 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 */ const NZ_CONFIG_MODULE_NAME = 'switch'; class NzSwitchComponent { constructor(nzConfigService, cdr, focusMonitor, directionality) { this.nzConfigService = nzConfigService; this.cdr = cdr; this.focusMonitor = focusMonitor; this.directionality = directionality; this._nzModuleName = NZ_CONFIG_MODULE_NAME; this.isChecked = false; this.onChange = () => { }; this.onTouched = () => { }; this.nzLoading = false; this.nzDisabled = false; this.nzControl = false; this.nzCheckedChildren = null; this.nzUnCheckedChildren = null; this.nzSize = 'default'; this.dir = 'ltr'; this.destroy$ = new Subject(); } onHostClick(e) { e.preventDefault(); if (!this.nzDisabled && !this.nzLoading && !this.nzControl) { this.updateValue(!this.isChecked); } } updateValue(value) { if (this.isChecked !== value) { this.isChecked = value; this.onChange(this.isChecked); } } onKeyDown(e) { if (!this.nzControl && !this.nzDisabled && !this.nzLoading) { if (e.keyCode === LEFT_ARROW) { this.updateValue(false); e.preventDefault(); } else if (e.keyCode === RIGHT_ARROW) { this.updateValue(true); e.preventDefault(); } else if (e.keyCode === SPACE || e.keyCode === ENTER) { this.updateValue(!this.isChecked); e.preventDefault(); } } } focus() { var _a; this.focusMonitor.focusVia((_a = this.switchElement) === null || _a === void 0 ? void 0 : _a.nativeElement, 'keyboard'); } blur() { var _a; (_a = this.switchElement) === null || _a === void 0 ? void 0 : _a.nativeElement.blur(); } ngOnInit() { var _a; (_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() { this.focusMonitor.monitor(this.switchElement.nativeElement, true).subscribe(focusOrigin => { if (!focusOrigin) { /** https://github.com/angular/angular/issues/17793 **/ Promise.resolve().then(() => this.onTouched()); } }); } ngOnDestroy() { this.focusMonitor.stopMonitoring(this.switchElement.nativeElement); this.destroy$.next(); this.destroy$.complete(); } writeValue(value) { this.isChecked = value; this.cdr.markForCheck(); } registerOnChange(fn) { this.onChange = fn; } registerOnTouched(fn) { this.onTouched = fn; } setDisabledState(disabled) { this.nzDisabled = disabled; this.cdr.markForCheck(); } } NzSwitchComponent.decorators = [ { type: Component, args: [{ selector: 'nz-switch', exportAs: 'nzSwitch', preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzSwitchComponent), multi: true } ], template: ` <button nz-wave type="button" class="ant-switch" #switchElement [disabled]="nzDisabled" [class.ant-switch-checked]="isChecked" [class.ant-switch-loading]="nzLoading" [class.ant-switch-disabled]="nzDisabled" [class.ant-switch-small]="nzSize === 'small'" [class.ant-switch-rtl]="dir === 'rtl'" [nzWaveExtraNode]="true" (keydown)="onKeyDown($event)" > <span class="ant-switch-handle"> <i *ngIf="nzLoading" nz-icon nzType="loading" class="ant-switch-loading-icon"></i> </span> <span class="ant-switch-inner"> <ng-container *ngIf="isChecked; else uncheckTemplate"> <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container> </ng-container> <ng-template #uncheckTemplate> <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container> </ng-template> </span> <div class="ant-click-animating-node"></div> </button> `, host: { '(click)': 'onHostClick($event)' } },] } ]; NzSwitchComponent.ctorParameters = () => [ { type: NzConfigService }, { type: ChangeDetectorRef }, { type: FocusMonitor }, { type: Directionality, decorators: [{ type: Optional }] } ]; NzSwitchComponent.propDecorators = { switchElement: [{ type: ViewChild, args: ['switchElement', { static: true },] }], nzLoading: [{ type: Input }], nzDisabled: [{ type: Input }], nzControl: [{ type: Input }], nzCheckedChildren: [{ type: Input }], nzUnCheckedChildren: [{ type: Input }], nzSize: [{ type: Input }] }; __decorate([ InputBoolean(), __metadata("design:type", Object) ], NzSwitchComponent.prototype, "nzLoading", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], NzSwitchComponent.prototype, "nzDisabled", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], NzSwitchComponent.prototype, "nzControl", void 0); __decorate([ WithConfig(), __metadata("design:type", String) ], NzSwitchComponent.prototype, "nzSize", 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 NzSwitchModule { } NzSwitchModule.decorators = [ { type: NgModule, args: [{ exports: [NzSwitchComponent], declarations: [NzSwitchComponent], imports: [BidiModule, CommonModule, NzWaveModule, NzIconModule, NzOutletModule] },] } ]; /** * 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 { NzSwitchComponent, NzSwitchModule }; //# sourceMappingURL=ng-zorro-antd-switch.js.map