2020-05-05 14:55:15 +02:00
|
|
|
/* global NotificationFx */
|
2016-04-01 22:05:09 +02:00
|
|
|
|
2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2016-04-01 22:05:09 +02:00
|
|
|
* Module: alert
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Paul-Vincent Roll https://paulvincentroll.com/
|
2016-04-01 22:05:09 +02:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
Module.register("alert", {
|
2021-10-15 06:14:45 +02:00
|
|
|
alerts: {},
|
|
|
|
|
2016-04-01 22:05:09 +02:00
|
|
|
defaults: {
|
2021-10-15 06:14:45 +02:00
|
|
|
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
|
2016-04-02 23:54:15 +02:00
|
|
|
position: "center",
|
2021-10-15 06:14:45 +02:00
|
|
|
welcome_message: false // shown at startup
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
|
|
|
getScripts() {
|
2020-04-20 10:44:56 +02:00
|
|
|
return ["notificationFx.js"];
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
|
|
|
getStyles() {
|
2021-10-15 06:26:23 +02:00
|
|
|
return ["font-awesome.css", this.file(`./styles/notificationFx.css`), this.file(`./styles/${this.config.position}.css`)];
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
|
|
|
getTranslations() {
|
2016-04-23 17:27:36 +02:00
|
|
|
return {
|
2021-10-15 06:05:06 +02:00
|
|
|
bg: "translations/bg.json",
|
|
|
|
da: "translations/da.json",
|
2017-06-13 20:28:24 +02:00
|
|
|
de: "translations/de.json",
|
2021-10-15 06:05:06 +02:00
|
|
|
en: "translations/en.json",
|
|
|
|
es: "translations/es.json",
|
|
|
|
fr: "translations/fr.json",
|
|
|
|
hu: "translations/hu.json",
|
|
|
|
nl: "translations/nl.json",
|
2023-02-20 20:03:42 +01:00
|
|
|
ru: "translations/ru.json",
|
|
|
|
th: "translations/th.json"
|
2016-04-23 17:27:36 +02:00
|
|
|
};
|
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
2021-10-15 06:23:50 +02:00
|
|
|
getTemplate(type) {
|
|
|
|
return `templates/${type}.njk`;
|
|
|
|
},
|
|
|
|
|
2023-02-22 18:58:29 +01:00
|
|
|
async start() {
|
2021-10-15 06:14:45 +02:00
|
|
|
Log.info(`Starting module: ${this.name}`);
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
if (this.config.effect === "slide") {
|
2021-10-15 06:14:45 +02:00
|
|
|
this.config.effect = `${this.config.effect}-${this.config.position}`;
|
2020-05-11 22:22:32 +02:00
|
|
|
}
|
2021-10-15 06:14:45 +02:00
|
|
|
|
|
|
|
if (this.config.welcome_message) {
|
|
|
|
const message = this.config.welcome_message === true ? this.translate("welcome") : this.config.welcome_message;
|
2023-02-22 18:58:29 +01:00
|
|
|
await this.showNotification({ title: this.translate("sysTitle"), message });
|
2021-10-15 06:14:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-10-15 06:50:54 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-10-15 06:23:50 +02:00
|
|
|
async showNotification(notification) {
|
2023-01-26 13:00:49 +01:00
|
|
|
const message = await this.renderMessage(notification.templateName || "notification", notification);
|
2016-12-29 22:23:08 -03:00
|
|
|
|
2016-04-02 19:56:19 +02:00
|
|
|
new NotificationFx({
|
2021-10-15 06:23:50 +02:00
|
|
|
message,
|
2016-04-05 14:35:11 -04:00
|
|
|
layout: "growl",
|
|
|
|
effect: this.config.effect,
|
2021-10-15 06:23:50 +02:00
|
|
|
ttl: notification.timer || this.config.display_time
|
2016-04-02 19:56:19 +02:00
|
|
|
}).show();
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
2021-10-15 06:31:07 +02:00
|
|
|
async showAlert(alert, sender) {
|
2021-10-15 06:43:53 +02:00
|
|
|
// If module already has an open alert close it
|
2016-04-05 14:35:11 -04:00
|
|
|
if (this.alerts[sender.name]) {
|
2021-10-15 06:03:51 +02:00
|
|
|
this.hideAlert(sender, false);
|
2016-04-03 03:04:38 +02:00
|
|
|
}
|
2016-04-05 14:35:11 -04:00
|
|
|
|
2021-10-15 06:43:53 +02:00
|
|
|
// Add overlay
|
|
|
|
if (!Object.keys(this.alerts).length) {
|
|
|
|
this.toggleBlur(true);
|
|
|
|
}
|
|
|
|
|
2023-01-26 13:00:49 +01:00
|
|
|
const message = await this.renderMessage(alert.templateName || "alert", alert);
|
2016-06-24 09:15:12 +00:00
|
|
|
|
2021-10-15 06:43:53 +02:00
|
|
|
// Store alert in this.alerts
|
2016-04-03 03:04:38 +02:00
|
|
|
this.alerts[sender.name] = new NotificationFx({
|
2021-10-15 06:31:07 +02:00
|
|
|
message,
|
2016-04-05 14:35:11 -04:00
|
|
|
effect: this.config.alert_effect,
|
2021-10-15 06:31:07 +02:00
|
|
|
ttl: alert.timer,
|
2021-10-15 06:03:51 +02:00
|
|
|
onClose: () => this.hideAlert(sender),
|
2016-04-03 03:04:38 +02:00
|
|
|
al_no: "ns-alert"
|
|
|
|
});
|
2020-12-21 10:57:18 +01:00
|
|
|
|
2021-10-15 06:43:53 +02:00
|
|
|
// Show alert
|
2016-04-05 14:35:11 -04:00
|
|
|
this.alerts[sender.name].show();
|
2020-12-21 10:57:18 +01:00
|
|
|
|
2021-10-15 06:43:53 +02:00
|
|
|
// Add timer to dismiss alert and overlay
|
2021-10-15 06:31:07 +02:00
|
|
|
if (alert.timer) {
|
2020-04-20 10:44:56 +02:00
|
|
|
setTimeout(() => {
|
2021-10-15 06:03:51 +02:00
|
|
|
this.hideAlert(sender);
|
2021-10-15 06:31:07 +02:00
|
|
|
}, alert.timer);
|
2016-04-03 03:04:38 +02:00
|
|
|
}
|
2016-04-02 03:59:18 +02:00
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
|
|
|
hideAlert(sender, close = true) {
|
2021-10-15 06:43:53 +02:00
|
|
|
// Dismiss alert and remove from this.alerts
|
2018-04-02 14:11:21 +02:00
|
|
|
if (this.alerts[sender.name]) {
|
2021-04-10 20:15:32 -03:00
|
|
|
this.alerts[sender.name].dismiss(close);
|
2021-10-15 06:43:53 +02:00
|
|
|
delete this.alerts[sender.name];
|
|
|
|
// Remove overlay
|
|
|
|
if (!Object.keys(this.alerts).length) {
|
|
|
|
this.toggleBlur(false);
|
|
|
|
}
|
2018-04-02 14:11:21 +02:00
|
|
|
}
|
2016-04-02 03:59:18 +02:00
|
|
|
},
|
2021-10-15 06:03:51 +02:00
|
|
|
|
2021-10-15 06:23:50 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-10-15 06:43:53 +02:00
|
|
|
toggleBlur(add = false) {
|
|
|
|
const method = add ? "add" : "remove";
|
|
|
|
const modules = document.querySelectorAll(".module");
|
|
|
|
for (const module of modules) {
|
|
|
|
module.classList[method]("alert-blur");
|
|
|
|
}
|
2016-04-01 22:05:09 +02:00
|
|
|
}
|
2016-04-05 14:35:11 -04:00
|
|
|
});
|