overlay_manager.js 5.3 KB

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