Skip to content

Commit

Permalink
fix(module:message): fix lazy load problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz committed Jul 15, 2019
1 parent c11ca9b commit 22e4073
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 79 deletions.
9 changes: 9 additions & 0 deletions components/core/message/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* 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
*/

export * from './public-api';
87 changes: 87 additions & 0 deletions components/core/message/nz-message-base.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* 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
*/

import { Overlay } from '@angular/cdk/overlay';
import { ApplicationRef, ComponentFactoryResolver, EmbeddedViewRef, Injector, Type } from '@angular/core';
import {
NzMessageConfig,
NzMessageContainerComponent,
NzMessageData,
NzMessageDataFilled,
NzMessageDataOptions
} from 'ng-zorro-antd';

let globalCounter = 0;
const containerMap = new Map<string, NzMessageContainerComponent>();

export class NzMessageBaseService<
ContainerClass extends NzMessageContainerComponent,
MessageData,
MessageConfig extends NzMessageConfig
> {
protected _container: ContainerClass;

constructor(
private overlay: Overlay,
private containerClass: Type<ContainerClass>,
private injector: Injector,
private cfr: ComponentFactoryResolver,
private appRef: ApplicationRef,
private name: string = ''
) {
this._container = this.createContainer();
containerMap.set(this.name, this._container);
}

remove(messageId?: string): void {
if (messageId) {
this._container.removeMessage(messageId);
} else {
this._container.removeMessageAll();
}
}

createMessage(message: MessageData, options?: NzMessageDataOptions): NzMessageDataFilled {
const resultMessage: NzMessageDataFilled = {
...(message as NzMessageData),
...{
createdAt: new Date(),
messageId: this._generateMessageId(),
options
}
};
this._container.createMessage(resultMessage);

return resultMessage;
}

config(config: MessageConfig): void {
this._container.setConfig(config);
}

protected _generateMessageId(): string {
return `${this.name}-${globalCounter++}`;
}

// Manually creating container for overlay to avoid multi-checking error, see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/391
// NOTE: we never clean up the container component and it's overlay resources, if we should, we need to do it by our own codes.
private createContainer(): ContainerClass {
if (containerMap.has(this.name)) {
return containerMap.get(this.name) as ContainerClass;
}
const factory = this.cfr.resolveComponentFactory(this.containerClass);
const componentRef = factory.create(this.injector); // Use root injector
componentRef.changeDetectorRef.detectChanges(); // Immediately change detection to avoid multi-checking error
this.appRef.attachView(componentRef.hostView); // Load view into app root
const overlayPane = this.overlay.create().overlayElement;
overlayPane.style.zIndex = '1010'; // Patching: assign the same zIndex of ant-message to it's parent overlay panel, to the ant-message's zindex work.
overlayPane.appendChild((componentRef.hostView as EmbeddedViewRef<{}>).rootNodes[0] as HTMLElement);

return componentRef.instance;
}
}
9 changes: 9 additions & 0 deletions components/core/message/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* 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
*/

export * from './nz-message-base.service';
1 change: 1 addition & 0 deletions components/core/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './dropdown/public-api';
export * from './logger/public-api';
export * from './responsive/public-api';
export * from './trans-button/public-api';
export * from './message/public-api';
3 changes: 2 additions & 1 deletion components/message/nz-message.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import { NzIconModule } from 'ng-zorro-antd/icon';
import { NZ_MESSAGE_DEFAULT_CONFIG_PROVIDER } from './nz-message-config';
import { NzMessageContainerComponent } from './nz-message-container.component';
import { NzMessageComponent } from './nz-message.component';
import { NzMessageServiceModule } from './nz-message.service.module';

