UNPKG

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