2022-06-16 20:19:26 +02:00
|
|
|
import BasicWidget from "../basic_widget.js";
|
2024-07-23 17:38:22 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2025-02-12 08:38:55 +01:00
|
|
|
import { Modal } from "bootstrap";
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
const DELETE_NOTE_BUTTON_CLASS = "confirm-dialog-delete-note";
|
|
|
|
|
|
|
|
const TPL = `
|
2022-09-18 13:52:19 +02:00
|
|
|
<div class="confirm-dialog modal mx-auto" tabindex="-1" role="dialog" style="z-index: 2000;">
|
2022-06-16 20:19:26 +02:00
|
|
|
<div class="modal-dialog modal-dialog-scrollable" role="document">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2025-01-09 18:07:02 +02:00
|
|
|
<h5 class="modal-title">${t("confirm.confirmation")}</h5>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("confirm.close")}"></button>
|
2022-06-16 20:19:26 +02:00
|
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
|
|
<div class="confirm-dialog-content"></div>
|
|
|
|
|
|
|
|
<div class="confirm-dialog-custom"></div>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
2025-01-09 18:07:02 +02:00
|
|
|
<button class="confirm-dialog-cancel-button btn btn-sm">${t("confirm.cancel")}</button>
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
<button class="confirm-dialog-ok-button btn btn-primary btn-sm">${t("confirm.ok")}</button>
|
2022-06-16 20:19:26 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
2024-12-21 17:39:14 +02:00
|
|
|
type ConfirmDialogCallback = (val: false | ConfirmDialogOptions) => void;
|
|
|
|
|
|
|
|
export interface ConfirmDialogOptions {
|
2024-12-21 17:32:50 +02:00
|
|
|
confirmed: boolean;
|
2025-01-09 18:07:02 +02:00
|
|
|
isDeleteNoteChecked: boolean;
|
2024-12-21 17:39:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For "showConfirmDialog"
|
2024-12-21 17:32:50 +02:00
|
|
|
|
2024-12-21 17:39:14 +02:00
|
|
|
export interface ConfirmWithMessageOptions {
|
2024-12-21 17:32:50 +02:00
|
|
|
message: string | HTMLElement | JQuery<HTMLElement>;
|
|
|
|
callback: ConfirmDialogCallback;
|
|
|
|
}
|
|
|
|
|
2024-12-21 23:47:18 +02:00
|
|
|
export interface ConfirmWithTitleOptions {
|
2024-12-21 17:32:50 +02:00
|
|
|
title: string;
|
2025-01-09 18:07:02 +02:00
|
|
|
callback: ConfirmDialogCallback;
|
2024-12-21 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 20:19:26 +02:00
|
|
|
export default class ConfirmDialog extends BasicWidget {
|
2024-12-21 17:32:50 +02:00
|
|
|
private resolve: ConfirmDialogCallback | null;
|
|
|
|
|
2025-02-12 08:38:55 +01:00
|
|
|
private modal!: Modal;
|
2024-12-21 17:32:50 +02:00
|
|
|
private $originallyFocused!: JQuery<HTMLElement> | null;
|
|
|
|
private $confirmContent!: JQuery<HTMLElement>;
|
|
|
|
private $okButton!: JQuery<HTMLElement>;
|
|
|
|
private $cancelButton!: JQuery<HTMLElement>;
|
|
|
|
private $custom!: JQuery<HTMLElement>;
|
|
|
|
|
2022-06-16 20:19:26 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.resolve = null;
|
2023-05-05 23:17:23 +02:00
|
|
|
this.$originallyFocused = null; // element focused before the dialog was opened, so we can return to it afterward
|
2022-06-16 20:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
2025-02-12 08:38:55 +01:00
|
|
|
this.modal = Modal.getOrCreateInstance(this.$widget[0]);
|
2022-06-16 20:19:26 +02:00
|
|
|
this.$confirmContent = this.$widget.find(".confirm-dialog-content");
|
|
|
|
this.$okButton = this.$widget.find(".confirm-dialog-ok-button");
|
|
|
|
this.$cancelButton = this.$widget.find(".confirm-dialog-cancel-button");
|
|
|
|
this.$custom = this.$widget.find(".confirm-dialog-custom");
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$widget.on("shown.bs.modal", () => this.$okButton.trigger("focus"));
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
this.$widget.on("hidden.bs.modal", () => {
|
|
|
|
if (this.resolve) {
|
|
|
|
this.resolve(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.$originallyFocused) {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$originallyFocused.trigger("focus");
|
2022-06-16 20:19:26 +02:00
|
|
|
this.$originallyFocused = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$cancelButton.on("click", () => this.doResolve(false));
|
|
|
|
this.$okButton.on("click", () => this.doResolve(true));
|
2022-06-16 20:19:26 +02:00
|
|
|
}
|
|
|
|
|
2024-12-21 17:32:50 +02:00
|
|
|
showConfirmDialogEvent({ message, callback }: ConfirmWithMessageOptions) {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$originallyFocused = $(":focus");
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
this.$custom.hide();
|
|
|
|
|
|
|
|
glob.activeDialog = this.$widget;
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (typeof message === "string") {
|
2022-06-16 20:19:26 +02:00
|
|
|
message = $("<div>").text(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$confirmContent.empty().append(message);
|
|
|
|
|
2024-09-03 18:15:10 +02:00
|
|
|
this.modal.show();
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
this.resolve = callback;
|
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
|
2024-12-21 17:32:50 +02:00
|
|
|
showConfirmDeleteNoteBoxWithNoteDialogEvent({ title, callback }: ConfirmWithTitleOptions) {
|
2022-06-16 20:19:26 +02:00
|
|
|
glob.activeDialog = this.$widget;
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$confirmContent.text(`${t("confirm.are_you_sure_remove_note", { title: title })}`);
|
2022-06-16 20:19:26 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$custom
|
|
|
|
.empty()
|
2022-06-16 20:19:26 +02:00
|
|
|
.append("<br/>")
|
2025-01-09 18:07:02 +02:00
|
|
|
.append(
|
|
|
|
$("<div>")
|
|
|
|
.addClass("form-check")
|
|
|
|
.append(
|
|
|
|
$("<label>")
|
|
|
|
.addClass("form-check-label")
|
|
|
|
.attr("style", "text-decoration: underline dotted var(--main-text-color)")
|
|
|
|
.attr("title", `${t("confirm.if_you_dont_check")}`)
|
|
|
|
.append($("<input>").attr("type", "checkbox").addClass(`form-check-input ${DELETE_NOTE_BUTTON_CLASS}`))
|
|
|
|
.append(`${t("confirm.also_delete_note")}`)
|
|
|
|
)
|
|
|
|
);
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
this.$custom.show();
|
|
|
|
|
2024-09-03 18:15:10 +02:00
|
|
|
this.modal.show();
|
2022-06-16 20:19:26 +02:00
|
|
|
|
|
|
|
this.resolve = callback;
|
|
|
|
}
|
|
|
|
|
2024-12-21 17:32:50 +02:00
|
|
|
doResolve(ret: boolean) {
|
|
|
|
if (this.resolve) {
|
|
|
|
this.resolve({
|
|
|
|
confirmed: ret,
|
|
|
|
isDeleteNoteChecked: this.$widget.find(`.${DELETE_NOTE_BUTTON_CLASS}:checked`).length > 0
|
|
|
|
});
|
|
|
|
}
|
2022-06-16 21:30:05 +02:00
|
|
|
|
2022-06-16 20:19:26 +02:00
|
|
|
this.resolve = null;
|
|
|
|
|
2024-09-03 18:15:10 +02:00
|
|
|
this.modal.hide();
|
2022-06-16 20:19:26 +02:00
|
|
|
}
|
|
|
|
}
|