forked from cyclejs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (52 loc) · 1.37 KB
/
app.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
let xs = require('xstream').default;
let {div, ul, li, a, section, h1, p} = require('@cycle/dom');
function renderMenu() {
return (
ul([
li([ a('.link', {attrs: {href: '/'}}, 'Home') ]),
li([ a('.link', {attrs: {href: '/about'}}, 'About') ]),
])
);
}
function renderHomePage() {
return (
section('.home', [
h1('The homepage'),
p('Welcome to our spectacular web page with nothing special here.'),
renderMenu(),
])
);
}
function renderAboutPage() {
return (
section('.about', [
h1('Read more about us'),
p('This is the page where we describe ourselves.'),
p('Contact us'),
renderMenu(),
])
);
}
function app(sources) {
let click$ = sources.DOM.select('.link').events('click');
let preventedEvent$ = click$;
let contextFromClick$ = click$
.map(ev => ({route: ev.currentTarget.attributes.href.value}));
let context$ = xs.merge(sources.context, contextFromClick$);
let vtree$ = context$
.map(({route}) => {
if (typeof window !== 'undefined') {
window.history.pushState(null, '', route);
}
switch (route) {
case '/': return renderHomePage();
case '/about': return renderAboutPage();
default: return div(`Unknown page ${route}`)
}
});
return {
DOM: vtree$,
PreventDefault: preventedEvent$
};
}
module.exports = app;