-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
alerts.js
176 lines (146 loc) · 4.07 KB
/
alerts.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
import * as components from './components';
import * as hooks from './hooks';
export function alert(params) {
params.alert_id = 'alert_button_' + (params.alert_id ? params.alert_id : new Date().getTime());
params.title = params.title ? params.title.trim() || '' : '';
params.message = params.message ? params.message.trim() : '';
params.type = params.type || 'info';
const alert = $('#' + params.alert_id);
if (alert.length) {
updateAlert(alert, params);
} else {
createNew(params);
}
}
export function success(message, timeout) {
alert({
alert_id: utils.generateUUID(),
title: '[[global:alert.success]]',
message: message,
type: 'success',
timeout: timeout !== undefined ? timeout : 5000,
});
}
export function info(message, timeout) {
alert({
alert_id: utils.generateUUID(),
title: '[[global:alert.info]]',
message: message,
type: 'info',
timeout: timeout !== undefined ? timeout : 5000,
});
}
export function warning(message, timeout) {
alert({
alert_id: utils.generateUUID(),
title: '[[global:alert.warning]]',
message: message,
type: 'warning',
timeout: timeout !== undefined ? timeout : 5000,
});
}
export function error(message, timeout) {
message = (message && message.message) || message;
if (message === '[[error:revalidate-failure]]') {
socket.disconnect();
app.reconnect();
return;
}
alert({
alert_id: utils.generateUUID(),
title: '[[global:alert.error]]',
message: message,
type: 'danger',
timeout: timeout || 10000,
});
}
export function remove(id) {
$('#alert_button_' + id).remove();
}
function updateAlert(alert, params) {
alert.find('strong').translateHtml(params.title);
alert.find('p').translateHtml(params.message);
alert.removeClass('alert-success alert-danger alert-info alert-warning')
.addClass(`alert-${params.type}`);
clearTimeout(parseInt(alert.attr('timeoutId'), 10));
if (params.timeout) {
startTimeout(alert, params);
}
hooks.fire('action:alert.update', { alert, params });
// Handle changes in the clickfn
alert.off('click').removeClass('pointer');
if (typeof params.clickfn === 'function') {
alert
.addClass('pointer')
.on('click', function (e) {
if (!$(e.target).is('.btn-close')) {
params.clickfn();
close(alert);
}
});
}
}
function createNew(params) {
app.parseAndTranslate('partials/toast', params, function (html) {
let alert = $('#' + params.alert_id);
if (alert.length) {
return updateAlert(alert, params);
}
alert = html;
alert.hide().fadeIn(200).prependTo(components.get('toaster/tray'));
alert.on('close.bs.alert', function () {
if (typeof params.closefn === 'function') {
params.closefn();
}
const timeoutId = alert.attr('timeoutId');
if (timeoutId) {
clearTimeout(timeoutId);
alert.removeAttr('timeoutId');
}
});
if (parseInt(params.timeout, 10)) {
startTimeout(alert, params);
}
if (typeof params.clickfn === 'function') {
alert
.addClass('pointer')
.on('click', function (e) {
if (!$(e.target).is('.btn-close')) {
params.clickfn(alert, params);
close(alert);
}
});
}
hooks.fire('action:alert.new', { alert, params });
});
}
function close(alert) {
alert.alert('close');
}
function startTimeout(alert, params) {
const timeout = parseInt(params.timeout, 10);
const timeoutId = setTimeout(function () {
alert.removeAttr('timeoutId');
close(alert);
if (typeof params.timeoutfn === 'function') {
params.timeoutfn(alert, params);
}
}, timeout);
alert.attr('timeoutId', timeoutId);
// Reset and start animation
const alertProgress = alert.find('.alert-progress');
alertProgress.css('transition-property', 'none');
alertProgress.removeClass('animate');
setTimeout(function () {
alertProgress.css('transition-property', '');
alertProgress.css('transition', 'width ' + (timeout + 450) + 'ms linear');
alertProgress.addClass('animate');
hooks.fire('action:alert.animate', { alert, alertProgress, params });
}, 50);
// Handle mouseenter/mouseleave
alert
.on('mouseenter', function () {
alertProgress.css('transition-duration', 0);
});
}