2 let componentMapping = {
3 'dropdown': require('./dropdown'),
4 'overlay': require('./overlay'),
5 'back-to-top': require('./back-top-top'),
6 'notification': require('./notification'),
7 'chapter-toggle': require('./chapter-toggle'),
8 'expand-toggle': require('./expand-toggle'),
9 'entity-selector-popup': require('./entity-selector-popup'),
10 'entity-selector': require('./entity-selector'),
11 'sidebar': require('./sidebar'),
12 'page-picker': require('./page-picker'),
13 'page-comments': require('./page-comments'),
16 window.components = {};
18 let componentNames = Object.keys(componentMapping);
22 * Initialize components of the given name within the given element.
23 * @param {String} componentName
24 * @param {HTMLElement|Document} parentElement
26 function initComponent(componentName, parentElement) {
27 let elems = parentElement.querySelectorAll(`[${componentName}]`);
28 if (elems.length === 0) return;
30 let component = componentMapping[componentName];
31 if (typeof window.components[componentName] === "undefined") window.components[componentName] = [];
32 for (let j = 0, jLen = elems.length; j < jLen; j++) {
33 let instance = new component(elems[j]);
34 if (typeof elems[j].components === 'undefined') elems[j].components = {};
35 elems[j].components[componentName] = instance;
36 window.components[componentName].push(instance);
41 * Initialize all components found within the given element.
42 * @param parentElement
44 function initAll(parentElement) {
45 if (typeof parentElement === 'undefined') parentElement = document;
46 for (let i = 0, len = componentNames.length; i < len; i++) {
47 initComponent(componentNames[i], parentElement);
51 window.components.init = initAll;