-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
alert.js
141 lines (119 loc) · 3.46 KB
/
alert.js
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
/* global NotificationFx */
Module.register("alert", {
alerts: {},
defaults: {
effect: "slide", // scale|slide|genie|jelly|flip|bouncyflip|exploader
alert_effect: "jelly", // scale|slide|genie|jelly|flip|bouncyflip|exploader
display_time: 3500, // time a notification is displayed in seconds
position: "center",
welcome_message: false // shown at startup
},
getScripts () {
return ["notificationFx.js"];
},
getStyles () {
return ["font-awesome.css", this.file("./styles/notificationFx.css"), this.file(`./styles/${this.config.position}.css`)];
},
getTranslations () {
return {
bg: "translations/bg.json",
da: "translations/da.json",
de: "translations/de.json",
en: "translations/en.json",
es: "translations/es.json",
fr: "translations/fr.json",
hu: "translations/hu.json",
nl: "translations/nl.json",
ru: "translations/ru.json",
th: "translations/th.json"
};
},
getTemplate (type) {
return `templates/${type}.njk`;
},
async start () {
Log.info(`Starting module: ${this.name}`);
if (this.config.effect === "slide") {
this.config.effect = `${this.config.effect}-${this.config.position}`;
}
if (this.config.welcome_message) {
const message = this.config.welcome_message === true ? this.translate("welcome") : this.config.welcome_message;
await this.showNotification({ title: this.translate("sysTitle"), message });
}
},
notificationReceived (notification, payload, sender) {
if (notification === "SHOW_ALERT") {
if (payload.type === "notification") {
this.showNotification(payload);
} else {
this.showAlert(payload, sender);
}
} else if (notification === "HIDE_ALERT") {
this.hideAlert(sender);
}
},
async showNotification (notification) {
const message = await this.renderMessage(notification.templateName || "notification", notification);
new NotificationFx({
message,
layout: "growl",
effect: this.config.effect,
ttl: notification.timer || this.config.display_time
}).show();
},
async showAlert (alert, sender) {
// If module already has an open alert close it
if (this.alerts[sender.name]) {
this.hideAlert(sender, false);
}
// Add overlay
if (!Object.keys(this.alerts).length) {
this.toggleBlur(true);
}
const message = await this.renderMessage(alert.templateName || "alert", alert);
// Store alert in this.alerts
this.alerts[sender.name] = new NotificationFx({
message,
effect: this.config.alert_effect,
ttl: alert.timer,
onClose: () => this.hideAlert(sender),
al_no: "ns-alert"
});
// Show alert
this.alerts[sender.name].show();
// Add timer to dismiss alert and overlay
if (alert.timer) {
setTimeout(() => {
this.hideAlert(sender);
}, alert.timer);
}
},
hideAlert (sender, close = true) {
// Dismiss alert and remove from this.alerts
if (this.alerts[sender.name]) {
this.alerts[sender.name].dismiss(close);
delete this.alerts[sender.name];
// Remove overlay
if (!Object.keys(this.alerts).length) {
this.toggleBlur(false);
}
}
},
renderMessage (type, data) {
return new Promise((resolve) => {
this.nunjucksEnvironment().render(this.getTemplate(type), data, function (err, res) {
if (err) {
Log.error("Failed to render alert", err);
}
resolve(res);
});
});
},
toggleBlur (add = false) {
const method = add ? "add" : "remove";
const modules = document.querySelectorAll(".module");
for (const module of modules) {
module.classList[method]("alert-blur");
}
}
});