git ssb

16+

Dominic / patchbay



Tree: c4c384106bcf50acf4c0fcb3f7b842ad68f25bcb

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

1689 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.defaultPrevented) return // TODO check this is in the right place
19 ev.preventDefault()
20 ev.stopPropagation()
21
22 var anchor = null
23 for (var n = ev.target; n.parentNode; n = n.parentNode) {
24 if (n.nodeName === 'A') {
25 anchor = n
26 break
27 }
28 }
29 if (!anchor) return true
30
31 var href = anchor.getAttribute('href')
32 if (!href || href === '#') return
33
34 var url
35
36 try {
37 url = new URL(href)
38 } catch (e) {
39 // In case we pass an invalid URL
40 url = {}
41 }
42
43 var opts = {
44 altKey: ev.altKey,
45 ctrlKey: ev.ctrlKey,
46 metaKey: ev.metaKey,
47 shiftKey: ev.shiftKey,
48 isExternal: !!url.host || url.protocol === 'magnet:' || url.protocol === 'dat:'
49 }
50
51 cb(href, opts)
52 })
53 }
54
55 function defaultCallback (link, { ctrlKey, isExternal }) {
56 if (isExternal) return api.app.html.externalConfirm(link)
57
58 const openBackground = ctrlKey
59 api.router.async.normalise(link, (err, location) => {
60 if (err) throw err
61 if (location) api.app.sync.goTo(location, { openBackground })
62 })
63 }
64}
65

Built with git-ssb-web