Files: aefef382635ee1b4f4e8b5370d9dfa59f24fe15f / modules / message.js
3137 bytesRaw
1 | var h = require('../lib/h') |
2 | var when = require('@mmckegg/mutant/when') |
3 | |
4 | var plugs = require('patchbay/plugs') |
5 | var message_content = plugs.first(exports.message_content = []) |
6 | var message_content_mini = plugs.first(exports.message_content_mini = []) |
7 | var message_link = plugs.first(exports.message_link = []) |
8 | var avatar_image = plugs.first(exports.avatar_image = []) |
9 | var avatar_name = plugs.first(exports.avatar_name = []) |
10 | var avatar_link = plugs.first(exports.avatar_link = []) |
11 | var message_meta = plugs.map(exports.message_meta = []) |
12 | var message_main_meta = plugs.map(exports.message_main_meta = []) |
13 | var message_action = plugs.map(exports.message_action = []) |
14 | var contextMenu = require('../lib/context-menu') |
15 | |
16 | exports.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 | |
30 | exports.message_render = function (msg, inContext, previousId) { |
31 | var elMini = message_content_mini(msg) |
32 | if (elMini) { |
33 | var div = h('Message', { |
34 | 'ev-contextmenu': contextMenu.bind(null, msg) |
35 | }, [ |
36 | h('header', [ |
37 | h('div', [ |
38 | avatar_link(msg.value.author, avatar_name(msg.value.author), ''), |
39 | ' ', elMini |
40 | ]), |
41 | h('div.message_meta.row', [message_meta(msg)]) |
42 | ]) |
43 | ]) |
44 | div.setAttribute('tabindex', '0') |
45 | return div |
46 | } |
47 | |
48 | var el = message_content(msg) |
49 | if (!el) return |
50 | |
51 | var classList = [] |
52 | var replyInfo = null |
53 | |
54 | if (msg.value.content.root) { |
55 | classList.push('-reply') |
56 | if (!inContext) { |
57 | replyInfo = h('span', ['in reply to ', message_link(msg.value.content.root)]) |
58 | } else if (previousId && last(msg.value.content.branch) && previousId !== last(msg.value.content.branch)) { |
59 | replyInfo = h('span', ['in reply to ', message_link(last(msg.value.content.branch))]) |
60 | } |
61 | } |
62 | |
63 | var element = h('Message', { |
64 | classList, |
65 | 'ev-contextmenu': contextMenu.bind(null, msg), |
66 | 'ev-keydown': function (ev) { |
67 | // on enter, hit first meta. |
68 | if (ev.keyCode === 13) { |
69 | element.querySelector('.enter').click() |
70 | } |
71 | } |
72 | }, [ |
73 | messageHeader(msg, replyInfo), |
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 | |
89 | function messageHeader (msg, replyInfo) { |
90 | return h('header', [ |
91 | h('div.main', [ |
92 | h('a.avatar', {href: `#${msg.value.author}`}, avatar_image(msg.value.author)), |
93 | h('div.main', [ |
94 | h('div.name', [ |
95 | h('a', {href: `#${msg.value.author}`}, avatar_name(msg.value.author)) |
96 | ]), |
97 | h('div.meta', [ |
98 | message_main_meta(msg), |
99 | ' ', replyInfo |
100 | ]) |
101 | ]) |
102 | ]), |
103 | h('div.meta', message_meta(msg)) |
104 | ]) |
105 | } |
106 | |
107 | function last (array) { |
108 | if (Array.isArray(array)) { |
109 | return array[array.length - 1] |
110 | } else { |
111 | return array |
112 | } |
113 | } |
114 |
Built with git-ssb-web