166 lines
4.6 KiB
JavaScript
Raw Normal View History

/* global NotificationFx */
2016-04-01 22:05:09 +02:00
/* Magic Mirror
* 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", {
2016-04-01 22:05:09 +02:00
defaults: {
2016-04-02 19:56:19 +02:00
// scale|slide|genie|jelly|flip|bouncyflip|exploader
2016-04-01 22:05:09 +02:00
effect: "slide",
2016-04-03 03:04:38 +02:00
// scale|slide|genie|jelly|flip|bouncyflip|exploader
2016-04-05 14:35:11 -04:00
alert_effect: "jelly",
2016-04-03 04:03:57 +02:00
//time a notification is displayed in seconds
display_time: 3500,
2016-04-02 23:54:15 +02:00
//Position
position: "center",
2016-04-01 22:05:09 +02:00
//shown at startup
welcome_message: false
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 ["notificationFx.css", "font-awesome.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
showNotification(message) {
if (this.config.effect === "slide") {
this.config.effect = this.config.effect + "-" + this.config.position;
}
2020-04-20 10:44:56 +02:00
let msg = "";
if (message.title) {
msg += "<span class='thin dimmed medium'>" + message.title + "</span>";
}
if (message.message) {
if (msg !== "") {
msg += "<br />";
2016-06-27 16:15:53 +00:00
}
msg += "<span class='light bright small'>" + message.message + "</span>";
}
2016-04-02 19:56:19 +02:00
new NotificationFx({
message: msg,
2016-04-05 14:35:11 -04:00
layout: "growl",
effect: this.config.effect,
2019-03-13 12:01:49 +01:00
ttl: message.timer !== undefined ? message.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
showAlert(params, sender) {
2020-04-20 10:44:56 +02:00
let image = "";
2016-04-03 03:04:38 +02:00
//Set standard params if not provided by module
if (typeof params.timer === "undefined") {
params.timer = null;
}
if (typeof params.imageHeight === "undefined") {
params.imageHeight = "80px";
}
if (typeof params.imageUrl === "undefined" && typeof params.imageFA === "undefined") {
2016-04-03 03:04:38 +02:00
params.imageUrl = null;
} else if (typeof params.imageFA === "undefined") {
image = "<img src='" + params.imageUrl.toString() + "' height='" + params.imageHeight.toString() + "' style='margin-bottom: 10px;'/><br />";
} else if (typeof params.imageUrl === "undefined") {
image = "<span class='bright " + "fa fa-" + params.imageFA + "' style='margin-bottom: 10px;font-size:" + params.imageHeight.toString() + ";'/></span><br />";
2016-04-03 03:04:38 +02:00
}
//Create overlay
2020-04-20 10:44:56 +02:00
const overlay = document.createElement("div");
2016-04-05 14:35:11 -04:00
overlay.id = "overlay";
overlay.innerHTML += '<div class="black_overlay"></div>';
2016-04-03 03:04:38 +02:00
document.body.insertBefore(overlay, document.body.firstChild);
2016-04-05 14:35:11 -04:00
2016-04-03 03:04:38 +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
//Display title and message only if they are provided in notification parameters
2020-04-20 10:44:56 +02:00
let message = "";
if (params.title) {
message += "<span class='light dimmed medium'>" + params.title + "</span>";
}
if (params.message) {
if (message !== "") {
2016-06-27 16:15:53 +00:00
message += "<br />";
}
message += "<span class='thin bright small'>" + params.message + "</span>";
}
2016-04-03 03:04:38 +02:00
//Store alert in this.alerts
this.alerts[sender.name] = new NotificationFx({
2016-04-05 14:35:11 -04:00
message: image + message,
effect: this.config.alert_effect,
2016-04-03 04:03:57 +02:00
ttl: params.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
2016-04-03 03:04:38 +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
2016-04-03 03:04:38 +02:00
//Add timer to dismiss alert and overlay
if (params.timer) {
2020-04-20 10:44:56 +02:00
setTimeout(() => {
2021-10-15 06:03:51 +02:00
this.hideAlert(sender);
2016-04-05 14:35:11 -04:00
}, params.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) {
2018-04-02 14:11:21 +02:00
//Dismiss alert and remove from this.alerts
if (this.alerts[sender.name]) {
2021-04-10 20:15:32 -03:00
this.alerts[sender.name].dismiss(close);
2018-04-02 14:11:21 +02:00
this.alerts[sender.name] = null;
//Remove overlay
2020-04-20 10:44:56 +02:00
const overlay = document.getElementById("overlay");
2018-04-02 14:11:21 +02:00
overlay.parentNode.removeChild(overlay);
}
2016-04-02 03:59:18 +02:00
},
2021-10-15 06:03:51 +02:00
notificationReceived(notification, payload, sender) {
2016-04-05 14:35:11 -04:00
if (notification === "SHOW_ALERT") {
if (typeof payload.type === "undefined") {
payload.type = "alert";
}
if (payload.type === "alert") {
2021-10-15 06:03:51 +02:00
this.showAlert(payload, sender);
} else if (payload.type === "notification") {
2021-10-15 06:03:51 +02:00
this.showNotification(payload);
}
2016-04-05 14:35:11 -04:00
} else if (notification === "HIDE_ALERT") {
2021-10-15 06:03:51 +02:00
this.hideAlert(sender);
2016-04-01 22:05:09 +02:00
}
},
2021-10-15 06:03:51 +02:00
start() {
2016-04-05 14:35:11 -04:00
this.alerts = {};
if (this.config.welcome_message) {
if (this.config.welcome_message === true) {
2021-10-15 06:03:51 +02:00
this.showNotification({ title: this.translate("sysTitle"), message: this.translate("welcome") });
} else {
2021-10-15 06:03:51 +02:00
this.showNotification({ title: this.translate("sysTitle"), message: this.config.welcome_message });
2016-04-23 17:27:36 +02:00
}
2016-04-02 19:12:59 +02:00
}
2016-04-05 14:35:11 -04:00
Log.info("Starting module: " + this.name);
2016-04-01 22:05:09 +02:00
}
2016-04-05 14:35:11 -04:00
});