Files: 364194e32458712cc0a1f6cd0e651f56556f0dba / main / html / app.js
2228 bytesRaw
1 | const { h } = require('mutant') |
2 | const nest = require('depnest') |
3 | const insertCss = require('insert-css') |
4 | const Tabs = require('hypertabs') |
5 | |
6 | exports.gives = nest('main.html.app') |
7 | |
8 | exports.needs = nest({ |
9 | main: { |
10 | async: { |
11 | catchLinkClick: 'first' |
12 | }, |
13 | html: { |
14 | error: 'first', |
15 | externalConfirm: 'first', |
16 | search: 'first' |
17 | }, |
18 | sync: { |
19 | catchKeyboardShortcut: 'first' |
20 | } |
21 | }, |
22 | 'router.html.page': 'first', |
23 | 'styles.css': 'reduce' |
24 | }) |
25 | |
26 | exports.create = function (api) { |
27 | return nest('main.html.app', app) |
28 | |
29 | function app () { |
30 | const css = values(api.styles.css()).join('\n') |
31 | insertCss(css) |
32 | |
33 | const search = api.main.html.search((path, change) => { |
34 | if (tabs.has(path)) { |
35 | tabs.select(path) |
36 | return true |
37 | } |
38 | |
39 | addPage(path, true, false) |
40 | return change |
41 | }) |
42 | const tabs = Tabs(onSelect, { append: h('div.navExtra', [ search ]) }) |
43 | function onSelect (indexes) { |
44 | search.input.value = tabs.get(indexes[0]).content.id |
45 | } |
46 | |
47 | const App = h('App', tabs) |
48 | |
49 | function addPage (link, change, split) { |
50 | const page = api.router.html.page(link) |
51 | if (!page) return |
52 | |
53 | page.id = page.id || link |
54 | tabs.add(page, change, split) |
55 | } |
56 | |
57 | const initialTabs = ['/public', '/private', '/notifications'] |
58 | initialTabs.forEach(p => addPage(p)) |
59 | tabs.select(0) |
60 | |
61 | // Catch keyboard shortcuts |
62 | api.main.sync.catchKeyboardShortcut(window, { tabs, search }) |
63 | |
64 | // Catch link clicks |
65 | api.main.async.catchLinkClick(App, (link, { ctrlKey: openBackground, isExternal }) => { |
66 | if (isExternal) return api.main.html.externalConfirm(link) |
67 | |
68 | if (tabs.has(link)) tabs.select(link) |
69 | else { |
70 | const changeTab = !openBackground |
71 | addPage(link, changeTab) |
72 | } |
73 | }) |
74 | |
75 | // Catch errors |
76 | var { container: errorPage, content: errorList } = api.router.html.page('/errors') |
77 | window.addEventListener('error', ev => { |
78 | if (!tabs.has('/errors')) tabs.add(errorPage, true) |
79 | |
80 | const error = api.main.html.error(ev.error || ev) |
81 | errorList.appendChild(error) |
82 | }) |
83 | |
84 | return App |
85 | } |
86 | } |
87 | |
88 | function values (object) { |
89 | const keys = Object.keys(object) |
90 | return keys.map(k => object[k]) |
91 | } |
92 | |
93 |
Built with git-ssb-web