]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/index.js
Add socialite authentication for okta
[bookstack] / resources / assets / js / components / index.js
1
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'),
14     'wysiwyg-editor': require('./wysiwyg-editor'),
15     'markdown-editor': require('./markdown-editor'),
16     'editor-toolbox': require('./editor-toolbox'),
17 };
18
19 window.components = {};
20
21 let componentNames = Object.keys(componentMapping);
22 initAll();
23
24 /**
25  * Initialize components of the given name within the given element.
26  * @param {String} componentName
27  * @param {HTMLElement|Document} parentElement
28  */
29 function initComponent(componentName, parentElement) {
30     let elems = parentElement.querySelectorAll(`[${componentName}]`);
31     if (elems.length === 0) return;
32
33     let component = componentMapping[componentName];
34     if (typeof window.components[componentName] === "undefined") window.components[componentName] = [];
35     for (let j = 0, jLen = elems.length; j < jLen; j++) {
36         let instance = new component(elems[j]);
37         if (typeof elems[j].components === 'undefined') elems[j].components = {};
38         elems[j].components[componentName] = instance;
39         window.components[componentName].push(instance);
40     }
41 }
42
43 /**
44  * Initialize all components found within the given element.
45  * @param parentElement
46  */
47 function initAll(parentElement) {
48     if (typeof parentElement === 'undefined') parentElement = document;
49     for (let i = 0, len = componentNames.length; i < len; i++) {
50         initComponent(componentNames[i], parentElement);
51     }
52 }
53
54 window.components.init = initAll;