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