UNPKG

2.63 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.OverlayManager = void 0;
28
29class OverlayManager {
30 #overlays = new WeakMap();
31 #active = null;
32
33 get active() {
34 return this.#active;
35 }
36
37 async register(dialog, canForceClose = false) {
38 if (typeof dialog !== "object") {
39 throw new Error("Not enough parameters.");
40 } else if (this.#overlays.has(dialog)) {
41 throw new Error("The overlay is already registered.");
42 }
43
44 this.#overlays.set(dialog, {
45 canForceClose
46 });
47 dialog.addEventListener("cancel", evt => {
48 this.#active = null;
49 });
50 }
51
52 async unregister(dialog) {
53 if (!this.#overlays.has(dialog)) {
54 throw new Error("The overlay does not exist.");
55 } else if (this.#active === dialog) {
56 throw new Error("The overlay cannot be removed while it is active.");
57 }
58
59 this.#overlays.delete(dialog);
60 }
61
62 async open(dialog) {
63 if (!this.#overlays.has(dialog)) {
64 throw new Error("The overlay does not exist.");
65 } else if (this.#active) {
66 if (this.#active === dialog) {
67 throw new Error("The overlay is already active.");
68 } else if (this.#overlays.get(dialog).canForceClose) {
69 await this.close();
70 } else {
71 throw new Error("Another overlay is currently active.");
72 }
73 }
74
75 this.#active = dialog;
76 dialog.showModal();
77 }
78
79 async close(dialog = this.#active) {
80 if (!this.#overlays.has(dialog)) {
81 throw new Error("The overlay does not exist.");
82 } else if (!this.#active) {
83 throw new Error("The overlay is currently not active.");
84 } else if (this.#active !== dialog) {
85 throw new Error("Another overlay is currently active.");
86 }
87
88 dialog.close();
89 this.#active = null;
90 }
91
92}
93
94exports.OverlayManager = OverlayManager;
\No newline at end of file