2019-08-26 20:21:43 +02:00
|
|
|
import ws from "./ws.js";
|
2018-03-25 22:37:02 -04:00
|
|
|
import utils from "./utils.js";
|
2018-03-25 21:29:35 -04:00
|
|
|
|
2024-12-19 22:25:48 +02:00
|
|
|
export interface ToastOptions {
|
2024-07-25 00:26:27 +03:00
|
|
|
id?: string;
|
|
|
|
|
icon: string;
|
|
|
|
|
title: string;
|
|
|
|
|
message: string;
|
|
|
|
|
delay?: number;
|
|
|
|
|
autohide?: boolean;
|
|
|
|
|
closeAfter?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toast(options: ToastOptions) {
|
2024-09-02 19:37:02 +02:00
|
|
|
const $toast = $(
|
|
|
|
|
`<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
|
|
|
<div class="toast-header">
|
|
|
|
|
<strong class="me-auto">
|
|
|
|
|
<span class="bx bx-${options.icon}"></span>
|
|
|
|
|
<span class="toast-title"></span>
|
|
|
|
|
</strong>
|
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="toast-body"></div>
|
|
|
|
|
</div>`
|
|
|
|
|
);
|
2019-10-17 20:03:05 +02:00
|
|
|
|
2022-07-05 22:40:41 +02:00
|
|
|
$toast.find('.toast-title').text(options.title);
|
2024-10-25 20:51:50 +03:00
|
|
|
$toast.find('.toast-body').html(options.message);
|
2022-07-05 22:40:41 +02:00
|
|
|
|
2019-10-17 20:44:51 +02:00
|
|
|
if (options.id) {
|
2022-12-21 15:19:05 +01:00
|
|
|
$toast.attr("id", `toast-${options.id}`);
|
2019-10-17 20:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-17 20:03:05 +02:00
|
|
|
$("#toast-container").append($toast);
|
|
|
|
|
|
|
|
|
|
$toast.toast({
|
2019-10-17 20:44:51 +02:00
|
|
|
delay: options.delay || 3000,
|
|
|
|
|
autohide: !!options.autohide
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$toast.on('hidden.bs.toast', e => e.target.remove());
|
|
|
|
|
|
|
|
|
|
$toast.toast("show");
|
|
|
|
|
|
|
|
|
|
return $toast;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function showPersistent(options: ToastOptions) {
|
2022-12-21 15:19:05 +01:00
|
|
|
let $toast = $(`#toast-${options.id}`);
|
2019-10-17 20:44:51 +02:00
|
|
|
|
|
|
|
|
if ($toast.length > 0) {
|
|
|
|
|
$toast.find('.toast-body').html(options.message);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
options.autohide = false;
|
|
|
|
|
|
|
|
|
|
$toast = toast(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.closeAfter) {
|
2019-10-29 18:59:56 +01:00
|
|
|
setTimeout(() => $toast.remove(), options.closeAfter);
|
2019-10-17 20:44:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function closePersistent(id: string) {
|
2022-12-21 15:19:05 +01:00
|
|
|
$(`#toast-${id}`).remove();
|
2019-10-17 20:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function showMessage(message: string, delay = 2000) {
|
2020-10-29 22:41:33 +01:00
|
|
|
console.debug(utils.now(), "message:", message);
|
2018-03-25 21:29:35 -04:00
|
|
|
|
2019-10-17 20:03:05 +02:00
|
|
|
toast({
|
|
|
|
|
title: "Info",
|
|
|
|
|
icon: "check",
|
|
|
|
|
message: message,
|
2019-10-17 20:44:51 +02:00
|
|
|
autohide: true,
|
2019-10-17 20:03:05 +02:00
|
|
|
delay
|
|
|
|
|
});
|
2018-03-25 21:29:35 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function showAndLogError(message: string, delay = 10000) {
|
2018-08-17 11:31:42 +02:00
|
|
|
showError(message, delay);
|
|
|
|
|
|
2019-08-26 20:21:43 +02:00
|
|
|
ws.logError(message);
|
2018-08-17 11:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function showError(message: string, delay = 10000) {
|
2018-03-25 22:37:02 -04:00
|
|
|
console.log(utils.now(), "error: ", message);
|
2018-03-25 21:29:35 -04:00
|
|
|
|
2019-10-17 20:03:05 +02:00
|
|
|
toast({
|
|
|
|
|
title: "Error",
|
|
|
|
|
icon: 'alert',
|
|
|
|
|
message: message,
|
2019-10-17 20:44:51 +02:00
|
|
|
autohide: true,
|
2019-10-17 20:03:05 +02:00
|
|
|
delay
|
|
|
|
|
});
|
2018-03-25 21:29:35 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function showErrorTitleAndMessage(title: string, message: string, delay = 10000) {
|
2022-12-22 14:57:00 +01:00
|
|
|
console.log(utils.now(), "error: ", message);
|
|
|
|
|
|
|
|
|
|
toast({
|
|
|
|
|
title: title,
|
|
|
|
|
icon: 'alert',
|
|
|
|
|
message: message,
|
|
|
|
|
autohide: true,
|
|
|
|
|
delay
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 00:26:27 +03:00
|
|
|
function throwError(message: string) {
|
2019-08-26 20:21:43 +02:00
|
|
|
ws.logError(message);
|
2018-03-25 21:29:35 -04:00
|
|
|
|
|
|
|
|
throw new Error(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
showMessage,
|
|
|
|
|
showError,
|
2022-12-22 14:57:00 +01:00
|
|
|
showErrorTitleAndMessage,
|
2018-08-17 11:31:42 +02:00
|
|
|
showAndLogError,
|
2019-10-17 20:44:51 +02:00
|
|
|
throwError,
|
|
|
|
|
showPersistent,
|
|
|
|
|
closePersistent
|
2020-08-06 00:06:42 +02:00
|
|
|
}
|