@bigfishtv/cockpit
Version:
60 lines (53 loc) • 1.4 kB
JavaScript
/**
* @module Core/pluginRegistry
*/
let components = []
/**
* Returns an object of convenience methods
*
* @param {String} id The container ID
*/
export function getContainer(id) {
const self = {
add: (name, component) => {
addComponent(id, name, component)
return self
},
all: () => getComponents(id),
clear: () => clearContainer(id),
}
return self
}
/**
* Adds a component to a container
*
* @param {String} container The container ID
* @param {String} name Name of the component
* @param {ReactComponent} component Component to render
*/
export function addComponent(container, name, component) {
if (components.find(c => c.container === container && c.name === name)) {
throw new Error(`Cockpit.pluginRegistry: Component "${name}" already exists within container "${container}".`)
}
components.push({ container, name, component })
}
/**
* RETURNS
* @param {String} container The container ID
*/
export function getComponents(container) {
return components.filter(c => c.container === container)
}
/**
* Adds a component to a container
*
* @param {String} container The container ID
* @param {String} name Name of the component
* @param {ReactComponent} component Component to render
*/
export function clearContainer(container) {
components = components.filter(c => c.container !== container)
}
export function clearAll() {
components = []
}