-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmain.js
138 lines (111 loc) · 2.88 KB
/
main.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
import roadtrip from 'roadtrip';
import Index from '../shared/routes/Index.html';
import BlogIndex from '../shared/routes/BlogIndex.html';
import BlogPost from '../shared/routes/BlogPost.html';
import Guide from '../shared/routes/Guide.html';
import Nav from '../shared/components/Nav.html';
import get from './get.js';
const header = document.querySelector( 'header' );
const main = document.querySelector( 'main' );
const nav = new Nav({
target: ( header.innerHTML = '', header )
});
let view;
// legacy
function redirect ( from, to ) {
roadtrip.add( from, {
enter: route => {
if ( typeof to === 'function' ) to = to( route );
roadtrip.goto( to, { replaceState: true });
}
});
}
redirect( '/blog/', '/blog' );
redirect( '/guide/', '/guide' );
redirect( '/blog:slug/', route => `/blog/${route.params.slug}` );
roadtrip
.add( '/', {
enter ( route ) {
nav.set({ route: 'index' });
if ( route.isInitial ) return; // page is static
document.title = 'Svelte • The magical disappearing UI framework';
if ( view ) {
view.destroy();
} else {
main.innerHTML = '';
}
view = new Index({
target: main
});
window.scrollTo( route.scrollX, route.scrollY );
}
})
.add( '/blog', {
enter ( route ) {
nav.set({ route: 'blog' });
if ( route.isInitial ) return; // page is static
document.title = 'Svelte • The magical disappearing UI framework';
if ( view ) {
view.destroy();
} else {
main.innerHTML = '';
}
return get( `/blog.json` ).then( JSON.parse ).then( posts => {
view = new BlogIndex({
target: main,
data: {
posts
}
});
window.scrollTo( route.scrollX, route.scrollY );
});
}
})
.add( '/blog/:slug', {
enter ( route ) {
nav.set({ route: 'blog' });
if ( route.isInitial ) return; // page is static
return get( `/blog/${route.params.slug}.json` ).then( JSON.parse ).then( post => {
document.title = `${post.metadata.title} • Svelte`;
if ( view ) {
view.destroy();
} else {
main.innerHTML = '';
}
view = new BlogPost({
target: main,
data: {
post
}
});
// TODO this doesn't work because it's the <main> that scrolls, not the window
window.scrollTo( route.scrollX, route.scrollY );
});
}
})
.add( '/guide', {
enter ( route ) {
nav.set({ route: 'guide' });
if ( route.isInitial ) return; // page is static
document.title = 'Learn Svelte';
if ( view ) {
view.destroy();
} else {
main.innerHTML = '';
}
return get( `/guide.json` ).then( JSON.parse ).then( sections => {
view = new Guide({
target: main,
data: {
sections
}
});
// scroll to section
if ( window.location.hash.length > 1 ) {
const h = main.querySelector( window.location.hash );
if ( h ) window.scrollTo( 0, h.getBoundingClientRect().top );
}
});
}
});
roadtrip.start();