Files: de41793084dc0680dd57842ec2fec0c18ac702e9 / main / html / app.js
2021 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 | 'router.html.page': 'first', |
10 | 'styles.css': 'reduce' |
11 | }) |
12 | |
13 | exports.create = function (api) { |
14 | return nest('main.html.app', app) |
15 | |
16 | function app () { |
17 | const css = values(api.styles.css()).join('\n') |
18 | insertCss(css) |
19 | |
20 | var tabs = Tabs() // optional onSelect cb |
21 | var App = h('App', tabs) |
22 | ;['/public', '/private', '/notifications'].forEach(addPage(tabs)) |
23 | tabs.select(0) |
24 | |
25 | catchClick(App, (link, { ctrlKey: openBackground, isExternal }) => { |
26 | if (tabs.has(link)) tabs.select(link) |
27 | else { |
28 | const changeTab = !openBackground |
29 | addPage(tabs, changeTab)(link) |
30 | } |
31 | |
32 | // TODO add external-links module |
33 | }) |
34 | |
35 | return App |
36 | } |
37 | |
38 | function addPage (tabs, change, split) { |
39 | return function (link) { |
40 | const page = api.router.html.page(link) |
41 | if (!page) return |
42 | |
43 | page.id = page.id || link |
44 | tabs.add(page, change, split) |
45 | } |
46 | } |
47 | } |
48 | |
49 | function values (object) { |
50 | const keys = Object.keys(object) |
51 | return keys.map(k => object[k]) |
52 | } |
53 | |
54 | // TODO - replace with extracted module |
55 | var Url = require('url') |
56 | |
57 | function catchClick (root, cb) { |
58 | root.addEventListener('click', (ev) => { |
59 | if (ev.target.tagName === 'INPUT' && ev.target.type === 'file') return |
60 | if (ev.defaultPrevented) return // TODO check this is in the right place |
61 | ev.preventDefault() |
62 | ev.stopPropagation() |
63 | |
64 | var anchor = null |
65 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
66 | if (n.nodeName === 'A') { |
67 | anchor = n |
68 | break |
69 | } |
70 | } |
71 | if (!anchor) return true |
72 | |
73 | var href = anchor.getAttribute('href') |
74 | if (!href) return |
75 | |
76 | var url = Url.parse(href) |
77 | var opts = { |
78 | altKey: ev.altKey, |
79 | ctrlKey: ev.ctrlKey, |
80 | metaKey: ev.metaKey, |
81 | shiftKey: ev.shiftKey, |
82 | isExternal: !!url.host |
83 | } |
84 | |
85 | cb(href, opts) |
86 | }) |
87 | } |
88 | |
89 |
Built with git-ssb-web