Files: 7cf0304bc911d7894179eec462e0b37166f9535c / app / html / app.js
1250 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.obs.store()(his => console.log('history', his)) // REMOVE) |
32 | api.history.sync.push({ page: 'home' }) |
33 | } |
34 | |
35 | function render (location) { |
36 | const newView = api.router.sync.router(location) |
37 | |
38 | if (!newView) { |
39 | api.history.obs.store().pop() // remove bogus location |
40 | return |
41 | } |
42 | |
43 | const oldView = document.body.firstChild |
44 | oldView |
45 | ? document.body.replaceChild(newView, oldView) |
46 | : document.body.appendChild(newView) |
47 | } |
48 | } |
49 |
Built with git-ssb-web