2025-06-19 22:44:02 +03:00
|
|
|
import { Modal } from "bootstrap";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../components/app_context.js";
|
2025-03-20 19:54:09 +02:00
|
|
|
import type { ConfirmDialogOptions, ConfirmDialogResult, ConfirmWithMessageOptions } from "../widgets/dialogs/confirm.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { PromptDialogOptions } from "../widgets/dialogs/prompt.js";
|
2025-06-19 22:44:02 +03:00
|
|
|
import { focusSavedElement, saveFocusedElement } from "./focus.js";
|
|
|
|
|
|
|
|
export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog = true) {
|
|
|
|
if (closeActDialog) {
|
|
|
|
closeActiveDialog();
|
|
|
|
glob.activeDialog = $dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveFocusedElement();
|
|
|
|
Modal.getOrCreateInstance($dialog[0]).show();
|
|
|
|
|
|
|
|
$dialog.on("hidden.bs.modal", () => {
|
|
|
|
const $autocompleteEl = $(".aa-input");
|
|
|
|
if ("autocomplete" in $autocompleteEl) {
|
|
|
|
$autocompleteEl.autocomplete("close");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!glob.activeDialog || glob.activeDialog === $dialog) {
|
|
|
|
focusSavedElement();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const keyboardActionsService = (await import("./keyboard_actions.js")).default;
|
|
|
|
keyboardActionsService.updateDisplayedShortcuts($dialog);
|
|
|
|
|
|
|
|
return $dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function closeActiveDialog() {
|
|
|
|
if (glob.activeDialog) {
|
|
|
|
Modal.getOrCreateInstance(glob.activeDialog[0]).hide();
|
|
|
|
glob.activeDialog = null;
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 19:53:33 +02:00
|
|
|
|
2024-12-21 17:39:14 +02:00
|
|
|
async function info(message: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
return new Promise((res) => appContext.triggerCommand("showInfoDialog", { message, callback: res }));
|
2022-06-16 19:53:33 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 17:39:14 +02:00
|
|
|
async function confirm(message: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
return new Promise((res) =>
|
2024-12-21 17:39:14 +02:00
|
|
|
appContext.triggerCommand("showConfirmDialog", <ConfirmWithMessageOptions>{
|
2022-06-16 21:30:05 +02:00
|
|
|
message,
|
2024-12-21 17:39:14 +02:00
|
|
|
callback: (x: false | ConfirmDialogOptions) => res(x && x.confirmed)
|
2025-01-09 18:07:02 +02:00
|
|
|
})
|
|
|
|
);
|
2022-06-16 21:30:05 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 17:39:14 +02:00
|
|
|
async function confirmDeleteNoteBoxWithNote(title: string) {
|
2025-03-20 19:54:09 +02:00
|
|
|
return new Promise<ConfirmDialogResult | undefined>((res) => appContext.triggerCommand("showConfirmDeleteNoteBoxWithNoteDialog", { title, callback: res }));
|
2022-06-16 20:19:26 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 17:39:14 +02:00
|
|
|
async function prompt(props: PromptDialogOptions) {
|
2025-01-11 11:40:22 +02:00
|
|
|
return new Promise<string | null>((res) => appContext.triggerCommand("showPromptDialog", { ...props, callback: res }));
|
2022-06-16 21:13:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:53:33 +02:00
|
|
|
export default {
|
2022-06-16 20:19:26 +02:00
|
|
|
info,
|
2022-06-16 21:13:09 +02:00
|
|
|
confirm,
|
2022-06-16 21:30:05 +02:00
|
|
|
confirmDeleteNoteBoxWithNote,
|
2022-06-16 21:13:09 +02:00
|
|
|
prompt
|
2022-06-16 19:53:33 +02:00
|
|
|
};
|