-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
83 lines (82 loc) · 2.52 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Wand JS router</title>
</head>
<body>
<h1><img src="favicon.ico"> Wand JS router</h1>
<nav>
<a href="#/">Home</a> |
<a href="#/hello/Mary">Hello Mary</a> |
<a
href="#/hello/John?age=25&pets[]=dog&pets[]=cat"
>Hello John</a> |
<a href="#/goodbye?name=stranger">Goodbye</a> |
<a href="#/clock">Clock</a> |
<a href="#/wrong/route">404</a> |
<a href="javascript:stop()">Stop Router</a> |
<a href="https://github.com/marcodpt/wand">Repository</a>
</nav>
<main>
<h1>Home Page</h1>
</main>
<pre>
<code></code>
</pre>
<script type="module">
import hashRouter from "./src/runtimes/hashRouter.js"
window.stop = hashRouter({
init: () => {
const main = document.body.querySelector('main')
return {
index: 0,
home: main.innerHTML.trim(),
render: html => {main.innerHTML = html}
}
},
routes: {
'/': ({
render, home
}) => render(home),
'/hello/:name': ({
render, Params
}) => render(`<h1>Hello ${Params.name}</h1>`),
'/clock': ({render}) => {
const tick = () => {
console.log('tick')
const time = new Date()
render(`<h1>${[
time.getHours(),
time.getMinutes(),
time.getSeconds()
].map(n => (n < 10 ? '0' : '')+n).join(':')}</h1>`)
}
const itv = setInterval(tick, 100)
return () => {clearInterval(itv)}
},
'/goodbye': ({render, Query}) => {
render(`<h1>Goodbye message before leaving!</h1>`)
return () => {window.alert(`Goodbye ${Query.name}!`)}
},
'*': ({
render
}) => render(`<h1>404: Page Not Found</h1>`)
},
plugins: [
state => {
state.index++
},
state => {
document.body.querySelector('code').
textContent = JSON.stringify(state, undefined, 2)
}
]
})
</script>
</body>
</html>