/
index.es
51 lines (34 loc) · 990 Bytes
/
index.es
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
Element `nav-view`
(class extends HTMLElement {
initialize () {
/comp|inter|loaded/.test (document.readyState)
? this.onload ()
: document.addEventListener
('DOMContentLoaded', this.onload)
}
onload () {
(this.context.views = Array.from
(document.body.querySelectorAll `[view]`))
.map (this.hide)
&& (this.context.views [0].hidden = false)
// what's the difference between `hidden`ing and `display: none`
this.render ()
}
onview (event, anchor = event.target) {
event.prevent ()
this.context.views.map (this.hide)
this.identify (event.target)
.hidden = false
document.title = anchor.title
}
hide (view)
{ return (view.hidden = true) && view }
identify (anchor) {
return document.getElementById
(anchor.href.match (/#(.+)$/)[1])
}
get anchors () {
return (this.context.views || [])
.map (view => ({ href: `#${view.id}`, text: view.title }))
}
})