80 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-03-16 17:31:28 +02:00
import type { EventData } from "../../components/app_context.js";
2024-07-24 17:34:26 +08:00
import { t } from "../../services/i18n.js";
2022-06-16 19:53:33 +02:00
import utils from "../../services/utils.js";
import BasicWidget from "../basic_widget.js";
import { Modal } from "bootstrap";
2025-03-16 17:31:28 +02:00
import type { ConfirmDialogCallback } from "./confirm.js";
2022-06-16 19:53:33 +02:00
const TPL = `
<div class="info-dialog modal mx-auto" tabindex="-1" role="dialog" style="z-index: 2000;">
2022-06-16 19:53:33 +02:00
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
2024-09-03 18:15:10 +02:00
<h5 class="modal-title">${t("info.modalTitle")}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("info.closeButton")}"></button>
2022-06-16 19:53:33 +02:00
</div>
<div class="modal-body">
<div class="info-dialog-content"></div>
</div>
<div class="modal-footer">
2024-07-24 17:34:26 +08:00
<button class="info-dialog-ok-button btn btn-primary btn-sm">${t("info.okButton")}</button>
2022-06-16 19:53:33 +02:00
</div>
</div>
</div>
</div>`;
export default class InfoDialog extends BasicWidget {
2025-03-16 17:31:28 +02:00
private resolve: ConfirmDialogCallback | null;
private modal!: bootstrap.Modal;
private $originallyFocused!: JQuery<HTMLElement> | null;
private $infoContent!: JQuery<HTMLElement>;
private $okButton!: JQuery<HTMLElement>;
2022-06-16 19:53:33 +02:00
constructor() {
super();
this.resolve = null;
this.$originallyFocused = null; // element focused before the dialog was opened, so we can return to it afterward
2022-06-16 19:53:33 +02:00
}
doRender() {
this.$widget = $(TPL);
2025-03-16 17:31:28 +02:00
this.modal = Modal.getOrCreateInstance(this.$widget[0]);
2022-06-16 19:53:33 +02:00
this.$infoContent = this.$widget.find(".info-dialog-content");
this.$okButton = this.$widget.find(".info-dialog-ok-button");
2025-01-09 18:07:02 +02:00
this.$widget.on("shown.bs.modal", () => this.$okButton.trigger("focus"));
2022-06-16 19:53:33 +02:00
this.$widget.on("hidden.bs.modal", () => {
if (this.resolve) {
this.resolve();
}
if (this.$originallyFocused) {
2025-01-09 18:07:02 +02:00
this.$originallyFocused.trigger("focus");
2022-06-16 19:53:33 +02:00
this.$originallyFocused = null;
}
});
2025-01-09 18:07:02 +02:00
this.$okButton.on("click", () => this.modal.hide());
2022-06-16 19:53:33 +02:00
}
2025-03-16 17:31:28 +02:00
showInfoDialogEvent({ message, callback }: EventData<"showInfoDialog">) {
2025-01-09 18:07:02 +02:00
this.$originallyFocused = $(":focus");
2022-06-16 19:53:33 +02:00
2025-03-16 17:31:28 +02:00
if (typeof message === "string") {
this.$infoContent.text(message);
} else if (Array.isArray(message)) {
this.$infoContent.html(message[0]);
} else {
this.$infoContent.html(message as HTMLElement);
}
2022-06-16 19:53:33 +02:00
utils.openDialog(this.$widget);
this.resolve = callback;
}
}