Files
Trilium/apps/client/src/services/toast.ts

126 lines
2.7 KiB
TypeScript
Raw Normal View History

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
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>`
);
2025-01-09 18:07:02 +02:00
$toast.find(".toast-title").text(options.title);
$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) {
$toast.attr("id", `toast-${options.id}`);
2019-10-17 20:44:51 +02:00
}
$("#toast-container").append($toast);
$toast.toast({
2019-10-17 20:44:51 +02:00
delay: options.delay || 3000,
autohide: !!options.autohide
});
2025-01-09 18:07:02 +02:00
$toast.on("hidden.bs.toast", (e) => e.target.remove());
2019-10-17 20:44:51 +02:00
$toast.toast("show");
return $toast;
}
2024-07-25 00:26:27 +03:00
function showPersistent(options: ToastOptions) {
let $toast = $(`#toast-${options.id}`);
2019-10-17 20:44:51 +02:00
if ($toast.length > 0) {
2025-01-09 18:07:02 +02:00
$toast.find(".toast-body").html(options.message);
} else {
2019-10-17 20:44:51 +02:00
options.autohide = false;
$toast = toast(options);
}
if (options.closeAfter) {
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) {
$(`#toast-${id}`).remove();
}
2024-07-25 00:26:27 +03:00
function showMessage(message: string, delay = 2000) {
console.debug(utils.now(), "message:", message);
2018-03-25 21:29:35 -04:00
toast({
title: "Info",
icon: "check",
message: message,
2019-10-17 20:44:51 +02:00
autohide: true,
delay
});
2018-03-25 21:29:35 -04:00
}
2024-07-25 00:26:27 +03:00
function showAndLogError(message: string, delay = 10000) {
showError(message, delay);
2019-08-26 20:21:43 +02:00
ws.logError(message);
}
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
toast({
title: "Error",
2025-01-09 18:07:02 +02:00
icon: "alert",
message: message,
2019-10-17 20:44:51 +02:00
autohide: true,
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,
2025-01-09 18:07:02 +02:00
icon: "alert",
2022-12-22 14:57:00 +01:00
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,
showAndLogError,
2019-10-17 20:44:51 +02:00
throwError,
showPersistent,
closePersistent
2025-01-09 18:07:02 +02:00
};