UNPKG

ng-zorro-antd

Version:

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

385 lines (384 loc) • 11.9 kB
import { __esDecorate, __runInitializers } from "tslib"; import { Directionality } from "@angular/cdk/bidi"; import * as i0 from "@angular/core"; import { ChangeDetectorRef, Component, ContentChild, DestroyRef, ElementRef, Input, NgModule, Renderer2, ViewEncapsulation, afterEveryRender, booleanAttribute, computed, contentChild, inject, signal, viewChild } from "@angular/core"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { Subject } from "rxjs"; import { filter, startWith } from "rxjs/operators"; import { WithConfig, onConfigChangeEventForComponent } from "ng-zorro-antd/core/config"; import { NZ_FORM_SIZE } from "ng-zorro-antd/core/form"; import { fromEventOutsideAngular } from "ng-zorro-antd/core/util"; import * as i2 from "ng-zorro-antd/icon"; import { NzIconDirective, NzIconModule } from "ng-zorro-antd/icon"; import * as i1 from "ng-zorro-antd/space"; import { NZ_SPACE_COMPACT_ITEM_TYPE, NZ_SPACE_COMPACT_SIZE, NzSpaceCompactItemDirective } from "ng-zorro-antd/space"; import { ɵNzTransitionPatchModule } from "ng-zorro-antd/core/transition-patch"; import { NzWaveModule } from "ng-zorro-antd/core/wave"; const NZ_CONFIG_MODULE_NAME = "button"; let NzButtonComponent = (() => { let _nzSize_decorators; let _nzSize_initializers = []; let _nzSize_extraInitializers = []; return class NzButtonComponent { static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; _nzSize_decorators = [WithConfig()]; __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); if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); } elementRef = inject(ElementRef); cdr = inject(ChangeDetectorRef); renderer = inject(Renderer2); destroyRef = inject(DestroyRef); dir = inject(Directionality).valueSignal; _nzModuleName = NZ_CONFIG_MODULE_NAME; nzIconDirectiveElement; nzBlock = false; nzGhost = false; nzLoading = false; nzDanger = false; disabled = false; tabIndex = null; nzType = null; nzShape = null; nzSize = __runInitializers(this, _nzSize_initializers, "default"); elementOnly = (__runInitializers(this, _nzSize_extraInitializers), signal(false, ...ngDevMode ? [{ debugName: "elementOnly" }] : /* istanbul ignore next */ [])); size = signal(this.nzSize, ...ngDevMode ? [{ debugName: "size" }] : /* istanbul ignore next */ []); loading$ = new Subject(); formSize = inject(NZ_FORM_SIZE, { optional: true }); compactSize = inject(NZ_SPACE_COMPACT_SIZE, { optional: true }); finalSize = computed(() => { if (this.formSize?.()) return this.formSize(); if (this.compactSize) return this.compactSize(); return this.size(); }, ...ngDevMode ? [{ debugName: "finalSize" }] : /* istanbul ignore next */ []); iconDir = contentChild(NzIconDirective, ...ngDevMode ? [{ debugName: "iconDir" }] : /* istanbul ignore next */ []); loadingIconDir = viewChild(NzIconDirective, ...ngDevMode ? [{ debugName: "loadingIconDir" }] : /* istanbul ignore next */ []); iconOnly = computed(() => this.elementOnly() && (!!this.iconDir() || !!this.loadingIconDir()), ...ngDevMode ? [{ debugName: "iconOnly" }] : /* istanbul ignore next */ []); constructor() { onConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME, () => { this.size.set(this.nzSize); this.cdr.markForCheck(); }); afterEveryRender({ read: () => { const { children } = this.elementRef.nativeElement; const visibleElement = Array.from(children).filter((element) => element.style.display !== "none"); this.elementOnly.set(visibleElement.length === 1); } }); } ngOnInit() { this.size.set(this.nzSize); fromEventOutsideAngular(this.elementRef.nativeElement, "click", { capture: true }).pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => { if (this.disabled && event.target?.tagName === "A" || this.nzLoading) { event.preventDefault(); event.stopImmediatePropagation(); } }); } ngOnChanges({ nzLoading, nzSize }) { if (nzLoading) this.loading$.next(this.nzLoading); if (nzSize) this.size.set(nzSize.currentValue); } ngAfterViewInit() { this.insertSpan(); } ngAfterContentInit() { this.loading$.pipe(startWith(this.nzLoading), filter(() => !!this.nzIconDirectiveElement), takeUntilDestroyed(this.destroyRef)).subscribe((loading) => { const nativeElement = this.nzIconDirectiveElement.nativeElement; if (loading) this.renderer.setStyle(nativeElement, "display", "none"); else this.renderer.removeStyle(nativeElement, "display"); }); } insertSpan() { this.elementRef.nativeElement.childNodes.forEach((node) => { if (node.nodeType === Node.TEXT_NODE && node.textContent.trim().length > 0) { const span = this.renderer.createElement("span"); const parent = this.renderer.parentNode(node); this.renderer.insertBefore(parent, span, node); this.renderer.appendChild(span, node); } }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzButtonComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzButtonComponent, isStandalone: true, selector: "button[nz-button], a[nz-button]", inputs: { nzBlock: [ "nzBlock", "nzBlock", booleanAttribute ], nzGhost: [ "nzGhost", "nzGhost", booleanAttribute ], nzLoading: [ "nzLoading", "nzLoading", booleanAttribute ], nzDanger: [ "nzDanger", "nzDanger", booleanAttribute ], disabled: [ "disabled", "disabled", booleanAttribute ], tabIndex: "tabIndex", nzType: "nzType", nzShape: "nzShape", nzSize: "nzSize" }, host: { properties: { "class.ant-btn-default": "nzType === 'default'", "class.ant-btn-primary": "nzType === 'primary'", "class.ant-btn-dashed": "nzType === 'dashed'", "class.ant-btn-link": "nzType === 'link'", "class.ant-btn-text": "nzType === 'text'", "class.ant-btn-circle": "nzShape === 'circle'", "class.ant-btn-round": "nzShape === 'round'", "class.ant-btn-lg": "finalSize() === 'large'", "class.ant-btn-sm": "finalSize() === 'small'", "class.ant-btn-dangerous": "nzDanger", "class.ant-btn-loading": "nzLoading", "class.ant-btn-background-ghost": "nzGhost", "class.ant-btn-block": "nzBlock", "class.ant-btn-rtl": "dir() === 'rtl'", "class.ant-btn-icon-only": "iconOnly()", "attr.tabindex": "disabled ? -1 : (tabIndex === null ? null : tabIndex)", "attr.disabled": "disabled || null" }, classAttribute: "ant-btn" }, providers: [{ provide: NZ_SPACE_COMPACT_ITEM_TYPE, useValue: "btn" }], queries: [{ propertyName: "iconDir", first: true, predicate: NzIconDirective, descendants: true, isSignal: true }, { propertyName: "nzIconDirectiveElement", first: true, predicate: NzIconDirective, descendants: true, read: ElementRef }], viewQueries: [{ propertyName: "loadingIconDir", first: true, predicate: NzIconDirective, descendants: true, isSignal: true }], exportAs: ["nzButton"], usesOnChanges: true, hostDirectives: [{ directive: i1.NzSpaceCompactItemDirective }], ngImport: i0, template: ` @if (nzLoading) { <span class="ant-btn-icon ant-btn-loading-icon"> <nz-icon nzType="loading" /> </span> } <ng-content /> `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i2.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: [ "nzType", "nzTheme", "nzTwotoneColor", "nzSpin", "nzRotate", "nzIconfont", "aria-label" ], exportAs: ["nzIcon"] }], encapsulation: i0.ViewEncapsulation.None }); }; })(); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzButtonComponent, decorators: [{ type: Component, args: [{ selector: "button[nz-button], a[nz-button]", exportAs: "nzButton", imports: [NzIconModule], encapsulation: ViewEncapsulation.None, template: ` @if (nzLoading) { <span class="ant-btn-icon ant-btn-loading-icon"> <nz-icon nzType="loading" /> </span> } <ng-content /> `, host: { class: "ant-btn", "[class.ant-btn-default]": `nzType === 'default'`, "[class.ant-btn-primary]": `nzType === 'primary'`, "[class.ant-btn-dashed]": `nzType === 'dashed'`, "[class.ant-btn-link]": `nzType === 'link'`, "[class.ant-btn-text]": `nzType === 'text'`, "[class.ant-btn-circle]": `nzShape === 'circle'`, "[class.ant-btn-round]": `nzShape === 'round'`, "[class.ant-btn-lg]": `finalSize() === 'large'`, "[class.ant-btn-sm]": `finalSize() === 'small'`, "[class.ant-btn-dangerous]": `nzDanger`, "[class.ant-btn-loading]": `nzLoading`, "[class.ant-btn-background-ghost]": `nzGhost`, "[class.ant-btn-block]": `nzBlock`, "[class.ant-btn-rtl]": `dir() === 'rtl'`, "[class.ant-btn-icon-only]": `iconOnly()`, "[attr.tabindex]": "disabled ? -1 : (tabIndex === null ? null : tabIndex)", "[attr.disabled]": "disabled || null" }, hostDirectives: [NzSpaceCompactItemDirective], providers: [{ provide: NZ_SPACE_COMPACT_ITEM_TYPE, useValue: "btn" }] }] }], ctorParameters: () => [], propDecorators: { nzIconDirectiveElement: [{ type: ContentChild, args: [NzIconDirective, { read: ElementRef }] }], nzBlock: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzGhost: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzLoading: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzDanger: [{ type: Input, args: [{ transform: booleanAttribute }] }], disabled: [{ type: Input, args: [{ transform: booleanAttribute }] }], tabIndex: [{ type: Input }], nzType: [{ type: Input }], nzShape: [{ type: Input }], nzSize: [{ type: Input }], iconDir: [{ type: i0.ContentChild, args: [i0.forwardRef(() => NzIconDirective), { isSignal: true }] }], loadingIconDir: [{ type: i0.ViewChild, args: [i0.forwardRef(() => NzIconDirective), { isSignal: true }] }] } }); var NzButtonModule = class NzButtonModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzButtonModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.2.2", ngImport: i0, type: NzButtonModule, imports: [NzButtonComponent], exports: [ NzButtonComponent, ɵNzTransitionPatchModule, NzWaveModule ] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzButtonModule, imports: [ NzButtonComponent, ɵNzTransitionPatchModule, NzWaveModule ] }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzButtonModule, decorators: [{ type: NgModule, args: [{ imports: [NzButtonComponent], exports: [ NzButtonComponent, ɵNzTransitionPatchModule, NzWaveModule ] }] }] }); export { NzButtonComponent, NzButtonModule }; //# sourceMappingURL=ng-zorro-antd-button.mjs.map