Files
Trilium/src/public/app/services/toast.js

114 lines
2.4 KiB
JavaScript
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
function toast(options) {
const $toast = $(`<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
2022-07-05 22:40:41 +02:00
<strong class="mr-auto"><span class="bx bx-${options.icon}"></span> <span class="toast-title"></span></strong>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
2022-07-05 22:40:41 +02:00
<div class="toast-body"></div>
</div>`);
2022-07-05 22:40:41 +02:00
$toast.find('.toast-title').text(options.title);
$toast.find('.toast-body').text(options.message);
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
});
$toast.on('hidden.bs.toast', e => e.target.remove());
$toast.toast("show");
return $toast;
}
function showPersistent(options) {
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) {
setTimeout(() => $toast.remove(), options.closeAfter);
2019-10-17 20:44:51 +02:00
}
}
function closePersistent(id) {
$(`#toast-${id}`).remove();
}
function showMessage(message, 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
}
function showAndLogError(message, delay = 10000) {
showError(message, delay);
2019-08-26 20:21:43 +02:00
ws.logError(message);
}
2018-03-25 21:29:35 -04:00
function showError(message, 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",
icon: 'alert',
message: message,
2019-10-17 20:44:51 +02:00
autohide: true,
delay
});
2018-03-25 21:29:35 -04:00
}
2022-12-22 14:57:00 +01:00
function showErrorTitleAndMessage(title, message, delay = 10000) {
console.log(utils.now(), "error: ", message);
toast({
title: title,
icon: 'alert',
message: message,
autohide: true,
delay
});
}
2018-03-25 21:29:35 -04:00
function throwError(message) {
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
2020-08-06 00:06:42 +02:00
}