git ssb

16+

Dominic / patchbay



Tree: 509333eccb040df5c7319d97b3f2f23c53a7e3e9

Files: 509333eccb040df5c7319d97b3f2f23c53a7e3e9 / app / async / catch-link-click.js

1446 bytesRaw
1const nest = require('depnest')
2const Url = require('url')
3
4exports.gives = nest('app.async.catchLinkClick')
5
6exports.needs = nest({
7 'app.html.externalConfirm': 'first',
8 'app.sync.goTo': 'first',
9 'router.sync.normalise': 'first',
10})
11
12exports.create = function (api) {
13 return nest('app.async.catchLinkClick', catchLinkClick)
14
15 function catchLinkClick (root, cb = defaultCallback) {
16 root.addEventListener('click', (ev) => {
17 if (ev.target.tagName === 'INPUT' && ev.target.type === 'file') 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 = Url.parse(href)
35 var opts = {
36 altKey: ev.altKey,
37 ctrlKey: ev.ctrlKey,
38 metaKey: ev.metaKey,
39 shiftKey: ev.shiftKey,
40 isExternal: !!url.host
41 }
42
43 cb(href, opts)
44 })
45 }
46
47 function defaultCallback (link, { ctrlKey, isExternal }) {
48 if (isExternal) return api.app.html.externalConfirm(link)
49
50 const location = api.router.sync.normalise(link)
51 const openBackground = ctrlKey
52 api.app.sync.goTo(location, openBackground)
53 }
54}
55
56

Built with git-ssb-web