overrides/patchcore/message/html/action/reply.jsView |
---|
1 | 1 | var h = require('mutant/h') |
2 | 2 | var nest = require('depnest') |
3 | 3 | |
4 | | -exports.needs = nest({'intl.sync.i18n': 'first'}) |
| 4 | +exports.needs = nest({ |
| 5 | + 'intl.sync.i18n': 'first', |
| 6 | + 'app.navigate': 'first', |
| 7 | + 'message.sync.root': 'first' |
5 | 8 | |
| 9 | +}) |
| 10 | + |
6 | 11 | exports.gives = nest('message.html.action') |
7 | 12 | |
8 | 13 | exports.create = (api) => { |
9 | 14 | const i18n = api.intl.sync.i18n |
10 | 15 | return nest('message.html.action', function reply (msg) { |
11 | | - return h('a', { href: msg.key, anchor: 'reply' }, i18n('Reply')) |
| 16 | + return h('a', { href: msg.key, anchor: 'reply', 'ev-click': {handleEvent, api, msg} }, i18n('Reply')) |
12 | 17 | }) |
13 | 18 | } |
| 19 | + |
| 20 | +function handleEvent (ev) { |
| 21 | + var {api, msg} = this |
| 22 | + var el = getMessageElement(ev.target) |
| 23 | + |
| 24 | + |
| 25 | + if (el && !el.nextElementSibling) { |
| 26 | + api.app.navigate(api.message.sync.root(msg), 'reply') |
| 27 | + ev.preventDefault() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function getMessageElement (el) { |
| 32 | + while (el && el.classList) { |
| 33 | + if (el.classList.contains('Message') && el.parentNode && el.parentNode.classList.contains('replies')) { |
| 34 | + return el |
| 35 | + } |
| 36 | + el = el.parentNode |
| 37 | + } |
| 38 | +} |