MagicMirror/modules/default/alert/notificationFx.js

145 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-04-01 22:05:09 +02:00
/**
* Based on work by
2020-04-20 10:44:56 +02:00
*
2016-04-01 22:05:09 +02:00
* notificationFx.js v1.0.0
* http://www.codrops.com
*
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
2016-04-05 14:35:11 -04:00
*
2016-04-01 22:05:09 +02:00
* Copyright 2014, Codrops
* http://www.codrops.com
*/
2016-04-05 14:35:11 -04:00
;(function(window) {
/**
* extend obj function
*/
function extend(a, b) {
for (let key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
2016-04-01 22:05:09 +02:00
/**
* NotificationFx function
*/
2016-04-05 14:35:11 -04:00
function NotificationFx(options) {
this.options = extend({}, this.options);
extend(this.options, options);
2016-04-01 22:05:09 +02:00
this._init();
}
/**
* NotificationFx options
*/
NotificationFx.prototype.options = {
// element to which the notification will be appended
// defaults to the document.body
2016-04-05 14:35:11 -04:00
wrapper: document.body,
2016-04-01 22:05:09 +02:00
// the message
2016-04-05 14:35:11 -04:00
message: "yo!",
2016-04-01 22:05:09 +02:00
// layout type: growl|attached|bar|other
2016-04-05 14:35:11 -04:00
layout: "growl",
2016-04-01 22:05:09 +02:00
// effects for the specified layout:
// for growl layout: scale|slide|genie|jelly
// for attached layout: flip|bouncyflip
// for other layout: boxspinner|cornerexpand|loadingcircle|thumbslider
// ...
2016-04-05 14:35:11 -04:00
effect: "slide",
2016-04-01 22:05:09 +02:00
// notice, warning, error, success
// will add class ns-type-warning, ns-type-error or ns-type-success
2016-04-05 14:35:11 -04:00
type: "notice",
// if the user doesn´t close the notification then we remove it
2016-04-01 22:05:09 +02:00
// after the following time
2016-04-05 14:35:11 -04:00
ttl: 6000,
2016-04-03 03:04:38 +02:00
al_no: "ns-box",
2016-04-01 22:05:09 +02:00
// callbacks
2016-04-05 14:35:11 -04:00
onClose: function() { return false; },
onOpen: function() { return false; }
};
2016-04-01 22:05:09 +02:00
/**
* init function
* initialize and cache some vars
*/
NotificationFx.prototype._init = function() {
// create HTML structure
2016-04-05 14:35:11 -04:00
this.ntf = document.createElement("div");
this.ntf.className = this.options.al_no + " ns-" + this.options.layout + " ns-effect-" + this.options.effect + " ns-type-" + this.options.type;
2020-04-20 10:44:56 +02:00
let strinner = "<div class=\"ns-box-inner\">";
2016-04-01 22:05:09 +02:00
strinner += this.options.message;
2016-04-05 14:35:11 -04:00
strinner += "</div>";
2016-04-01 22:05:09 +02:00
this.ntf.innerHTML = strinner;
// append to body or the element specified in options.wrapper
2016-04-05 14:35:11 -04:00
this.options.wrapper.insertBefore(this.ntf, this.options.wrapper.nextSibling);
2016-04-01 22:05:09 +02:00
// dismiss after [options.ttl]ms
2016-04-05 14:35:11 -04:00
if (this.options.ttl) {
2020-04-20 10:44:56 +02:00
this.dismissttl = setTimeout(() => {
if (this.active) {
this.dismiss();
}
}, this.options.ttl);
2016-04-03 03:04:38 +02:00
}
2016-04-01 22:05:09 +02:00
// init events
this._initEvents();
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
* init events
*/
NotificationFx.prototype._initEvents = function() {
// dismiss notification by tapping on it if someone has a touchscreen
2020-04-20 10:44:56 +02:00
this.ntf.querySelector(".ns-box-inner").addEventListener("click", () => { this.dismiss(); });
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
* show the notification
*/
NotificationFx.prototype.show = function() {
this.active = true;
2020-04-20 10:44:56 +02:00
this.ntf.classList.remove("ns-hide");
this.ntf.classList.add("ns-show");
2016-04-01 22:05:09 +02:00
this.options.onOpen();
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
* dismiss the notification
*/
NotificationFx.prototype.dismiss = function() {
this.active = false;
2016-04-05 14:35:11 -04:00
clearTimeout(this.dismissttl);
2020-04-20 10:44:56 +02:00
this.ntf.classList.remove("ns-show");
setTimeout(() => {
this.ntf.classList.add("ns-hide");
2016-04-05 14:35:11 -04:00
2016-04-01 22:05:09 +02:00
// callback
2020-04-20 10:44:56 +02:00
this.options.onClose();
2016-04-05 14:35:11 -04:00
}, 25);
2016-04-01 22:05:09 +02:00
// after animation ends remove ntf from the DOM
2020-04-20 10:44:56 +02:00
const onEndAnimationFn = (ev) => {
if (ev.target !== this.ntf) {return false;}
2020-04-20 10:44:56 +02:00
this.ntf.removeEventListener("animationend", onEndAnimationFn);
2016-05-03 16:59:18 +02:00
2020-04-20 10:44:56 +02:00
if (ev.target.parentNode === this.options.wrapper) {
this.options.wrapper.removeChild(this.ntf);
2016-05-03 16:59:18 +02:00
}
2016-04-01 22:05:09 +02:00
};
2020-04-20 10:44:56 +02:00
this.ntf.addEventListener("animationend", onEndAnimationFn);
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
* add to global namespace
*/
window.NotificationFx = NotificationFx;
2016-04-05 14:35:11 -04:00
})(window);