iwmlib/lib/poppable.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-03-21 09:57:27 +01:00
/** 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.
2019-07-18 12:26:39 +02:00
* @param {*} context
2019-03-21 09:57:27 +01:00
*/
register(context) {
let registered = Poppable.get(context)
if (registered != null) {
registered.close()
}
Poppable.set(context, this)
}
/**
* Unregister object from context
2019-07-18 12:26:39 +02:00
* @param {*} context
2019-03-21 09:57:27 +01:00
*/
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)
}
2019-07-18 12:26:39 +02:00
/** Sets the poppable in the given context
* @static
* @param {*} context
* @param {*} poppable
* @returns
* @memberof Poppable
*/
2019-03-21 09:57:27 +01:00
static set(context, poppable) {
return Poppable.registrations.set(context, poppable)
}
/** Test whether a poppable exists in the given context
2019-07-18 12:26:39 +02:00
*
* @param {*} context
2019-03-21 09:57:27 +01:00
*/
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() {
2019-07-18 12:26:39 +02:00
console.error('Must be implemented')
2019-03-21 09:57:27 +01:00
}
}
2019-07-18 12:26:39 +02:00
Poppable.registrations = new Map()