overlay_manager.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  29. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  30. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  31. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  32. 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); } }
  33. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  34. var OverlayManager =
  35. /*#__PURE__*/
  36. function () {
  37. function OverlayManager() {
  38. _classCallCheck(this, OverlayManager);
  39. this._overlays = {};
  40. this._active = null;
  41. this._keyDownBound = this._keyDown.bind(this);
  42. }
  43. _createClass(OverlayManager, [{
  44. key: "register",
  45. value: function () {
  46. var _register = _asyncToGenerator(
  47. /*#__PURE__*/
  48. _regenerator["default"].mark(function _callee(name, element) {
  49. var callerCloseMethod,
  50. canForceClose,
  51. container,
  52. _args = arguments;
  53. return _regenerator["default"].wrap(function _callee$(_context) {
  54. while (1) {
  55. switch (_context.prev = _context.next) {
  56. case 0:
  57. callerCloseMethod = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
  58. canForceClose = _args.length > 3 && _args[3] !== undefined ? _args[3] : false;
  59. if (!(!name || !element || !(container = element.parentNode))) {
  60. _context.next = 6;
  61. break;
  62. }
  63. throw new Error('Not enough parameters.');
  64. case 6:
  65. if (!this._overlays[name]) {
  66. _context.next = 8;
  67. break;
  68. }
  69. throw new Error('The overlay is already registered.');
  70. case 8:
  71. this._overlays[name] = {
  72. element: element,
  73. container: container,
  74. callerCloseMethod: callerCloseMethod,
  75. canForceClose: canForceClose
  76. };
  77. case 9:
  78. case "end":
  79. return _context.stop();
  80. }
  81. }
  82. }, _callee, this);
  83. }));
  84. function register(_x, _x2) {
  85. return _register.apply(this, arguments);
  86. }
  87. return register;
  88. }()
  89. }, {
  90. key: "unregister",
  91. value: function () {
  92. var _unregister = _asyncToGenerator(
  93. /*#__PURE__*/
  94. _regenerator["default"].mark(function _callee2(name) {
  95. return _regenerator["default"].wrap(function _callee2$(_context2) {
  96. while (1) {
  97. switch (_context2.prev = _context2.next) {
  98. case 0:
  99. if (this._overlays[name]) {
  100. _context2.next = 4;
  101. break;
  102. }
  103. throw new Error('The overlay does not exist.');
  104. case 4:
  105. if (!(this._active === name)) {
  106. _context2.next = 6;
  107. break;
  108. }
  109. throw new Error('The overlay cannot be removed while it is active.');
  110. case 6:
  111. delete this._overlays[name];
  112. case 7:
  113. case "end":
  114. return _context2.stop();
  115. }
  116. }
  117. }, _callee2, this);
  118. }));
  119. function unregister(_x3) {
  120. return _unregister.apply(this, arguments);
  121. }
  122. return unregister;
  123. }()
  124. }, {
  125. key: "open",
  126. value: function () {
  127. var _open = _asyncToGenerator(
  128. /*#__PURE__*/
  129. _regenerator["default"].mark(function _callee3(name) {
  130. return _regenerator["default"].wrap(function _callee3$(_context3) {
  131. while (1) {
  132. switch (_context3.prev = _context3.next) {
  133. case 0:
  134. if (this._overlays[name]) {
  135. _context3.next = 4;
  136. break;
  137. }
  138. throw new Error('The overlay does not exist.');
  139. case 4:
  140. if (!this._active) {
  141. _context3.next = 14;
  142. break;
  143. }
  144. if (!this._overlays[name].canForceClose) {
  145. _context3.next = 9;
  146. break;
  147. }
  148. this._closeThroughCaller();
  149. _context3.next = 14;
  150. break;
  151. case 9:
  152. if (!(this._active === name)) {
  153. _context3.next = 13;
  154. break;
  155. }
  156. throw new Error('The overlay is already active.');
  157. case 13:
  158. throw new Error('Another overlay is currently active.');
  159. case 14:
  160. this._active = name;
  161. this._overlays[this._active].element.classList.remove('hidden');
  162. this._overlays[this._active].container.classList.remove('hidden');
  163. window.addEventListener('keydown', this._keyDownBound);
  164. case 18:
  165. case "end":
  166. return _context3.stop();
  167. }
  168. }
  169. }, _callee3, this);
  170. }));
  171. function open(_x4) {
  172. return _open.apply(this, arguments);
  173. }
  174. return open;
  175. }()
  176. }, {
  177. key: "close",
  178. value: function () {
  179. var _close = _asyncToGenerator(
  180. /*#__PURE__*/
  181. _regenerator["default"].mark(function _callee4(name) {
  182. return _regenerator["default"].wrap(function _callee4$(_context4) {
  183. while (1) {
  184. switch (_context4.prev = _context4.next) {
  185. case 0:
  186. if (this._overlays[name]) {
  187. _context4.next = 4;
  188. break;
  189. }
  190. throw new Error('The overlay does not exist.');
  191. case 4:
  192. if (this._active) {
  193. _context4.next = 8;
  194. break;
  195. }
  196. throw new Error('The overlay is currently not active.');
  197. case 8:
  198. if (!(this._active !== name)) {
  199. _context4.next = 10;
  200. break;
  201. }
  202. throw new Error('Another overlay is currently active.');
  203. case 10:
  204. this._overlays[this._active].container.classList.add('hidden');
  205. this._overlays[this._active].element.classList.add('hidden');
  206. this._active = null;
  207. window.removeEventListener('keydown', this._keyDownBound);
  208. case 14:
  209. case "end":
  210. return _context4.stop();
  211. }
  212. }
  213. }, _callee4, this);
  214. }));
  215. function close(_x5) {
  216. return _close.apply(this, arguments);
  217. }
  218. return close;
  219. }()
  220. }, {
  221. key: "_keyDown",
  222. value: function _keyDown(evt) {
  223. if (this._active && evt.keyCode === 27) {
  224. this._closeThroughCaller();
  225. evt.preventDefault();
  226. }
  227. }
  228. }, {
  229. key: "_closeThroughCaller",
  230. value: function _closeThroughCaller() {
  231. if (this._overlays[this._active].callerCloseMethod) {
  232. this._overlays[this._active].callerCloseMethod();
  233. }
  234. if (this._active) {
  235. this.close(this._active);
  236. }
  237. }
  238. }, {
  239. key: "active",
  240. get: function get() {
  241. return this._active;
  242. }
  243. }]);
  244. return OverlayManager;
  245. }();
  246. exports.OverlayManager = OverlayManager;