147 lines
3.5 KiB
JavaScript
Raw Normal View History

/* global NotificationFx */
2016-04-01 22:05:09 +02: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.
*/
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
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() {
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",
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",
ru: "translations/ru.json"
2016-04-23 17:27:36 +02:00
};
},
2021-10-15 06:03:51 +02:00
getTemplate(type) {
return `templates/${type}.njk`;
},
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;
this.showNotification({ title: this.translate("sysTitle"), message });
}
},
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);
}
},
async showNotification(notification) {
const message = await this.renderMessage(notification.templateName || "notification", notification);
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
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);
}
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
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
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
});