UNPKG

3.41 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2020 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 constructor() {
31 this._overlays = {};
32 this._active = null;
33 this._keyDownBound = this._keyDown.bind(this);
34 }
35
36 get active() {
37 return this._active;
38 }
39
40 async register(name, element, callerCloseMethod = null, canForceClose = false) {
41 let container;
42
43 if (!name || !element || !(container = element.parentNode)) {
44 throw new Error("Not enough parameters.");
45 } else if (this._overlays[name]) {
46 throw new Error("The overlay is already registered.");
47 }
48
49 this._overlays[name] = {
50 element,
51 container,
52 callerCloseMethod,
53 canForceClose
54 };
55 }
56
57 async unregister(name) {
58 if (!this._overlays[name]) {
59 throw new Error("The overlay does not exist.");
60 } else if (this._active === name) {
61 throw new Error("The overlay cannot be removed while it is active.");
62 }
63
64 delete this._overlays[name];
65 }
66
67 async open(name) {
68 if (!this._overlays[name]) {
69 throw new Error("The overlay does not exist.");
70 } else if (this._active) {
71 if (this._overlays[name].canForceClose) {
72 this._closeThroughCaller();
73 } else if (this._active === name) {
74 throw new Error("The overlay is already active.");
75 } else {
76 throw new Error("Another overlay is currently active.");
77 }
78 }
79
80 this._active = name;
81
82 this._overlays[this._active].element.classList.remove("hidden");
83
84 this._overlays[this._active].container.classList.remove("hidden");
85
86 window.addEventListener("keydown", this._keyDownBound);
87 }
88
89 async close(name) {
90 if (!this._overlays[name]) {
91 throw new Error("The overlay does not exist.");
92 } else if (!this._active) {
93 throw new Error("The overlay is currently not active.");
94 } else if (this._active !== name) {
95 throw new Error("Another overlay is currently active.");
96 }
97
98 this._overlays[this._active].container.classList.add("hidden");
99
100 this._overlays[this._active].element.classList.add("hidden");
101
102 this._active = null;
103 window.removeEventListener("keydown", this._keyDownBound);
104 }
105
106 _keyDown(evt) {
107 if (this._active && evt.keyCode === 27) {
108 this._closeThroughCaller();
109
110 evt.preventDefault();
111 }
112 }
113
114 _closeThroughCaller() {
115 if (this._overlays[this._active].callerCloseMethod) {
116 this._overlays[this._active].callerCloseMethod();
117 }
118
119 if (this._active) {
120 this.close(this._active);
121 }
122 }
123
124}
125
126exports.OverlayManager = OverlayManager;
\No newline at end of file