Files: 8e75010c68d2c079e58d57c19ae1919e2b878d31 / app / html / app.js
1900 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const insertCss = require('insert-css') |
4 | |
5 | exports.gives = nest('app.html.app') |
6 | |
7 | exports.needs = nest({ |
8 | 'app.async.catchLinkClick': 'first', |
9 | 'app.html.externalConfirm': 'first', |
10 | 'app.html.tabs': 'first', |
11 | 'app.page.errors': 'first', |
12 | 'app.sync.window': 'reduce', |
13 | 'app.sync.addPage': 'first', |
14 | 'app.sync.catchKeyboardShortcut': 'first', |
15 | 'router.sync.router': 'first', |
16 | 'router.sync.normalise': 'first', |
17 | 'styles.css': 'reduce' |
18 | }) |
19 | |
20 | exports.create = function (api) { |
21 | return nest('app.html.app', app) |
22 | |
23 | function app () { |
24 | window = api.app.sync.window(window) |
25 | const css = values(api.styles.css()).join('\n') |
26 | insertCss(css) |
27 | |
28 | const initialTabs = [ '/public', '/private', '/notifications' ] |
29 | // NB router converts these to { page: '/public' } |
30 | const tabs = api.app.html.tabs(initialTabs) |
31 | |
32 | const App = h('App', tabs) |
33 | |
34 | // Catch keyboard shortcuts |
35 | api.app.sync.catchKeyboardShortcut(window, { tabs }) |
36 | |
37 | // Catch link clicks |
38 | api.app.async.catchLinkClick(App, (link, { ctrlKey: openBackground, isExternal }) => { |
39 | if (isExternal) return api.app.html.externalConfirm(link) |
40 | |
41 | // TODO tidy up who and where this logic happens (do when adding patch-history) |
42 | const location = api.router.sync.normalise(link) |
43 | const tabId = JSON.stringify(location) |
44 | if (tabs.has(tabId)) tabs.select(tabId) |
45 | else { |
46 | const changeTab = !openBackground |
47 | api.app.sync.addPage(location, changeTab) |
48 | } |
49 | }) |
50 | |
51 | // Catch errors |
52 | var { container: errorPage, addError } = api.router.sync.router('/errors') |
53 | window.addEventListener('error', ev => { |
54 | if (!tabs.has('/errors')) tabs.add(errorPage, true) |
55 | |
56 | addError(ev.error || ev) |
57 | }) |
58 | |
59 | return App |
60 | } |
61 | } |
62 | |
63 | function values (object) { |
64 | const keys = Object.keys(object) |
65 | return keys.map(k => object[k]) |
66 | } |
67 |
Built with git-ssb-web