trilium/src/public/javascripts/services/toast.js

99 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-08-27 02:21:43 +08:00
import ws from "./ws.js";
2018-03-26 10:37:02 +08:00
import utils from "./utils.js";
2018-03-26 09:29:35 +08:00
function toast(options) {
const $toast = $(`<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="mr-auto"><span class="jam jam-${options.icon}"></span> ${options.title}</strong>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="toast-body">
${options.message}
</div>
</div>`);
2019-10-18 02:44:51 +08:00
if (options.id) {
$toast.attr("id", "toast-" + options.id);
}
$("#toast-container").append($toast);
$toast.toast({
2019-10-18 02:44:51 +08: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-18 02:44:51 +08:00
if ($toast.length > 0) {
$toast.find('.toast-body').html(options.message);
}
else {
options.autohide = false;
$toast = toast(options);
}
if (options.closeAfter) {
setTimeout(() => $toast.toast('dispose'), options.closeAfter);
}
}
function closePersistent(id) {
$("#toast-" + id).toast("dispose");
}
function showMessage(message, delay = 2000) {
2018-10-30 15:53:30 +08:00
console.debug(utils.now(), "message: ", message);
2018-03-26 09:29:35 +08:00
toast({
title: "Info",
icon: "check",
message: message,
2019-10-18 02:44:51 +08:00
autohide: true,
delay
});
2018-03-26 09:29:35 +08:00
}
function showAndLogError(message, delay = 10000) {
showError(message, delay);
2019-08-27 02:21:43 +08:00
ws.logError(message);
}
2018-03-26 09:29:35 +08:00
function showError(message, delay = 10000) {
2018-03-26 10:37:02 +08:00
console.log(utils.now(), "error: ", message);
2018-03-26 09:29:35 +08:00
toast({
title: "Error",
icon: 'alert',
message: message,
2019-10-18 02:44:51 +08:00
autohide: true,
delay
});
2018-03-26 09:29:35 +08:00
}
function throwError(message) {
2019-08-27 02:21:43 +08:00
ws.logError(message);
2018-03-26 09:29:35 +08:00
throw new Error(message);
}
export default {
showMessage,
showError,
showAndLogError,
2019-10-18 02:44:51 +08:00
throwError,
showPersistent,
closePersistent
2018-03-26 09:29:35 +08:00
}