Files
MagicMirror/defaultmodules/alert/alert.js
T

145 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-05-05 14:55:15 +02:00
/* global NotificationFx */
2016-04-01 22:05:09 +02:00
2020-05-11 22:22:32 +02:00
Module.register("alert", {
alerts: {},
2016-04-01 22:05:09 +02:00
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
2016-04-02 23:54:15 +02:00
position: "center",
welcome_message: false // shown at startup
2016-04-01 22:05:09 +02:00
},
2021-10-15 06:03:51 +02:00
2024-01-01 15:38:08 +01: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
2024-01-01 15:38:08 +01:00
getStyles () {
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
2024-01-01 15:38:08 +01: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",
2025-04-01 20:11:02 +02:00
eo: "translations/eo.json",
2021-10-15 06:05:06 +02:00
es: "translations/es.json",
fr: "translations/fr.json",
hu: "translations/hu.json",
nl: "translations/nl.json",
2026-01-01 08:45:36 -06:00
pt: "translations/pt.json",
"pt-br": "translations/pt-br.json",
2023-04-04 20:44:32 +02: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
2024-01-01 15:38:08 +01:00
getTemplate (type) {
return `templates/${type}.njk`;
},
2024-01-01 15:38:08 +01:00
async start () {
Log.info(`Starting module: ${this.name}`);
2020-05-11 22:22:32 +02:00
if (this.config.effect === "slide") {
this.config.effect = `${this.config.effect}-${this.config.position}`;
2020-05-11 22:22:32 +02:00
}
if (this.config.welcome_message) {
const message = this.config.welcome_message === true ? this.translate("welcome") : this.config.welcome_message;
2023-04-04 20:44:32 +02:00
await this.showNotification({ title: this.translate("sysTitle"), message });
}
},
2024-01-01 15:38:08 +01:00
notificationReceived (notification, payload, sender) {
2021-10-15 06:50:54 +02:00
if (notification === "SHOW_ALERT") {
if (payload.type === "notification") {
this.showNotification(payload);
} else {
this.showAlert(payload, sender);
}
} else if (notification === "HIDE_ALERT") {
this.hideAlert(sender);
}
},
2024-01-01 15:38:08 +01:00
async showNotification (notification) {
2023-04-04 20:44:32 +02: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({
message,
2016-04-05 14:35:11 -04:00
layout: "growl",
effect: this.config.effect,
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
2024-01-01 15:38:08 +01: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-04-04 20:44:32 +02:00
const message = await this.renderMessage(alert.templateName || "alert", alert);
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({
message,
2016-04-05 14:35:11 -04:00
effect: this.config.alert_effect,
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
if (alert.timer) {
2020-04-20 10:44:56 +02:00
setTimeout(() => {
2021-10-15 06:03:51 +02:00
this.hideAlert(sender);
}, 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
2024-01-01 15:38:08 +01: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
2024-01-01 15:38:08 +01:00
renderMessage (type, data) {
return new Promise((resolve) => {
this.nunjucksEnvironment().render(this.getTemplate(type), data, function (err, res) {
if (err) {
2026-01-01 08:45:36 -06:00
Log.error("[alert] Failed to render alert", err);
}
resolve(res);
});
});
},
2024-01-01 15:38:08 +01:00
toggleBlur (add = false) {
2021-10-15 06:43:53 +02:00
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
});