-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
119 lines (98 loc) · 3.43 KB
/
index.html
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
<html>
<head>
<script>
const window = globalThis; // for browser compatibility
class Xhr {
constructor () {
this.headers = {};
}
addEventListener(name, callback){
switch (name) {
case "load":
this.load = callback;
}
}
open(method, url){
this.method = method;
this.url = url;
}
setRequestHeader(name, value){
this.headers[name] = value;
}
async send(body){
console.log("req: " + this.url);
var result = await fetch(this.url, {
method: this.method,
headers: this.headers
});
console.log(result.headers);
this.status = result.status;
this.statusText = result.statusText;
var responseHeaders = "";
for(var h in result.headers){
responseHeaders += h + ": " + result.headers[h] + "\r\n"
}
this.responseHeaders = responseHeaders;
this.response = await result.text();
// this.response = result.body;
if(this.load){
console.log("load");
this.load(result.body);
}
}
getAllResponseHeaders(){
return this.responseHeaders;
}
}
window.XMLHttpRequest = Xhr;
</script>
<script src="elm/elm.js"></script>
</head>
<body>
</body>
<script>
var app = Elm.Main.init({});
app.ports.render.subscribe((doc) => {
Window.this.caption = doc.title;
var rendered = doc.body.map(render);
document.body.patch(rendered);
});
function render(html) {
if (typeof html == "string"){
return html;
}
else {
return JSX(html.tag, renderAttributes(html.attributes), html.content.map(render));
}
}
function renderAttributes(attributes){
var result = {};
for (var attribute of attributes) {
if(attribute.attribute){
result[attribute.name] = attribute.value;
}
else if(attribute.property){
// ???
}
else if (attribute.event){
result["on" + attribute.name] = (ev) => {
var r = {};
for (var k in ev){
r[k]=ev[k];
}
if(ev.target){
var t = {}
for (var k in ev.target){
t[k]=ev.target[k];
}
r.target = t;
}
console.log(r);
app.ports.onEvent.send([attribute.key, r]);
}
}
}
return result;
}
</script>
</html>