ng-zorro-antd-switch.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { __esDecorate, __runInitializers } from 'tslib';
  2. import { LEFT_ARROW, RIGHT_ARROW, SPACE, ENTER } from '@angular/cdk/keycodes';
  3. import * as i0 from '@angular/core';
  4. import { forwardRef, booleanAttribute, Input, ViewChild, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
  5. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  6. import { Subject } from 'rxjs';
  7. import { takeUntil } from 'rxjs/operators';
  8. import * as i1 from 'ng-zorro-antd/core/config';
  9. import { WithConfig } from 'ng-zorro-antd/core/config';
  10. import * as i6 from 'ng-zorro-antd/core/outlet';
  11. import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
  12. import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
  13. import * as i4 from 'ng-zorro-antd/core/wave';
  14. import { NzWaveModule } from 'ng-zorro-antd/core/wave';
  15. import * as i5 from 'ng-zorro-antd/icon';
  16. import { NzIconModule } from 'ng-zorro-antd/icon';
  17. import * as i2 from '@angular/cdk/a11y';
  18. import * as i3 from '@angular/cdk/bidi';
  19. const NZ_CONFIG_MODULE_NAME = 'switch';
  20. let NzSwitchComponent = (() => {
  21. let _nzSize_decorators;
  22. let _nzSize_initializers = [];
  23. let _nzSize_extraInitializers = [];
  24. return class NzSwitchComponent {
  25. static {
  26. const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
  27. _nzSize_decorators = [WithConfig()];
  28. __esDecorate(null, null, _nzSize_decorators, { kind: "field", name: "nzSize", static: false, private: false, access: { has: obj => "nzSize" in obj, get: obj => obj.nzSize, set: (obj, value) => { obj.nzSize = value; } }, metadata: _metadata }, _nzSize_initializers, _nzSize_extraInitializers);
  29. if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
  30. }
  31. nzConfigService;
  32. host;
  33. ngZone;
  34. cdr;
  35. focusMonitor;
  36. directionality;
  37. _nzModuleName = NZ_CONFIG_MODULE_NAME;
  38. isChecked = false;
  39. onChange = () => { };
  40. onTouched = () => { };
  41. switchElement;
  42. nzLoading = false;
  43. nzDisabled = false;
  44. nzControl = false;
  45. nzCheckedChildren = null;
  46. nzUnCheckedChildren = null;
  47. nzSize = __runInitializers(this, _nzSize_initializers, 'default');
  48. nzId = (__runInitializers(this, _nzSize_extraInitializers), null);
  49. dir = 'ltr';
  50. destroy$ = new Subject();
  51. isNzDisableFirstChange = true;
  52. updateValue(value) {
  53. if (this.isChecked !== value) {
  54. this.isChecked = value;
  55. this.onChange(this.isChecked);
  56. }
  57. }
  58. focus() {
  59. this.focusMonitor.focusVia(this.switchElement.nativeElement, 'keyboard');
  60. }
  61. blur() {
  62. this.switchElement.nativeElement.blur();
  63. }
  64. constructor(nzConfigService, host, ngZone, cdr, focusMonitor, directionality) {
  65. this.nzConfigService = nzConfigService;
  66. this.host = host;
  67. this.ngZone = ngZone;
  68. this.cdr = cdr;
  69. this.focusMonitor = focusMonitor;
  70. this.directionality = directionality;
  71. }
  72. ngOnInit() {
  73. this.directionality.change.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
  74. this.dir = direction;
  75. this.cdr.detectChanges();
  76. });
  77. this.dir = this.directionality.value;
  78. fromEventOutsideAngular(this.host.nativeElement, 'click')
  79. .pipe(takeUntil(this.destroy$))
  80. .subscribe(event => {
  81. event.preventDefault();
  82. if (this.nzControl || this.nzDisabled || this.nzLoading) {
  83. return;
  84. }
  85. this.ngZone.run(() => {
  86. this.updateValue(!this.isChecked);
  87. this.cdr.markForCheck();
  88. });
  89. });
  90. fromEventOutsideAngular(this.switchElement.nativeElement, 'keydown')
  91. .pipe(takeUntil(this.destroy$))
  92. .subscribe(event => {
  93. if (this.nzControl || this.nzDisabled || this.nzLoading) {
  94. return;
  95. }
  96. const { keyCode } = event;
  97. if (keyCode !== LEFT_ARROW && keyCode !== RIGHT_ARROW && keyCode !== SPACE && keyCode !== ENTER) {
  98. return;
  99. }
  100. event.preventDefault();
  101. this.ngZone.run(() => {
  102. if (keyCode === LEFT_ARROW) {
  103. this.updateValue(false);
  104. }
  105. else if (keyCode === RIGHT_ARROW) {
  106. this.updateValue(true);
  107. }
  108. else if (keyCode === SPACE || keyCode === ENTER) {
  109. this.updateValue(!this.isChecked);
  110. }
  111. this.cdr.markForCheck();
  112. });
  113. });
  114. }
  115. ngAfterViewInit() {
  116. this.focusMonitor
  117. .monitor(this.switchElement.nativeElement, true)
  118. .pipe(takeUntil(this.destroy$))
  119. .subscribe(focusOrigin => {
  120. if (!focusOrigin) {
  121. /** https://github.com/angular/angular/issues/17793 **/
  122. Promise.resolve().then(() => this.onTouched());
  123. }
  124. });
  125. }
  126. ngOnDestroy() {
  127. this.focusMonitor.stopMonitoring(this.switchElement.nativeElement);
  128. this.destroy$.next();
  129. this.destroy$.complete();
  130. }
  131. writeValue(value) {
  132. this.isChecked = value;
  133. this.cdr.markForCheck();
  134. }
  135. registerOnChange(fn) {
  136. this.onChange = fn;
  137. }
  138. registerOnTouched(fn) {
  139. this.onTouched = fn;
  140. }
  141. setDisabledState(disabled) {
  142. this.nzDisabled = (this.isNzDisableFirstChange && this.nzDisabled) || disabled;
  143. this.isNzDisableFirstChange = false;
  144. this.cdr.markForCheck();
  145. }
  146. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSwitchComponent, deps: [{ token: i1.NzConfigService }, { token: i0.ElementRef }, { token: i0.NgZone }, { token: i0.ChangeDetectorRef }, { token: i2.FocusMonitor }, { token: i3.Directionality }], target: i0.ɵɵFactoryTarget.Component });
  147. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzSwitchComponent, isStandalone: true, selector: "nz-switch", inputs: { nzLoading: ["nzLoading", "nzLoading", booleanAttribute], nzDisabled: ["nzDisabled", "nzDisabled", booleanAttribute], nzControl: ["nzControl", "nzControl", booleanAttribute], nzCheckedChildren: "nzCheckedChildren", nzUnCheckedChildren: "nzUnCheckedChildren", nzSize: "nzSize", nzId: "nzId" }, providers: [
  148. {
  149. provide: NG_VALUE_ACCESSOR,
  150. useExisting: forwardRef(() => NzSwitchComponent),
  151. multi: true
  152. }
  153. ], viewQueries: [{ propertyName: "switchElement", first: true, predicate: ["switchElement"], descendants: true, static: true }], exportAs: ["nzSwitch"], ngImport: i0, template: `
  154. <button
  155. nz-wave
  156. type="button"
  157. class="ant-switch"
  158. #switchElement
  159. [attr.id]="nzId"
  160. [disabled]="nzDisabled"
  161. [class.ant-switch-checked]="isChecked"
  162. [class.ant-switch-loading]="nzLoading"
  163. [class.ant-switch-disabled]="nzDisabled"
  164. [class.ant-switch-small]="nzSize === 'small'"
  165. [class.ant-switch-rtl]="dir === 'rtl'"
  166. [nzWaveExtraNode]="true"
  167. >
  168. <span class="ant-switch-handle">
  169. @if (nzLoading) {
  170. <nz-icon nzType="loading" class="ant-switch-loading-icon" />
  171. }
  172. </span>
  173. <span class="ant-switch-inner">
  174. @if (isChecked) {
  175. <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container>
  176. } @else {
  177. <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container>
  178. }
  179. </span>
  180. <div class="ant-click-animating-node"></div>
  181. </button>
  182. `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzWaveModule }, { kind: "directive", type: i4.NzWaveDirective, selector: "[nz-wave],button[nz-button]:not([nzType=\"link\"]):not([nzType=\"text\"])", inputs: ["nzWaveExtraNode"], exportAs: ["nzWave"] }, { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i5.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }, { kind: "ngmodule", type: NzOutletModule }, { kind: "directive", type: i6.NzStringTemplateOutletDirective, selector: "[nzStringTemplateOutlet]", inputs: ["nzStringTemplateOutletContext", "nzStringTemplateOutlet"], exportAs: ["nzStringTemplateOutlet"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  183. };
  184. })();
  185. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSwitchComponent, decorators: [{
  186. type: Component,
  187. args: [{
  188. selector: 'nz-switch',
  189. exportAs: 'nzSwitch',
  190. preserveWhitespaces: false,
  191. changeDetection: ChangeDetectionStrategy.OnPush,
  192. encapsulation: ViewEncapsulation.None,
  193. providers: [
  194. {
  195. provide: NG_VALUE_ACCESSOR,
  196. useExisting: forwardRef(() => NzSwitchComponent),
  197. multi: true
  198. }
  199. ],
  200. template: `
  201. <button
  202. nz-wave
  203. type="button"
  204. class="ant-switch"
  205. #switchElement
  206. [attr.id]="nzId"
  207. [disabled]="nzDisabled"
  208. [class.ant-switch-checked]="isChecked"
  209. [class.ant-switch-loading]="nzLoading"
  210. [class.ant-switch-disabled]="nzDisabled"
  211. [class.ant-switch-small]="nzSize === 'small'"
  212. [class.ant-switch-rtl]="dir === 'rtl'"
  213. [nzWaveExtraNode]="true"
  214. >
  215. <span class="ant-switch-handle">
  216. @if (nzLoading) {
  217. <nz-icon nzType="loading" class="ant-switch-loading-icon" />
  218. }
  219. </span>
  220. <span class="ant-switch-inner">
  221. @if (isChecked) {
  222. <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container>
  223. } @else {
  224. <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container>
  225. }
  226. </span>
  227. <div class="ant-click-animating-node"></div>
  228. </button>
  229. `,
  230. imports: [NzWaveModule, NzIconModule, NzOutletModule]
  231. }]
  232. }], ctorParameters: () => [{ type: i1.NzConfigService }, { type: i0.ElementRef }, { type: i0.NgZone }, { type: i0.ChangeDetectorRef }, { type: i2.FocusMonitor }, { type: i3.Directionality }], propDecorators: { switchElement: [{
  233. type: ViewChild,
  234. args: ['switchElement', { static: true }]
  235. }], nzLoading: [{
  236. type: Input,
  237. args: [{ transform: booleanAttribute }]
  238. }], nzDisabled: [{
  239. type: Input,
  240. args: [{ transform: booleanAttribute }]
  241. }], nzControl: [{
  242. type: Input,
  243. args: [{ transform: booleanAttribute }]
  244. }], nzCheckedChildren: [{
  245. type: Input
  246. }], nzUnCheckedChildren: [{
  247. type: Input
  248. }], nzSize: [{
  249. type: Input
  250. }], nzId: [{
  251. type: Input
  252. }] } });
  253. /**
  254. * Use of this source code is governed by an MIT-style license that can be
  255. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  256. */
  257. class NzSwitchModule {
  258. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSwitchModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  259. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzSwitchModule, imports: [NzSwitchComponent], exports: [NzSwitchComponent] });
  260. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSwitchModule, imports: [NzSwitchComponent] });
  261. }
  262. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSwitchModule, decorators: [{
  263. type: NgModule,
  264. args: [{
  265. imports: [NzSwitchComponent],
  266. exports: [NzSwitchComponent]
  267. }]
  268. }] });
  269. /**
  270. * Use of this source code is governed by an MIT-style license that can be
  271. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  272. */
  273. /**
  274. * Generated bundle index. Do not edit.
  275. */
  276. export { NzSwitchComponent, NzSwitchModule };
  277. //# sourceMappingURL=ng-zorro-antd-switch.mjs.map