-
Notifications
You must be signed in to change notification settings - Fork 3
/
notificationFx.js
executable file
·160 lines (146 loc) · 3.86 KB
/
notificationFx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Based on work by
*
* notificationFx.js v1.0.0
* https://tympanus.net/codrops/
*
* Licensed under the MIT license.
* https://opensource.org/licenses/mit-license.php
*
* Copyright 2014, Codrops
* https://tympanus.net/codrops/
*
* @param {object} window The window object
*/
(function (window) {
/**
* Extend one object with another one
*
* @param {object} a The object to extend
* @param {object} b The object which extends the other, overwrites existing keys
* @returns {object} The merged object
*/
function extend(a, b) {
for (let key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
/**
* NotificationFx constructor
*
* @param {object} options The configuration options
* @class
*/
function NotificationFx(options) {
this.options = extend({}, this.options);
extend(this.options, options);
this._init();
}
/**
* NotificationFx options
*/
NotificationFx.prototype.options = {
// element to which the notification will be appended
// defaults to the document.body
wrapper: document.body,
// the message
message: "yo!",
// layout type: growl|attached|bar|other
layout: "growl",
// effects for the specified layout:
// for growl layout: scale|slide|genie|jelly
// for attached layout: flip|bouncyflip
// for other layout: boxspinner|cornerexpand|loadingcircle|thumbslider
// ...
effect: "slide",
// notice, warning, error, success
// will add class ns-type-warning, ns-type-error or ns-type-success
type: "notice",
// if the user doesn´t close the notification then we remove it
// after the following time
ttl: 6000,
al_no: "ns-box",
// callbacks
onClose: function () {
return false;
},
onOpen: function () {
return false;
}
};
/**
* Initialize and cache some vars
*/
NotificationFx.prototype._init = function () {
// create HTML structure
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;
let strinner = '<div class="ns-box-inner">';
strinner += this.options.message;
strinner += "</div>";
this.ntf.innerHTML = strinner;
// append to body or the element specified in options.wrapper
this.options.wrapper.insertBefore(this.ntf, this.options.wrapper.nextSibling);
// dismiss after [options.ttl]ms
if (this.options.ttl) {
this.dismissttl = setTimeout(() => {
if (this.active) {
this.dismiss();
}
}, this.options.ttl);
}
// init events
this._initEvents();
};
/**
* Init events
*/
NotificationFx.prototype._initEvents = function () {
// dismiss notification by tapping on it if someone has a touchscreen
this.ntf.querySelector(".ns-box-inner").addEventListener("click", () => {
this.dismiss();
});
};
/**
* Show the notification
*/
NotificationFx.prototype.show = function () {
this.active = true;
this.ntf.classList.remove("ns-hide");
this.ntf.classList.add("ns-show");
this.options.onOpen();
};
/**
* Dismiss the notification
*
* @param {boolean} [close] call the onClose callback at the end
*/
NotificationFx.prototype.dismiss = function (close = true) {
this.active = false;
clearTimeout(this.dismissttl);
this.ntf.classList.remove("ns-show");
setTimeout(() => {
this.ntf.classList.add("ns-hide");
// callback
if (close) this.options.onClose();
}, 25);
// after animation ends remove ntf from the DOM
const onEndAnimationFn = (ev) => {
if (ev.target !== this.ntf) {
return false;
}
this.ntf.removeEventListener("animationend", onEndAnimationFn);
if (ev.target.parentNode === this.options.wrapper) {
this.options.wrapper.removeChild(this.ntf);
}
};
this.ntf.addEventListener("animationend", onEndAnimationFn);
};
/**
* Add to global namespace
*/
window.NotificationFx = NotificationFx;
})(window);