overlay_manager.js 3.3 KB

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