@bigfishtv/cockpit
Version:
77 lines (70 loc) • 1.81 kB
JavaScript
;
exports.__esModule = true;
exports.getContainer = getContainer;
exports.addComponent = addComponent;
exports.getComponents = getComponents;
exports.clearContainer = clearContainer;
exports.clearAll = clearAll;
/**
* @module Core/pluginRegistry
*/
var components = [];
/**
* Returns an object of convenience methods
*
* @param {String} id The container ID
*/
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
*/
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
*/
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
*/
function clearContainer(container) {
components = components.filter(function (c) {
return c.container !== container;
});
}
function clearAll() {
components = [];
}