2014-05-13 19:05:29 +09:00
|
|
|
/* Copyright 2014 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-05-26 21:52:23 +09:00
|
|
|
class OverlayManager {
|
2022-03-25 22:10:22 +09:00
|
|
|
#overlays = new WeakMap();
|
2022-03-22 00:01:32 +09:00
|
|
|
|
|
|
|
#active = null;
|
|
|
|
|
2017-05-26 21:52:23 +09:00
|
|
|
get active() {
|
2022-03-22 00:01:32 +09:00
|
|
|
return this.#active;
|
2017-05-26 21:52:23 +09:00
|
|
|
}
|
2014-05-13 19:05:29 +09:00
|
|
|
|
|
|
|
/**
|
2022-03-25 22:10:13 +09:00
|
|
|
* @param {HTMLDialogElement} dialog - The overlay's DOM element.
|
2019-10-12 23:30:32 +09:00
|
|
|
* @param {boolean} [canForceClose] - Indicates if opening the overlay closes
|
|
|
|
* an active overlay. The default is `false`.
|
2014-05-13 19:05:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
|
|
|
* registered.
|
|
|
|
*/
|
2022-03-25 22:10:22 +09:00
|
|
|
async register(dialog, canForceClose = false) {
|
|
|
|
if (typeof dialog !== "object") {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("Not enough parameters.");
|
2022-03-25 22:10:22 +09:00
|
|
|
} else if (this.#overlays.has(dialog)) {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("The overlay is already registered.");
|
|
|
|
}
|
2022-03-25 22:10:22 +09:00
|
|
|
this.#overlays.set(dialog, { canForceClose });
|
2022-03-25 22:10:13 +09:00
|
|
|
|
|
|
|
dialog.addEventListener("cancel", evt => {
|
|
|
|
this.#active = null;
|
|
|
|
});
|
2017-05-26 21:52:23 +09:00
|
|
|
}
|
2014-05-13 19:05:29 +09:00
|
|
|
|
|
|
|
/**
|
2022-03-25 22:10:22 +09:00
|
|
|
* @param {HTMLDialogElement} dialog - The overlay's DOM element.
|
2014-05-13 19:05:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
|
|
|
* opened.
|
|
|
|
*/
|
2022-03-25 22:10:22 +09:00
|
|
|
async open(dialog) {
|
|
|
|
if (!this.#overlays.has(dialog)) {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("The overlay does not exist.");
|
2022-03-22 00:01:32 +09:00
|
|
|
} else if (this.#active) {
|
2022-03-25 22:10:22 +09:00
|
|
|
if (this.#active === dialog) {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("The overlay is already active.");
|
2022-03-25 22:10:22 +09:00
|
|
|
} else if (this.#overlays.get(dialog).canForceClose) {
|
2022-03-25 22:10:13 +09:00
|
|
|
await this.close();
|
2018-07-30 23:39:06 +09:00
|
|
|
} else {
|
|
|
|
throw new Error("Another overlay is currently active.");
|
2014-05-13 19:05:29 +09:00
|
|
|
}
|
2018-07-30 23:39:06 +09:00
|
|
|
}
|
2022-03-25 22:10:22 +09:00
|
|
|
this.#active = dialog;
|
|
|
|
dialog.showModal();
|
2017-05-26 21:52:23 +09:00
|
|
|
}
|
2014-05-13 19:05:29 +09:00
|
|
|
|
|
|
|
/**
|
2022-03-25 22:10:22 +09:00
|
|
|
* @param {HTMLDialogElement} dialog - The overlay's DOM element.
|
2014-05-13 19:05:29 +09:00
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been
|
|
|
|
* closed.
|
|
|
|
*/
|
2022-03-25 22:10:22 +09:00
|
|
|
async close(dialog = this.#active) {
|
|
|
|
if (!this.#overlays.has(dialog)) {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("The overlay does not exist.");
|
2022-03-22 00:01:32 +09:00
|
|
|
} else if (!this.#active) {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("The overlay is currently not active.");
|
2022-03-25 22:10:22 +09:00
|
|
|
} else if (this.#active !== dialog) {
|
2018-07-30 23:39:06 +09:00
|
|
|
throw new Error("Another overlay is currently active.");
|
|
|
|
}
|
2022-03-25 22:10:22 +09:00
|
|
|
dialog.close();
|
2022-03-22 00:01:32 +09:00
|
|
|
this.#active = null;
|
2014-05-13 19:05:29 +09:00
|
|
|
}
|
2017-05-26 21:52:23 +09:00
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export { OverlayManager };
|