118 lines
4.0 KiB
JavaScript
Raw Normal View History

2016-04-01 22:05:09 +02:00
/* global Module */
/* Magic Mirror
* Module: alert
*
* By Paul-Vincent Roll http://paulvincentroll.com
* 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
welcome_message: "Welcome, start was successfull!"
},
getScripts: function() {
2016-04-05 14:35:11 -04:00
return ["classie.js", "modernizr.custom.js", "notificationFx.js"];
2016-04-01 22:05:09 +02:00
},
getStyles: function() {
2016-04-05 14:35:11 -04:00
return ["ns-default.css"];
2016-04-01 22:05:09 +02:00
},
2016-04-05 14:35:11 -04:00
show_notification: function(message) {
if (this.config.effect == "slide") {this.config.effect = this.config.effect + "-" + this.config.position;}
message = "<span class='thin' style='line-height: 35px; font-size:24px' color='#4A4A4A'>" + message.title + "</span><br /><span class='light' style='font-size:28px;line-height: 30px;'>" + message.message + "</span>";
2016-04-02 19:56:19 +02:00
new NotificationFx({
2016-04-05 14:35:11 -04:00
message: message,
layout: "growl",
effect: this.config.effect,
ttl: 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) {
var self = this;
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") {
2016-04-03 03:04:38 +02:00
params.imageUrl = null;
2016-04-05 14:35:11 -04:00
image = "";
} else {
image = "<img src='" + (params.imageUrl).toString() + "' height=" + (params.imageHeight).toString() + " style='margin-bottom: 10px;'/><br />";
2016-04-03 03:04:38 +02:00
}
//Create overlay
var 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
message = "<span class='light' style='line-height: 35px; font-size:30px' color='#4A4A4A'>" + params.title + "</span><br /><span class='thin' style='font-size:22px;line-height: 30px;'>" + 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) {
2016-04-05 14:35:11 -04:00
setTimeout(function() {
self.hide_alert(sender);
}, 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) {
2016-04-03 03:04:38 +02:00
//Dismiss alert and remove from this.alerts
2016-04-05 14:35:11 -04:00
this.alerts[sender.name].dismiss();
this.alerts[sender.name] = null;
2016-04-03 03:04:38 +02:00
//Remove overlay
var overlay = document.getElementById("overlay");
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
2016-04-05 14:35:11 -04:00
var 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") {
this.show_alert(payload, sender);
} else if (payload.type = "notification") {
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) {
this.show_notification({title: "MagicMirror Notification", 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
});