-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelepathic-loader.js
46 lines (44 loc) · 2.09 KB
/
telepathic-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { importModule } from "./importModule.js";
//IIFE to try and load all *-element - class name must match SomethingElement while tag must match something-element
export default class TelepathicLoader{
static async Load(dom){
let nodeList = dom.querySelectorAll('*');
for(let element of nodeList){
if(element.tagName.includes('-ELEMENT')){
let tagName = element.tagName.toLowerCase();
//First see if there is already something in the custom element registry
if(!window.customElements.get(tagName)){
let jsFileName = `${tagName}.js`;
let classParts = tagName.split('-');
let className = "";
for(let part of classParts){
className += part.charAt(0).toUpperCase() + part.slice(1);
}
window[tagName] = className;
let path = window.location.pathname.split('/');
path.pop();
path = path.join('/');
window[className] = path;
let jsLoc = `${path}/${tagName}/${jsFileName}`;
let module;
try{
console.log(`importing ${jsLoc}`);
module = await importModule(jsLoc);
}catch(err){
//module is probably remote
path = 'https://telepathic-elements.github.io';
jsLoc = `${path}/${tagName}/${jsFileName}`;
console.log(`importing ${jsLoc}`);
module = await importModule(jsLoc);
}
console.log(tagName+" module is ",module);
await window.customElements.define(tagName,module.default);
}else{
console.log(`${tagName} already loaded, skipping!`);
}
}
}
}
}
window.TelepathicLoader = TelepathicLoader;
TelepathicLoader.Load(document);