Files: ef0e26935108e5a9748cd1b03ef830fceff93640 / app / async / catch-link-click.js
1018 bytesRaw
1 | const nest = require('depnest') |
2 | const Url = require('url') |
3 | |
4 | exports.gives = nest('app.async.catchLinkClick') |
5 | |
6 | exports.create = function (api) { |
7 | return nest('app.async.catchLinkClick', catchLinkClick) |
8 | |
9 | function catchLinkClick (root, cb) { |
10 | root.addEventListener('click', (ev) => { |
11 | if (ev.target.tagName === 'INPUT' && ev.target.type === 'file') return |
12 | if (ev.defaultPrevented) return // TODO check this is in the right place |
13 | ev.preventDefault() |
14 | ev.stopPropagation() |
15 | |
16 | var anchor = null |
17 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
18 | if (n.nodeName === 'A') { |
19 | anchor = n |
20 | break |
21 | } |
22 | } |
23 | if (!anchor) return true |
24 | |
25 | var href = anchor.getAttribute('href') |
26 | if (!href) return |
27 | |
28 | var url = Url.parse(href) |
29 | var opts = { |
30 | altKey: ev.altKey, |
31 | ctrlKey: ev.ctrlKey, |
32 | metaKey: ev.metaKey, |
33 | shiftKey: ev.shiftKey, |
34 | isExternal: !!url.host |
35 | } |
36 | |
37 | cb(href, opts) |
38 | }) |
39 | } |
40 | } |
41 | |
42 |
Built with git-ssb-web