overlay_manager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 overlayManagerRegister(name, element, callerCloseMethod, canForceClose) {
  23. return new Promise(function (resolve) {
  24. var container;
  25. if (!name || !element || !(container = element.parentNode)) {
  26. throw new Error('Not enough parameters.');
  27. } else if (this.overlays[name]) {
  28. throw new Error('The overlay is already registered.');
  29. }
  30. this.overlays[name] = {
  31. element: element,
  32. container: container,
  33. callerCloseMethod: callerCloseMethod || null,
  34. canForceClose: canForceClose || false
  35. };
  36. resolve();
  37. }.bind(this));
  38. },
  39. unregister: function overlayManagerUnregister(name) {
  40. return new Promise(function (resolve) {
  41. if (!this.overlays[name]) {
  42. throw new Error('The overlay does not exist.');
  43. } else if (this.active === name) {
  44. throw new Error('The overlay cannot be removed while it is active.');
  45. }
  46. delete this.overlays[name];
  47. resolve();
  48. }.bind(this));
  49. },
  50. open: function overlayManagerOpen(name) {
  51. return new Promise(function (resolve) {
  52. if (!this.overlays[name]) {
  53. throw new Error('The overlay does not exist.');
  54. } else if (this.active) {
  55. if (this.overlays[name].canForceClose) {
  56. this._closeThroughCaller();
  57. } else if (this.active === name) {
  58. throw new Error('The overlay is already active.');
  59. } else {
  60. throw new Error('Another overlay is currently active.');
  61. }
  62. }
  63. this.active = name;
  64. this.overlays[this.active].element.classList.remove('hidden');
  65. this.overlays[this.active].container.classList.remove('hidden');
  66. window.addEventListener('keydown', this._keyDown);
  67. resolve();
  68. }.bind(this));
  69. },
  70. close: function overlayManagerClose(name) {
  71. return new Promise(function (resolve) {
  72. if (!this.overlays[name]) {
  73. throw new Error('The overlay does not exist.');
  74. } else if (!this.active) {
  75. throw new Error('The overlay is currently not active.');
  76. } else if (this.active !== name) {
  77. throw new Error('Another overlay is currently active.');
  78. }
  79. this.overlays[this.active].container.classList.add('hidden');
  80. this.overlays[this.active].element.classList.add('hidden');
  81. this.active = null;
  82. window.removeEventListener('keydown', this._keyDown);
  83. resolve();
  84. }.bind(this));
  85. },
  86. _keyDown: function overlayManager_keyDown(evt) {
  87. var self = OverlayManager;
  88. if (self.active && evt.keyCode === 27) {
  89. self._closeThroughCaller();
  90. evt.preventDefault();
  91. }
  92. },
  93. _closeThroughCaller: function overlayManager_closeThroughCaller() {
  94. if (this.overlays[this.active].callerCloseMethod) {
  95. this.overlays[this.active].callerCloseMethod();
  96. }
  97. if (this.active) {
  98. this.close(this.active);
  99. }
  100. }
  101. };
  102. exports.OverlayManager = OverlayManager;