@NgModule({
imports: [CommonModule, OverlayModule, NzIconModule, NzAddOnModule],
imports: [CommonModule, OverlayModule, NzIconModule, NzAddOnModule, NzMessageServiceModule],
declarations: [NzMessageContainerComponent, NzMessageComponent],
providers: [NZ_MESSAGE_DEFAULT_CONFIG_PROVIDER],
entryComponents: [NzMessageContainerComponent]
Expand Down
12 changes: 12 additions & 0 deletions components/message/nz-message.service.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* 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
*/

import { NgModule } from '@angular/core';

@NgModule()
export class NzMessageServiceModule {}
80 changes: 5 additions & 75 deletions components/message/nz-message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,17 @@
*/

import { Overlay } from '@angular/cdk/overlay';
import {
ApplicationRef,
ComponentFactoryResolver,
EmbeddedViewRef,
Injectable,
Injector,
TemplateRef,
Type
} from '@angular/core';
import { ApplicationRef, ComponentFactoryResolver, Injectable, Injector, TemplateRef } from '@angular/core';

import { NzMessageBaseService } from 'ng-zorro-antd/core';

import { NzMessageConfig } from './nz-message-config';
import { NzMessageContainerComponent } from './nz-message-container.component';
import { NzMessageData, NzMessageDataFilled, NzMessageDataOptions } from './nz-message.definitions';

let globalCounter = 0;

export class NzMessageBaseService<
ContainerClass extends NzMessageContainerComponent,
MessageData,
MessageConfig extends NzMessageConfig
> {
protected _container: ContainerClass;

constructor(
private overlay: Overlay,
private containerClass: Type<ContainerClass>,
private injector: Injector,
private cfr: ComponentFactoryResolver,
private appRef: ApplicationRef,
private _idPrefix: string = ''
) {
this._container = this.createContainer();
}

remove(messageId?: string): void {
if (messageId) {
this._container.removeMessage(messageId);
} else {
this._container.removeMessageAll();
}
}

createMessage(message: MessageData, options?: NzMessageDataOptions): NzMessageDataFilled {
const resultMessage: NzMessageDataFilled = {
...(message as NzMessageData),
...{
createdAt: new Date(),
messageId: this._generateMessageId(),
options
}
};
this._container.createMessage(resultMessage);

return resultMessage;
}

config(config: MessageConfig): void {
this._container.setConfig(config);
}

protected _generateMessageId(): string {
return this._idPrefix + globalCounter++;
}

// Manually creating container for overlay to avoid multi-checking error, see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/391
// NOTE: we never clean up the container component and it's overlay resources, if we should, we need to do it by our own codes.
private createContainer(): ContainerClass {
const factory = this.cfr.resolveComponentFactory(this.containerClass);
const componentRef = factory.create(this.injector); // Use root injector
componentRef.changeDetectorRef.detectChanges(); // Immediately change detection to avoid multi-checking error
this.appRef.attachView(componentRef.hostView); // Load view into app root
const overlayPane = this.overlay.create().overlayElement;
overlayPane.style.zIndex = '1010'; // Patching: assign the same zIndex of ant-message to it's parent overlay panel, to the ant-message's zindex work.
overlayPane.appendChild((componentRef.hostView as EmbeddedViewRef<{}>).rootNodes[0] as HTMLElement);

return componentRef.instance;
}
}
import { NzMessageServiceModule } from './nz-message.service.module';

@Injectable({
providedIn: 'root'
providedIn: NzMessageServiceModule
})
export class NzMessageService extends NzMessageBaseService<
NzMessageContainerComponent,
Expand Down
1 change: 1 addition & 0 deletions components/message/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

export * from './nz-message.service';
export * from './nz-message.service.module';
export * from './nz-message.module';
export * from './nz-message.component';
export * from './nz-message.definitions';
Expand Down
3 changes: 2 additions & 1 deletion components/notification/nz-notification.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import { NzIconModule } from 'ng-zorro-antd/icon';
import { NZ_NOTIFICATION_DEFAULT_CONFIG_PROVIDER } from './nz-notification-config';
import { NzNotificationContainerComponent } from './nz-notification-container.component';
import { NzNotificationComponent } from './nz-notification.component';
import { NzNotificationServiceModule } from './nz-notification.service.module';

@NgModule({
imports: [CommonModule, OverlayModule, NzIconModule],
imports: [CommonModule, OverlayModule, NzIconModule, NzNotificationServiceModule],
declarations: [NzNotificationComponent, NzNotificationContainerComponent],
providers: [NZ_NOTIFICATION_DEFAULT_CONFIG_PROVIDER],
entryComponents: [NzNotificationContainerComponent]
Expand Down
12 changes: 12 additions & 0 deletions components/notification/nz-notification.service.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* 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
*/

import { NgModule } from '@angular/core';

@NgModule()
export class NzNotificationServiceModule {}
5 changes: 3 additions & 2 deletions components/notification/nz-notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import { Overlay } from '@angular/cdk/overlay';
import { ApplicationRef, ComponentFactoryResolver, Injectable, Injector, TemplateRef } from '@angular/core';

import { NzMessageBaseService } from 'ng-zorro-antd/message';
import { NzMessageBaseService } from 'ng-zorro-antd/core';

import { NzNotificationConfig } from './nz-notification-config';
import { NzNotificationContainerComponent } from './nz-notification-container.component';
import { NzNotificationData, NzNotificationDataFilled, NzNotificationDataOptions } from './nz-notification.definitions';
import { NzNotificationServiceModule } from './nz-notification.service.module';

@Injectable({
providedIn: 'root'
providedIn: NzNotificationServiceModule
})
export class NzNotificationService extends NzMessageBaseService<
NzNotificationContainerComponent,
Expand Down
1 change: 1 addition & 0 deletions components/notification/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export * from './nz-notification.component';
export * from './nz-notification.module';
export * from './nz-notification.definitions';
export * from './nz-notification.service';
export * from './nz-notification.service.module';
export * from './nz-notification-container.component';

0 comments on commit 22e4073

Please sign in to comment.