iwmlib/lib/poppable.js

72 lines
1.6 KiB
JavaScript

/** Basic class for poppable elements that need to be closed as soon as one poppable is
* shown.
*/
export default class Poppable {
/** Register the poppable element in a context. Closes previously registered ones.
* @param {*} context
*/
register(context) {
let registered = Poppable.get(context)
if (registered != null) {
registered.close()
}
Poppable.set(context, this)
}
/**
* Unregister object from context
* @param {*} context
*/
unregister(context) {
Poppable.delete(context)
}
/**
* Returns the given poppable in a context
* @static
* @param {*} context
* @returns
* @memberof Poppable
*/
static get(context) {
return Poppable.registrations.get(context)
}
/** Sets the poppable in the given context
* @static
* @param {*} context
* @param {*} poppable
* @returns
* @memberof Poppable
*/
static set(context, poppable) {
return Poppable.registrations.set(context, poppable)
}
/** Test whether a poppable exists in the given context
*
* @param {*} context
*/
static has(context) {
return Poppable.registrations.has(context)
}
/**
* Removes the poppable from the given context.
*
* @static
* @param {*} context
* @memberof Poppable
*/
static delete(context) {
Poppable.registrations.delete(context)
}
/** All poppable must implement a close method. */
close() {
console.error("Must be implemented")
}
}
Poppable.registrations = new Map()