|
|
@ -13,132 +13,137 @@ |
|
|
|
* limitations under the License. |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
var OverlayManager = { |
|
|
|
class OverlayManager { |
|
|
|
overlays: {}, |
|
|
|
constructor() { |
|
|
|
active: null, |
|
|
|
this._overlays = {}; |
|
|
|
|
|
|
|
this._active = null; |
|
|
|
|
|
|
|
this._keyDownBound = this._keyDown.bind(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get active() { |
|
|
|
|
|
|
|
return this._active; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param {string} name The name of the overlay that is registered. |
|
|
|
* @param {string} name - The name of the overlay that is registered. |
|
|
|
* @param {HTMLDivElement} element The overlay's DOM element. |
|
|
|
* @param {HTMLDivElement} element - The overlay's DOM element. |
|
|
|
* @param {function} callerCloseMethod (optional) The method that, if present, |
|
|
|
* @param {function} callerCloseMethod - (optional) The method that, if |
|
|
|
* will call OverlayManager.close from the Object |
|
|
|
* present, calls `OverlayManager.close` from the object |
|
|
|
* registering the overlay. Access to this method is |
|
|
|
* registering the overlay. Access to this method is |
|
|
|
* necessary in order to run cleanup code when e.g. |
|
|
|
* necessary in order to run cleanup code when e.g. |
|
|
|
* the overlay is force closed. The default is null. |
|
|
|
* the overlay is force closed. The default is `null`. |
|
|
|
* @param {boolean} canForceClose (optional) Indicates if opening the overlay |
|
|
|
* @param {boolean} canForceClose - (optional) Indicates if opening the |
|
|
|
* will close an active overlay. The default is false. |
|
|
|
* overlay closes an active overlay. The default is `false`. |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* registered. |
|
|
|
* registered. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
register(name, element, callerCloseMethod, canForceClose) { |
|
|
|
register(name, element, callerCloseMethod = null, canForceClose = false) { |
|
|
|
return new Promise((resolve) => { |
|
|
|
return new Promise((resolve) => { |
|
|
|
var container; |
|
|
|
let container; |
|
|
|
if (!name || !element || !(container = element.parentNode)) { |
|
|
|
if (!name || !element || !(container = element.parentNode)) { |
|
|
|
throw new Error('Not enough parameters.'); |
|
|
|
throw new Error('Not enough parameters.'); |
|
|
|
} else if (this.overlays[name]) { |
|
|
|
} else if (this._overlays[name]) { |
|
|
|
throw new Error('The overlay is already registered.'); |
|
|
|
throw new Error('The overlay is already registered.'); |
|
|
|
} |
|
|
|
} |
|
|
|
this.overlays[name] = { |
|
|
|
this._overlays[name] = { |
|
|
|
element, |
|
|
|
element, |
|
|
|
container, |
|
|
|
container, |
|
|
|
callerCloseMethod: (callerCloseMethod || null), |
|
|
|
callerCloseMethod, |
|
|
|
canForceClose: (canForceClose || false), |
|
|
|
canForceClose, |
|
|
|
}; |
|
|
|
}; |
|
|
|
resolve(); |
|
|
|
resolve(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param {string} name The name of the overlay that is unregistered. |
|
|
|
* @param {string} name - The name of the overlay that is unregistered. |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* unregistered. |
|
|
|
* unregistered. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
unregister(name) { |
|
|
|
unregister(name) { |
|
|
|
return new Promise((resolve) => { |
|
|
|
return new Promise((resolve) => { |
|
|
|
if (!this.overlays[name]) { |
|
|
|
if (!this._overlays[name]) { |
|
|
|
throw new Error('The overlay does not exist.'); |
|
|
|
throw new Error('The overlay does not exist.'); |
|
|
|
} else if (this.active === name) { |
|
|
|
} else if (this._active === name) { |
|
|
|
throw new Error('The overlay cannot be removed while it is active.'); |
|
|
|
throw new Error('The overlay cannot be removed while it is active.'); |
|
|
|
} |
|
|
|
} |
|
|
|
delete this.overlays[name]; |
|
|
|
delete this._overlays[name]; |
|
|
|
|
|
|
|
|
|
|
|
resolve(); |
|
|
|
resolve(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param {string} name The name of the overlay that should be opened. |
|
|
|
* @param {string} name - The name of the overlay that should be opened. |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* opened. |
|
|
|
* opened. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
open(name) { |
|
|
|
open(name) { |
|
|
|
return new Promise((resolve) => { |
|
|
|
return new Promise((resolve) => { |
|
|
|
if (!this.overlays[name]) { |
|
|
|
if (!this._overlays[name]) { |
|
|
|
throw new Error('The overlay does not exist.'); |
|
|
|
throw new Error('The overlay does not exist.'); |
|
|
|
} else if (this.active) { |
|
|
|
} else if (this._active) { |
|
|
|
if (this.overlays[name].canForceClose) { |
|
|
|
if (this._overlays[name].canForceClose) { |
|
|
|
this._closeThroughCaller(); |
|
|
|
this._closeThroughCaller(); |
|
|
|
} else if (this.active === name) { |
|
|
|
} else if (this._active === name) { |
|
|
|
throw new Error('The overlay is already active.'); |
|
|
|
throw new Error('The overlay is already active.'); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw new Error('Another overlay is currently active.'); |
|
|
|
throw new Error('Another overlay is currently active.'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
this.active = name; |
|
|
|
this._active = name; |
|
|
|
this.overlays[this.active].element.classList.remove('hidden'); |
|
|
|
this._overlays[this._active].element.classList.remove('hidden'); |
|
|
|
this.overlays[this.active].container.classList.remove('hidden'); |
|
|
|
this._overlays[this._active].container.classList.remove('hidden'); |
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener('keydown', this._keyDown); |
|
|
|
window.addEventListener('keydown', this._keyDownBound); |
|
|
|
resolve(); |
|
|
|
resolve(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param {string} name The name of the overlay that should be closed. |
|
|
|
* @param {string} name - The name of the overlay that should be closed. |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* @returns {Promise} A promise that is resolved when the overlay has been |
|
|
|
* closed. |
|
|
|
* closed. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
close(name) { |
|
|
|
close(name) { |
|
|
|
return new Promise((resolve) => { |
|
|
|
return new Promise((resolve) => { |
|
|
|
if (!this.overlays[name]) { |
|
|
|
if (!this._overlays[name]) { |
|
|
|
throw new Error('The overlay does not exist.'); |
|
|
|
throw new Error('The overlay does not exist.'); |
|
|
|
} else if (!this.active) { |
|
|
|
} else if (!this._active) { |
|
|
|
throw new Error('The overlay is currently not active.'); |
|
|
|
throw new Error('The overlay is currently not active.'); |
|
|
|
} else if (this.active !== name) { |
|
|
|
} else if (this._active !== name) { |
|
|
|
throw new Error('Another overlay is currently active.'); |
|
|
|
throw new Error('Another overlay is currently active.'); |
|
|
|
} |
|
|
|
} |
|
|
|
this.overlays[this.active].container.classList.add('hidden'); |
|
|
|
this._overlays[this._active].container.classList.add('hidden'); |
|
|
|
this.overlays[this.active].element.classList.add('hidden'); |
|
|
|
this._overlays[this._active].element.classList.add('hidden'); |
|
|
|
this.active = null; |
|
|
|
this._active = null; |
|
|
|
|
|
|
|
|
|
|
|
window.removeEventListener('keydown', this._keyDown); |
|
|
|
window.removeEventListener('keydown', this._keyDownBound); |
|
|
|
resolve(); |
|
|
|
resolve(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @private |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
_keyDown(evt) { |
|
|
|
_keyDown(evt) { |
|
|
|
var self = OverlayManager; |
|
|
|
if (this._active && evt.keyCode === 27) { // Esc key.
|
|
|
|
if (self.active && evt.keyCode === 27) { // Esc key.
|
|
|
|
this._closeThroughCaller(); |
|
|
|
self._closeThroughCaller(); |
|
|
|
|
|
|
|
evt.preventDefault(); |
|
|
|
evt.preventDefault(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @private |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
_closeThroughCaller() { |
|
|
|
_closeThroughCaller() { |
|
|
|
if (this.overlays[this.active].callerCloseMethod) { |
|
|
|
if (this._overlays[this._active].callerCloseMethod) { |
|
|
|
this.overlays[this.active].callerCloseMethod(); |
|
|
|
this._overlays[this._active].callerCloseMethod(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this._active) { |
|
|
|
|
|
|
|
this.close(this._active); |
|
|
|
} |
|
|
|
} |
|
|
|
if (this.active) { |
|
|
|
|
|
|
|
this.close(this.active); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export { |
|
|
|
export { |
|
|
|
OverlayManager, |
|
|
|
OverlayManager, |
|
|
|