git ssb

16+

Dominic / patchbay



Tree: f1d9a256d5c365f61e080c2fe9a7a0ba1d32b619

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

1548 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.async.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' &&
18 (ev.target.type === 'file' || ev.target.type === 'checkbox')) return
19 if (ev.defaultPrevented) return // TODO check this is in the right place
20 ev.preventDefault()
21 ev.stopPropagation()
22
23 var anchor = null
24 for (var n = ev.target; n.parentNode; n = n.parentNode) {
25 if (n.nodeName === 'A') {
26 anchor = n
27 break
28 }
29 }
30 if (!anchor) return true
31
32 var href = anchor.getAttribute('href')
33 if (!href || href === '#') return
34
35 var url = Url.parse(href)
36 var opts = {
37 altKey: ev.altKey,
38 ctrlKey: ev.ctrlKey,
39 metaKey: ev.metaKey,
40 shiftKey: ev.shiftKey,
41 isExternal: !!url.host
42 }
43
44 cb(href, opts)
45 })
46 }
47
48 function defaultCallback (link, { ctrlKey, isExternal }) {
49 if (isExternal) return api.app.html.externalConfirm(link)
50
51 const openBackground = ctrlKey
52 api.router.async.normalise(link, (err, location) => {
53 if (err) throw err
54 if (location) api.app.sync.goTo(location, { openBackground })
55 })
56 }
57}
58

Built with git-ssb-web