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