ng-zorro-antd-message.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. import { ComponentPortal } from '@angular/cdk/portal';
  2. import * as i0 from '@angular/core';
  3. import { inject, ChangeDetectorRef, Directive, EventEmitter, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule, Injectable } from '@angular/core';
  4. import { Subject } from 'rxjs';
  5. import { filter, take, takeUntil } from 'rxjs/operators';
  6. import { NzConfigService } from 'ng-zorro-antd/core/config';
  7. import { NzSingletonService } from 'ng-zorro-antd/core/services';
  8. import { toCssPixel } from 'ng-zorro-antd/core/util';
  9. import { moveUpMotion } from 'ng-zorro-antd/core/animation';
  10. import * as i2 from 'ng-zorro-antd/core/outlet';
  11. import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
  12. import * as i1 from 'ng-zorro-antd/icon';
  13. import { NzIconModule } from 'ng-zorro-antd/icon';
  14. import * as i1$1 from '@angular/cdk/overlay';
  15. let globalCounter = 0;
  16. class NzMNService {
  17. overlay;
  18. injector;
  19. container;
  20. nzSingletonService = inject(NzSingletonService);
  21. constructor(overlay, injector) {
  22. this.overlay = overlay;
  23. this.injector = injector;
  24. }
  25. remove(id) {
  26. if (this.container) {
  27. if (id) {
  28. this.container.remove(id);
  29. }
  30. else {
  31. this.container.removeAll();
  32. }
  33. }
  34. }
  35. getInstanceId() {
  36. return `${this.componentPrefix}-${globalCounter++}`;
  37. }
  38. withContainer(ctor) {
  39. let containerInstance = this.nzSingletonService.getSingletonWithKey(this.componentPrefix);
  40. if (containerInstance) {
  41. return containerInstance;
  42. }
  43. const overlayRef = this.overlay.create({
  44. hasBackdrop: false,
  45. scrollStrategy: this.overlay.scrollStrategies.noop(),
  46. positionStrategy: this.overlay.position().global()
  47. });
  48. const componentPortal = new ComponentPortal(ctor, null, this.injector);
  49. const componentRef = overlayRef.attach(componentPortal);
  50. const overlayWrapper = overlayRef.hostElement;
  51. overlayWrapper.style.zIndex = '1010';
  52. if (!containerInstance) {
  53. this.container = containerInstance = componentRef.instance;
  54. this.nzSingletonService.registerSingletonWithKey(this.componentPrefix, containerInstance);
  55. this.container.afterAllInstancesRemoved.subscribe(() => {
  56. this.container = undefined;
  57. this.nzSingletonService.unregisterSingletonWithKey(this.componentPrefix);
  58. overlayRef.dispose();
  59. });
  60. }
  61. return containerInstance;
  62. }
  63. }
  64. class NzMNContainerComponent {
  65. config;
  66. instances = [];
  67. _afterAllInstancesRemoved = new Subject();
  68. afterAllInstancesRemoved = this._afterAllInstancesRemoved.asObservable();
  69. cdr = inject(ChangeDetectorRef);
  70. nzConfigService = inject(NzConfigService);
  71. destroy$ = new Subject();
  72. ngOnInit() {
  73. this.subscribeConfigChange();
  74. }
  75. ngOnDestroy() {
  76. this.destroy$.next();
  77. this.destroy$.complete();
  78. }
  79. create(data) {
  80. const instance = this.onCreate(data);
  81. if (this.instances.length >= this.config.nzMaxStack) {
  82. this.instances = this.instances.slice(1);
  83. }
  84. this.instances = [...this.instances, instance];
  85. this.readyInstances();
  86. return instance;
  87. }
  88. remove(id, userAction = false) {
  89. this.instances
  90. .map((instance, index) => ({ index, instance }))
  91. .filter(({ instance }) => instance.messageId === id)
  92. .forEach(({ index, instance }) => {
  93. this.instances.splice(index, 1);
  94. this.instances = [...this.instances];
  95. this.onRemove(instance, userAction);
  96. this.readyInstances();
  97. });
  98. if (!this.instances.length) {
  99. this.onAllInstancesRemoved();
  100. }
  101. }
  102. removeAll() {
  103. this.instances.forEach(i => this.onRemove(i, false));
  104. this.instances = [];
  105. this.readyInstances();
  106. this.onAllInstancesRemoved();
  107. }
  108. onCreate(instance) {
  109. instance.options = this.mergeOptions(instance.options);
  110. instance.onClose = new Subject();
  111. return instance;
  112. }
  113. onRemove(instance, userAction) {
  114. instance.onClose.next(userAction);
  115. instance.onClose.complete();
  116. }
  117. onAllInstancesRemoved() {
  118. this._afterAllInstancesRemoved.next();
  119. this._afterAllInstancesRemoved.complete();
  120. }
  121. readyInstances() {
  122. this.cdr.detectChanges();
  123. }
  124. mergeOptions(options) {
  125. const { nzDuration, nzAnimate, nzPauseOnHover } = this.config;
  126. return { nzDuration, nzAnimate, nzPauseOnHover, ...options };
  127. }
  128. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMNContainerComponent, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  129. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.2", type: NzMNContainerComponent, isStandalone: true, ngImport: i0 });
  130. }
  131. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMNContainerComponent, decorators: [{
  132. type: Directive
  133. }] });
  134. class NzMNComponent {
  135. cdr = inject(ChangeDetectorRef);
  136. animationStateChanged = new Subject();
  137. options;
  138. autoClose;
  139. closeTimer;
  140. userAction = false;
  141. eraseTimer;
  142. eraseTimingStart;
  143. eraseTTL;
  144. ngOnInit() {
  145. this.options = this.instance.options;
  146. if (this.options.nzAnimate) {
  147. this.instance.state = 'enter';
  148. this.animationStateChanged
  149. .pipe(filter(event => event.phaseName === 'done' && event.toState === 'leave'), take(1))
  150. .subscribe(() => {
  151. clearTimeout(this.closeTimer);
  152. this.destroyed.next({ id: this.instance.messageId, userAction: this.userAction });
  153. });
  154. }
  155. this.autoClose = this.options.nzDuration > 0;
  156. if (this.autoClose) {
  157. this.initErase();
  158. this.startEraseTimeout();
  159. }
  160. }
  161. ngOnDestroy() {
  162. if (this.autoClose) {
  163. this.clearEraseTimeout();
  164. }
  165. this.animationStateChanged.complete();
  166. }
  167. onEnter() {
  168. if (this.autoClose && this.options.nzPauseOnHover) {
  169. this.clearEraseTimeout();
  170. this.updateTTL();
  171. }
  172. }
  173. onLeave() {
  174. if (this.autoClose && this.options.nzPauseOnHover) {
  175. this.startEraseTimeout();
  176. }
  177. }
  178. destroy(userAction = false) {
  179. this.userAction = userAction;
  180. if (this.options.nzAnimate) {
  181. this.instance.state = 'leave';
  182. this.cdr.detectChanges();
  183. this.closeTimer = setTimeout(() => {
  184. this.closeTimer = undefined;
  185. this.destroyed.next({ id: this.instance.messageId, userAction });
  186. }, 200);
  187. }
  188. else {
  189. this.destroyed.next({ id: this.instance.messageId, userAction });
  190. }
  191. }
  192. initErase() {
  193. this.eraseTTL = this.options.nzDuration;
  194. this.eraseTimingStart = Date.now();
  195. }
  196. updateTTL() {
  197. if (this.autoClose) {
  198. this.eraseTTL -= Date.now() - this.eraseTimingStart;
  199. }
  200. }
  201. startEraseTimeout() {
  202. if (this.eraseTTL > 0) {
  203. this.clearEraseTimeout();
  204. this.eraseTimer = setTimeout(() => this.destroy(), this.eraseTTL);
  205. this.eraseTimingStart = Date.now();
  206. }
  207. else {
  208. this.destroy();
  209. }
  210. }
  211. clearEraseTimeout() {
  212. if (this.eraseTimer !== null) {
  213. clearTimeout(this.eraseTimer);
  214. this.eraseTimer = undefined;
  215. }
  216. }
  217. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMNComponent, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  218. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.2", type: NzMNComponent, isStandalone: true, ngImport: i0 });
  219. }
  220. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMNComponent, decorators: [{
  221. type: Directive
  222. }] });
  223. /**
  224. * Use of this source code is governed by an MIT-style license that can be
  225. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  226. */
  227. class NzMessageComponent extends NzMNComponent {
  228. instance;
  229. destroyed = new EventEmitter();
  230. index;
  231. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageComponent, deps: null, target: i0.ɵɵFactoryTarget.Component });
  232. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzMessageComponent, isStandalone: true, selector: "nz-message", inputs: { instance: "instance" }, outputs: { destroyed: "destroyed" }, exportAs: ["nzMessage"], usesInheritance: true, ngImport: i0, template: `
  233. <div
  234. class="ant-message-notice"
  235. [@moveUpMotion]="instance.state"
  236. (@moveUpMotion.done)="animationStateChanged.next($event)"
  237. (mouseenter)="onEnter()"
  238. (mouseleave)="onLeave()"
  239. >
  240. <div class="ant-message-notice-content">
  241. <div class="ant-message-custom-content" [class]="'ant-message-' + instance.type">
  242. @switch (instance.type) {
  243. @case ('success') {
  244. <nz-icon nzType="check-circle" />
  245. }
  246. @case ('info') {
  247. <nz-icon nzType="info-circle" />
  248. }
  249. @case ('warning') {
  250. <nz-icon nzType="exclamation-circle" />
  251. }
  252. @case ('error') {
  253. <nz-icon nzType="close-circle" />
  254. }
  255. @case ('loading') {
  256. <nz-icon nzType="loading" />
  257. }
  258. }
  259. <ng-container
  260. *nzStringTemplateOutlet="instance.content; context: { $implicit: this, data: instance.options?.nzData }"
  261. >
  262. <span [innerHTML]="instance.content"></span>
  263. </ng-container>
  264. </div>
  265. </div>
  266. </div>
  267. `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i1.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }, { kind: "ngmodule", type: NzOutletModule }, { kind: "directive", type: i2.NzStringTemplateOutletDirective, selector: "[nzStringTemplateOutlet]", inputs: ["nzStringTemplateOutletContext", "nzStringTemplateOutlet"], exportAs: ["nzStringTemplateOutlet"] }], animations: [moveUpMotion], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  268. }
  269. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageComponent, decorators: [{
  270. type: Component,
  271. args: [{
  272. changeDetection: ChangeDetectionStrategy.OnPush,
  273. encapsulation: ViewEncapsulation.None,
  274. selector: 'nz-message',
  275. exportAs: 'nzMessage',
  276. preserveWhitespaces: false,
  277. animations: [moveUpMotion],
  278. template: `
  279. <div
  280. class="ant-message-notice"
  281. [@moveUpMotion]="instance.state"
  282. (@moveUpMotion.done)="animationStateChanged.next($event)"
  283. (mouseenter)="onEnter()"
  284. (mouseleave)="onLeave()"
  285. >
  286. <div class="ant-message-notice-content">
  287. <div class="ant-message-custom-content" [class]="'ant-message-' + instance.type">
  288. @switch (instance.type) {
  289. @case ('success') {
  290. <nz-icon nzType="check-circle" />
  291. }
  292. @case ('info') {
  293. <nz-icon nzType="info-circle" />
  294. }
  295. @case ('warning') {
  296. <nz-icon nzType="exclamation-circle" />
  297. }
  298. @case ('error') {
  299. <nz-icon nzType="close-circle" />
  300. }
  301. @case ('loading') {
  302. <nz-icon nzType="loading" />
  303. }
  304. }
  305. <ng-container
  306. *nzStringTemplateOutlet="instance.content; context: { $implicit: this, data: instance.options?.nzData }"
  307. >
  308. <span [innerHTML]="instance.content"></span>
  309. </ng-container>
  310. </div>
  311. </div>
  312. </div>
  313. `,
  314. imports: [NzIconModule, NzOutletModule]
  315. }]
  316. }], propDecorators: { instance: [{
  317. type: Input
  318. }], destroyed: [{
  319. type: Output
  320. }] } });
  321. const NZ_CONFIG_COMPONENT_NAME = 'message';
  322. const NZ_MESSAGE_DEFAULT_CONFIG = {
  323. nzAnimate: true,
  324. nzDuration: 3000,
  325. nzMaxStack: 7,
  326. nzPauseOnHover: true,
  327. nzTop: 24,
  328. nzDirection: 'ltr'
  329. };
  330. class NzMessageContainerComponent extends NzMNContainerComponent {
  331. dir = this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME)?.nzDirection || 'ltr';
  332. top;
  333. constructor() {
  334. super();
  335. this.updateConfig();
  336. }
  337. subscribeConfigChange() {
  338. this.nzConfigService
  339. .getConfigChangeEventForComponent(NZ_CONFIG_COMPONENT_NAME)
  340. .pipe(takeUntil(this.destroy$))
  341. .subscribe(() => {
  342. this.updateConfig();
  343. this.dir = this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME)?.nzDirection || this.dir;
  344. });
  345. }
  346. updateConfig() {
  347. this.config = {
  348. ...NZ_MESSAGE_DEFAULT_CONFIG,
  349. ...this.config,
  350. ...this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME)
  351. };
  352. this.top = toCssPixel(this.config.nzTop);
  353. this.cdr.markForCheck();
  354. }
  355. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageContainerComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  356. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzMessageContainerComponent, isStandalone: true, selector: "nz-message-container", exportAs: ["nzMessageContainer"], usesInheritance: true, ngImport: i0, template: `
  357. <div class="ant-message" [class.ant-message-rtl]="dir === 'rtl'" [style.top]="top">
  358. @for (instance of instances; track instance) {
  359. <nz-message [instance]="instance" (destroyed)="remove($event.id, $event.userAction)"></nz-message>
  360. }
  361. </div>
  362. `, isInline: true, dependencies: [{ kind: "component", type: NzMessageComponent, selector: "nz-message", inputs: ["instance"], outputs: ["destroyed"], exportAs: ["nzMessage"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  363. }
  364. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageContainerComponent, decorators: [{
  365. type: Component,
  366. args: [{
  367. changeDetection: ChangeDetectionStrategy.OnPush,
  368. encapsulation: ViewEncapsulation.None,
  369. selector: 'nz-message-container',
  370. exportAs: 'nzMessageContainer',
  371. preserveWhitespaces: false,
  372. template: `
  373. <div class="ant-message" [class.ant-message-rtl]="dir === 'rtl'" [style.top]="top">
  374. @for (instance of instances; track instance) {
  375. <nz-message [instance]="instance" (destroyed)="remove($event.id, $event.userAction)"></nz-message>
  376. }
  377. </div>
  378. `,
  379. imports: [NzMessageComponent]
  380. }]
  381. }], ctorParameters: () => [] });
  382. /**
  383. * Use of this source code is governed by an MIT-style license that can be
  384. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  385. */
  386. /**
  387. * @deprecated This module is no longer needed, will be removed in v20, please remove its import.
  388. */
  389. class NzMessageModule {
  390. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  391. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzMessageModule, imports: [NzMessageContainerComponent, NzMessageComponent] });
  392. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageModule, imports: [NzMessageContainerComponent, NzMessageComponent] });
  393. }
  394. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageModule, decorators: [{
  395. type: NgModule,
  396. args: [{
  397. imports: [NzMessageContainerComponent, NzMessageComponent]
  398. }]
  399. }] });
  400. class NzMessageService extends NzMNService {
  401. componentPrefix = 'message-';
  402. constructor(overlay, injector) {
  403. super(overlay, injector);
  404. }
  405. success(content, options) {
  406. return this.createInstance({ type: 'success', content }, options);
  407. }
  408. error(content, options) {
  409. return this.createInstance({ type: 'error', content }, options);
  410. }
  411. info(content, options) {
  412. return this.createInstance({ type: 'info', content }, options);
  413. }
  414. warning(content, options) {
  415. return this.createInstance({ type: 'warning', content }, options);
  416. }
  417. loading(content, options) {
  418. return this.createInstance({ type: 'loading', content }, options);
  419. }
  420. create(type, content, options) {
  421. return this.createInstance({ type, content }, options);
  422. }
  423. createInstance(message, options) {
  424. this.container = this.withContainer(NzMessageContainerComponent);
  425. return this.container.create({
  426. ...message,
  427. ...{
  428. createdAt: new Date(),
  429. messageId: this.getInstanceId(),
  430. options
  431. }
  432. });
  433. }
  434. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageService, deps: [{ token: i1$1.Overlay }, { token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable });
  435. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageService, providedIn: 'root' });
  436. }
  437. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzMessageService, decorators: [{
  438. type: Injectable,
  439. args: [{
  440. providedIn: 'root'
  441. }]
  442. }], ctorParameters: () => [{ type: i1$1.Overlay }, { type: i0.Injector }] });
  443. /**
  444. * Use of this source code is governed by an MIT-style license that can be
  445. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  446. */
  447. /**
  448. * Use of this source code is governed by an MIT-style license that can be
  449. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  450. */
  451. /**
  452. * Generated bundle index. Do not edit.
  453. */
  454. export { NzMNComponent, NzMNContainerComponent, NzMNService, NzMessageComponent, NzMessageContainerComponent, NzMessageModule, NzMessageService };
  455. //# sourceMappingURL=ng-zorro-antd-message.mjs.map