Files: a69e9da25b4bc586435174712185486605414551 / plugs / message / html / render / gathering.js
4392 bytesRaw
1 | var {h, computed, when, map, send} = require('mutant') |
2 | var nest = require('depnest') |
3 | var extend = require('xtend') |
4 | var moment = require('moment') |
5 | |
6 | exports.needs = nest({ |
7 | 'message.html.markdown': 'first', |
8 | 'message.html.layout': 'first', |
9 | 'message.html.decorate': 'reduce', |
10 | 'message.async.publish': 'first', |
11 | 'keys.sync.id': 'first', |
12 | 'about.html.image': 'first', |
13 | 'about.obs.latestValue': 'first', |
14 | 'about.obs.groupedValues': 'first', |
15 | 'about.obs.valueFrom': 'first', |
16 | 'about.obs.name': 'first', |
17 | 'contact.obs.following': 'first', |
18 | 'blob.sync.url': 'first', |
19 | 'gathering.sheet.edit': 'first' |
20 | }) |
21 | |
22 | exports.gives = nest('message.html.render') |
23 | |
24 | exports.create = function (api) { |
25 | var following = null |
26 | |
27 | return nest('message.html.render', function renderGathering (msg, opts) { |
28 | if (msg.value.content.type !== 'gathering') return |
29 | var yourId = api.keys.sync.id() |
30 | var hidden = api.about.obs.valueFrom(msg.key, 'hidden', yourId) |
31 | var image = api.about.obs.latestValue(msg.key, 'image') |
32 | var title = api.about.obs.latestValue(msg.key, 'title') |
33 | var description = api.about.obs.latestValue(msg.key, 'description') |
34 | var location = api.about.obs.latestValue(msg.key, 'location') |
35 | var startDateTime = api.about.obs.latestValue(msg.key, 'startDateTime') |
36 | var endDateTime = api.about.obs.latestValue(msg.key, 'endDateTime') |
37 | var attendees = computed([api.about.obs.groupedValues(msg.key, 'attendee')], getAttendees) |
38 | if (!following) { |
39 | following = api.contact.obs.following(yourId) |
40 | } |
41 | |
42 | var imageUrl = computed(image, (id) => api.blob.sync.url(id)) |
43 | var content = h('GatheringCard', [ |
44 | h('div.title', [ |
45 | h('a', {href: msg.key}, title), |
46 | h('button', { |
47 | 'ev-click': send(api.gathering.sheet.edit, msg.key) |
48 | }, 'Edit Details') |
49 | ]), |
50 | h('div.time', computed(startDateTime, formatTime)), |
51 | when(image, h('a.image', { |
52 | href: imageUrl, |
53 | style: { |
54 | 'background-image': computed(imageUrl, (url) => `url(${url})`) |
55 | } |
56 | })), |
57 | h('div.attending', [ |
58 | h('div.title', ['Attendees', ' (', computed(attendees, (x) => x.length), ')']), |
59 | h('div.attendees', [ |
60 | map(attendees, (attendee) => { |
61 | return h('a.attendee', { |
62 | href: attendee, |
63 | title: nameAndFollowWarning(attendee) |
64 | }, api.about.html.image(attendee)) |
65 | }) |
66 | ]), |
67 | h('div.actions', [ |
68 | h('button -attend', { |
69 | 'ev-click': send(publishAttending, msg.key) |
70 | }, `Attending`), |
71 | h('button -attend', { |
72 | 'ev-click': send(publishNotAttending, msg.key) |
73 | }, `Can't Attend`) |
74 | ]) |
75 | ]), |
76 | h('div.location', markdown(location)), |
77 | when(description, h('div.description', markdown(description))) |
78 | ]) |
79 | |
80 | var element = api.message.html.layout(msg, extend({ |
81 | content, |
82 | miniContent: 'Added a gathering', |
83 | layout: 'mini' |
84 | }, opts)) |
85 | |
86 | // hide if no title set or hidden |
87 | var visible = computed([title, hidden], (title, hidden) => { |
88 | return title && !hidden |
89 | }) |
90 | |
91 | return when(visible, api.message.html.decorate(element, { msg })) |
92 | }) |
93 | |
94 | function publishAttending (id) { |
95 | var yourId = api.keys.sync.id() |
96 | |
97 | // publish with confirm |
98 | api.message.async.publish({ |
99 | type: 'about', |
100 | about: id, |
101 | attendee: { |
102 | link: yourId |
103 | } |
104 | }) |
105 | } |
106 | |
107 | function publishNotAttending (id) { |
108 | var yourId = api.keys.sync.id() |
109 | |
110 | // publish with confirm |
111 | api.message.async.publish({ |
112 | type: 'about', |
113 | about: id, |
114 | attendee: { |
115 | link: yourId, |
116 | remove: true |
117 | } |
118 | }) |
119 | } |
120 | |
121 | function nameAndFollowWarning (id) { |
122 | var yourId = api.keys.sync.id() |
123 | return computed([api.about.obs.name(id), id, following], function nameAndFollowWarning (name, id, following) { |
124 | if (id === yourId) { |
125 | return `${name} (you)` |
126 | } else if (following.includes(id)) { |
127 | return `${name}` |
128 | } else { |
129 | return `${name} (not following)` |
130 | } |
131 | }) |
132 | } |
133 | |
134 | function markdown (obs) { |
135 | return computed(obs, (text) => { |
136 | if (typeof text === 'string') return api.message.html.markdown(text) |
137 | }) |
138 | } |
139 | } |
140 | |
141 | function formatTime (time) { |
142 | if (time && time.epoch) { |
143 | return moment(time.epoch).format('LLLL') |
144 | } |
145 | } |
146 | |
147 | function getAttendees (lookup) { |
148 | return Object.keys(lookup) |
149 | } |
150 |
Built with git-ssb-web