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