/
toast-noanimation.component.ts
239 lines (227 loc) Β· 6.51 KB
/
toast-noanimation.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { CommonModule } from '@angular/common';
import { ModuleWithProviders } from '@angular/compiler/src/core';
import {
ApplicationRef,
Component,
HostBinding,
HostListener,
NgModule,
OnDestroy,
} from '@angular/core';
import { SafeHtml } from '@angular/platform-browser';
import { Subscription } from 'rxjs';
import {
DefaultNoComponentGlobalConfig,
GlobalConfig,
IndividualConfig,
ToastPackage,
TOAST_CONFIG,
} from './toastr-config';
import { ToastrService } from './toastr.service';
@Component({
selector: '[toast-component]',
template: `
<button *ngIf="options.closeButton" (click)="remove()" class="toast-close-button" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div *ngIf="title" [class]="options.titleClass" [attr.aria-label]="title">
{{ title }}
</div>
<div *ngIf="message && options.enableHtml" role="alert" aria-live="polite"
[class]="options.messageClass" [innerHTML]="message">
</div>
<div *ngIf="message && !options.enableHtml" role="alert" aria-live="polite"
[class]="options.messageClass" [attr.aria-label]="message">
{{ message }}
</div>
<div *ngIf="options.progressBar">
<div class="toast-progress" [style.width]="width + '%'"></div>
</div>
`,
})
export class ToastNoAnimation implements OnDestroy {
message?: string | SafeHtml | null;
title?: string;
options: IndividualConfig;
originalTimeout: number;
/** width of progress bar */
width = -1;
/** a combination of toast type and options.toastClass */
@HostBinding('class') toastClasses = '';
/** hides component when waiting to be displayed */
@HostBinding('style.display')
get displayStyle() {
if (this.state === 'inactive') {
return 'none';
}
return 'inherit';
}
/** controls animation */
state = 'inactive';
private timeout: any;
private intervalId: any;
private hideTime: number;
private sub: Subscription;
private sub1: Subscription;
private sub2: Subscription;
constructor(
protected toastrService: ToastrService,
public toastPackage: ToastPackage,
protected appRef: ApplicationRef,
) {
this.message = toastPackage.message;
this.title = toastPackage.title;
this.options = toastPackage.config;
this.originalTimeout = toastPackage.config.timeOut;
this.toastClasses = `${toastPackage.toastType} ${
toastPackage.config.toastClass
}`;
this.sub = toastPackage.toastRef.afterActivate().subscribe(() => {
this.activateToast();
});
this.sub1 = toastPackage.toastRef.manualClosed().subscribe(() => {
this.remove();
});
this.sub2 = toastPackage.toastRef.timeoutReset().subscribe(() => {
this.resetTimeout();
});
}
ngOnDestroy() {
this.sub.unsubscribe();
this.sub1.unsubscribe();
this.sub2.unsubscribe();
clearInterval(this.intervalId);
clearTimeout(this.timeout);
}
/**
* activates toast and sets timeout
*/
activateToast() {
this.state = 'active';
if (!this.options.disableTimeOut && this.options.timeOut) {
this.timeout = setTimeout(() => {
this.remove();
}, this.options.timeOut);
this.hideTime = new Date().getTime() + this.options.timeOut;
if (this.options.progressBar) {
this.intervalId = setInterval(() => this.updateProgress(), 10);
}
}
if (this.options.onActivateTick) {
this.appRef.tick();
}
}
/**
* updates progress bar width
*/
updateProgress() {
if (this.width === 0 || this.width === 100 || !this.options.timeOut) {
return;
}
const now = new Date().getTime();
const remaining = this.hideTime - now;
this.width = (remaining / this.options.timeOut) * 100;
if (this.options.progressAnimation === 'increasing') {
this.width = 100 - this.width;
}
if (this.width <= 0) {
this.width = 0;
}
if (this.width >= 100) {
this.width = 100;
}
}
resetTimeout() {
clearTimeout(this.timeout);
clearInterval(this.intervalId);
this.state = 'active';
this.options.timeOut = this.originalTimeout;
this.timeout = setTimeout(() => this.remove(), this.originalTimeout);
this.hideTime = new Date().getTime() + (this.originalTimeout || 0);
this.width = -1;
if (this.options.progressBar) {
this.intervalId = setInterval(() => this.updateProgress(), 10);
}
}
/**
* tells toastrService to remove this toast after animation time
*/
remove() {
if (this.state === 'removed') {
return;
}
clearTimeout(this.timeout);
this.state = 'removed';
this.timeout = setTimeout(() =>
this.toastrService.remove(this.toastPackage.toastId),
);
}
@HostListener('click')
tapToast() {
if (this.state === 'removed') {
return;
}
this.toastPackage.triggerTap();
if (this.options.tapToDismiss) {
this.remove();
}
}
@HostListener('mouseenter')
stickAround() {
if (this.state === 'removed') {
return;
}
clearTimeout(this.timeout);
this.options.timeOut = 0;
this.hideTime = 0;
// disable progressBar
clearInterval(this.intervalId);
this.width = 0;
}
@HostListener('mouseleave')
delayedHideToast() {
if (
this.options.disableTimeOut ||
this.options.extendedTimeOut === 0 ||
this.state === 'removed'
) {
return;
}
this.timeout = setTimeout(
() => this.remove(),
this.options.extendedTimeOut,
);
this.options.timeOut = this.options.extendedTimeOut;
this.hideTime = new Date().getTime() + (this.options.timeOut || 0);
this.width = -1;
if (this.options.progressBar) {
this.intervalId = setInterval(() => this.updateProgress(), 10);
}
}
}
export const DefaultNoAnimationsGlobalConfig: GlobalConfig = {
...DefaultNoComponentGlobalConfig,
toastComponent: ToastNoAnimation,
};
@NgModule({
imports: [CommonModule],
declarations: [ToastNoAnimation],
exports: [ToastNoAnimation],
entryComponents: [ToastNoAnimation],
})
export class ToastNoAnimationModule {
static forRoot(config: Partial<GlobalConfig> = {}): ModuleWithProviders {
return {
ngModule: ToastNoAnimationModule,
providers: [
{
provide: TOAST_CONFIG,
useValue: {
default: DefaultNoAnimationsGlobalConfig,
config,
},
},
],
};
}
}