MagicMirror/modules/default/alert/notificationFx.js

163 lines
3.9 KiB
JavaScript
Raw Normal View History

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
// jscs:disable
;(function(window) {
"use strict";
2016-04-01 22:05:09 +02:00
var docElem = window.document.documentElement,
2016-04-05 14:35:11 -04:00
support = {animations: Modernizr.cssanimations},
2016-04-01 22:05:09 +02:00
animEndEventNames = {
2016-04-05 14:35:11 -04:00
"WebkitAnimation": "webkitAnimationEnd",
"OAnimation": "oAnimationEnd",
"msAnimation": "MSAnimationEnd",
"animation": "animationend"
2016-04-01 22:05:09 +02:00
},
// animation end event name
2016-04-05 14:35:11 -04:00
animEndEventName = animEndEventNames[ Modernizr.prefixed("animation") ];
2016-04-01 22:05:09 +02:00
/**
* extend obj function
*/
2016-04-05 14:35:11 -04:00
function extend(a, b) {
for (var key in b) {
if (b.hasOwnProperty(key)) {
2016-04-01 22:05:09 +02:00
a[key] = b[key];
}
}
return a;
}
/**
* 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;
var 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
var self = this;
2016-04-05 14:35:11 -04:00
if (this.options.ttl) {
this.dismissttl = setTimeout(function() {
if (self.active) {
2016-04-01 22:05:09 +02:00
self.dismiss();
}
2016-04-05 14:35:11 -04:00
}, 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() {
var self = this;
// dismiss notification by tapping on it if someone has a touchscreen
2016-04-05 14:35:11 -04:00
this.ntf.querySelector(".ns-box-inner").addEventListener("click", function() { self.dismiss(); });
};
2016-04-01 22:05:09 +02:00
/**
* show the notification
*/
NotificationFx.prototype.show = function() {
this.active = true;
2016-04-05 14:35:11 -04:00
classie.remove(this.ntf, "ns-hide");
classie.add(this.ntf, "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() {
var self = this;
this.active = false;
2016-04-05 14:35:11 -04:00
clearTimeout(this.dismissttl);
classie.remove(this.ntf, "ns-show");
setTimeout(function() {
classie.add(self.ntf, "ns-hide");
2016-04-01 22:05:09 +02:00
// callback
self.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
2016-04-05 14:35:11 -04:00
var onEndAnimationFn = function(ev) {
if (support.animations) {
if (ev.target !== self.ntf) return false;
this.removeEventListener(animEndEventName, onEndAnimationFn);
2016-04-01 22:05:09 +02:00
}
2016-04-05 14:35:11 -04:00
self.options.wrapper.removeChild(this);
2016-04-01 22:05:09 +02:00
};
2016-04-05 14:35:11 -04:00
if (support.animations) {
this.ntf.addEventListener(animEndEventName, onEndAnimationFn);
} else {
2016-04-01 22:05:09 +02:00
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);