-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrispy-toast.js
88 lines (70 loc) · 2.38 KB
/
crispy-toast.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
// crispy-toast.js
var CrispyToast = {};
var Timer;
CrispyToast.toasts = [];
CrispyToast.createToast = function(message, options) {
options = options || {};
var toast = {
message: message,
type: options.type || 'message',
position: options.position || 'top-right',
timeout: options.timeout || 1000
};
CrispyToast.toasts.push(toast);
CrispyToast.renderToast(toast);
};
CrispyToast.clearall = function() {
if (Timer !=null) {
return true;
}
var toastElements = document.querySelectorAll('.crispy-toast');
if (toastElements.length > 0) {
console.log('clear');
toastElements.forEach(function(element) {
document.body.removeChild(element);
});
}
}
CrispyToast.renderToast = function(toast) {
var toastContainer = document.createElement('div');
toastContainer.className = 'crispy-toast ' + toast.type + ' ' + toast.position;
// Create an element for the message
var messageElement = document.createElement('span');
messageElement.className = 'toast-message';
messageElement.textContent = toast.message;
toastContainer.appendChild(messageElement);
document.body.appendChild(toastContainer);
Timer = setTimeout(function() {
CrispyToast.removeToast(toast);
Timer=null;
}, toast.timeout);
};
CrispyToast.removeToast = function(toast) {
var index = CrispyToast.toasts.indexOf(toast);
if (index !== -1) {
CrispyToast.toasts.splice(index, 1);
}
var toastElements = document.querySelectorAll('.crispy-toast.' + toast.position);
if (toastElements.length > 0) {
toastElements.forEach(function(element) {
document.body.removeChild(element);
});
}
};
CrispyToast.message = function(message, options) {
CrispyToast.createToast(message, Object.assign({}, options, { type: 'message' }));
};
CrispyToast.success = function(message, options) {
CrispyToast.createToast(message, Object.assign({}, options, { type: 'success' }));
};
CrispyToast.info = function(message, options) {
CrispyToast.createToast(message, Object.assign({}, options, { type: 'info' }));
};
CrispyToast.warning = function(message, options) {
CrispyToast.createToast(message, Object.assign({}, options, { type: 'warning' }));
};
CrispyToast.error = function(message, options) {
CrispyToast.createToast(message, Object.assign({}, options, { type: 'error' }));
};
// Attach CrispyToast to the global object (window)
window.CrispyToast = CrispyToast;