-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.js
213 lines (167 loc) · 4.09 KB
/
os.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
// -- constants --
/// a map of element ids
const kId = {
Page: "page",
}
/// an enum of visit types
const kVisit = {
None: 0,
SameOrigin: 1,
SamePath: 2
}
/// the os
class Os {
// -- props --
/// the current location
url = null
// -- p/el
/// the page container
$page = null
// -- lifetime --
/// create a new os
constructor() {
const m = this
// set props
m.url = document.location
m.$page = document.getElementById(kId.Page)
}
// -- commands --
/// start the os
start() {
const m = this
// bind events
const d = document
d.addEventListener("click", m.didClick)
const w = window
w.addEventListener("popstate", m.didPopState)
// run post visit events first time
m.didFinishVisit()
}
/// navigate to the url
navigate(url) {
const m = this
// add history entry
history.pushState({}, "", url)
// visit page
m.visit(url)
}
/// visit the url and update the game
async visit(url) {
const m = this
// run pre visit events
m.didStartVisit()
// update the browser url
m.url = url
// download the page
const resp = await fetch(url)
const text = await resp.text()
// render the element
const $el = document.createElement("html")
$el.innerHTML = text
// extract the page
const $next = $el.querySelector(`#${kId.Page}`)
// replace children of page element
while (m.$page.firstChild) {
m.$page.removeChild(m.$page.lastChild)
}
for (const child of Array.from($next.children)) {
m.$page.appendChild(child)
}
// TODO: do we need this?
// activate any inert script tags on the new page
const scripts = m.$page.querySelectorAll("script")
for (const inert of Array.from(scripts)) {
// clone the inert script tag
const script = document.createElement("script")
script.textContent = inert.textContent
for (const { name, value } of inert.attributes) {
script.setAttribute(name, value)
}
// and replace it with the active one
const parent = inert.parentElement
parent.replaceChild(script, inert)
}
// run post visit events
m.didFinishVisit()
}
// -- queries --
/// get the visit type for a change to this url
getVisit(url) {
const m = this
// default to no visit, browser nav
let type = kVisit.None
// if the origin matches
if (m.url.origin === url.origin) {
type = kVisit.SameOrigin
// if the path matches
if (m.url.pathname === url.pathname) {
type = kVisit.SamePath
}
}
return type
}
// -- events --
/// when anything is clicked on
didClick = (evt) => {
const m = this
// see if there is an enclosing link
let $t = evt.target
while ($t != null && $t.tagName.toLowerCase() !== "a") {
$t = $t.parentElement
}
// if, we didn't find a link, ignore
if ($t == null) {
return
}
// if it has a target (like "_blank"), ignore
if ($t.getAttribute("target")) {
return
}
// grab its url (an svg link's href is an object)
let href = $t.href
if (typeof href === "object") {
href = href.baseVal.toString()
}
// if it has no url, ignore
if (!href) {
return
}
// get the visit type
const url = new URL(href, m.url)
const visit = m.getVisit(url)
// if none, ignore
if (visit === kVisit.None) {
return
}
// if some, cancel the click
evt.preventDefault()
// if not same path, run the visit
if (visit != kVisit.SamePath) {
m.navigate(url)
}
}
/// when back is clicked
didPopState = () => {
const m = this
// get the visit for this url
const url = new URL(document.location.href)
const visit = m.getVisit(url)
// if none, do what the browser wants
if (visit === kVisit.None) {
return
}
// otherwise, visit the url
m.visit(url)
}
/// when a visit starts
didStartVisit() {
}
/// when a visit finishes
didFinishVisit() {
}
}
// -- singleton --
const os = new Os()
window.os = os
// -- bootstrap --
os.start()