Files: 84955f6a151e18bfc8131c8ffbcd043272b13500 / main / html / app.js
2992 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.html': { |
10 | error: 'first', |
11 | externalConfirm: 'first', |
12 | search: 'first' |
13 | }, |
14 | 'router.html.page': 'first', |
15 | 'styles.css': 'reduce' |
16 | }) |
17 | |
18 | exports.create = function (api) { |
19 | return nest('main.html.app', app) |
20 | |
21 | function app () { |
22 | const css = values(api.styles.css()).join('\n') |
23 | insertCss(css) |
24 | |
25 | function onSelect (indexes) { |
26 | const ids = indexes.map(index => tabs.get(index).content.id) |
27 | if (!search) { console.log('THERE SHOULD BE A SEARCH, GOT', search) ; return } |
28 | |
29 | if (ids.length > 1) search.input.value = 'split('+ids.join(',')+')' |
30 | else search.input.value = ids[0] |
31 | } |
32 | const search = api.main.html.search((path, change) => { |
33 | if (tabs.has(path)) { |
34 | tabs.select(path) |
35 | return true |
36 | } |
37 | |
38 | var page = addPage(path, true, false) |
39 | return change |
40 | }) |
41 | const tabs = Tabs(onSelect, { append: h('div.navExtra', [ search ]) }) |
42 | const App = h('App', tabs) |
43 | |
44 | function addPage (link, change, split) { |
45 | const page = api.router.html.page(link) |
46 | if (!page) return |
47 | |
48 | page.id = page.id || link |
49 | tabs.add(page, change, split) |
50 | } |
51 | const initialTabs = ['/public', '/private', '/notifications'] |
52 | initialTabs.forEach(p => addPage(p)) |
53 | tabs.select(0) |
54 | |
55 | catchClick(App, (link, { ctrlKey: openBackground, isExternal }) => { |
56 | if (isExternal) api.main.html.externalConfirm(link) |
57 | |
58 | if (tabs.has(link)) tabs.select(link) |
59 | else { |
60 | const changeTab = !openBackground |
61 | addPage(link, changeTab) |
62 | } |
63 | }) |
64 | |
65 | // Catch errors |
66 | var { container: errorPage, content: errorList } = api.router.html.page('/errors') |
67 | window.addEventListener('error', ev => { |
68 | if (!tabs.has('/errors')) tabs.add(errorPage, true) |
69 | |
70 | const error = api.main.html.error(ev.error || ev) |
71 | errorList.appendChild(error) |
72 | }) |
73 | |
74 | return App |
75 | } |
76 | } |
77 | |
78 | function values (object) { |
79 | const keys = Object.keys(object) |
80 | return keys.map(k => object[k]) |
81 | } |
82 | |
83 | // TODO - replace with extracted module |
84 | var Url = require('url') |
85 | |
86 | function catchClick (root, cb) { |
87 | root.addEventListener('click', (ev) => { |
88 | if (ev.target.tagName === 'INPUT' && ev.target.type === 'file') return |
89 | if (ev.defaultPrevented) return // TODO check this is in the right place |
90 | ev.preventDefault() |
91 | ev.stopPropagation() |
92 | |
93 | var anchor = null |
94 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
95 | if (n.nodeName === 'A') { |
96 | anchor = n |
97 | break |
98 | } |
99 | } |
100 | if (!anchor) return true |
101 | |
102 | var href = anchor.getAttribute('href') |
103 | if (!href) return |
104 | |
105 | var url = Url.parse(href) |
106 | var opts = { |
107 | altKey: ev.altKey, |
108 | ctrlKey: ev.ctrlKey, |
109 | metaKey: ev.metaKey, |
110 | shiftKey: ev.shiftKey, |
111 | isExternal: !!url.host |
112 | } |
113 | |
114 | cb(href, opts) |
115 | }) |
116 | } |
117 | |
118 |
Built with git-ssb-web