git ssb

10+

Matt McKegg / patchwork



Tree: 45a926949dc7ae72b90ae8fcc4bd39a796355669

Files: 45a926949dc7ae72b90ae8fcc4bd39a796355669 / plugs / message / html / layout / default.js

2763 bytesRaw
1const { when, h, map, computed } = require('mutant')
2var nest = require('depnest')
3var ref = require('ssb-ref')
4
5exports.needs = nest({
6 'profile.html.person': 'first',
7 'message.obs.backlinks': 'first',
8 'message.obs.name': 'first',
9 'message.html': {
10 link: 'first',
11 meta: 'map',
12 action: 'map',
13 timestamp: 'first'
14 },
15 'about.html.image': 'first'
16})
17
18exports.gives = nest('message.html.layout')
19
20exports.create = function (api) {
21 return nest('message.html.layout', layout)
22
23 function layout (msg, opts) {
24 if (!(opts.layout === undefined || opts.layout === 'default')) return
25
26 var backlinks = opts.backlinks ? api.message.obs.backlinks(msg.key) : []
27 var classList = ['Message']
28 var replyInfo = null
29
30 if (msg.value.content.root) {
31 classList.push('-reply')
32 var branch = msg.value.content.branch
33 if (branch) {
34 if (!opts.previousId || (opts.previousId && last(branch) && opts.previousId !== last(branch))) {
35 replyInfo = h('span', ['in reply to ', api.message.html.link(last(branch))])
36 }
37 }
38 } else if (msg.value.content.project) {
39 replyInfo = h('span', ['on ', api.message.html.link(msg.value.content.project)])
40 }
41
42 if (opts.priority === 2) {
43 classList.push('-new')
44 }
45
46 return h('div', {
47 classList
48 }, [
49 messageHeader(msg, replyInfo, opts.priority),
50 h('section', [opts.content]),
51 computed(msg.key, (key) => {
52 if (ref.isMsg(key)) {
53 return h('footer', [
54 h('div.actions', [
55 api.message.html.action(msg)
56 ])
57 ])
58 }
59 }),
60 map(backlinks, backlink => {
61 return h('a.backlink', {
62 href: backlink,
63 title: backlink
64 }, [
65 h('strong', 'Referenced from'), ' ', api.message.obs.name(backlink)
66 ])
67 })
68 ])
69
70 // scoped
71
72 function messageHeader (msg, replyInfo, priority) {
73 var additionalMeta = []
74 if (opts.priority >= 2) {
75 additionalMeta.push(h('span.flag -new', {title: 'New Message'}))
76 }
77 return h('header', [
78 h('div.main', [
79 h('a.avatar', {href: `${msg.value.author}`}, [
80 api.about.html.image(msg.value.author)
81 ]),
82 h('div.main', [
83 h('div.name', [
84 api.profile.html.person(msg.value.author)
85 ]),
86 h('div.meta', [
87 api.message.html.timestamp(msg), ' ', replyInfo
88 ])
89 ])
90 ]),
91 h('div.meta', [
92 api.message.html.meta(msg),
93 additionalMeta
94 ])
95 ])
96 }
97 }
98}
99
100function last (array) {
101 if (Array.isArray(array)) {
102 return array[array.length - 1]
103 } else {
104 return array
105 }
106}
107

Built with git-ssb-web