-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
178 lines (154 loc) · 4.68 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
export const loadComponent = async (options) => {
!options.urlPrefix && (options.urlPrefix = "");
let filePromises = [];
let template;
filePromises.push(
(async () => {
template = await getFile(
options.urlPrefix + options.template,
options.version
);
})()
);
let styles = [];
for (let i of options.styles)
styles.push(getFile(options.urlPrefix + i, options.version));
filePromises.push(
(async () => {
for (let promiseIndex in styles) {
styles[promiseIndex] = await styles[promiseIndex];
}
})()
);
await Promise.all(filePromises);
customElements.define(
options.name,
class extends HTMLElement {
component;
constructor() {
super();
let loadQueueResolve;
window.uiBuilder.loadQueue.push(
new Promise((r) => (loadQueueResolve = r))
);
let templateElement = document.createElement("template");
templateElement.innerHTML = template;
const shadow = this.attachShadow({ mode: "open" });
shadow.appendChild(templateElement.content.cloneNode(true));
if (options.styles)
(async () => {
for (let i in options.styles) {
let style = document.createElement("style");
style.innerHTML = styles[i];
this.shadowRoot.appendChild(style);
}
})();
import(
(options.importPrefix ? options.importPrefix : options.urlPrefix) +
options.component
).then(async (componentImport) => {
this.component = await new componentImport.Component({
shadowDom: this.shadowRoot,
component: this,
document,
});
this.existingAttributeCallbackEmitter();
this.ready = true;
let event = new Event("ready", {
bubbles: false,
cancelable: false,
});
this.dispatchEvent(event);
loadQueueResolve();
});
let observer = new MutationObserver(
this.attributeChangedCallback.bind(this)
);
observer.observe(this, { attributeOldValue: true });
}
attributeChangedCallback(mutations) {
for (let mutation of mutations) {
if (mutation.type == "attributes") {
this.component?.attributeChangedCallback?.(
mutation.attributeName,
mutation.oldValue,
mutation.target.getAttribute(mutation.attributeName)
);
}
}
}
existingAttributeCallbackEmitter() {
for (let attribute of this.attributes)
this.component?.attributeChangedCallback?.(
attribute.name,
null,
attribute.value
);
}
}
);
};
export const loadPack = async (packUrl, options) => {
let pack = await (await fetch(packUrl)).json();
await applyPack({ ...options, ...{ pack } });
};
export const applyPack = async (options) => {
let components = options.pack.components;
options.version = options.pack.version;
let componentLoadPromises = [];
if (options.customStyleSheets) {
for (let i of Object.keys(components)) {
components[i].styles = [
...components[i].styles,
...options.customStyleSheets,
];
}
}
let componentsAwait = [];
for (let i of Object.keys(components)) {
componentsAwait.push(window.customElements.whenDefined(components[i].name));
let component = components[i];
componentLoadPromises.push(loadComponent({ ...options, ...component }));
}
await Promise.all(componentLoadPromises);
await Promise.all(componentsAwait);
await Promise.all(uiBuilder.loadQueue);
};
export const ready = async (elem) => {
if (elem.ready) return;
await new Promise((r) => elem.addEventListener("ready", r));
};
window.uiBuilder = {
ready,
applyPack,
loadPack,
loadComponent,
loadQueue: [],
};
const loadFile = (url) => {
let filesRaw = localStorage.getItem("files");
if (!filesRaw) return { success: false };
let files = JSON.parse(filesRaw);
if (!files[url]) {
return { success: false };
}
return { success: true, file: files[url] };
};
const saveFile = (url, data, version) => {
let filesRaw = localStorage.getItem("files");
if (!filesRaw) filesRaw = "{}";
let files = JSON.parse(filesRaw);
files[url] = {
data,
version,
};
localStorage.setItem("files", JSON.stringify(files));
};
const getFile = async (url, version) => {
let cachedFile = loadFile(url);
if (cachedFile.success && version && version == cachedFile.file.version)
return cachedFile.file.data;
let text = await (await fetch(url)).text();
saveFile(url, text, version);
return text;
};