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