Files: 8397cee5bd7b17b9f2ebf584813ed0c0a035023d / modules / message / render.js
3470 bytesRaw
1 | var when = require('mutant/when') |
2 | var h = require('../../lib/h') |
3 | var contextMenu = require('../../lib/context-menu') |
4 | |
5 | exports.needs = { |
6 | message: { |
7 | content: 'first', |
8 | content_mini: 'first', |
9 | link: 'first', |
10 | meta: 'map', |
11 | main_meta: 'map', |
12 | action: 'map' |
13 | }, |
14 | about: { |
15 | image: 'first', |
16 | name: 'first', |
17 | link: 'first', |
18 | name_link: 'first' |
19 | } |
20 | } |
21 | |
22 | exports.gives = { |
23 | message: { |
24 | data_render: true, |
25 | render: true |
26 | } |
27 | } |
28 | |
29 | exports.create = function (api) { |
30 | return { |
31 | message: { |
32 | data_render (msg) { |
33 | var div = h('Message -data', { |
34 | 'ev-contextmenu': contextMenu.bind(null, msg) |
35 | }, [ |
36 | messageHeader(msg), |
37 | h('section', [ |
38 | h('pre', [ |
39 | JSON.stringify(msg, null, 2) |
40 | ]) |
41 | ]) |
42 | ]) |
43 | return div |
44 | }, |
45 | |
46 | render (msg, opts) { |
47 | opts = opts || {} |
48 | var inContext = opts.inContext |
49 | var previousId = opts.previousId |
50 | var inSummary = opts.inSummary |
51 | |
52 | var elMini = api.message.content_mini(msg) |
53 | var el = api.message.content(msg) |
54 | |
55 | if (elMini && (!el || inSummary)) { |
56 | var div = h('Message', { |
57 | 'ev-contextmenu': contextMenu.bind(null, msg) |
58 | }, [ |
59 | h('header', [ |
60 | h('div.mini', [ |
61 | api.about.link(msg.value.author, api.about.name(msg.value.author), ''), |
62 | ' ', elMini |
63 | ]), |
64 | h('div.meta', [api.message.main_meta(msg)]) |
65 | ]) |
66 | ]) |
67 | div.setAttribute('tabindex', '0') |
68 | return div |
69 | } |
70 | |
71 | if (!el) return |
72 | |
73 | var classList = [] |
74 | var replyInfo = null |
75 | |
76 | if (msg.value.content.root) { |
77 | classList.push('-reply') |
78 | if (!inContext) { |
79 | replyInfo = h('span', ['in reply to ', api.message.link(msg.value.content.root)]) |
80 | } else if (previousId && last(msg.value.content.branch) && previousId !== last(msg.value.content.branch)) { |
81 | replyInfo = h('span', ['in reply to ', api.message.link(last(msg.value.content.branch))]) |
82 | } |
83 | } |
84 | |
85 | var element = h('Message', { |
86 | classList, |
87 | 'ev-contextmenu': contextMenu.bind(null, msg), |
88 | 'ev-keydown': function (ev) { |
89 | // on enter, hit first meta. |
90 | if (ev.keyCode === 13) { |
91 | element.querySelector('.enter').click() |
92 | } |
93 | } |
94 | }, [ |
95 | messageHeader(msg, replyInfo), |
96 | h('section', [el]), |
97 | when(msg.key, h('footer', [ |
98 | h('div.actions', [ |
99 | api.message.action(msg) |
100 | ]) |
101 | ])) |
102 | ]) |
103 | |
104 | // ); hyperscript does not seem to set attributes correctly. |
105 | element.setAttribute('tabindex', '0') |
106 | |
107 | return element |
108 | } |
109 | } |
110 | } |
111 | |
112 | function messageHeader (msg, replyInfo) { |
113 | return h('header', [ |
114 | h('div.main', [ |
115 | h('a.avatar', {href: `#${msg.value.author}`}, api.about.image(msg.value.author)), |
116 | h('div.main', [ |
117 | h('div.name', [ |
118 | h('a', {href: `#${msg.value.author}`}, api.about.name(msg.value.author)) |
119 | ]), |
120 | h('div.meta', [ |
121 | api.message.main_meta(msg), |
122 | ' ', replyInfo |
123 | ]) |
124 | ]) |
125 | ]), |
126 | h('div.meta', api.message.meta(msg)) |
127 | ]) |
128 | } |
129 | } |
130 | |
131 | function last (array) { |
132 | if (Array.isArray(array)) { |
133 | return array[array.length - 1] |
134 | } else { |
135 | return array |
136 | } |
137 | } |
138 |
Built with git-ssb-web