-
Notifications
You must be signed in to change notification settings - Fork 1
/
toast-js.js
137 lines (135 loc) · 4.26 KB
/
toast-js.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
(function (window){
// Close toast
document.addEventListener("click",function(e) {
if (e.target && (e.target.matches('span.toast-close') || e.target.matches('span.toast-action'))) {
closeToast(e);
}
});
/* Create a toast
* @param {Object} data.
*/
function createToast(options){
/* Options Parameter Keys
* - message : text
* - toastClass : text - multiple classes separated by spaces
* - action : Object
* - action.actionText : text | html
* - action.actionClass : text - multiple classes separated by spaces
* - closeButton : Boolean : default true
* - closeClickEvent : function : default undefined
* - autoHide : Boolean : default true : true if closeButton is false
* - autoHideTime : integer : default 3000ms
* - unique : unique toast check : for this toastId parameter is must
* - toastId : string : for uniqueness of the toast
*/
var data = {}, html = '', closeFn = '';
options = options || {};
if (options.unique && options.toastId) {
var elements = document.getElementById('toast-container');
if (!elements) {
return;
}
var toast = [], elemChild = elements.children;
for(var i = 0; i < elemChild.length; i++){
if(elemChild[i].getAttribute('data-toastid') === options.toastId) {
toast.push(elemChild[i]);
}
}
if(toast.length !== 0) {
return;
}
}
data.message = (options.message? options.message : 'Toast');
data.toastClass = (options.toastClass? options.toastClass : '');
data.closeButton = (typeof options.closeButton == "boolean"? options.closeButton : true);
data.autoHide = (typeof options.autoHide == "boolean"? options.autoHide : true);
data.autoHideTime = (options.autoHideTime? options.autoHideTime:3000);
if(options.action){
data.action = {};
data.action.actionText = (options.action.actionText? options.action.actionText : '');
data.action.actionClass = (options.action.actionClass? options.action.actionClass : '');
}
html += '<div class="toast '+data.toastClass+'"';
if (options.unique && options.toastId) {
html += 'data-toastid = "'+options.toastId+'"';
}
if (data.autoHide) {
html += 'autoHide = true';
}
html += ' >';
html += '<span class="toast-message">'+data.message+'</span>';
if(options.action){
html += '<span class="toast-action '+data.action.actionClass+'">'+data.action.actionText+'</span>';
}
if(data.closeButton){
html += '<span class="toast-close" ';
if(options.closeClickEvent && typeof options.closeClickEvent === "function"){
html += 'onclick="closeFnEvent(this)" ';
closeFn = '<script> var closeFnEvent = '+options.closeClickEvent+';</script>';
}
html += '>X</span>';
}
else{
data.autoHide = true;
}
html += '</div>';
html += closeFn;
var container = document.getElementById('toast-container');
if(container){
var htmlelem = document.createElement('div');
htmlelem.innerHTML = html;
container.prepend(htmlelem.children[0]);
} else{
var htmlelem = document.createElement('div');
htmlelem.setAttribute('id', 'toast-container');
htmlelem.innerHTML = html;
document.getElementsByTagName('body')[0].append(htmlelem);
}
if(data.autoHide){
setTimeout(function(){
closeToast();
}, data.autoHideTime);
}
}
window.createToast = createToast;
// close toast
function closeToast(ev){
var clickedElem = (ev && ev.target) ? ev.target : this;
var elem = closest.call(clickedElem, 'div.toast');
var toastLength = ((document.getElementById('toast-container') || {}).children || []).length;
if(1 == toastLength){
document.getElementById('toast-container').remove();
}
else{
if(elem){
elem.remove();
}
else{
var elements = (document.getElementById('toast-container') || {}).children;
if (elements) {
for(var i = elements.length - 1; i >= 0; i--) {
if(elements[i].matches('div.toast[autoHide = true]')) {
elements[i].remove();
break;
}
}
}
}
}
}
function closest(elem) {
var parent = this.parentNode;
if (!parent) {
return null;
}
try {
if (parent.matches(elem)) {
return parent;
}
return closest.call(parent, elem);
} catch(e) {
console.log(e);
return null;
}
}
})(window)