-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtoast.js
More file actions
141 lines (119 loc) · 5.32 KB
/
toast.js
File metadata and controls
141 lines (119 loc) · 5.32 KB
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
/**
* @author Script47 (https://github.com/Script47/Toast)
* @description Toast - A Bootstrap 4.2+ jQuery plugin for the toast component
* @version 0.7.1
**/
(function ($) {
var TOAST_CONTAINER_HTML = '<div class="toast-container" aria-live="polite" aria-atomic="true"></div>';
var TOAST_WRAPPER_HTML = '<div class="toast-wrapper"></div>';
$.toast = function (opts) {
// If container is not set in opts use body
var general_container = $('body');
if (opts.container && opts.container.length === 1)
general_container = opts.container;
// if toast container and wrapper are not present in container create them
if (!general_container.children('.toast-container').length) {
general_container.prepend(TOAST_CONTAINER_HTML);
general_container.children('.toast-container').append(TOAST_WRAPPER_HTML);
general_container.on('hidden.bs.toast', '.toast', function () {
$(this).remove();
});
}
var toast_wrapper = general_container.children('.toast-container').children('.toast-wrapper');
var id = 'toast-' + ($('.toast').length + 1),
html = '',
bg_header_class = '',
fg_header_class = '',
fg_subtitle_class = 'text-muted',
fg_dismiss_class = '',
title = opts.title || 'Notice!',
subtitle = opts.subtitle || '',
content = opts.content || '',
type = opts.type || 'info',
delay = opts.delay || -1,
img = opts.img,
icon = opts.icon,
pause_on_hover = opts.pause_on_hover || false,
pause = false,
delay_or_autohide = '';
switch (type) {
case 'info':
bg_header_class = 'bg-info';
fg_header_class = 'text-white';
fg_subtitle_class = 'text-white';
fg_dismiss_class = 'text-white';
break;
case 'success':
bg_header_class = 'bg-success';
fg_header_class = 'text-white';
fg_subtitle_class = 'text-white';
fg_dismiss_class = 'text-white';
break;
case 'warning':
case 'warn':
bg_header_class = 'bg-warning';
fg_header_class = 'text-white';
fg_subtitle_class = 'text-white';
fg_dismiss_class = 'text-white';
break;
case 'error':
case 'danger':
bg_header_class = 'bg-danger';
fg_header_class = 'text-white';
fg_subtitle_class = 'text-white';
fg_dismiss_class = 'text-white';
break;
}
if (pause_on_hover !== false) {
var hide_timestamp = Math.floor(Date.now() / 1000) + (delay / 1000);
delay_or_autohide = 'data-autohide="false"';
pause_on_hover = 'data-hide-timestamp="' + hide_timestamp + '"';
} else {
if (delay === -1) {
delay_or_autohide = 'data-autohide="false"';
} else {
delay_or_autohide = 'data-delay="' + delay + '"';
}
}
html = '<div id="' + id + '" class="toast" role="alert" aria-live="assertive" aria-atomic="true" ' + delay_or_autohide + ' ' + pause_on_hover + '>';
html += '<div class="toast-header ' + bg_header_class + ' ' + fg_header_class + '">';
if (typeof img !== 'undefined') {
html += '<img src="' + img.src + '" class="' + (img.class || '') + ' mr-2" alt="' + (img.alt || 'Image') + '" ' + (typeof img.title !== 'undefined' ? 'data-toggle="tooltip" title="' + img.title + '"' : '') + '>';
}
if (typeof icon !== 'undefined') {
html += '<i class="' + (icon || '') + '" aria-hidden="true"></i>';
}
html += '<strong class="mr-auto">' + title + '</strong>';
html += '<small class="' + fg_subtitle_class + '">' + subtitle + '</small>';
html += '<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">';
html += '<span aria-hidden="true" class="' + fg_dismiss_class + '">×</span>';
html += '</button>';
html += '</div>';
if (content !== '') {
html += '<div class="toast-body">'
html += content
html += '</div>';
}
html += '</div>';
toast_wrapper.append(html);
toast_wrapper.find('.toast:last').toast('show');
if (pause_on_hover !== false) {
setTimeout(function () {
if (!pause) {
$('#' + id).toast('hide');
}
}, delay);
$(document).on('mouseover', '#' + id, function () {
pause = true;
});
$(document).on('mouseleave', '#' + id, function () {
var current = Math.floor(Date.now() / 1000),
future = parseInt($(this).data('hide-timestamp'));
pause = false;
if (current >= future) {
$(this).toast('hide');
}
});
}
}
}(jQuery));