forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(module:message,notification): refactor (NG-ZORRO#4723)
* feat(module:notification): support opening at different places close NG-ZORRO#4338 chore(module:message,notification): refactor * chore: test with cdr * chore: remove console
- Loading branch information
Wendell
authored
Mar 15, 2020
1 parent
efb5c4a
commit d2b93f1
Showing
29 changed files
with
428 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,5 +240,8 @@ | |
"@schematics/angular:directive": { | ||
"prefix": "app" | ||
} | ||
}, | ||
"cli": { | ||
"analytics": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* @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 { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; | ||
|
||
import { moveUpMotion } from 'ng-zorro-antd/core'; | ||
|
||
import { NzMessageContainerComponent } from './message-container.component'; | ||
import { NzMessageDataFilled, NzMessageDataOptions } from './typings'; | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
selector: 'nz-message', | ||
exportAs: 'nzMessage', | ||
preserveWhitespaces: false, | ||
animations: [moveUpMotion], | ||
template: ` | ||
<div class="ant-message-notice" [@moveUpMotion]="nzMessage.state" (mouseenter)="onEnter()" (mouseleave)="onLeave()"> | ||
<div class="ant-message-notice-content"> | ||
<div class="ant-message-custom-content" [ngClass]="'ant-message-' + nzMessage.type"> | ||
<ng-container [ngSwitch]="nzMessage.type"> | ||
<i *ngSwitchCase="'success'" nz-icon nzType="check-circle"></i> | ||
<i *ngSwitchCase="'info'" nz-icon nzType="info-circle"></i> | ||
<i *ngSwitchCase="'warning'" nz-icon nzType="exclamation-circle"></i> | ||
<i *ngSwitchCase="'error'" nz-icon nzType="close-circle"></i> | ||
<i *ngSwitchCase="'loading'" nz-icon nzType="loading"></i> | ||
</ng-container> | ||
<ng-container *nzStringTemplateOutlet="nzMessage.content"> | ||
<span [innerHTML]="nzMessage.content"></span> | ||
</ng-container> | ||
</div> | ||
</div> | ||
</div> | ||
` | ||
}) | ||
export class NzMessageComponent implements OnInit, OnDestroy { | ||
@Input() nzMessage: NzMessageDataFilled; | ||
@Input() nzIndex: number; | ||
|
||
protected options: Required<NzMessageDataOptions>; | ||
|
||
// Whether to set a timeout to destroy itself. | ||
private autoClose: boolean; | ||
|
||
private eraseTimer: number | null = null; | ||
private eraseTimingStart: number; | ||
private eraseTTL: number; // Time to live. | ||
|
||
constructor(private messageContainerComponent: NzMessageContainerComponent, protected cdr: ChangeDetectorRef) {} | ||
|
||
ngOnInit(): void { | ||
// `NzMessageContainer` does its job so all properties cannot be undefined. | ||
this.options = this.nzMessage.options as Required<NzMessageDataOptions>; | ||
|
||
if (this.options.nzAnimate) { | ||
this.nzMessage.state = 'enter'; | ||
} | ||
|
||
this.autoClose = this.options.nzDuration > 0; | ||
|
||
if (this.autoClose) { | ||
this.initErase(); | ||
this.startEraseTimeout(); | ||
} | ||
} | ||
|
||
ngOnDestroy(): void { | ||
if (this.autoClose) { | ||
this.clearEraseTimeout(); | ||
} | ||
} | ||
|
||
onEnter(): void { | ||
if (this.autoClose && this.options.nzPauseOnHover) { | ||
this.clearEraseTimeout(); | ||
this.updateTTL(); | ||
} | ||
} | ||
|
||
onLeave(): void { | ||
if (this.autoClose && this.options.nzPauseOnHover) { | ||
this.startEraseTimeout(); | ||
} | ||
} | ||
|
||
// Remove self | ||
protected destroy(userAction: boolean = false): void { | ||
if (this.options.nzAnimate) { | ||
this.nzMessage.state = 'leave'; | ||
this.cdr.detectChanges(); | ||
setTimeout(() => this.messageContainerComponent.removeMessage(this.nzMessage.messageId, userAction), 200); | ||
} else { | ||
this.messageContainerComponent.removeMessage(this.nzMessage.messageId, userAction); | ||
} | ||
} | ||
|
||
private initErase(): void { | ||
this.eraseTTL = this.options.nzDuration; | ||
this.eraseTimingStart = Date.now(); | ||
} | ||
|
||
private updateTTL(): void { | ||
if (this.autoClose) { | ||
this.eraseTTL -= Date.now() - this.eraseTimingStart; | ||
} | ||
} | ||
|
||
private startEraseTimeout(): void { | ||
if (this.eraseTTL > 0) { | ||
this.clearEraseTimeout(); | ||
this.eraseTimer = setTimeout(() => this.destroy(), this.eraseTTL); | ||
this.eraseTimingStart = Date.now(); | ||
} else { | ||
this.destroy(); | ||
} | ||
} | ||
|
||
private clearEraseTimeout(): void { | ||
if (this.eraseTimer !== null) { | ||
clearTimeout(this.eraseTimer); | ||
this.eraseTimer = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.