@bigfishtv/cockpit
Version:
69 lines (63 loc) • 1.62 kB
JavaScript
/**
* @module Core/pluginRegistry
*/
var components = [];
/**
* Returns an object of convenience methods
*
* @param {String} id The container ID
*/
export function getContainer(id) {
var self = {
add: function add(name, component) {
addComponent(id, name, component);
return self;
},
all: function all() {
return getComponents(id);
},
clear: function clear() {
return 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(function (c) {
return c.container === container && c.name === name;
})) {
throw new Error("Cockpit.pluginRegistry: Component \"" + name + "\" already exists within container \"" + container + "\".");
}
components.push({ container: container, name: name, component: component });
}
/**
* RETURNS
* @param {String} container The container ID
*/
export function getComponents(container) {
return components.filter(function (c) {
return 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(function (c) {
return c.container !== container;
});
}
export function clearAll() {
components = [];
}