Skip to content

Commit 02696f2

Browse files
committed
Remove code to update existing toasts. It made the code very complex and was used only in two places
Signed-off-by: yubiuser <github@yubiuser.dev>
1 parent 03e34c5 commit 02696f2

4 files changed

Lines changed: 25 additions & 138 deletions

File tree

scripts/js/groups-common.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ function processGroupResult(data, type, done, notDone) {
7272

7373
// Loop over data.processed.success and show toasts
7474
for (const item of data.processed.success) {
75-
utils.showAlert("success", "fas fa-pencil-alt", `Successfully ${done} ${type}`, itemLabel(item));
75+
utils.showAlert(
76+
"success",
77+
"fas fa-pencil-alt",
78+
`Successfully ${done} ${type}`,
79+
itemLabel(item)
80+
);
7681
}
7782

7883
// Loop over errors and display them

scripts/js/messages.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"use strict";
1111

1212
let table;
13-
const toasts = {};
1413

1514
$(() => {
1615
const url = document.body.dataset.apiurl + "/info/messages";
@@ -151,7 +150,7 @@ function deleteMessage() {
151150
function delMsg(id) {
152151
id = Math.trunc(Number(id));
153152
utils.disableAll();
154-
toasts[id] = utils.showAlert("info", "", "Deleting message...", "ID: " + id, null);
153+
utils.showAlert("info", "", "Deleting message...", "ID: " + id);
155154

156155
$.ajax({
157156
url: document.body.dataset.apiurl + "/info/messages/" + id,
@@ -160,24 +159,12 @@ function delMsg(id) {
160159
.done(response => {
161160
utils.enableAll();
162161
if (response === undefined) {
163-
utils.showAlert(
164-
"success",
165-
"far fa-trash-alt",
166-
"Successfully deleted message",
167-
"ID: " + id,
168-
toasts[id]
169-
);
162+
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted message", "ID: " + id);
170163
table.row(id).remove();
171164

172165
table.draw(false).ajax.reload(null, false);
173166
} else {
174-
utils.showAlert(
175-
"error",
176-
"",
177-
"Error while deleting message: " + id,
178-
response.message,
179-
toasts[id]
180-
);
167+
utils.showAlert("error", "", "Error while deleting message: " + id, response.message);
181168
}
182169

183170
// Clear selection after deletion
@@ -189,13 +176,7 @@ function delMsg(id) {
189176
)
190177
.fail((jqXHR, exception) => {
191178
utils.enableAll();
192-
utils.showAlert(
193-
"error",
194-
"",
195-
"Error while deleting message: " + id,
196-
jqXHR.responseText,
197-
toasts[id]
198-
);
179+
utils.showAlert("error", "", "Error while deleting message: " + id, jqXHR.responseText);
199180
console.log(exception); // eslint-disable-line no-console
200181
});
201182
}

scripts/js/settings-dhcp.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"use strict";
1111

1212
let dhcpLeaesTable = null;
13-
const toasts = {};
1413

1514
// DHCP leases tooltips
1615
// Bootstrap 5 has no jQuery-plugin tooltip delegation, so lazily instantiate
@@ -184,7 +183,7 @@ function deleteLease() {
184183

185184
function delLease(ip) {
186185
utils.disableAll();
187-
toasts[ip] = utils.showAlert("info", "", "Deleting lease...", ip, null);
186+
utils.showAlert("info", "", "Deleting lease...", ip);
188187

189188
$.ajax({
190189
url: document.body.dataset.apiurl + "/dhcp/leases/" + encodeURIComponent(ip),
@@ -193,22 +192,10 @@ function delLease(ip) {
193192
.done(response => {
194193
utils.enableAll();
195194
if (response === undefined) {
196-
utils.showAlert(
197-
"success",
198-
"far fa-trash-alt",
199-
"Successfully deleted lease",
200-
ip,
201-
toasts[ip]
202-
);
195+
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted lease", ip);
203196
dhcpLeaesTable.ajax.reload(null, false);
204197
} else {
205-
utils.showAlert(
206-
"error",
207-
"",
208-
"Error while deleting lease: " + ip,
209-
response.lease,
210-
toasts[ip]
211-
);
198+
utils.showAlert("error", "", "Error while deleting lease: " + ip, response.lease);
212199
}
213200

214201
// Clear selection after deletion
@@ -217,13 +204,7 @@ function delLease(ip) {
217204
})
218205
.fail((jqXHR, exception) => {
219206
utils.enableAll();
220-
utils.showAlert(
221-
"error",
222-
"",
223-
"Error while deleting lease: " + ip,
224-
jqXHR.responseText,
225-
toasts[ip]
226-
);
207+
utils.showAlert("error", "", "Error while deleting lease: " + ip, jqXHR.responseText);
227208
console.log(exception); // eslint-disable-line no-console
228209
});
229210
}

scripts/js/utils.js

Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ function padNumber(num) {
102102
return ("00" + num).substr(-2, 2);
103103
}
104104

105-
let showAlertBox = null;
106-
107105
// Gets ToastContainer if it exists, otherwise creates a new one and return it
108106
function getOrCreateToastContainer() {
109107
const existing =
@@ -127,6 +125,8 @@ function createToast(alertState) {
127125
const toast = document.createElement("div");
128126
toast.className = "toast align-items-center border-0 shadow rounded overflow-hidden";
129127
toast.dataset.toastType = alertState.type;
128+
toast.dataset.bsAutohide = String(alertState.autohide);
129+
toast.dataset.bsDelay = String(alertState.delay);
130130
toast.setAttribute("role", alertState.role);
131131
toast.setAttribute("aria-live", alertState.live);
132132
toast.setAttribute("aria-atomic", alertState.ariaAtomic);
@@ -182,7 +182,7 @@ function createToast(alertState) {
182182
return toast;
183183
}
184184

185-
function showAlert(type, icon, title, message, toast) {
185+
function showAlert(type, icon, title, message) {
186186
// sets all properties of the alertState object based on the type of alert and the provided parameters
187187
const alertState = {
188188
title: "&nbsp;<strong>" + escapeHtml(title) + "</strong><br>",
@@ -249,97 +249,17 @@ function showAlert(type, icon, title, message, toast) {
249249
}
250250

251251
const container = getOrCreateToastContainer();
252+
const toastElement = createToast(alertState);
253+
const toastInstance = new bootstrap.Toast(toastElement);
252254

253-
const toastController = {
254-
element: createToast(alertState),
255-
instance: null,
256-
alertState,
257-
update(partialState) {
258-
this.alertState = { ...this.alertState, ...partialState };
259-
this.render();
260-
this.show();
261-
return this;
262-
},
263-
render() {
264-
const renderedToast = createToast(this.alertState);
265-
266-
this.element.className = renderedToast.className;
267-
this.element.dataset.toastType = renderedToast.dataset.toastType;
268-
this.element.setAttribute("role", renderedToast.getAttribute("role"));
269-
this.element.setAttribute("aria-live", renderedToast.getAttribute("aria-live"));
270-
this.element.setAttribute("aria-atomic", renderedToast.getAttribute("aria-atomic"));
271-
this.element.replaceChildren(...renderedToast.childNodes);
272-
},
273-
ensureInstance() {
274-
if (this.instance === null) {
275-
this.instance = new bootstrap.Toast(this.element, {
276-
autohide: this.alertState.autohide,
277-
delay: this.alertState.delay,
278-
});
279-
}
280-
281-
return this.instance;
282-
},
283-
show() {
284-
if (!this.element.isConnected) {
285-
container.prepend(this.element);
286-
}
287-
288-
this.ensureInstance().show();
289-
return this;
290-
},
291-
hide() {
292-
if (this.instance !== null) {
293-
this.instance.hide();
294-
}
295-
296-
return this;
297-
},
298-
dispose() {
299-
if (this.instance !== null) {
300-
this.instance.dispose();
301-
this.instance = null;
302-
}
303-
304-
this.element.remove();
305-
if (showAlertBox === this) {
306-
showAlertBox = null;
307-
}
308-
309-
return this;
310-
},
311-
};
312-
313-
toastController.element.addEventListener("hidden.bs.toast", () => {
314-
toastController.element.remove();
315-
if (showAlertBox === toastController) {
316-
showAlertBox = null;
317-
}
255+
toastElement.addEventListener("hidden.bs.toast", () => {
256+
toastElement.remove();
257+
toastInstance.dispose();
318258
});
319259

320-
if (toast === null) {
321-
// Always create a new toast
322-
return toastController.show();
323-
}
324-
325-
if (toast !== undefined) {
326-
// Update existing toast
327-
return toast.update(alertState);
328-
}
329-
330-
if (type === "info") {
331-
// Create a new notification for info boxes
332-
showAlertBox = toastController;
333-
return toastController.show();
334-
}
335-
336-
if (showAlertBox !== null) {
337-
// Update existing notification for other boxes (if available)
338-
return showAlertBox.update(alertState);
339-
}
340-
341-
// Create a new notification for other boxes if no previous info box exists
342-
return toastController.show();
260+
container.prepend(toastElement);
261+
toastInstance.show();
262+
return toastInstance;
343263
}
344264

345265
function datetime(date, html, humanReadable) {

0 commit comments

Comments
 (0)