git ssb

16+

Dominic / patchbay



Tree: be455d9a9440fde700f8e6c7df90386250b322ef

Files: be455d9a9440fde700f8e6c7df90386250b322ef / app / async / catch-link-click.js

1785 bytesRaw
1const nest = require('depnest')
2
3exports.gives = nest('app.async.catchLinkClick')
4
5exports.needs = nest({
6 'app.html.externalConfirm': 'first',
7 'app.sync.goTo': 'first',
8 'router.async.normalise': 'first'
9})
10
11exports.create = function (api) {
12 return nest('app.async.catchLinkClick', catchLinkClick)
13
14 function catchLinkClick (root, cb = defaultCallback) {
15 root.addEventListener('click', (ev) => {
16 if (ev.target.tagName === 'INPUT' &&
17 (ev.target.type === 'file' || ev.target.type === 'checkbox')) return
18 if (ev.target.tagName === 'AUDIO') return
19 if (ev.target.tagName === 'VIDEO') return
20 if (ev.defaultPrevented) return // TODO check this is in the right place
21 ev.preventDefault()
22 ev.stopPropagation()
23
24 var anchor = null
25 for (var n = ev.target; n.parentNode; n = n.parentNode) {
26 if (n.nodeName === 'A') {
27 anchor = n
28 break
29 }
30 }
31 if (!anchor) return true
32
33 var href = anchor.getAttribute('href')
34 if (!href || href === '#') return
35
36 var url
37
38 try {
39 url = new URL(href)
40 } catch (e) {
41 // In case we pass an invalid URL
42 url = {}
43 }
44
45 var opts = {
46 altKey: ev.altKey,
47 ctrlKey: ev.ctrlKey,
48 metaKey: ev.metaKey,
49 shiftKey: ev.shiftKey,
50 isExternal: !!url.host || url.protocol === 'magnet:' || url.protocol === 'dat:'
51 }
52
53 cb(href, opts)
54 })
55 }
56
57 function defaultCallback (link, { ctrlKey, isExternal }) {
58 if (isExternal) return api.app.html.externalConfirm(link)
59
60 const openBackground = ctrlKey
61 api.router.async.normalise(link, (err, location) => {
62 if (err) throw err
63 if (location) api.app.sync.goTo(location, { openBackground })
64 })
65 }
66}
67

Built with git-ssb-web