2
0

overlay_manager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. var OverlayManager = {
  20. overlays: {},
  21. active: null,
  22. register: function register(name, element, callerCloseMethod, canForceClose) {
  23. var _this = this;
  24. return new Promise(function (resolve) {
  25. var container;
  26. if (!name || !element || !(container = element.parentNode)) {
  27. throw new Error('Not enough parameters.');
  28. } else if (_this.overlays[name]) {
  29. throw new Error('The overlay is already registered.');
  30. }
  31. _this.overlays[name] = {
  32. element: element,
  33. container: container,
  34. callerCloseMethod: callerCloseMethod || null,
  35. canForceClose: canForceClose || false
  36. };
  37. resolve();
  38. });
  39. },
  40. unregister: function unregister(name) {
  41. var _this2 = this;
  42. return new Promise(function (resolve) {
  43. if (!_this2.overlays[name]) {
  44. throw new Error('The overlay does not exist.');
  45. } else if (_this2.active === name) {
  46. throw new Error('The overlay cannot be removed while it is active.');
  47. }
  48. delete _this2.overlays[name];
  49. resolve();
  50. });
  51. },
  52. open: function open(name) {
  53. var _this3 = this;
  54. return new Promise(function (resolve) {
  55. if (!_this3.overlays[name]) {
  56. throw new Error('The overlay does not exist.');
  57. } else if (_this3.active) {
  58. if (_this3.overlays[name].canForceClose) {
  59. _this3._closeThroughCaller();
  60. } else if (_this3.active === name) {
  61. throw new Error('The overlay is already active.');
  62. } else {
  63. throw new Error('Another overlay is currently active.');
  64. }
  65. }
  66. _this3.active = name;
  67. _this3.overlays[_this3.active].element.classList.remove('hidden');
  68. _this3.overlays[_this3.active].container.classList.remove('hidden');
  69. window.addEventListener('keydown', _this3._keyDown);
  70. resolve();
  71. });
  72. },
  73. close: function close(name) {
  74. var _this4 = this;
  75. return new Promise(function (resolve) {
  76. if (!_this4.overlays[name]) {
  77. throw new Error('The overlay does not exist.');
  78. } else if (!_this4.active) {
  79. throw new Error('The overlay is currently not active.');
  80. } else if (_this4.active !== name) {
  81. throw new Error('Another overlay is currently active.');
  82. }
  83. _this4.overlays[_this4.active].container.classList.add('hidden');
  84. _this4.overlays[_this4.active].element.classList.add('hidden');
  85. _this4.active = null;
  86. window.removeEventListener('keydown', _this4._keyDown);
  87. resolve();
  88. });
  89. },
  90. _keyDown: function _keyDown(evt) {
  91. var self = OverlayManager;
  92. if (self.active && evt.keyCode === 27) {
  93. self._closeThroughCaller();
  94. evt.preventDefault();
  95. }
  96. },
  97. _closeThroughCaller: function _closeThroughCaller() {
  98. if (this.overlays[this.active].callerCloseMethod) {
  99. this.overlays[this.active].callerCloseMethod();
  100. }
  101. if (this.active) {
  102. this.close(this.active);
  103. }
  104. }
  105. };
  106. exports.OverlayManager = OverlayManager;