overlay_manager.js 3.3 KB

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