try {
instance = new componentModel(element);
instance.$el = element;
- instance.$refs = parseRefs(name, element);
+ const allRefs = parseRefs(name, element);
+ instance.$refs = allRefs.refs;
+ instance.$manyRefs = allRefs.manyRefs;
instance.$opts = parseOpts(name, element);
if (typeof instance.setup === 'function') {
instance.setup();
*/
function parseRefs(name, element) {
const refs = {};
+ const manyRefs = {};
+
const prefix = `${name}@`
- const refElems = element.querySelectorAll(`[refs*="${prefix}"]`);
+ const selector = `[refs*="${prefix}"]`;
+ const refElems = [...element.querySelectorAll(selector)];
+ if (element.matches(selector)) {
+ refElems.push(element);
+ }
+
for (const el of refElems) {
const refNames = el.getAttribute('refs')
.split(' ')
.filter(str => str.startsWith(prefix))
- .map(str => str.replace(prefix, ''));
+ .map(str => str.replace(prefix, ''))
+ .map(kebabToCamel);
for (const ref of refNames) {
refs[ref] = el;
+ if (typeof manyRefs[ref] === 'undefined') {
+ manyRefs[ref] = [];
+ }
+ manyRefs[ref].push(el);
}
}
- return refs;
+ return {refs, manyRefs};
}
/**
}
window.components.init = initAll;
+window.components.first = (name) => (window.components[name] || [null])[0];
export default initAll;
* @typedef Component
* @property {HTMLElement} $el
* @property {Object<String, HTMLElement>} $refs
+ * @property {Object<String, HTMLElement[]>} $manyRefs
* @property {Object<String, String>} $opts
*/
\ No newline at end of file