forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.component.ts
124 lines (107 loc) · 2.82 KB
/
countdown.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* 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 { Directionality } from '@angular/cdk/bidi';
import { Platform } from '@angular/cdk/platform';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Optional,
Output,
SimpleChanges,
ViewEncapsulation
} from '@angular/core';
import { interval, Subscription } from 'rxjs';
import { NzStatisticComponent } from './statistic.component';
const REFRESH_INTERVAL = 1000 / 30;
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
selector: 'nz-countdown',
exportAs: 'nzCountdown',
template: `
<nz-statistic
[nzValue]="diff"
[nzValueStyle]="nzValueStyle"
[nzValueTemplate]="nzValueTemplate || countDownTpl"
[nzTitle]="nzTitle"
[nzPrefix]="nzPrefix"
[nzSuffix]="nzSuffix"
></nz-statistic>
<ng-template #countDownTpl>{{ diff | nzTimeRange: nzFormat }}</ng-template>
`
})
export class NzCountdownComponent extends NzStatisticComponent implements OnInit, OnChanges, OnDestroy {
@Input() nzFormat: string = 'HH:mm:ss';
@Output() readonly nzCountdownFinish = new EventEmitter<void>();
diff!: number;
private target: number = 0;
private updater_?: Subscription | null;
constructor(
cdr: ChangeDetectorRef,
private ngZone: NgZone,
private platform: Platform,
@Optional() directionality: Directionality
) {
super(cdr, directionality);
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.nzValue) {
this.target = Number(changes.nzValue.currentValue);
if (!changes.nzValue.isFirstChange()) {
this.syncTimer();
}
}
}
ngOnInit(): void {
super.ngOnInit();
this.syncTimer();
}
ngOnDestroy(): void {
this.stopTimer();
}
syncTimer(): void {
if (this.target >= Date.now()) {
this.startTimer();
} else {
this.stopTimer();
}
}
startTimer(): void {
if (this.platform.isBrowser) {
this.ngZone.runOutsideAngular(() => {
this.stopTimer();
this.updater_ = interval(REFRESH_INTERVAL).subscribe(() => {
this.updateValue();
this.cdr.detectChanges();
});
});
}
}
stopTimer(): void {
if (this.updater_) {
this.updater_.unsubscribe();
this.updater_ = null;
}
}
/**
* Update time that should be displayed on the screen.
*/
protected updateValue(): void {
this.diff = Math.max(this.target - Date.now(), 0);
if (this.diff === 0) {
this.stopTimer();
if (this.nzCountdownFinish.observers.length) {
this.ngZone.run(() => this.nzCountdownFinish.emit());
}
}
}
}