git ssb

10+

Matt McKegg / patchwork



Tree: 13be5ee9906beb5d8c2a24db46c68ef183c78d3a

Files: 13be5ee9906beb5d8c2a24db46c68ef183c78d3a / modules / message.js

2812 bytesRaw
1var h = require('../lib/h')
2var when = require('@mmckegg/mutant/when')
3
4var plugs = require('patchbay/plugs')
5var message_content = plugs.first(exports.message_content = [])
6var message_content_mini = plugs.first(exports.message_content_mini = [])
7var message_link = plugs.first(exports.message_link = [])
8var avatar_image = plugs.first(exports.avatar_image = [])
9var avatar_name = plugs.first(exports.avatar_name = [])
10var avatar_link = plugs.first(exports.avatar_link = [])
11var message_meta = plugs.map(exports.message_meta = [])
12var message_main_meta = plugs.map(exports.message_main_meta = [])
13var message_action = plugs.map(exports.message_action = [])
14var contextMenu = require('../lib/context-menu')
15
16exports.message_render = function (msg, inContext, previousId) {
17 var elMini = message_content_mini(msg)
18 if (elMini) {
19 var div = h('Message', {
20 'ev-contextmenu': contextMenu.bind(null, msg)
21 }, [
22 h('header', [
23 h('div', [
24 avatar_link(msg.value.author, avatar_name(msg.value.author), ''),
25 ' ', elMini
26 ]),
27 h('div.message_meta.row', [message_meta(msg)])
28 ])
29 ])
30 div.setAttribute('tabindex', '0')
31 return div
32 }
33
34 var el = message_content(msg)
35 if (!el) return
36
37 var classList = []
38 var replyInfo = null
39
40 if (msg.value.content.root) {
41 classList.push('-reply')
42 if (!inContext) {
43 replyInfo = h('span', ['in reply to ', message_link(msg.value.content.root)])
44 } else if (previousId && last(msg.value.content.branch) && previousId !== last(msg.value.content.branch)) {
45 replyInfo = h('span', ['in reply to ', message_link(last(msg.value.content.branch))])
46 }
47 }
48
49 var element = h('Message', {
50 classList,
51 'ev-contextmenu': contextMenu.bind(null, msg),
52 'ev-keydown': function (ev) {
53 // on enter, hit first meta.
54 if (ev.keyCode === 13) {
55 element.querySelector('.enter').click()
56 }
57 }
58 }, [
59 h('header', [
60 h('div.main', [
61 h('a.avatar', {href: `#${msg.value.author}`}, avatar_image(msg.value.author)),
62 h('div.main', [
63 h('div.name', [
64 h('a', {href: `#${msg.value.author}`}, avatar_name(msg.value.author))
65 ]),
66 h('div.meta', [
67 message_main_meta(msg),
68 ' ', replyInfo
69 ])
70 ])
71 ]),
72 h('div.meta', message_meta(msg))
73 ]),
74 h('section', [el]),
75 when(msg.key, h('footer', [
76 h('div.actions', [
77 message_action(msg),
78 h('a', {href: '#' + msg.key}, 'Reply')
79 ])
80 ]))
81 ])
82
83 // ); hyperscript does not seem to set attributes correctly.
84 element.setAttribute('tabindex', '0')
85
86 return element
87}
88
89function last (array) {
90 if (Array.isArray(array)) {
91 return array[array.length - 1]
92 } else {
93 return array
94 }
95}
96

Built with git-ssb-web