git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: fbe32d268a26202c637c4452de4b6a47fcc7bb81

Files: fbe32d268a26202c637c4452de4b6a47fcc7bb81 / modules / message.js

3243 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.data_render = function (msg) {
17 var div = h('Message -data', {
18 'ev-contextmenu': contextMenu.bind(null, msg)
19 }, [
20 messageHeader(msg),
21 h('section', [
22 h('pre', [
23 JSON.stringify(msg.value, null, 2)
24 ])
25 ])
26 ])
27 return div
28}
29
30exports.message_render = function (msg, opts) {
31 var inContext = opts.inContext
32 var previousId = opts.previousId
33 var inSummary = opts.inSummary
34
35 var elMini = message_content_mini(msg)
36 var el = message_content(msg)
37
38 if (elMini && (!el || inSummary)) {
39 var div = h('Message', {
40 'ev-contextmenu': contextMenu.bind(null, msg)
41 }, [
42 h('header', [
43 h('div.mini', [
44 avatar_link(msg.value.author, avatar_name(msg.value.author), ''),
45 ' ', elMini
46 ]),
47 h('div.meta', [message_main_meta(msg)])
48 ])
49 ])
50 div.setAttribute('tabindex', '0')
51 return div
52 }
53
54 if (!el) return
55
56 var classList = []
57 var replyInfo = null
58
59 if (msg.value.content.root) {
60 classList.push('-reply')
61 if (!inContext) {
62 replyInfo = h('span', ['in reply to ', message_link(msg.value.content.root)])
63 } else if (previousId && last(msg.value.content.branch) && previousId !== last(msg.value.content.branch)) {
64 replyInfo = h('span', ['in reply to ', message_link(last(msg.value.content.branch))])
65 }
66 }
67
68 var element = h('Message', {
69 classList,
70 'ev-contextmenu': contextMenu.bind(null, msg),
71 'ev-keydown': function (ev) {
72 // on enter, hit first meta.
73 if (ev.keyCode === 13) {
74 element.querySelector('.enter').click()
75 }
76 }
77 }, [
78 messageHeader(msg, replyInfo),
79 h('section', [el]),
80 when(msg.key, h('footer', [
81 h('div.actions', [
82 message_action(msg),
83 h('a', {href: '#' + msg.key}, 'Reply')
84 ])
85 ]))
86 ])
87
88 // ); hyperscript does not seem to set attributes correctly.
89 element.setAttribute('tabindex', '0')
90
91 return element
92}
93
94function messageHeader (msg, replyInfo) {
95 return h('header', [
96 h('div.main', [
97 h('a.avatar', {href: `#${msg.value.author}`}, avatar_image(msg.value.author)),
98 h('div.main', [
99 h('div.name', [
100 h('a', {href: `#${msg.value.author}`}, avatar_name(msg.value.author))
101 ]),
102 h('div.meta', [
103 message_main_meta(msg),
104 ' ', replyInfo
105 ])
106 ])
107 ]),
108 h('div.meta', message_meta(msg))
109 ])
110}
111
112function last (array) {
113 if (Array.isArray(array)) {
114 return array[array.length - 1]
115 } else {
116 return array
117 }
118}
119

Built with git-ssb-web