-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspa.js
246 lines (222 loc) · 6.73 KB
/
spa.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// simple homemade promise class :D
class Promise {
constructor(resolve, reject) {
this.resolved = null;
this.handleResolve = resolve;
this.rejected = null;
this.handleReject = reject;
}
then(fn) {
this.handleResolve = fn;
if (this.resolved) this.handleResolve(...this.resolved);
}
catch(fn) {
this.handleReject = fn;
if (this.rejected) this.handleReject(...this.rejected);
}
resolve(...args) {
if (this.handleResolve) this.handleResolve(...args);
else this.resolved = [...args];
}
reject(...args) {
if (this.handleReject) this.handleReject(...args);
else this.rejected = [...args];
}
}
let controller = { };
const loadFile = file => {
const p = new Promise();
const xhr = new XMLHttpRequest();
xhr.open("GET", file);
xhr.onload = () => p.resolve(xhr.responseText);
xhr.onerror = () => p.reject(xhr.statusText);
xhr.send();
return p;
}
const loadPage = hash => {
const p = new Promise();
const { page, file } = getFilenameFromHash(hash);
loadFile(file).then(contents => {
p.resolve(page, contents);
});
return p;
}
const writeContents = (hash, contents) => {
const hotspots = document.querySelectorAll('[hotspot]');
let destination;
let blank;
for (let i = 0; i < hotspots.length; i++) {
const spot = hotspots[i];
const hashes = spot.getAttribute('hotspot');
if (!hashes) blank = spot;
const hashArray = hashes.split(' ');
if (hashArray.includes(hash)) {
destination = spot;
break;
}
}
// if we didn't find the hotspot for this hash, use the default one
if (!destination) destination = blank;
// if we STILL don't have a destination, we have nowhere to put content
if (!destination) {
console.error('NO DESTINATION FOR HASH', hash);
return;
}
const contentspot = destination.querySelector('[content]') || destination;
loadContents(contentspot, contents).then(() => {
const last = document.querySelector('[hotspot]:not([hidden])');
last && last.setAttribute('hidden', '');
destination.removeAttribute('hidden');
})
}
const loadContents = (element, contents) => {
const p = new Promise();
const { css, html, js } = parseContents(contents);
let lastCss = document.head.querySelector('[temporary]');
if (lastCss) document.head.removeChild(lastCss);
document.head.innerHTML += css;
element.innerHTML = html;
// wrap this js in it's own scope
let me = eval('(() => {' + js + '})()');
processChildren(element, me).then(() => p.resolve());
return p;
}
const processChildren = (element, ctrl) => {
let p = new Promise();
var me = ctrl;
const children = element.children;
let waiting = children.length;
const proceed = (child, scope) => {
for (let a = 0; a < child.attributes.length; a++) {
let attr = child.attributes[a];
if (attr.name.startsWith('on')) {
let eventName = attr.name;
let action = attr.value;
child[eventName] = (...args) => {
eval(action);
};
}
}
processChildren(child, scope).then(() => {
waiting--;
if (waiting == 0) {
p.resolve();
}
});
}
if (waiting == 0) p.resolve();
for (let c = 0; c < children.length; c++){
let child = children[c];
if(child.tagName === 'TEMPLATE') {
const innerHtml = child.innerHTML;
loadPage(child.getAttribute('type')).then((type, contents) => {
let { css, html, js } = parseContents(contents);
for (let a = 0; a < child.attributes.length; a++) {
let attr = child.attributes[a];
var patt = new RegExp('{{' + attr.name + '}}', 'gi');
html = html.replace(patt, attr.value);
}
if (css) document.head.innerHTML += css;
child.outerHTML = html
child = children[c];
child.innerHTML += innerHtml;
if (js) {
const parent = me;
me = eval('(() => {' + js + '})()');
me.parent = parent;
}
proceed(child, me);
});
} else {
proceed(child, me);
}
}
return p;
}
const process = (element) => {
}
// warning - does not work with nested tags of the same type!
const findTag = (contents, tag, options) => {
options = options || {};
const open = '<' + tag + (!options.selfClosing ? '>' : '');
let start = contents.indexOf(open);
let end;
const close = options.selfClosing ? '/>' : '</' + tag + '>';
const closeLength = close.length;
if (start > -1) {
end = contents.substr(start).indexOf(close);
}
if (start > -1 && end > -1) {
let result = contents.substr(start, end + closeLength);
if (options.innerHTML) {
result = result.substr(open.length, result.length - (open.length + closeLength))
}
return result;
}
return '';
}
const setAttribute = (contents, name, value) => {
let attributeText;
if (value) {
attributeText = ' ' + name + '="' + value + '" ';
} else {
attributeText = ' ' + name + ' ';
}
// handle self-closing tags
if (contents.endsWith('/>')) {
return contents.substr(0, contents.length - 2) + attributeText + '/>';
}
var index = contents.indexOf('>');
return contents.substr(0, index) + attributeText + contents.substr(index);
}
const getAttribute = (contents, name) => {
var attr = contents.indexOf(name + '="');
if (attr < 0) {
attr = contents.indexOf(name);
if (attr > -1) return true;
return '';
};
var valText = contents.substr(attr).indexOf('"');
return contents.substr(attr, valText);
}
const parseContents = contents => {
let css = findTag(contents, 'style');
if (!css) css = findTag(contents, 'link', { selfClosing: true });
if (css) css = setAttribute(css, 'temporary', '');
let html = findTag(contents, 'html', { innerHTML: true });
let js = findTag(contents, 'script', { innerHTML: true });
if (!js) {
js = findTag(contents, 'script', { selfClosing: true });
// this isn't supported... yet
}
console.log(css, '\n\n', html, '\n\n', js);
html = html || contents;
return { css, html, js };
}
const getFilenameFromHash = hash => {
const path = window.location.pathname;
if (!hash || hash == '/') {
hash = document.body.getAttribute('start') || 'index';
}
return { page: hash, file: path + hash + '.html' };
}
window.addEventListener('hashchange', e => {
loadPage(window.location.hash.substr(1)).then(writeContents);
}, false);
document.addEventListener('DOMContentLoaded', e => {
document.head.innerHTML += style;
const hotspots = document.querySelectorAll('[hotspot]');
let destination;
for (let i = 0; i < hotspots.length; i++) {
const spot = hotspots[i];
spot.setAttribute('hidden', '');
}
loadPage(window.location.hash.substr(1)).then(writeContents);
}, false);
const style = `
<style>
[hotspot][hidden] {
display: none;
}
</style>
`;