git ssb

16+

Dominic / patchbay



Tree: 3c63691a3ff381ff83e86a97ce3246807bccae1e

Files: 3c63691a3ff381ff83e86a97ce3246807bccae1e / main / html / app.js

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

Built with git-ssb-web