Convert the OverlayManager
class to use private fields/methods
This commit is contained in:
parent
feea2b78fa
commit
bace0623e5
@ -14,14 +14,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class OverlayManager {
|
class OverlayManager {
|
||||||
constructor() {
|
#overlays = Object.create(null);
|
||||||
this._overlays = {};
|
|
||||||
this._active = null;
|
#active = null;
|
||||||
this._keyDownBound = this._keyDown.bind(this);
|
|
||||||
}
|
#keyDownBound = null;
|
||||||
|
|
||||||
get active() {
|
get active() {
|
||||||
return this._active;
|
return this.#active;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,10 +46,10 @@ class OverlayManager {
|
|||||||
let 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,
|
||||||
@ -63,12 +63,12 @@ class OverlayManager {
|
|||||||
* unregistered.
|
* unregistered.
|
||||||
*/
|
*/
|
||||||
async unregister(name) {
|
async unregister(name) {
|
||||||
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,22 +77,23 @@ class OverlayManager {
|
|||||||
* opened.
|
* opened.
|
||||||
*/
|
*/
|
||||||
async open(name) {
|
async open(name) {
|
||||||
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._keyDownBound);
|
this.#keyDownBound = this.#keyDown.bind(this);
|
||||||
|
window.addEventListener("keydown", this.#keyDownBound);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,39 +102,34 @@ class OverlayManager {
|
|||||||
* closed.
|
* closed.
|
||||||
*/
|
*/
|
||||||
async close(name) {
|
async close(name) {
|
||||||
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._keyDownBound);
|
window.removeEventListener("keydown", this.#keyDownBound);
|
||||||
|
this.#keyDownBound = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
#keyDown(evt) {
|
||||||
* @private
|
if (this.#active && evt.keyCode === /* Esc = */ 27) {
|
||||||
*/
|
this.#closeThroughCaller();
|
||||||
_keyDown(evt) {
|
|
||||||
if (this._active && evt.keyCode === /* Esc = */ 27) {
|
|
||||||
this._closeThroughCaller();
|
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
#closeThroughCaller() {
|
||||||
* @private
|
if (this.#overlays[this.#active].callerCloseMethod) {
|
||||||
*/
|
this.#overlays[this.#active].callerCloseMethod();
|
||||||
_closeThroughCaller() {
|
|
||||||
if (this._overlays[this._active].callerCloseMethod) {
|
|
||||||
this._overlays[this._active].callerCloseMethod();
|
|
||||||
}
|
}
|
||||||
if (this._active) {
|
if (this.#active) {
|
||||||
this.close(this._active);
|
this.close(this.#active);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user