git ssb

16+

Dominic / patchbay



Tree: 9ebbbea9829c7cc67a7fd3e50d57216f25b6a4fb

Files: 9ebbbea9829c7cc67a7fd3e50d57216f25b6a4fb / main / html / app.js

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

Built with git-ssb-web