123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- class OverlayManager {
- #overlays = new WeakMap();
- #active = null;
- get active() {
- return this.#active;
- }
-
- async register(dialog, canForceClose = false) {
- if (typeof dialog !== "object") {
- throw new Error("Not enough parameters.");
- } else if (this.#overlays.has(dialog)) {
- throw new Error("The overlay is already registered.");
- }
- this.#overlays.set(dialog, { canForceClose });
- if (
- typeof PDFJSDev !== "undefined" &&
- PDFJSDev.test("GENERIC && !SKIP_BABEL") &&
- !dialog.showModal
- ) {
- const dialogPolyfill = require("dialog-polyfill/dist/dialog-polyfill.js");
- dialogPolyfill.registerDialog(dialog);
- if (!this._dialogPolyfillCSS) {
- this._dialogPolyfillCSS = true;
- const style = document.createElement("style");
- style.textContent = PDFJSDev.eval("DIALOG_POLYFILL_CSS");
- document.head.prepend(style);
- }
- }
- dialog.addEventListener("cancel", evt => {
- this.#active = null;
- });
- }
-
- async unregister(dialog) {
- if (!this.#overlays.has(dialog)) {
- throw new Error("The overlay does not exist.");
- } else if (this.#active === dialog) {
- throw new Error("The overlay cannot be removed while it is active.");
- }
- this.#overlays.delete(dialog);
- }
-
- async open(dialog) {
- if (!this.#overlays.has(dialog)) {
- throw new Error("The overlay does not exist.");
- } else if (this.#active) {
- if (this.#active === dialog) {
- throw new Error("The overlay is already active.");
- } else if (this.#overlays.get(dialog).canForceClose) {
- await this.close();
- } else {
- throw new Error("Another overlay is currently active.");
- }
- }
- this.#active = dialog;
- dialog.showModal();
- }
-
- async close(dialog = this.#active) {
- if (!this.#overlays.has(dialog)) {
- throw new Error("The overlay does not exist.");
- } else if (!this.#active) {
- throw new Error("The overlay is currently not active.");
- } else if (this.#active !== dialog) {
- throw new Error("Another overlay is currently active.");
- }
- dialog.close();
- this.#active = null;
- }
- }
- export { OverlayManager };
|