Skip to content

Commit

Permalink
refactor(module:core): support provide wave config with DI (#2594)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz authored Dec 11, 2018
1 parent fa9160c commit dfba49f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
11 changes: 7 additions & 4 deletions components/button/nz-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
ContentChildren,
ElementRef,
HostBinding,
Inject,
Input,
NgZone,
OnDestroy,
OnInit,
OnInit, Optional,
QueryList,
Renderer2,
ViewChild,
Expand All @@ -21,7 +22,7 @@ import { NzSizeLDSType } from '../core/types/size';
import { isEmpty } from '../core/util/check';
import { toBoolean } from '../core/util/convert';
import { findFirstNotEmptyNode, findLastNotEmptyNode } from '../core/util/dom';
import { NzWaveDirective } from '../core/wave/nz-wave.directive';
import { NzWaveConfig, NzWaveDirective, NZ_WAVE_GLOBAL_CONFIG } from '../core/wave/nz-wave.directive';
import { NzIconDirective } from '../icon/nz-icon.directive';

export type NzButtonType = 'primary' | 'dashed' | 'danger';
Expand All @@ -39,7 +40,7 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy {
readonly el: HTMLElement = this.elementRef.nativeElement;
@ViewChild('contentElement') contentElement: ElementRef;
@ContentChildren(NzIconDirective, { read: ElementRef }) listOfIconElement: QueryList<ElementRef>;
@HostBinding('attr.nz-wave') nzWave = new NzWaveDirective(this.ngZone, this.elementRef);
@HostBinding('attr.nz-wave') nzWave = new NzWaveDirective(this.ngZone, this.elementRef, this.waveConfig);

@Input()
set nzBlock(value: boolean) {
Expand Down Expand Up @@ -112,7 +113,9 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy {
return this._loading;
}

constructor(private elementRef: ElementRef, private cdr: ChangeDetectorRef, private renderer: Renderer2, private nzUpdateHostClassService: NzUpdateHostClassService, private ngZone: NgZone) {
constructor(private elementRef: ElementRef, private cdr: ChangeDetectorRef, private renderer: Renderer2,
private nzUpdateHostClassService: NzUpdateHostClassService, private ngZone: NgZone,
@Optional() @Inject(NZ_WAVE_GLOBAL_CONFIG) private waveConfig: NzWaveConfig) {
}

private _ghost = false;
Expand Down
2 changes: 1 addition & 1 deletion components/core/wave/nz-wave-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NgZone } from '@angular/core';

export class NzWaveRenderer {

readonly waveTransitionDuration = 400;
private waveTransitionDuration = 400;
private styleForPseudo: HTMLStyleElement | null;
private extraNode: HTMLDivElement | null;
private lastTime = 0;
Expand Down
46 changes: 41 additions & 5 deletions components/core/wave/nz-wave.directive.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
import { Directive, ElementRef, Input, NgZone, OnDestroy, OnInit } from '@angular/core';
import {
Directive,
ElementRef,
Inject,
InjectionToken,
Input,
NgZone,
OnDestroy,
OnInit,
Optional
} from '@angular/core';
import { NzWaveRenderer } from './nz-wave-renderer';

export interface NzWaveConfig {
disabled?: boolean;
}

export const NZ_WAVE_GLOBAL_DEFAULT_CONFIG: NzWaveConfig = {
disabled: false
};

export const NZ_WAVE_GLOBAL_CONFIG = new InjectionToken<NzWaveConfig>('nz-wave-global-options', {
providedIn: 'root',
factory: NZ_WAVE_GLOBAL_CONFIG_FACTORY
});

export function NZ_WAVE_GLOBAL_CONFIG_FACTORY(): NzWaveConfig {
return NZ_WAVE_GLOBAL_DEFAULT_CONFIG;
}

@Directive({
selector: '[nz-wave]'
})
export class NzWaveDirective implements OnInit, OnDestroy {
@Input() nzWaveExtraNode = false;

private waveRenderer: NzWaveRenderer;
private waveDisabled: boolean = false;

@Input() nzWaveExtraNode = false;

constructor(private ngZone: NgZone, private elementRef: ElementRef) {
constructor(private ngZone: NgZone,
private elementRef: ElementRef,
@Optional() @Inject(NZ_WAVE_GLOBAL_CONFIG) config: NzWaveConfig) {
if (config && typeof config.disabled === 'boolean') {
this.waveDisabled = config.disabled;
}
}

ngOnDestroy(): void {
Expand All @@ -20,7 +52,11 @@ export class NzWaveDirective implements OnInit, OnDestroy {
}

ngOnInit(): void {
if (this.elementRef.nativeElement) {
this.renderWaveIfEnabled();
}

renderWaveIfEnabled(): void {
if (!this.waveDisabled && this.elementRef.nativeElement) {
this.waveRenderer = new NzWaveRenderer(this.elementRef.nativeElement, this.ngZone, this.nzWaveExtraNode);
}
}
Expand Down

0 comments on commit dfba49f

Please sign in to comment.