Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(module:empty): remove injection token #4465

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions components/empty/nz-embed-empty.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,29 @@ import {
ViewEncapsulation
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { Subscription } from 'rxjs';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { NZ_EMPTY_COMPONENT_NAME, NzEmptyCustomContent, NzEmptySize, simpleEmptyImage } from './nz-empty-config';
import { NzEmptyService } from './nz-empty.service';

function getEmptySize(componentName: string): NzEmptySize {
switch (componentName) {
case 'table':
case 'list':
return 'normal';
case 'select':
case 'tree-select':
case 'cascader':
case 'transfer':
return 'small';
default:
return '';
}
}

type NzEmptyContentType = 'component' | 'template' | 'string';

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
Expand All @@ -40,11 +58,12 @@ export class NzEmbedEmptyComponent implements OnChanges, OnInit, OnDestroy {
@Input() specificContent: NzEmptyCustomContent;

content?: NzEmptyCustomContent;
contentType: 'component' | 'template' | 'string' = 'string';
contentType: NzEmptyContentType = 'string';
contentPortal?: Portal<any>; // tslint:disable-line:no-any
defaultSvg = this.sanitizer.bypassSecurityTrustResourceUrl(simpleEmptyImage);
size: NzEmptySize = '';
subs_ = new Subscription();

private $destroy = new Subject<void>();

constructor(
public emptyService: NzEmptyService,
Expand All @@ -56,7 +75,7 @@ export class NzEmbedEmptyComponent implements OnChanges, OnInit, OnDestroy {

ngOnChanges(changes: SimpleChanges): void {
if (changes.nzComponentName) {
this.size = this.getEmptySize(changes.nzComponentName.currentValue);
this.size = getEmptySize(changes.nzComponentName.currentValue);
}

if (changes.specificContent && !changes.specificContent.isFirstChange()) {
Expand All @@ -66,31 +85,15 @@ export class NzEmbedEmptyComponent implements OnChanges, OnInit, OnDestroy {
}

ngOnInit(): void {
const userContent_ = this.emptyService.userDefaultContent$.subscribe(content => {
this.emptyService.userDefaultContent$.pipe(takeUntil(this.$destroy)).subscribe(content => {
this.content = this.specificContent || content;
this.renderEmpty();
});

this.subs_.add(userContent_);
}

ngOnDestroy(): void {
this.subs_.unsubscribe();
}

private getEmptySize(componentName: string): NzEmptySize {
switch (componentName) {
case 'table':
case 'list':
return 'normal';
case 'select':
case 'tree-select':
case 'cascader':
case 'transfer':
return 'small';
default:
return '';
}
this.$destroy.next();
this.$destroy.complete();
}

private renderEmpty(): void {
Expand Down
51 changes: 17 additions & 34 deletions components/empty/nz-empty.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,33 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { Inject, Injectable, Optional, TemplateRef, Type } from '@angular/core';
import { Injectable, TemplateRef, Type } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { startWith } from 'rxjs/operators';

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

import { NZ_DEFAULT_EMPTY_CONTENT, NzEmptyCustomContent } from './nz-empty-config';
import { NzEmptyCustomContent } from './nz-empty-config';

@Injectable({
providedIn: 'root'
})
// tslint:disable-next-line:no-any
export class NzEmptyService<T = any> {
export class NzEmptyService {
userDefaultContent$ = new BehaviorSubject<NzEmptyCustomContent | undefined>(undefined);

constructor(
private nzConfigService: NzConfigService,
@Inject(NZ_DEFAULT_EMPTY_CONTENT) @Optional() private legacyDefaultEmptyContent: Type<T>
) {
if (legacyDefaultEmptyContent) {
warnDeprecation(`'NZ_DEFAULT_EMPTY_CONTENT' is deprecated and would be removed in 9.0.0. Please migrate to 'NZ_CONFIG'.`);
}

const userDefaultEmptyContent = this.getUserDefaultEmptyContent();

if (userDefaultEmptyContent) {
this.userDefaultContent$.next(userDefaultEmptyContent);
}
constructor(public nzConfigService: NzConfigService) {
this.subscribeDefaultEmptyContentChange();
}

this.nzConfigService.getConfigChangeEventForComponent('empty').subscribe(() => {
private subscribeDefaultEmptyContentChange(): void {
this.nzConfigService.getConfigChangeEventForComponent('empty').pipe(startWith(true)).subscribe(() => {
this.userDefaultContent$.next(this.getUserDefaultEmptyContent());
});
}

setDefaultContent(content?: NzEmptyCustomContent): void {
warnDeprecation(`'setDefaultContent' is deprecated and would be removed in 9.0.0. Please migrate to 'NzConfigService'.`);
// tslint:disable-next-line:no-any
private getUserDefaultEmptyContent(): Type<any> | TemplateRef<string> | string | undefined {
const content = (this.nzConfigService.getConfigForComponent('empty') || {}).nzDefaultEmptyContent;

if (
typeof content === 'string' ||
Expand All @@ -49,20 +41,11 @@ export class NzEmptyService<T = any> {
content instanceof TemplateRef ||
content instanceof Type
) {
this.userDefaultContent$.next(content);
return content;
} else {
throw new Error(`${PREFIX} 'useDefaultContent' expect 'string', 'templateRef' or 'component' but get ${content}.`);
throw new Error(
`${PREFIX} default empty content expects a 'null', 'undefined', 'string', 'TemplateRef' or 'Component' but get ${content}.`
);
}
}

resetDefault(): void {
warnDeprecation(
`'resetDefault' is deprecated and would be removed in 9.0.0. Please migrate to 'NzConfigService' and provide an 'undefined'.`
);
this.userDefaultContent$.next(undefined);
}

private getUserDefaultEmptyContent(): Type<T> | TemplateRef<string> | string {
return (this.nzConfigService.getConfigForComponent('empty') || {}).nzDefaultEmptyContent || this.legacyDefaultEmptyContent;
}
}
33 changes: 14 additions & 19 deletions components/empty/nz-empty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { CommonModule } from '@angular/common';
import { Component, DebugElement, Inject, NgModule, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, TestBedStatic, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NZ_CONFIG } from 'ng-zorro-antd';

import { NzI18nService } from '../i18n';
import en_US from '../i18n/languages/en_US';
import { NzListModule } from '../list';

import { NzEmbedEmptyComponent } from './nz-embed-empty.component';
import { NZ_DEFAULT_EMPTY_CONTENT, NZ_EMPTY_COMPONENT_NAME } from './nz-empty-config';
import { NZ_EMPTY_COMPONENT_NAME } from './nz-empty-config';
import { NzEmptyComponent } from './nz-empty.component';
import { NzEmptyModule } from './nz-empty.module';
import { NzEmptyService } from './nz-empty.service';

describe('nz-empty', () => {
describe('NzEmpty', () => {
describe('basic', () => {
let testBed: TestBedStatic;
let fixture: ComponentFixture<NzEmptyTestBasicComponent>;
Expand Down Expand Up @@ -182,7 +183,7 @@ describe('nz-empty', () => {
it('should raise error when set a invalid default value', () => {
expect(() => {
// tslint:disable-next-line:no-any
testComponent.emptyService.setDefaultContent(false as any);
testComponent.emptyService.nzConfigService.set('empty', { nzDefaultEmptyContent: false as any });
fixture.detectChanges();
tick();
fixture.detectChanges();
Expand All @@ -200,7 +201,7 @@ describe('nz-empty', () => {
};

// String.
testComponent.emptyService.setDefaultContent('empty');
testComponent.emptyService.nzConfigService.set('empty', { nzDefaultEmptyContent: 'empty' });
refresh();
expect(embedComponent).toBeTruthy();
expect(emptyComponent).toBeFalsy();
Expand Down Expand Up @@ -241,16 +242,6 @@ describe('nz-empty', () => {
expect(imageEl.firstElementChild.getAttribute('alt')).toBe('empty');
expect(imageEl.firstElementChild.src).toContain('data:image/svg+xml');
}));

it('should raise error when set a invalid default value', () => {
expect(() => {
// tslint:disable-next-line:no-any
testComponent.emptyService.setDefaultContent(false as any);
fixture.detectChanges();
tick();
fixture.detectChanges();
}).toThrowError();
});
});

/**
Expand Down Expand Up @@ -318,18 +309,18 @@ export class NzEmptyTestBasicComponent {
`
})
export class NzEmptyTestServiceComponent {
@ViewChild('tpl', { static: false }) template: TemplateRef<void>;
@ViewChild('tpl', { static: false }) template: TemplateRef<string>;

noResult?: string | null;

constructor(public emptyService: NzEmptyService) {}

reset(): void {
this.emptyService.resetDefault();
this.emptyService.nzConfigService.set('empty', { nzDefaultEmptyContent: undefined });
}

changeToTemplate(): void {
this.emptyService.setDefaultContent(this.template);
this.emptyService.nzConfigService.set('empty', { nzDefaultEmptyContent: this.template });
}
}

Expand Down Expand Up @@ -359,8 +350,12 @@ export class NzEmptyTestServiceModule {}
exports: [NzEmptyTestServiceComponent, NzEmptyTestCustomComponent],
providers: [
{
provide: NZ_DEFAULT_EMPTY_CONTENT,
useValue: NzEmptyTestCustomComponent
provide: NZ_CONFIG,
useValue: {
empty: {
nzDefaultEmptyContent: NzEmptyTestCustomComponent
}
}
}
]
})
Expand Down