UNPKG

ng-zorro-antd

Version:

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

592 lines (591 loc) • 16.7 kB
import { createGlobalPositionStrategy, createNoopScrollStrategy, createOverlayRef } from "@angular/cdk/overlay"; import { ComponentPortal } from "@angular/cdk/portal"; import * as i0 from "@angular/core"; import { ChangeDetectorRef, Component, DestroyRef, Directive, ElementRef, EventEmitter, Injectable, Injector, Input, Output, ViewEncapsulation, inject, viewChild } from "@angular/core"; import { Subject } from "rxjs"; import { NzConfigService, onConfigChangeEventForComponent } from "ng-zorro-antd/core/config"; import { NzSingletonService } from "ng-zorro-antd/core/services"; import { toCssPixel } from "ng-zorro-antd/core/util"; import * as i2 from "ng-zorro-antd/core/outlet"; import { NzOutletModule } from "ng-zorro-antd/core/outlet"; import * as i1 from "ng-zorro-antd/icon"; import { NzIconModule } from "ng-zorro-antd/icon"; let globalCounter = 0; var NzMNService = class { container; nzSingletonService = inject(NzSingletonService); injector = inject(Injector); remove(id) { if (this.container) { if (id) this.container.remove(id); else this.container.removeAll(); } } getInstanceId() { return `${this.componentPrefix}-${globalCounter++}`; } withContainer(ctor) { let containerInstance = this.nzSingletonService.getSingletonWithKey(this.componentPrefix); if (containerInstance) return containerInstance; const overlayRef = createOverlayRef(this.injector, { hasBackdrop: false, scrollStrategy: createNoopScrollStrategy(), positionStrategy: createGlobalPositionStrategy(this.injector) }); const componentPortal = new ComponentPortal(ctor, null, this.injector); const componentRef = overlayRef.attach(componentPortal); const overlayWrapper = overlayRef.hostElement; overlayWrapper.style.zIndex = "1010"; if (!containerInstance) { this.container = containerInstance = componentRef.instance; this.nzSingletonService.registerSingletonWithKey(this.componentPrefix, containerInstance); this.container.afterAllInstancesRemoved.subscribe(() => { this.container = void 0; this.nzSingletonService.unregisterSingletonWithKey(this.componentPrefix); overlayRef.dispose(); }); } return containerInstance; } }; var NzMNContainerComponent = class NzMNContainerComponent { config; instances = []; _afterAllInstancesRemoved = new Subject(); afterAllInstancesRemoved = this._afterAllInstancesRemoved.asObservable(); cdr = inject(ChangeDetectorRef); nzConfigService = inject(NzConfigService); constructor() { this.subscribeConfigChange(); } create(data) { const instance = this.onCreate(data); if (this.instances.length >= this.config.nzMaxStack) this.instances = this.instances.slice(1); this.instances = [...this.instances, instance]; this.readyInstances(); return instance; } remove(id, userAction = false) { this.instances.map((instance, index) => ({ index, instance })).filter(({ instance }) => instance.messageId === id).forEach(({ index, instance }) => { this.instances.splice(index, 1); this.instances = [...this.instances]; this.onRemove(instance, userAction); this.readyInstances(); }); if (!this.instances.length) this.onAllInstancesRemoved(); } removeAll() { this.instances.forEach((i) => this.onRemove(i, false)); this.instances = []; this.readyInstances(); this.onAllInstancesRemoved(); } onCreate(instance) { instance.options = this.mergeOptions(instance.options); instance.onClose = new Subject(); return instance; } onRemove(instance, userAction) { instance.onClose.next(userAction); instance.onClose.complete(); } onAllInstancesRemoved() { this._afterAllInstancesRemoved.next(); this._afterAllInstancesRemoved.complete(); } readyInstances() { this.cdr.detectChanges(); } mergeOptions(options) { const { nzDuration, nzAnimate, nzPauseOnHover } = this.config; return { nzDuration, nzAnimate, nzPauseOnHover, ...options }; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMNContainerComponent, deps: [], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "22.2.2", type: NzMNContainerComponent, isStandalone: true, ngImport: i0 }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMNContainerComponent, decorators: [{ type: Directive }], ctorParameters: () => [] }); var NzMNComponent = class NzMNComponent { destroyRef = inject(DestroyRef); elementRef = inject(ElementRef); options; autoClose; userAction = false; eraseTimer; eraseTimingStart; eraseTTL; constructor() { this.destroyRef.onDestroy(() => { if (this.autoClose) this.clearEraseTimeout(); }); } ngOnInit() { this.options = this.instance.options; if (this.options.nzAnimate) this._startEnterAnimation(); this.autoClose = this.options.nzDuration > 0; if (this.autoClose) { this.initErase(); this.startEraseTimeout(); } } onEnter() { if (this.autoClose && this.options.nzPauseOnHover) { this.clearEraseTimeout(); this.updateTTL(); } } onLeave() { if (this.autoClose && this.options.nzPauseOnHover) this.startEraseTimeout(); } destroy(userAction = false) { this.userAction = userAction; if (this.options.nzAnimate) this._startLeaveAnimation(() => this.destroyed.next({ id: this.instance.messageId, userAction })); else this.destroyed.next({ id: this.instance.messageId, userAction }); } initErase() { this.eraseTTL = this.options.nzDuration; this.eraseTimingStart = Date.now(); } updateTTL() { if (this.autoClose) this.eraseTTL -= Date.now() - this.eraseTimingStart; } startEraseTimeout() { if (this.eraseTTL > 0) { this.clearEraseTimeout(); this.eraseTimer = setTimeout(() => this.destroy(), this.eraseTTL); this.eraseTimingStart = Date.now(); } else this.destroy(); } clearEraseTimeout() { if (this.eraseTimer !== null) { clearTimeout(this.eraseTimer); this.eraseTimer = void 0; } } _startEnterAnimation() { const element = this.animationElement().nativeElement; element.classList.add(this._animationClassMap.enter); const onAnimationEnd = (event) => { if (this.matchAnimationKeyframe(event, this._animationKeyframeMap.enter)) { element.removeEventListener("animationend", onAnimationEnd); element.classList.remove(this._animationClassMap.enter); } }; element.addEventListener("animationend", onAnimationEnd); } _startLeaveAnimation(callback) { const element = this.animationElement().nativeElement; element.classList.remove(this._animationClassMap.enter); element.classList.add(this._animationClassMap.leave); const onAnimationEnd = (event) => { if (this.matchAnimationKeyframe(event, this._animationKeyframeMap.leave)) { element.removeEventListener("animationend", onAnimationEnd); callback(); } }; element.addEventListener("animationend", onAnimationEnd); } matchAnimationKeyframe(event, animationName) { return typeof animationName === "string" ? event.animationName === animationName : animationName.includes(event.animationName); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMNComponent, deps: [], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "22.2.2", type: NzMNComponent, isStandalone: true, ngImport: i0 }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMNComponent, decorators: [{ type: Directive }], ctorParameters: () => [] }); var NzMessageComponent = class NzMessageComponent extends NzMNComponent { instance; destroyed = new EventEmitter(); index; animationElement = viewChild.required("animationElement", { ...ngDevMode ? { debugName: "animationElement" } : /* istanbul ignore next */ {}, read: ElementRef }); _animationKeyframeMap = { enter: "MessageMoveIn", leave: "MessageMoveOut" }; _animationClassMap = { enter: "ant-message-move-up-enter", leave: "ant-message-move-up-leave" }; static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzMessageComponent, isStandalone: true, selector: "nz-message", inputs: { instance: "instance" }, outputs: { destroyed: "destroyed" }, viewQueries: [{ propertyName: "animationElement", first: true, predicate: ["animationElement"], descendants: true, read: ElementRef, isSignal: true }], exportAs: ["nzMessage"], usesInheritance: true, ngImport: i0, template: ` <div #animationElement class="ant-message-notice" [class]="instance.options?.nzClass" [style]="instance.options?.nzStyle" (mouseenter)="onEnter()" (mouseleave)="onLeave()" > <div class="ant-message-notice-content"> <div class="ant-message-custom-content" [class]="'ant-message-' + instance.type"> @switch (instance.type) { @case ('success') { <nz-icon nzType="check-circle" /> } @case ('info') { <nz-icon nzType="info-circle" /> } @case ('warning') { <nz-icon nzType="exclamation-circle" /> } @case ('error') { <nz-icon nzType="close-circle" /> } @case ('loading') { <nz-icon nzType="loading" /> } } <ng-container *nzStringTemplateOutlet="instance.content; context: { $implicit: this, data: instance.options?.nzData }" > <span [innerHTML]="instance.content"></span> </ng-container> </div> </div> </div> `, isInline: true, dependencies: [ { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i1.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: [ "nzType", "nzTheme", "nzTwotoneColor", "nzSpin", "nzRotate", "nzIconfont", "aria-label" ], exportAs: ["nzIcon"] }, { kind: "ngmodule", type: NzOutletModule }, { kind: "directive", type: i2.NzStringTemplateOutletDirective, selector: "[nzStringTemplateOutlet]", inputs: ["nzStringTemplateOutletContext", "nzStringTemplateOutlet"], exportAs: ["nzStringTemplateOutlet"] } ], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageComponent, decorators: [{ type: Component, args: [{ selector: "nz-message", exportAs: "nzMessage", imports: [NzIconModule, NzOutletModule], template: ` <div #animationElement class="ant-message-notice" [class]="instance.options?.nzClass" [style]="instance.options?.nzStyle" (mouseenter)="onEnter()" (mouseleave)="onLeave()" > <div class="ant-message-notice-content"> <div class="ant-message-custom-content" [class]="'ant-message-' + instance.type"> @switch (instance.type) { @case ('success') { <nz-icon nzType="check-circle" /> } @case ('info') { <nz-icon nzType="info-circle" /> } @case ('warning') { <nz-icon nzType="exclamation-circle" /> } @case ('error') { <nz-icon nzType="close-circle" /> } @case ('loading') { <nz-icon nzType="loading" /> } } <ng-container *nzStringTemplateOutlet="instance.content; context: { $implicit: this, data: instance.options?.nzData }" > <span [innerHTML]="instance.content"></span> </ng-container> </div> </div> </div> `, encapsulation: ViewEncapsulation.None }] }], propDecorators: { instance: [{ type: Input }], destroyed: [{ type: Output }], animationElement: [{ type: i0.ViewChild, args: ["animationElement", { read: ElementRef, isSignal: true }] }] } }); const NZ_CONFIG_COMPONENT_NAME = "message"; const NZ_MESSAGE_DEFAULT_CONFIG = { nzAnimate: true, nzDuration: 3e3, nzMaxStack: 7, nzPauseOnHover: true, nzTop: 24, nzDirection: "ltr" }; var NzMessageContainerComponent = class NzMessageContainerComponent extends NzMNContainerComponent { dir = this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME)?.nzDirection || "ltr"; top; constructor() { super(); this.updateConfig(); } subscribeConfigChange() { onConfigChangeEventForComponent(NZ_CONFIG_COMPONENT_NAME, () => { this.updateConfig(); this.dir = this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME)?.nzDirection || this.dir; }); } updateConfig() { this.config = { ...NZ_MESSAGE_DEFAULT_CONFIG, ...this.config, ...this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME) }; this.top = toCssPixel(this.config.nzTop); this.cdr.markForCheck(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageContainerComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzMessageContainerComponent, isStandalone: true, selector: "nz-message-container", exportAs: ["nzMessageContainer"], usesInheritance: true, ngImport: i0, template: ` <div class="ant-message" [class.ant-message-rtl]="dir === 'rtl'" [style.top]="top"> @for (instance of instances; track instance) { <nz-message [instance]="instance" (destroyed)="remove($event.id, $event.userAction)" /> } </div> `, isInline: true, dependencies: [{ kind: "component", type: NzMessageComponent, selector: "nz-message", inputs: ["instance"], outputs: ["destroyed"], exportAs: ["nzMessage"] }], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageContainerComponent, decorators: [{ type: Component, args: [{ selector: "nz-message-container", exportAs: "nzMessageContainer", encapsulation: ViewEncapsulation.None, template: ` <div class="ant-message" [class.ant-message-rtl]="dir === 'rtl'" [style.top]="top"> @for (instance of instances; track instance) { <nz-message [instance]="instance" (destroyed)="remove($event.id, $event.userAction)" /> } </div> `, imports: [NzMessageComponent] }] }], ctorParameters: () => [] }); var NzMessageService = class NzMessageService extends NzMNService { componentPrefix = "message-"; success(content, options) { return this.createInstance({ type: "success", content }, options); } error(content, options) { return this.createInstance({ type: "error", content }, options); } info(content, options) { return this.createInstance({ type: "info", content }, options); } warning(content, options) { return this.createInstance({ type: "warning", content }, options); } loading(content, options) { return this.createInstance({ type: "loading", content }, options); } create(type, content, options) { return this.createInstance({ type, content }, options); } createInstance(message, options) { this.container = this.withContainer(NzMessageContainerComponent); return this.container.create({ ...message, createdAt: /* @__PURE__ */ new Date(), messageId: this.getInstanceId(), options }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageService, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzMessageService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }] }); export { NzMNComponent, NzMNContainerComponent, NzMNService, NzMessageComponent, NzMessageContainerComponent, NzMessageService }; //# sourceMappingURL=ng-zorro-antd-message.mjs.map