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