Files: 8737709db876420b4db1aea9a323d248fd75fe20 / app / async / catch-link-click.js
910 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 | const href = anchor.getAttribute('href') |
26 | if (!href) return |
27 | |
28 | const url = Url.parse(href) |
29 | const opts = { |
30 | isExternal: !!url.host |
31 | } |
32 | |
33 | cb(href, opts) |
34 | }) |
35 | } |
36 | } |
37 | |
38 | |
39 |
Built with git-ssb-web