Commit b87644d4179bd0b35e02a999a6ddb4208fe4615b
add link click handler
mix irving committed on 8/10/2017, 10:07:15 AMParent: 119e5a731640d722b01b4aa15a4ec30a09b17285
Files changed
app/html/app.js | changed |
app/html/nav.js | changed |
app/index.js | changed |
app/async/catch-link-click.js | added |
package-lock.json | changed |
package.json | changed |
styles/global.mcss | changed |
app/html/app.js | ||
---|---|---|
@@ -1,11 +1,13 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | 2 | const values = require('lodash/values') |
3 | 3 | const insertCss = require('insert-css') |
4 | +const openExternal = require('open-external') | |
4 | 5 | |
5 | 6 | exports.gives = nest('app.html.app') |
6 | 7 | |
7 | 8 | exports.needs = nest({ |
9 | + 'app.async.catchLinkClick': 'first', | |
8 | 10 | 'history.sync.push': 'first', |
9 | 11 | 'history.obs.location': 'first', |
10 | 12 | 'history.obs.store': 'first', |
11 | 13 | 'router.sync.router': 'first', |
@@ -18,8 +20,14 @@ | ||
18 | 20 | function app () { |
19 | 21 | const css = values(api.styles.css()).join('\n') |
20 | 22 | insertCss(css) |
21 | 23 | |
24 | + api.app.async.catchLinkClick(document.body, (link, { isExternal }) => { | |
25 | + if (isExternal) return openExternal(link) | |
26 | + | |
27 | + api.history.sync.push(link) | |
28 | + }) | |
29 | + | |
22 | 30 | api.history.obs.location()(render) |
23 | 31 | api.history.sync.push({ page: 'home' }) |
24 | 32 | } |
25 | 33 |
app/html/nav.js | ||
---|---|---|
@@ -17,8 +17,9 @@ | ||
17 | 17 | h('div.back', { 'ev-click': back }, '←'), |
18 | 18 | h('div', { 'ev-click': () => push({page: 'home'}) }, 'Home'), |
19 | 19 | // h('div', { 'ev-click': () => push({type: 'group', key: '%sadlkjas;lkdjas'}) }, 'Group'), |
20 | 20 | h('div', { 'ev-click': () => push({key: '%fXXZgQrwnj7F+Y19H0IXxNriuvPFoahvusih3UzpkfA=.sha256'}) }, 'Thread A'), |
21 | - h('div', { 'ev-click': () => push({key: '%3cWZHeN6k03XpvDBxrxP5bGLsNByFLTvr/rKYFV4f+c=.sha256'}) }, 'Thread B') | |
21 | + h('div', { 'ev-click': () => push({key: '%3cWZHeN6k03XpvDBxrxP5bGLsNByFLTvr/rKYFV4f+c=.sha256'}) }, 'Thread B'), | |
22 | + h('a', { href: '%YRhFXmsAwipgyiwuHSP+EBr9fGjSqrMpWXUxgWcHxkM=.sha256' }, 'href link') | |
22 | 23 | ]) |
23 | 24 | } |
24 | 25 | } |
app/index.js | ||
---|---|---|
@@ -1,5 +1,8 @@ | ||
1 | 1 | module.exports = { |
2 | + async: { | |
3 | + catchLinkClick: require('./async/catch-link-click'), | |
4 | + }, | |
2 | 5 | html: { |
3 | 6 | app: require('./html/app'), |
4 | 7 | thread: require('./html/thread'), |
5 | 8 | nav: require('./html/nav') |
app/async/catch-link-click.js | ||
---|---|---|
@@ -1,0 +1,38 @@ | ||
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 | + |
package-lock.json | ||
---|---|---|
@@ -1001,8 +1001,13 @@ | ||
1001 | 1001 | "version": "1.1.0", |
1002 | 1002 | "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", |
1003 | 1003 | "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=" |
1004 | 1004 | }, |
1005 | + "open-external": { | |
1006 | + "version": "0.1.1", | |
1007 | + "resolved": "https://registry.npmjs.org/open-external/-/open-external-0.1.1.tgz", | |
1008 | + "integrity": "sha1-GfrTVRhBp3TwY8Nxs5+ycXXKjS4=" | |
1009 | + }, | |
1005 | 1010 | "optimist": { |
1006 | 1011 | "version": "0.6.1", |
1007 | 1012 | "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", |
1008 | 1013 | "integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=", |
@@ -1051,11 +1056,11 @@ | ||
1051 | 1056 | "mutant": "3.21.2" |
1052 | 1057 | } |
1053 | 1058 | }, |
1054 | 1059 | "patchcore": { |
1055 | - "version": "1.9.0", | |
1056 | - "resolved": "https://registry.npmjs.org/patchcore/-/patchcore-1.9.0.tgz", | |
1057 | - "integrity": "sha512-d9bJ7oivSS9uLknOZxnpJMDWTGVSyLmUt38mOTrL0LD1YEAAmThhi+6i/+mPXhm6605DFbYzWtxBVmXQ2t2SjQ==", | |
1060 | + "version": "1.9.1", | |
1061 | + "resolved": "https://registry.npmjs.org/patchcore/-/patchcore-1.9.1.tgz", | |
1062 | + "integrity": "sha512-TLm96QXRvutu0nvYkpkpCqmlpAxHlVex6rbz6pXdTnne5AYkM1k/VaKX+30vYRQimWtqIpl1zuKb8Q34g2B3iw==", | |
1058 | 1063 | "requires": { |
1059 | 1064 | "bulk-require": "1.0.1", |
1060 | 1065 | "bulkify": "1.4.2", |
1061 | 1066 | "color-hash": "1.0.3", |
@@ -1073,9 +1078,9 @@ | ||
1073 | 1078 | "pull-stream": "3.6.0", |
1074 | 1079 | "simple-mime": "0.1.0", |
1075 | 1080 | "sorted-array-functions": "1.0.0", |
1076 | 1081 | "split-buffer": "1.0.0", |
1077 | - "ssb-client": "4.5.0", | |
1082 | + "ssb-client": "4.5.1", | |
1078 | 1083 | "ssb-config": "2.2.0", |
1079 | 1084 | "ssb-feed": "2.3.0", |
1080 | 1085 | "ssb-keys": "7.0.10", |
1081 | 1086 | "ssb-markdown": "3.3.0", |
@@ -1216,8 +1221,18 @@ | ||
1216 | 1221 | "relative-url": "1.0.2", |
1217 | 1222 | "ws": "1.1.4" |
1218 | 1223 | } |
1219 | 1224 | }, |
1225 | + "punycode": { | |
1226 | + "version": "1.3.2", | |
1227 | + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", | |
1228 | + "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" | |
1229 | + }, | |
1230 | + "querystring": { | |
1231 | + "version": "0.2.0", | |
1232 | + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", | |
1233 | + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" | |
1234 | + }, | |
1220 | 1235 | "quote-stream": { |
1221 | 1236 | "version": "0.0.0", |
1222 | 1237 | "resolved": "https://registry.npmjs.org/quote-stream/-/quote-stream-0.0.0.tgz", |
1223 | 1238 | "integrity": "sha1-zeKelMQJsW4Z3HCYuJtmWPlyHTs=", |
@@ -1449,11 +1464,11 @@ | ||
1449 | 1464 | "resolved": "https://registry.npmjs.org/split-buffer/-/split-buffer-1.0.0.tgz", |
1450 | 1465 | "integrity": "sha1-t+jgq1E0UVi3LB9tvvJAbVHx0Cc=" |
1451 | 1466 | }, |
1452 | 1467 | "ssb-client": { |
1453 | - "version": "4.5.0", | |
1454 | - "resolved": "https://registry.npmjs.org/ssb-client/-/ssb-client-4.5.0.tgz", | |
1455 | - "integrity": "sha1-oiXi20o6q0NmGyemRLODfPNsuRA=", | |
1468 | + "version": "4.5.1", | |
1469 | + "resolved": "https://registry.npmjs.org/ssb-client/-/ssb-client-4.5.1.tgz", | |
1470 | + "integrity": "sha512-Kn4eoSl3ZuS4BteQAtRIFM52jsGJPar4NJHLjis5OFxV22pNub/JfZHC2FdJBTtkPWJIJZi0ullCsm/EQ1Xs7Q==", | |
1456 | 1471 | "requires": { |
1457 | 1472 | "explain-error": "1.0.4", |
1458 | 1473 | "multicb": "1.2.2", |
1459 | 1474 | "multiserver": "1.10.0", |
@@ -1721,8 +1736,17 @@ | ||
1721 | 1736 | "version": "1.0.2", |
1722 | 1737 | "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz", |
1723 | 1738 | "integrity": "sha1-rOEWq1V80Zc4ak6I9GhTeMiy5Po=" |
1724 | 1739 | }, |
1740 | + "url": { | |
1741 | + "version": "0.11.0", | |
1742 | + "resolved": "https://registry.npmjs.org/url/-/url-0.11.0.tgz", | |
1743 | + "integrity": "sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=", | |
1744 | + "requires": { | |
1745 | + "punycode": "1.3.2", | |
1746 | + "querystring": "0.2.0" | |
1747 | + } | |
1748 | + }, | |
1725 | 1749 | "util-deprecate": { |
1726 | 1750 | "version": "1.0.2", |
1727 | 1751 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", |
1728 | 1752 | "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" |
package.json | ||
---|---|---|
@@ -22,11 +22,13 @@ | ||
22 | 22 | "libnested": "^1.2.1", |
23 | 23 | "lodash": "^4.17.4", |
24 | 24 | "micro-css": "^2.0.1", |
25 | 25 | "mutant": "^3.21.2", |
26 | + "open-external": "^0.1.1", | |
26 | 27 | "patch-history": "^1.0.0", |
27 | - "patchcore": "^1.9.0", | |
28 | + "patchcore": "^1.9.1", | |
28 | 29 | "pull-stream": "^3.6.0", |
29 | 30 | "read-directory": "^2.1.0", |
30 | - "setimmediate": "^1.0.5" | |
31 | + "setimmediate": "^1.0.5", | |
32 | + "url": "^0.11.0" | |
31 | 33 | } |
32 | 34 | } |
Built with git-ssb-web