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
|
|
|
|
2019-10-18 02:03:05 +08:00
|
|
|
function toast(options) {
|
|
|
|
const $toast = $(`<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
|
|
|
|
<div class="toast-header">
|
2019-11-02 19:17:00 +08:00
|
|
|
<strong class="mr-auto"><span class="bx bx-${options.icon}"></span> ${options.title}</strong>
|
2019-10-18 02:03:05 +08:00
|
|
|
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
|
|
|
|
<span aria-hidden="true">×</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);
|
|
|
|
}
|
|
|
|
|
2019-10-18 02:03:05 +08:00
|
|
|
$("#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) {
|
2019-10-29 03:44:11 +08:00
|
|
|
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) {
|
2019-10-30 01:59:56 +08:00
|
|
|
setTimeout(() => $toast.remove(), options.closeAfter);
|
2019-10-18 02:44:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function closePersistent(id) {
|
2019-10-30 01:59:56 +08:00
|
|
|
$("#toast-" + id).remove();
|
2019-10-18 02:03:05 +08:00
|
|
|
}
|
|
|
|
|
2019-10-20 23:49:58 +08:00
|
|
|
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
|
|
|
|
2019-10-18 02:03:05 +08:00
|
|
|
toast({
|
|
|
|
title: "Info",
|
|
|
|
icon: "check",
|
|
|
|
message: message,
|
2019-10-18 02:44:51 +08:00
|
|
|
autohide: true,
|
2019-10-18 02:03:05 +08:00
|
|
|
delay
|
|
|
|
});
|
2018-03-26 09:29:35 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:31:42 +08:00
|
|
|
function showAndLogError(message, delay = 10000) {
|
|
|
|
showError(message, delay);
|
|
|
|
|
2019-08-27 02:21:43 +08:00
|
|
|
ws.logError(message);
|
2018-08-17 17:31:42 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-10-18 02:03:05 +08:00
|
|
|
toast({
|
|
|
|
title: "Error",
|
|
|
|
icon: 'alert',
|
|
|
|
message: message,
|
2019-10-18 02:44:51 +08:00
|
|
|
autohide: true,
|
2019-10-18 02:03:05 +08:00
|
|
|
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,
|
2018-08-17 17:31:42 +08:00
|
|
|
showAndLogError,
|
2019-10-18 02:44:51 +08:00
|
|
|
throwError,
|
|
|
|
showPersistent,
|
|
|
|
closePersistent
|
2018-03-26 09:29:35 +08:00
|
|
|
}
|