2020-06-24 15:38:08 -04:00
|
|
|
const componentMapping = {};
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2020-06-24 15:38:08 -04:00
|
|
|
const definitionFiles = require.context('./', false, /\.js$/);
|
|
|
|
for (const fileName of definitionFiles.keys()) {
|
|
|
|
const name = fileName.replace('./', '').split('.')[0];
|
|
|
|
if (name !== 'index') {
|
|
|
|
componentMapping[name] = definitionFiles(fileName).default;
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2020-06-24 15:38:08 -04:00
|
|
|
window.components = {};
|
2017-08-06 13:01:49 -04:00
|
|
|
|
2017-09-09 10:56:24 -04:00
|
|
|
/**
|
|
|
|
* Initialize components of the given name within the given element.
|
|
|
|
* @param {String} componentName
|
|
|
|
* @param {HTMLElement|Document} parentElement
|
|
|
|
*/
|
2020-06-24 15:38:08 -04:00
|
|
|
function searchForComponentInParent(componentName, parentElement) {
|
|
|
|
const elems = parentElement.querySelectorAll(`[${componentName}]`);
|
2017-08-06 13:01:49 -04:00
|
|
|
for (let j = 0, jLen = elems.length; j < jLen; j++) {
|
2020-06-24 15:38:08 -04:00
|
|
|
initComponent(componentName, elems[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a component instance on the given dom element.
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
function initComponent(name, element) {
|
|
|
|
const componentModel = componentMapping[name];
|
|
|
|
if (componentModel === undefined) return;
|
|
|
|
|
|
|
|
// Create our component instance
|
|
|
|
let instance;
|
|
|
|
try {
|
|
|
|
instance = new componentModel(element);
|
|
|
|
instance.$el = element;
|
|
|
|
instance.$refs = parseRefs(name, element);
|
|
|
|
instance.$opts = parseOpts(name, element);
|
|
|
|
if (typeof instance.setup === 'function') {
|
|
|
|
instance.setup();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to create component', e, name, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add to global listing
|
|
|
|
if (typeof window.components[name] === "undefined") {
|
|
|
|
window.components[name] = [];
|
|
|
|
}
|
|
|
|
window.components[name].push(instance);
|
|
|
|
|
|
|
|
// Add to element listing
|
|
|
|
if (typeof element.components === 'undefined') {
|
|
|
|
element.components = {};
|
|
|
|
}
|
|
|
|
element.components[name] = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse out the element references within the given element
|
|
|
|
* for the given component name.
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
function parseRefs(name, element) {
|
|
|
|
const refs = {};
|
|
|
|
const prefix = `${name}@`
|
|
|
|
const refElems = element.querySelectorAll(`[refs*="${prefix}"]`);
|
|
|
|
for (const el of refElems) {
|
|
|
|
const refNames = el.getAttribute('refs')
|
|
|
|
.split(' ')
|
|
|
|
.filter(str => str.startsWith(prefix))
|
|
|
|
.map(str => str.replace(prefix, ''));
|
|
|
|
for (const ref of refNames) {
|
|
|
|
refs[ref] = el;
|
|
|
|
}
|
2017-08-06 13:01:49 -04:00
|
|
|
}
|
2020-06-24 15:38:08 -04:00
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse out the element component options.
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Element} element
|
|
|
|
* @return {Object<String, String>}
|
|
|
|
*/
|
|
|
|
function parseOpts(name, element) {
|
|
|
|
const opts = {};
|
|
|
|
const prefix = `option:${name}:`;
|
|
|
|
for (const {name, value} of element.attributes) {
|
|
|
|
if (name.startsWith(prefix)) {
|
|
|
|
const optName = name.replace(prefix, '');
|
|
|
|
opts[kebabToCamel(optName)] = value || '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a kebab-case string to camelCase
|
|
|
|
* @param {String} kebab
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function kebabToCamel(kebab) {
|
|
|
|
const ucFirst = (word) => word.slice(0,1).toUpperCase() + word.slice(1);
|
|
|
|
const words = kebab.split('-');
|
|
|
|
return words[0] + words.slice(1).map(ucFirst).join();
|
2017-09-09 10:56:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize all components found within the given element.
|
|
|
|
* @param parentElement
|
|
|
|
*/
|
|
|
|
function initAll(parentElement) {
|
|
|
|
if (typeof parentElement === 'undefined') parentElement = document;
|
2020-06-24 15:38:08 -04:00
|
|
|
|
|
|
|
// Old attribute system
|
|
|
|
for (const componentName of Object.keys(componentMapping)) {
|
|
|
|
searchForComponentInParent(componentName, parentElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
// New component system
|
|
|
|
const componentElems = parentElement.querySelectorAll(`[component],[components]`);
|
|
|
|
|
|
|
|
for (const el of componentElems) {
|
|
|
|
const componentNames = `${el.getAttribute('component') || ''} ${(el.getAttribute('components'))}`.toLowerCase().split(' ').filter(Boolean);
|
|
|
|
for (const name of componentNames) {
|
|
|
|
initComponent(name, el);
|
|
|
|
}
|
2017-09-09 10:56:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 12:16:30 -04:00
|
|
|
window.components.init = initAll;
|
|
|
|
|
2018-11-11 06:44:35 -05:00
|
|
|
export default initAll;
|
2020-06-24 15:38:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef Component
|
|
|
|
* @property {HTMLElement} $el
|
|
|
|
* @property {Object<String, HTMLElement>} $refs
|
|
|
|
* @property {Object<String, String>} $opts
|
|
|
|
*/
|