Files: f770fd82103ce2fb8d0b3f737c7354d9da7524e3 / app / async / catch-link-click.js
1446 bytesRaw
1 | const nest = require('depnest') |
2 | const Url = require('url') |
3 | |
4 | exports.gives = nest('app.async.catchLinkClick') |
5 | |
6 | exports.needs = nest({ |
7 | 'app.html.externalConfirm': 'first', |
8 | 'app.sync.goTo': 'first', |
9 | 'router.sync.normalise': 'first', |
10 | }) |
11 | |
12 | exports.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