overlay_manager.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.OverlayManager = void 0;
  27. class OverlayManager {
  28. #overlays = new WeakMap();
  29. #active = null;
  30. get active() {
  31. return this.#active;
  32. }
  33. async register(dialog, canForceClose = false) {
  34. if (typeof dialog !== "object") {
  35. throw new Error("Not enough parameters.");
  36. } else if (this.#overlays.has(dialog)) {
  37. throw new Error("The overlay is already registered.");
  38. }
  39. this.#overlays.set(dialog, {
  40. canForceClose
  41. });
  42. dialog.addEventListener("cancel", evt => {
  43. this.#active = null;
  44. });
  45. }
  46. async unregister(dialog) {
  47. if (!this.#overlays.has(dialog)) {
  48. throw new Error("The overlay does not exist.");
  49. } else if (this.#active === dialog) {
  50. throw new Error("The overlay cannot be removed while it is active.");
  51. }
  52. this.#overlays.delete(dialog);
  53. }
  54. async open(dialog) {
  55. if (!this.#overlays.has(dialog)) {
  56. throw new Error("The overlay does not exist.");
  57. } else if (this.#active) {
  58. if (this.#active === dialog) {
  59. throw new Error("The overlay is already active.");
  60. } else if (this.#overlays.get(dialog).canForceClose) {
  61. await this.close();
  62. } else {
  63. throw new Error("Another overlay is currently active.");
  64. }
  65. }
  66. this.#active = dialog;
  67. dialog.showModal();
  68. }
  69. async close(dialog = this.#active) {
  70. if (!this.#overlays.has(dialog)) {
  71. throw new Error("The overlay does not exist.");
  72. } else if (!this.#active) {
  73. throw new Error("The overlay is currently not active.");
  74. } else if (this.#active !== dialog) {
  75. throw new Error("Another overlay is currently active.");
  76. }
  77. dialog.close();
  78. this.#active = null;
  79. }
  80. }
  81. exports.OverlayManager = OverlayManager;