Files: 119e5a731640d722b01b4aa15a4ec30a09b17285 / app / html / app.js
923 bytesRaw
1 | const nest = require('depnest') |
2 | const values = require('lodash/values') |
3 | const insertCss = require('insert-css') |
4 | |
5 | exports.gives = nest('app.html.app') |
6 | |
7 | exports.needs = nest({ |
8 | 'history.sync.push': 'first', |
9 | 'history.obs.location': 'first', |
10 | 'history.obs.store': 'first', |
11 | 'router.sync.router': 'first', |
12 | 'styles.css': 'first' |
13 | }) |
14 | |
15 | exports.create = (api) => { |
16 | return nest('app.html.app', app) |
17 | |
18 | function app () { |
19 | const css = values(api.styles.css()).join('\n') |
20 | insertCss(css) |
21 | |
22 | api.history.obs.location()(render) |
23 | api.history.sync.push({ page: 'home' }) |
24 | } |
25 | |
26 | function render (location) { |
27 | const newView = api.router.sync.router(location) |
28 | |
29 | if (!newView) { |
30 | api.history.obs.store().pop() // remove bogus location |
31 | return |
32 | } |
33 | |
34 | const oldView = document.body.firstChild |
35 | oldView |
36 | ? document.body.replaceChild(newView, oldView) |
37 | : document.body.appendChild(newView) |
38 | } |
39 | } |
40 |
Built with git-ssb-web