-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoastr.ts
84 lines (71 loc) · 2.62 KB
/
toastr.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
import {
Envelope,
FlasherNotification,
FlasherOptions,
NotificationFactoryInterface,
} from '@flasher/flasher';
import toastr from 'toastr';
import 'toastr/build/toastr.min.css';
export default class ToastrFactory implements NotificationFactoryInterface {
success(message: string|FlasherOptions, title?: string|FlasherOptions, options?: FlasherOptions): void {
this.flash('success', message, title, options);
}
info(message: string|FlasherOptions, title?: string|FlasherOptions, options?: FlasherOptions): void {
this.flash('info', message, title, options);
}
warning(message: string|FlasherOptions, title?: string|FlasherOptions, options?: FlasherOptions): void {
this.flash('warning', message, title, options);
}
error(message: string|FlasherOptions, title?: string|FlasherOptions, options?: FlasherOptions): void {
this.flash('error', message, title, options);
}
flash(type: string|FlasherOptions, message: string|FlasherOptions, title?: string|FlasherOptions, options?: FlasherOptions): void {
const notification = this.createNotification(type, message, title, options);
this.renderOptions({});
this.render({ notification });
}
createNotification(
type: string|FlasherOptions,
message?: string|FlasherOptions,
title?: string|FlasherOptions,
options?: FlasherOptions
): FlasherNotification {
if (typeof type === 'object') {
options = type;
type = options.type as unknown as string;
message = options.message as unknown as string;
title = options.title as unknown as string;
} else if (typeof message === 'object') {
options = message;
message = options.message as unknown as string;
title = options.title as unknown as string;
} else if (typeof title === 'object') {
options = title;
title = options.title as unknown as string;
}
if (undefined === message) {
throw new Error('message option is required');
}
return {
type: type || 'info',
message,
title,
options
};
}
render(envelope: Envelope): void {
const { notification } = envelope;
const { message, title, options } = notification;
let type = notification.type || 'info';
const instance = toastr[type as ToastrType](message, title, options as ToastrOptions);
instance.parent().attr('data-turbo-cache', 'false');
instance.parent().addClass('fl-no-cache');
}
renderOptions(options: FlasherOptions): void {
toastr.options = {
timeOut: (options.timeOut || 5000) as any,
progressBar: (options.progressBar || 5000) as any,
...options,
} as ToastrOptions;
}
}