overlay_manager.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  20. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  21. var OverlayManager = function () {
  22. function OverlayManager() {
  23. _classCallCheck(this, OverlayManager);
  24. this._overlays = {};
  25. this._active = null;
  26. this._keyDownBound = this._keyDown.bind(this);
  27. }
  28. _createClass(OverlayManager, [{
  29. key: 'register',
  30. value: function register(name, element) {
  31. var _this = this;
  32. var callerCloseMethod = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  33. var canForceClose = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  34. return new Promise(function (resolve) {
  35. var container = void 0;
  36. if (!name || !element || !(container = element.parentNode)) {
  37. throw new Error('Not enough parameters.');
  38. } else if (_this._overlays[name]) {
  39. throw new Error('The overlay is already registered.');
  40. }
  41. _this._overlays[name] = {
  42. element: element,
  43. container: container,
  44. callerCloseMethod: callerCloseMethod,
  45. canForceClose: canForceClose
  46. };
  47. resolve();
  48. });
  49. }
  50. }, {
  51. key: 'unregister',
  52. value: function unregister(name) {
  53. var _this2 = this;
  54. return new Promise(function (resolve) {
  55. if (!_this2._overlays[name]) {
  56. throw new Error('The overlay does not exist.');
  57. } else if (_this2._active === name) {
  58. throw new Error('The overlay cannot be removed while it is active.');
  59. }
  60. delete _this2._overlays[name];
  61. resolve();
  62. });
  63. }
  64. }, {
  65. key: 'open',
  66. value: function open(name) {
  67. var _this3 = this;
  68. return new Promise(function (resolve) {
  69. if (!_this3._overlays[name]) {
  70. throw new Error('The overlay does not exist.');
  71. } else if (_this3._active) {
  72. if (_this3._overlays[name].canForceClose) {
  73. _this3._closeThroughCaller();
  74. } else if (_this3._active === name) {
  75. throw new Error('The overlay is already active.');
  76. } else {
  77. throw new Error('Another overlay is currently active.');
  78. }
  79. }
  80. _this3._active = name;
  81. _this3._overlays[_this3._active].element.classList.remove('hidden');
  82. _this3._overlays[_this3._active].container.classList.remove('hidden');
  83. window.addEventListener('keydown', _this3._keyDownBound);
  84. resolve();
  85. });
  86. }
  87. }, {
  88. key: 'close',
  89. value: function close(name) {
  90. var _this4 = this;
  91. return new Promise(function (resolve) {
  92. if (!_this4._overlays[name]) {
  93. throw new Error('The overlay does not exist.');
  94. } else if (!_this4._active) {
  95. throw new Error('The overlay is currently not active.');
  96. } else if (_this4._active !== name) {
  97. throw new Error('Another overlay is currently active.');
  98. }
  99. _this4._overlays[_this4._active].container.classList.add('hidden');
  100. _this4._overlays[_this4._active].element.classList.add('hidden');
  101. _this4._active = null;
  102. window.removeEventListener('keydown', _this4._keyDownBound);
  103. resolve();
  104. });
  105. }
  106. }, {
  107. key: '_keyDown',
  108. value: function _keyDown(evt) {
  109. if (this._active && evt.keyCode === 27) {
  110. this._closeThroughCaller();
  111. evt.preventDefault();
  112. }
  113. }
  114. }, {
  115. key: '_closeThroughCaller',
  116. value: function _closeThroughCaller() {
  117. if (this._overlays[this._active].callerCloseMethod) {
  118. this._overlays[this._active].callerCloseMethod();
  119. }
  120. if (this._active) {
  121. this.close(this._active);
  122. }
  123. }
  124. }, {
  125. key: 'active',
  126. get: function get() {
  127. return this._active;
  128. }
  129. }]);
  130. return OverlayManager;
  131. }();
  132. exports.OverlayManager = OverlayManager;