]> BookStack Code Mirror - bookstack/blob - resources/js/components/index.js
Started migrating tag manager JS to HTML-first component
[bookstack] / resources / js / components / index.js
1 const componentMapping = {};
2
3 const definitionFiles = require.context('./', false, /\.js$/);
4 for (const fileName of definitionFiles.keys()) {
5     const name = fileName.replace('./', '').split('.')[0];
6     if (name !== 'index') {
7         componentMapping[name] = definitionFiles(fileName).default;
8     }
9 }
10
11 window.components = {};
12
13 /**
14  * Initialize components of the given name within the given element.
15  * @param {String} componentName
16  * @param {HTMLElement|Document} parentElement
17  */
18 function searchForComponentInParent(componentName, parentElement) {
19     const elems = parentElement.querySelectorAll(`[${componentName}]`);
20     for (let j = 0, jLen = elems.length; j < jLen; j++) {
21         initComponent(componentName, elems[j]);
22     }
23 }
24
25 /**
26  * Initialize a component instance on the given dom element.
27  * @param {String} name
28  * @param {Element} element
29  */
30 function initComponent(name, element) {
31     const componentModel = componentMapping[name];
32     if (componentModel === undefined) return;
33
34     // Create our component instance
35     let instance;
36     try {
37         instance = new componentModel(element);
38         instance.$el = element;
39         const allRefs = parseRefs(name, element);
40         instance.$refs = allRefs.refs;
41         instance.$manyRefs = allRefs.manyRefs;
42         instance.$opts = parseOpts(name, element);
43         if (typeof instance.setup === 'function') {
44             instance.setup();
45         }
46     } catch (e) {
47         console.error('Failed to create component', e, name, element);
48     }
49
50
51     // Add to global listing
52     if (typeof window.components[name] === "undefined") {
53         window.components[name] = [];
54     }
55     window.components[name].push(instance);
56
57     // Add to element listing
58     if (typeof element.components === 'undefined') {
59         element.components = {};
60     }
61     element.components[name] = instance;
62 }
63
64 /**
65  * Parse out the element references within the given element
66  * for the given component name.
67  * @param {String} name
68  * @param {Element} element
69  */
70 function parseRefs(name, element) {
71     const refs = {};
72     const manyRefs = {};
73     const prefix = `${name}@`
74     const refElems = element.querySelectorAll(`[refs*="${prefix}"]`);
75     for (const el of refElems) {
76         const refNames = el.getAttribute('refs')
77             .split(' ')
78             .filter(str => str.startsWith(prefix))
79             .map(str => str.replace(prefix, ''));
80         for (const ref of refNames) {
81             refs[ref] = el;
82             if (typeof manyRefs[ref] === 'undefined') {
83                 manyRefs[ref] = [];
84             }
85             manyRefs[ref].push(el);
86         }
87     }
88     return {refs, manyRefs};
89 }
90
91 /**
92  * Parse out the element component options.
93  * @param {String} name
94  * @param {Element} element
95  * @return {Object<String, String>}
96  */
97 function parseOpts(name, element) {
98     const opts = {};
99     const prefix = `option:${name}:`;
100     for (const {name, value} of element.attributes) {
101         if (name.startsWith(prefix)) {
102             const optName = name.replace(prefix, '');
103             opts[kebabToCamel(optName)] = value || '';
104         }
105     }
106     return opts;
107 }
108
109 /**
110  * Convert a kebab-case string to camelCase
111  * @param {String} kebab
112  * @returns {string}
113  */
114 function kebabToCamel(kebab) {
115     const ucFirst = (word) => word.slice(0,1).toUpperCase() + word.slice(1);
116     const words = kebab.split('-');
117     return words[0] + words.slice(1).map(ucFirst).join();
118 }
119
120 /**
121  * Initialize all components found within the given element.
122  * @param parentElement
123  */
124 function initAll(parentElement) {
125     if (typeof parentElement === 'undefined') parentElement = document;
126
127     // Old attribute system
128     for (const componentName of Object.keys(componentMapping)) {
129         searchForComponentInParent(componentName, parentElement);
130     }
131
132     // New component system
133     const componentElems = parentElement.querySelectorAll(`[component],[components]`);
134
135     for (const el of componentElems) {
136         const componentNames = `${el.getAttribute('component') || ''} ${(el.getAttribute('components'))}`.toLowerCase().split(' ').filter(Boolean);
137         for (const name of componentNames) {
138             initComponent(name, el);
139         }
140     }
141 }
142
143 window.components.init = initAll;
144 window.components.first = (name) => (window.components[name] || [null])[0];
145
146 export default initAll;
147
148 /**
149  * @typedef Component
150  * @property {HTMLElement} $el
151  * @property {Object<String, HTMLElement>} $refs
152  * @property {Object<String, HTMLElement[]>} $manyRefs
153  * @property {Object<String, String>} $opts
154  */