git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 4f2f50a77ef55ab8e505ad02c47746b3cab34157

Files: 4f2f50a77ef55ab8e505ad02c47746b3cab34157 / plugs / message / html / layout / default.js

3442 bytesRaw
1const { h, computed, Value, when } = require('mutant')
2var nest = require('depnest')
3var ref = require('ssb-ref')
4var ExpanderHook = require('../../../../lib/expander-hook')
5
6exports.needs = nest({
7 'profile.html.person': 'first',
8 'message.obs.backlinks': 'first',
9 'message.obs.name': 'first',
10 'message.obs.author': 'first',
11 'message.html': {
12 link: 'first',
13 meta: 'map',
14 action: 'map',
15 timestamp: 'first',
16 backlinks: 'first'
17 },
18 'about.html.image': 'first',
19 'intl.sync.i18n': 'first'
20})
21
22exports.gives = nest('message.html.layout')
23
24exports.create = function (api) {
25 const i18n = api.intl.sync.i18n
26 return nest('message.html.layout', layout)
27
28 function layout (msg, {layout, previousId, priority, content, includeReferences = false, includeForks = true}) {
29 if (!(layout === undefined || layout === 'default')) return
30
31 var classList = ['Message']
32 var replyInfo = null
33
34 var needsExpand = Value(false)
35 var expanded = Value(false)
36
37 if (msg.value.content.root) {
38 classList.push('-reply')
39 var branch = msg.value.content.branch
40 if (branch) {
41 if (!previousId || (previousId && last(branch) && previousId !== last(branch))) {
42 replyInfo = h('span', [i18n('in reply to '), api.message.html.link(last(branch))])
43 }
44 }
45 } else if (msg.value.content.project) {
46 replyInfo = h('span', [i18n('on '), api.message.html.link(msg.value.content.project)])
47 }
48
49 if (priority === 2) {
50 classList.push('-new')
51 }
52
53 return h('div', {
54 classList
55 }, [
56 messageHeader(msg, { replyInfo, priority, needsExpand, expanded }),
57 h('section', {
58 classList: [ when(expanded, '-expanded') ],
59 hooks: [ ExpanderHook(needsExpand) ]
60 }, [content]),
61 computed(msg.key, (key) => {
62 if (ref.isMsg(key)) {
63 return h('footer', [
64 when(needsExpand, h('div.expander', {
65 classList: when(expanded, null, '-truncated')
66 }, [
67 h('a', {
68 href: '#',
69 'ev-click': toggle(expanded)
70 }, when(expanded, i18n('See less'), i18n('See more')))
71 ])),
72 h('div.actions', [
73 api.message.html.action(msg)
74 ])
75 ])
76 }
77 }),
78 api.message.html.backlinks(msg, {includeReferences, includeForks})
79 ])
80
81 // scoped
82
83 function messageHeader (msg, {replyInfo, priority}) {
84 var additionalMeta = []
85 if (priority >= 2) {
86 additionalMeta.push(h('span.flag -new', {title: i18n('New Message')}))
87 }
88 return h('header', [
89 h('div.main', [
90 h('a.avatar', {href: `${msg.value.author}`}, [
91 api.about.html.image(msg.value.author)
92 ]),
93 h('div.main', [
94 h('div.name', [
95 api.profile.html.person(msg.value.author)
96 ]),
97 h('div.meta', [
98 api.message.html.timestamp(msg), ' ',
99 replyInfo
100 ])
101 ])
102 ]),
103 h('div.meta', [
104 api.message.html.meta(msg),
105 additionalMeta
106 ])
107 ])
108 }
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
120function toggle (param) {
121 return {
122 handleEvent: handleToggle,
123 param
124 }
125}
126
127function handleToggle (ev) {
128 this.param.set(!this.param())
129}
130

Built with git-ssb-web