154 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-04-01 22:05:09 +02:00
/* global Module */
/* 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.
*/
2016-04-05 14:35:11 -04: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
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
2016-05-03 16:59:18 +02:00
welcome_message: false,
2016-04-01 22:05:09 +02:00
},
getScripts: function() {
2020-04-20 10:44:56 +02:00
return ["notificationFx.js"];
2016-04-01 22:05:09 +02:00
},
getStyles: function() {
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.
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json",
nl: "translations/nl.json",
2016-04-23 17:27:36 +02:00
};
},
2016-04-05 14:35:11 -04:00
show_notification: function(message) {
2019-06-05 09:32:10 +02:00
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){
2019-06-05 09:32:10 +02:00
if (msg !== ""){
2016-06-27 16:15:53 +00:00
msg+= "<br />";
}
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
},
2016-04-05 14:35:11 -04: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
2016-04-05 14:35:11 -04:00
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]) {
this.hide_alert(sender);
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,
2016-04-03 03:04:38 +02:00
al_no: "ns-alert"
});
//Show alert
2016-04-05 14:35:11 -04:00
this.alerts[sender.name].show();
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-05 14:35:11 -04:00
2016-04-02 03:59:18 +02:00
},
2016-04-05 14:35:11 -04:00
hide_alert: function(sender) {
2018-04-02 14:11:21 +02:00
//Dismiss alert and remove from this.alerts
if (this.alerts[sender.name]) {
this.alerts[sender.name].dismiss();
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
},
2016-04-05 14:35:11 -04: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");
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);
2016-04-05 14:35:11 -04:00
2016-04-02 23:54:15 +02:00
},
2016-04-01 22:05:09 +02:00
notificationReceived: function(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") {
2016-04-05 14:35:11 -04:00
this.show_alert(payload, sender);
} else if (payload.type === "notification") {
2016-04-05 14:35:11 -04:00
this.show_notification(payload);
}
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
}
},
start: function() {
2016-04-05 14:35:11 -04:00
this.alerts = {};
this.setPosition(this.config.position);
if (this.config.welcome_message) {
if (this.config.welcome_message === true){
2016-04-23 17:27:36 +02:00
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-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
});