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