Skip to content

Commit

Permalink
chore(module:message,notification): remove old injection tokens (NG-Z…
Browse files Browse the repository at this point in the history
…ORRO#4404)

* chore(module:message,notification): remove old injection tokens

* fix: lint
* fix: fix lint
* docs: fix global injection doc

BREAKING CHANGE:

- `NZ_MESSAGE_CONFIG` is removed. Please use `NzGlobalConfigService` instead.
- `NZ_NOTIFICATION_CONFIG` is removed. Please use `NzGlobalConfigService` instead.
- `config` method of `NzMessageService` and `NzNotificationService` is removed. Please use `set` method of `NzGlobalConfigService` instead.
  • Loading branch information
Wendell authored and hsuanxyz committed Jan 8, 2020
1 parent ebdd9c1 commit 173f75a
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 169 deletions.
17 changes: 2 additions & 15 deletions components/message/nz-message-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@

import { Overlay } from '@angular/cdk/overlay';
import { ApplicationRef, ComponentFactoryResolver, EmbeddedViewRef, Injector, Type } from '@angular/core';
import { NzSingletonService, warnDeprecation } from 'ng-zorro-antd/core';
import { NzSingletonService } from 'ng-zorro-antd/core';

import { NzMessageConfigLegacy } 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 NzMessageConfigLegacy
> {
export class NzMessageBaseService<ContainerClass extends NzMessageContainerComponent, MessageData> {
protected _container: ContainerClass;

constructor(
Expand Down Expand Up @@ -58,14 +53,6 @@ export class NzMessageBaseService<
return resultMessage;
}

config(config: MessageConfig): void {
warnDeprecation(
`'config' of 'NzMessageService' and 'NzNotificationService' is deprecated and will be removed in 9.0.0. Please use 'set' of 'NzConfigService' instead.`
);

this._container.setConfig(config);
}

protected _generateMessageId(): string {
return `${this.name}-${globalCounter++}`;
}
Expand Down
38 changes: 0 additions & 38 deletions components/message/nz-message-config.ts

This file was deleted.

47 changes: 21 additions & 26 deletions components/message/nz-message-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, OnInit, Optional, ViewEncapsulation } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Subject } from 'rxjs';

import { NzConfigService, toCssPixel, warnDeprecation } from 'ng-zorro-antd/core';

import { NZ_MESSAGE_CONFIG, NZ_MESSAGE_DEFAULT_CONFIG, NzMessageConfigLegacy } from './nz-message-config';
import { MessageConfig, NzConfigService, toCssPixel } from 'ng-zorro-antd/core';
import { NzMessageDataFilled, NzMessageDataOptions } from './nz-message.definitions';

const NZ_CONFIG_COMPONENT_NAME = 'message';
const NZ_MESSAGE_DEFAULT_CONFIG: Required<MessageConfig> = {
nzAnimate: true,
nzDuration: 3000,
nzMaxStack: 7,
nzPauseOnHover: true,
nzTop: 24
};

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
Expand All @@ -26,33 +31,17 @@ const NZ_CONFIG_COMPONENT_NAME = 'message';
})
export class NzMessageContainerComponent implements OnInit {
messages: NzMessageDataFilled[] = [];
config: Required<NzMessageConfigLegacy>;
config: Required<MessageConfig>;
top: string | null;

constructor(
protected cdr: ChangeDetectorRef,
protected nzConfigService: NzConfigService,
@Optional() @Inject(NZ_MESSAGE_DEFAULT_CONFIG) defaultConfig: NzMessageConfigLegacy,
@Optional() @Inject(NZ_MESSAGE_CONFIG) config: NzMessageConfigLegacy
) {
if (!!config) {
warnDeprecation(
`Injection token 'NZ_MESSAGE_CONFIG' is deprecated and will be removed in 9.0.0. Please use 'NzConfigService' instead.`
);
}
this.setConfig({ ...defaultConfig, ...config });
constructor(protected cdr: ChangeDetectorRef, protected nzConfigService: NzConfigService) {
this.updateConfig();
}

ngOnInit(): void {
this.subscribeConfigChange();
}

setConfig(config?: NzMessageConfigLegacy): void {
this.config = this.mergeMessageConfig(config);
this.top = toCssPixel(this.config.nzTop);
this.cdr.markForCheck();
}

/**
* Create a new message.
* @param message Parsed message configuration.
Expand Down Expand Up @@ -93,14 +82,20 @@ export class NzMessageContainerComponent implements OnInit {
this.cdr.detectChanges();
}

protected updateConfig(): void {
this.config = this.updateConfigFromConfigService();
this.top = toCssPixel(this.config.nzTop);
this.cdr.markForCheck();
}

protected subscribeConfigChange(): void {
this.nzConfigService.getConfigChangeEventForComponent(NZ_CONFIG_COMPONENT_NAME).subscribe(() => this.setConfig());
this.nzConfigService.getConfigChangeEventForComponent(NZ_CONFIG_COMPONENT_NAME).subscribe(() => this.updateConfig());
}

protected mergeMessageConfig(config?: NzMessageConfigLegacy): Required<NzMessageConfigLegacy> {
protected updateConfigFromConfigService(): Required<MessageConfig> {
return {
...NZ_MESSAGE_DEFAULT_CONFIG,
...this.config,
...config,
...this.nzConfigService.getConfigForComponent(NZ_CONFIG_COMPONENT_NAME)
};
}
Expand Down
2 changes: 0 additions & 2 deletions components/message/nz-message.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import { NgModule } from '@angular/core';
import { NzAddOnModule } from 'ng-zorro-antd/core';
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, NzMessageServiceModule],
declarations: [NzMessageContainerComponent, NzMessageComponent],
providers: [NZ_MESSAGE_DEFAULT_CONFIG_PROVIDER],
entryComponents: [NzMessageContainerComponent]
})
export class NzMessageModule {}
3 changes: 1 addition & 2 deletions components/message/nz-message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import { ApplicationRef, ComponentFactoryResolver, Injectable, Injector, Templat
import { NzSingletonService } from 'ng-zorro-antd/core';

import { NzMessageBaseService } from './nz-message-base.service';
import { NzMessageConfigLegacy } from './nz-message-config';
import { NzMessageContainerComponent } from './nz-message-container.component';
import { NzMessageData, NzMessageDataFilled, NzMessageDataOptions } from './nz-message.definitions';
import { NzMessageServiceModule } from './nz-message.service.module';

@Injectable({
providedIn: NzMessageServiceModule
})
export class NzMessageService extends NzMessageBaseService<NzMessageContainerComponent, NzMessageData, NzMessageConfigLegacy> {
export class NzMessageService extends NzMessageBaseService<NzMessageContainerComponent, NzMessageData> {
constructor(
nzSingletonService: NzSingletonService,
overlay: Overlay,
Expand Down
28 changes: 9 additions & 19 deletions components/message/nz-message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Component, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { dispatchMouseEvent, NzConfigService } from 'ng-zorro-antd/core';
import { dispatchMouseEvent, NZ_CONFIG, NzConfig, NzConfigService } from 'ng-zorro-antd/core';

import { NZ_MESSAGE_CONFIG } from './nz-message-config';
import { NzMessageModule } from './nz-message.module';
import { NzMessageService } from './nz-message.service';

Expand All @@ -17,26 +16,29 @@ describe('NzMessage', () => {
let nzConfigService: NzConfigService;

beforeEach(fakeAsync(() => {
const MESSAGE_CONFIG: NzConfig['message'] = {
nzMaxStack: 2,
nzTop: 24
};

TestBed.configureTestingModule({
imports: [NzMessageModule, NoopAnimationsModule],
declarations: [NzTestMessageBasicComponent],
providers: [{ provide: NZ_MESSAGE_CONFIG, useValue: { nzMaxStack: 2, nzTop: 24 } }]
providers: [{ provide: NZ_CONFIG, useValue: { message: MESSAGE_CONFIG } }]
});

TestBed.compileComponents();
}));

beforeEach(inject([NzMessageService, OverlayContainer], (m: NzMessageService, oc: OverlayContainer) => {
messageService = m;
// @ts-ignore
nzConfigService = messageService._container.nzConfigService;
if (!overlayContainerElement) {
overlayContainerElement = oc.getContainerElement();
}
}));

beforeEach(inject([NzConfigService], (c: NzConfigService) => {
nzConfigService = c;
}));

afterEach(() => {
messageService.remove();
});
Expand Down Expand Up @@ -161,17 +163,6 @@ describe('NzMessage', () => {
expect(overlayContainerElement.textContent).not.toContain('EXISTS');
}));

/**
* @deprecated This test is going to be removed in 9.0.0
*/
it('should reset default config dynamically', fakeAsync(() => {
messageService.config({ nzDuration: 0 });
messageService.create('loading', 'EXISTS');
fixture.detectChanges();
tick(10000);
expect(overlayContainerElement.textContent).toContain('EXISTS');
}));

it('should reset default config from config service', fakeAsync(() => {
nzConfigService.set('message', { nzDuration: 0 });
messageService.create('loading', 'EXISTS');
Expand All @@ -181,7 +172,6 @@ describe('NzMessage', () => {
}));

it('should emit event when message close', fakeAsync(() => {
messageService.config({ nzDuration: 2000 });
const closeSpy = jasmine.createSpy('message closed');
const msg = messageService.create('loading', 'CLOSE');
const messageId = msg.messageId;
Expand Down
1 change: 0 additions & 1 deletion components/message/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ export * from './nz-message.module';
export * from './nz-message.component';
export * from './nz-message.definitions';
export * from './nz-message-container.component';
export * from './nz-message-config';
5 changes: 3 additions & 2 deletions components/notification/demo/placement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from '@angular/core';
import { NzConfigService } from 'ng-zorro-antd/core';
import { NzNotificationService } from 'ng-zorro-antd/notification';

@Component({
Expand All @@ -21,7 +22,7 @@ export class NzDemoNotificationPlacementComponent {
}

createBasicNotification(): void {
this.notification.config({
this.configService.set('notification', {
nzPlacement: this.placement
});
this.notification.blank(
Expand All @@ -30,5 +31,5 @@ export class NzDemoNotificationPlacementComponent {
);
}

constructor(private notification: NzNotificationService) {}
constructor(private notification: NzNotificationService, private configService: NzConfigService) {}
}
Loading

0 comments on commit 173f75a

Please sign in to comment.