2016-04-01 22:05:09 +02:00
|
|
|
|
/**
|
2020-04-20 10:58:27 +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
|
2020-04-28 23:05:28 +02:00
|
|
|
|
* https://tympanus.net/codrops/
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the MIT license.
|
2020-04-28 23:05:28 +02:00
|
|
|
|
* https://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
|
2020-04-28 23:05:28 +02:00
|
|
|
|
* https://tympanus.net/codrops/
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
|
(function (window) {
|
2020-04-20 11:24:03 +02:00
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* Extend one object with another one
|
2020-07-27 14:24:30 +02:00
|
|
|
|
*
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* @param {object} a The object to extend
|
|
|
|
|
* @param {object} b The object which extends the other, overwrites existing keys
|
|
|
|
|
* @returns {object} The merged object
|
2020-04-20 11:24:03 +02:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* NotificationFx constructor
|
2020-07-27 14:24:30 +02:00
|
|
|
|
*
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* @param {object} options The configuration options
|
|
|
|
|
* @class
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
2016-04-05 14:35:11 -04:00
|
|
|
|
function NotificationFx(options) {
|
2020-04-20 11:24:03 +02:00
|
|
|
|
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
|
2020-05-11 22:22:32 +02:00
|
|
|
|
onClose: function () {
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
onOpen: function () {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-04-05 14:35:11 -04:00
|
|
|
|
};
|
2016-04-01 22:05:09 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* Initialize and cache some vars
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
|
NotificationFx.prototype._init = function () {
|
2016-04-01 22:05:09 +02:00
|
|
|
|
// create HTML structure
|
2016-04-05 14:35:11 -04:00
|
|
|
|
this.ntf = document.createElement("div");
|
2020-04-20 11:24:03 +02:00
|
|
|
|
this.ntf.className = this.options.al_no + " ns-" + this.options.layout + " ns-effect-" + this.options.effect + " ns-type-" + this.options.type;
|
2020-05-11 22:22:32 +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
|
|
|
|
|
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* Init events
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
|
NotificationFx.prototype._initEvents = function () {
|
2016-04-01 22:05:09 +02:00
|
|
|
|
// dismiss notification by tapping on it if someone has a touchscreen
|
2020-05-11 22:22:32 +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
|
|
|
|
|
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* Show the notification
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
|
NotificationFx.prototype.show = function () {
|
2016-04-01 22:05:09 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* Dismiss the notification
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
|
NotificationFx.prototype.dismiss = function () {
|
2016-04-01 22:05:09 +02:00
|
|
|
|
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) => {
|
2020-05-11 22:22:32 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/**
|
2020-07-30 09:33:19 +02:00
|
|
|
|
* Add to global namespace
|
2016-04-01 22:05:09 +02:00
|
|
|
|
*/
|
|
|
|
|
window.NotificationFx = NotificationFx;
|
2016-04-05 14:35:11 -04:00
|
|
|
|
})(window);
|