Files: 0a1c7842563bee8ca52221c858cfe7309da160da / main / html / app.js
1834 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: change, isExternal }) => { |
26 | if (tabs.has(link)) tabs.select(link) |
27 | else addPage(tabs, change)(link) |
28 | |
29 | // TODO add external-links module |
30 | }) |
31 | |
32 | return App |
33 | } |
34 | |
35 | function addPage (tabs, change, split) { |
36 | return function (link) { |
37 | const page = api.router.html.page(link) |
38 | if (!page) return |
39 | |
40 | page.id = page.id || link |
41 | tabs.add(page, change, split) |
42 | } |
43 | } |
44 | } |
45 | |
46 | function values (object) { |
47 | const keys = Object.keys(object) |
48 | return keys.map(k => object[k]) |
49 | } |
50 | |
51 | // TODO - replace with extracted module |
52 | var Url = require('url') |
53 | |
54 | function catchClick (root, cb) { |
55 | root.addEventListener('click', (ev) => { |
56 | ev.preventDefault() |
57 | ev.stopPropagation() |
58 | if (ev.defaultPrevented) return |
59 | |
60 | var anchor = null |
61 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
62 | if (n.nodeName === 'A') { |
63 | anchor = n |
64 | break |
65 | } |
66 | } |
67 | if (!anchor) return true |
68 | |
69 | var href = anchor.getAttribute('href') |
70 | if (!href) return |
71 | |
72 | var url = Url.parse(href) |
73 | var opts = { |
74 | altKey: ev.altKey, |
75 | ctrlKey: ev.ctrlKey, |
76 | metaKey: ev.metaKey, |
77 | shiftKey: ev.shiftKey, |
78 | isExternal: !!url.host |
79 | } |
80 | |
81 | cb(href, opts) |
82 | }) |
83 | } |
84 | |
85 |
Built with git-ssb-web