2020-05-05 14:55:15 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
2016-04-03 20:28:51 +02:00
|
|
|
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
|
2020-05-11 22:22:32 +02:00
|
|
|
welcome_message: false
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
getScripts: function () {
|
2020-04-20 10:44:56 +02:00
|
|
|
return ["notificationFx.js"];
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
getStyles: function () {
|
2020-04-20 10:58:27 +02:00
|
|
|
return ["notificationFx.css", "font-awesome.css"];
|
2016-04-01 22:05:09 +02:00
|
|
|
},
|
2016-04-23 17:27:36 +02:00
|
|
|
// Define required translations.
|
2020-05-11 22:22:32 +02:00
|
|
|
getTranslations: function () {
|
2016-04-23 17:27:36 +02:00
|
|
|
return {
|
|
|
|
en: "translations/en.json",
|
2017-06-13 20:28:24 +02:00
|
|
|
de: "translations/de.json",
|
2020-05-11 22:22:32 +02:00
|
|
|
nl: "translations/nl.json"
|
2016-04-23 17:27:36 +02:00
|
|
|
};
|
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
show_notification: function (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 = "";
|
2016-06-24 09:15:12 +00:00
|
|
|
if (message.title) {
|
2018-08-01 09:37:27 +02:00
|
|
|
msg += "<span class='thin dimmed medium'>" + message.title + "</span>";
|
2016-06-24 09:15:12 +00:00
|
|
|
}
|
2020-05-11 22:22:32 +02:00
|
|
|
if (message.message) {
|
|
|
|
if (msg !== "") {
|
|
|
|
msg += "<br />";
|
2016-06-27 16:15:53 +00:00
|
|
|
}
|
2018-08-01 09:37:27 +02:00
|
|
|
msg += "<span class='light bright small'>" + message.message + "</span>";
|
2016-06-24 09:15:12 +00:00
|
|
|
}
|
2016-12-29 22:23:08 -03:00
|
|
|
|
2016-04-02 19:56:19 +02:00
|
|
|
new NotificationFx({
|
2016-06-24 09:15:12 +00:00
|
|
|
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
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
show_alert: function (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
|
2020-05-11 22:22:32 +02:00
|
|
|
if (typeof params.timer === "undefined") {
|
|
|
|
params.timer = null;
|
|
|
|
}
|
|
|
|
if (typeof params.imageHeight === "undefined") {
|
|
|
|
params.imageHeight = "80px";
|
|
|
|
}
|
2016-04-12 20:54:30 +02:00
|
|
|
if (typeof params.imageUrl === "undefined" && typeof params.imageFA === "undefined") {
|
2016-04-03 03:04:38 +02:00
|
|
|
params.imageUrl = null;
|
2020-05-11 22:22:32 +02:00
|
|
|
} 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";
|
2020-05-11 22:22:32 +02:00
|
|
|
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-04-10 20:15:32 -03:00
|
|
|
this.hide_alert(sender,false);
|
2016-04-03 03:04:38 +02:00
|
|
|
}
|
2016-04-05 14:35:11 -04:00
|
|
|
|
2016-06-24 09:15:12 +00:00
|
|
|
//Display title and message only if they are provided in notification parameters
|
2020-04-20 10:44:56 +02:00
|
|
|
let message = "";
|
2016-06-24 09:15:12 +00:00
|
|
|
if (params.title) {
|
2018-08-01 09:37:27 +02:00
|
|
|
message += "<span class='light dimmed medium'>" + params.title + "</span>";
|
2016-06-24 09:15:12 +00:00
|
|
|
}
|
|
|
|
if (params.message) {
|
2020-05-11 22:22:32 +02:00
|
|
|
if (message !== "") {
|
2016-06-27 16:15:53 +00:00
|
|
|
message += "<br />";
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:37:27 +02:00
|
|
|
message += "<span class='thin bright small'>" + params.message + "</span>";
|
2016-06-24 09:15:12 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2020-12-17 18:31:18 +01:00
|
|
|
onClose: () => this.hide_alert(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(() => {
|
|
|
|
this.hide_alert(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-04-10 20:15:32 -03:00
|
|
|
hide_alert: function (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
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
setPosition: function (pos) {
|
2016-04-03 03:04:38 +02:00
|
|
|
//Add css to body depending on the set position for notifications
|
2020-04-20 10:44:56 +02:00
|
|
|
const sheet = document.createElement("style");
|
2020-05-11 22:22:32 +02:00
|
|
|
if (pos === "center") {
|
|
|
|
sheet.innerHTML = ".ns-box {margin-left: auto; margin-right: auto;text-align: center;}";
|
|
|
|
}
|
|
|
|
if (pos === "right") {
|
|
|
|
sheet.innerHTML = ".ns-box {margin-left: auto;text-align: right;}";
|
|
|
|
}
|
|
|
|
if (pos === "left") {
|
|
|
|
sheet.innerHTML = ".ns-box {margin-right: auto;text-align: left;}";
|
|
|
|
}
|
2016-04-02 23:54:15 +02:00
|
|
|
document.body.appendChild(sheet);
|
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
notificationReceived: function (notification, payload, sender) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification === "SHOW_ALERT") {
|
2020-05-11 22:22:32 +02:00
|
|
|
if (typeof payload.type === "undefined") {
|
|
|
|
payload.type = "alert";
|
|
|
|
}
|
2018-08-01 09:37:27 +02:00
|
|
|
if (payload.type === "alert") {
|
2016-04-05 14:35:11 -04:00
|
|
|
this.show_alert(payload, sender);
|
2019-06-05 17:01:54 +02:00
|
|
|
} else if (payload.type === "notification") {
|
2016-04-05 14:35:11 -04:00
|
|
|
this.show_notification(payload);
|
2016-04-03 20:28:51 +02:00
|
|
|
}
|
2016-04-05 14:35:11 -04:00
|
|
|
} else if (notification === "HIDE_ALERT") {
|
|
|
|
this.hide_alert(sender);
|
2016-04-01 22:05:09 +02:00
|
|
|
}
|
|
|
|
},
|
2020-05-11 22:22:32 +02:00
|
|
|
start: function () {
|
2016-04-05 14:35:11 -04:00
|
|
|
this.alerts = {};
|
|
|
|
this.setPosition(this.config.position);
|
|
|
|
if (this.config.welcome_message) {
|
2020-05-11 22:22:32 +02:00
|
|
|
if (this.config.welcome_message === true) {
|
|
|
|
this.show_notification({ title: this.translate("sysTitle"), message: this.translate("welcome") });
|
|
|
|
} else {
|
|
|
|
this.show_notification({ 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
|
|
|
});
|