-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
397 lines (349 loc) · 12.3 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
const $locationList = document.getElementById('location-list')
const $sidePane = document.getElementById('side-pane')
const $locationsButton = document.getElementById('locations-toggle-button');
const $body = document.body;
// we're using the map color from google sheet to indicate location status,
// but using a different display color for accessibility. so the original
// color is treated as an ID
const unknownStatus = {
id: '#aaaaaa',
name: 'unknown',
label: 'status unknown',
accessibleColor: '#ffffbf'
}
const statusOptions = [
{
id: '#fc03df',
name: 'recieving',
label: 'open for receiving donations',
accessibleColor: '#2c7bb6'
},
{
id: '#03bafc',
name: 'distributing',
label: 'open for distributing donations',
accessibleColor: '#abd9e9'
},
{
id: '#9f48ea',
name: 'both',
label: 'open for both',
accessibleColor: '#fdae61'
},
{
id: '#c70000',
name: 'closed',
label: 'currently closed',
accessibleColor: '#d7191c'
},
unknownStatus
]
let langs
// show all langs in dev mode
if (window.location.search.indexOf('dev') > -1) {
langs = ['eng', 'spa', 'kar', 'som', 'hmn', 'amh', 'orm']
// otherwise only show these
} else {
langs = ['eng', 'spa']
}
// initialize translator and load translations file
const translator = new Translator({ enabledLanguages: langs })
let welcome
// get the translation data and then run the translator
fetch(TRANSLATION_URL).then(async (resp) => {
try {
const data = await resp.json()
// add translation definitions to translator
translator.setTranslations(Translator.ParseGoogleSheetData(data))
// create the welcome modal
welcome = new WelcomeModal({
languages: translator.availableLanguages,
// when language is selected, run translation
onLanguageSelect: lang => {
translator.language = lang
translator.translate()
}
})
// show welcome modal if no language is selected
if (!translator.language) {
welcome.open()
// otherwise just run translator
} else {
translator.translate()
}
// when language button is clicked, re-open welcome modal
const languageButton = document.getElementById('lang-select-button')
languageButton.addEventListener('click', () => welcome.open())
} catch (e) {
console.error('Translation error', e)
}
})
let locations = []
// Alternative base style: 'mapbox://styles/mapbox/light-v10',
// See also: https://docs.mapbox.com/mapbox-gl-js/example/setstyle/
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/saman/ckawvg6bk011x1ipepu7nqlbh',
zoom: 10,
center: [-93.212471, 44.934473],
})
map.setPadding({ top: 300, bottom: 20, left: 20, right: 20 })
map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
trackProximity: true,
proximity: true,
collapsed: true,
clearAndBlurOnEsc: true,
clearOnBlur: true,
marker: false,
flyTo: {}
}), 'bottom-right'
)
// Add geolocate control to the map.
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
}), 'bottom-right'
)
// Add zoom and rotate controls
map.addControl(new mapboxgl.NavigationControl(), 'bottom-right');
// convert case
function camelToTitle(str) {
const result = str.replace(/([A-Z])/g,' $1')
return result.charAt(0).toUpperCase() + result.slice(1)
}
function truthy(str) {
const normalizedStr = _.toUpper(str);
return _.includes(['TRUE', 'YES', 'T', 'Y'], normalizedStr);
}
// open/close location sidebar
function toggleSidePane() {
if ($body.classList.contains('list-active')) {
$locationsButton.innerText = translator.get('show_list_button', 'Show list of locations')
$body.classList.remove('list-active')
} else {
$locationsButton.innerText = translator.get('hide_list_button', 'Hide list of locations')
$body.classList.add('list-active')
}
}
// open/close help info
function toggleHelpInfo() {
if (window.location.hash === '#help-info') {
// this currently leaves a '#' at the end of the URL on close. there's definitely a
// better solution out there, but this works even if it's not pretty
window.location.hash = ''
} else {
window.location.hash = '#help-info'
}
}
// close popups for all locations
function closePopups() {
locations.forEach(location => {
location.marker.getPopup().remove()
})
}
function needsMoneyComponent(location) {
if (!location.seekingMoney) return ''
let link = '';
if (location.seekingMoneyURL && location.seekingMoneyURL !== '') {
link = `<a data-translation-id="seeking_money_link" href="${location.seekingMoneyURL}" target="_blank">DONATE NOW!</a>`;
}
return `<span class="seekingMoney seeking-money location-list--badge"><span data-translation-id="seeking_money">Needs Money</span> ${link}</span>`;
}
function addressComponent(address) {
return `<address><a href="https://maps.google.com?saddr=Current+Location&daddr=${encodeURI(address)}" target="_blank">${address}</a></address>`;
}
// get the status info for a location using the color as ID, else default to unknown.
const getStatus = id => {
const status = _.find(statusOptions, s => (s.id === id.toLowerCase()))
return status || unknownStatus
}
// create an item for the side pane using a location
const createListItem = (location, status, lng, lat) => {
const urgentNeed = location.urgentNeed ? `<p class="urgentNeed p location-list--important"><span data-translation-id="urgent_need">Urgent Need</span>: ${location.urgentNeed}</p>` : ''
const seekingMoney = needsMoneyComponent(location);
let seekingVolunteers = ''
if (location.seekingVolunteers && location.seekingVolunteers.match(/(?:\byes\b)/i)) {
seekingVolunteers = `<span data-translation-id="seeking_volunteers" class="seekingVolunteers location-list--badge">Needs Volunteer Support</span>`
}
const $item = document.createElement('div')
$item.classList.add('location-list--item')
$item.dataset.id = status.id;
$item.innerHTML = `
<div class="flex">
<span title="${status.id}" class="status location-list--indicator" style="background-color: ${status.accessibleColor};">${status.id}</span>
<div>
<h2 class='h2'>
<span class="name">${location.name}</span>
</h2>
<h3 class="h3 neighborhood">${location.neighborhood}</h3>
${urgentNeed}
${seekingVolunteers}
${seekingMoney}
</div>
</div>
`
$item.addEventListener('click', (evt) => {
const popup = location.marker.getPopup()
if (popup.isOpen()) {
popup.remove()
} else {
closePopups()
toggleSidePane()
map.jumpTo({
center: popup.getLngLat(),
essential: true,
zoom: 13
})
console.log('popup', popup)
popup.addTo(map)
if (translator.language) translator.translate()
}
})
return $item
}
//////////////////////////
// Protect against columns not yet existing in the spreadsheet.
// We can remove once they are added to the sheet.
function extractSeekingMoney(item) {
try {
return truthy(item.gsx$seekingmoney.$t);
} catch (err) {
console.info("Seeking Money Column does not exist yet.");
return false;
}
}
function extractSeekingMoneyURL(item) {
try {
return item.gsx$seekingmoneyurl.$t;
} catch (err) {
console.info("Seeking Money URL Column does not exist yet.");
return '';
}
}
//////////////////////////
function extractRawLocation(item) {
return {
name: item.gsx$nameoforganization.$t,
neighborhood: item.gsx$neighborhood.$t,
address: item.gsx$addresswithlink.$t,
mostRecentlyUpdatedAt: item.gsx$mostrecentlyupdated.$t,
seekingMoney: extractSeekingMoney(item),
seekingMoneyURL: extractSeekingMoneyURL(item),
currentlyOpenForDistributing: item.gsx$currentlyopenfordistributing.$t,
openingForDistributingDontations: item.gsx$openingfordistributingdonations.$t,
closingForDistributingDonations: item.gsx$closingfordistributingdonations.$t,
accepting: item.gsx$accepting.$t,
notAccepting: item.gsx$notaccepting.$t,
currentlyOpenForReceiving: item.gsx$currentlyopenforreceiving.$t,
openingForReceivingDontations: item.gsx$openingforreceivingdonations.$t,
closingForReceivingDonations: item.gsx$closingforreceivingdonations.$t,
seekingVolunteers: item.gsx$seekingvolunteers.$t,
urgentNeed: item.gsx$urgentneed.$t,
notes: item.gsx$notes.$t
}
}
// start fetching data right away
const dataPromise = fetch(DATA_URL)
// handle the map load event
const onMapLoad = async () => {
const resp = await dataPromise
const data = await resp.json()
// filter and transform data from google sheet
locations = _.chain(data.feed.entry)
.filter(item => (item.gsx$nameoforganization.$t != '') && (item.gsx$longitude.$t != '') && (item.gsx$latitude.$t != '')) // only items with names and lon,lat
.sortBy(item => item.gsx$nameoforganization.$t )
.map(item => {
try {
// the location schema
const rawLocation = extractRawLocation(item);
const location = _.pickBy(rawLocation, val => val != '');
const status = getStatus(item.gsx$color.$t);
// transform location properties into HTML
const propertyTransforms = {
name: (name, _) => `<h2 class='h2'>${name}</h2>`,
neighborhood: (neighborhood, _) => `<h3 class='h3'>${neighborhood}</h3>`,
address: addressComponent, // driving directions in google, consider doing inside mapbox
seekingMoney: (value, location) => needsMoneyComponent(location),
seekingMoneyURL: (value, _) => '',
mostRecentlyUpdatedAt: (datetime, _) => `<div class='updated-at' title='${datetime}'><span data-translation-id='last_updated'>Last updated</span> ${moment(datetime, 'H:m M/D').fromNow()}</div>`
}
// render HTML for marker
const markerHtml = _.map(location, (value, key) => {
if (propertyTransforms[key]) return propertyTransforms[key](value, location)
else return `<div class='p row'><p data-translation-id="${_.snakeCase(key)}"class='txt-deemphasize key'>${camelToTitle(key)}</p><p class='value'>${value}</p></div>`
}).join('')
// create marker
location.marker = new mapboxgl.Marker({ color: status.accessibleColor })
.setLngLat([ parseFloat(item.gsx$longitude.$t), parseFloat(item.gsx$latitude.$t) ])
.setPopup(new mapboxgl.Popup().setMaxWidth('275px').setHTML(`<div class='popup-content'>${markerHtml}</div>`))
.addTo(map);
location.marker.getElement().className += " status-" + status.name;
// run translation when popup opens
const popup = location.marker.getPopup()
popup.on('open', () => translator.translate(popup.getElement()))
// add to the side panel
$locationList.appendChild(createListItem(location, status, item.gsx$longitude.$, item.gsx$latitude.$))
return location
} catch (e) {
console.error(e)
return
}
}).value()
// add nav
const filter = new Filter($sidePane, {
sortOptions: [
{
name: 'urgentNeed',
label: 'Urgent requests first',
sort: { order: 'desc' }
},
{
name: 'name',
label: 'Alphabetical (name)',
sort: { order: 'asc' },
selected: true
},
{
name: 'status',
label: 'Location status',
sort: { order: 'desc' }
},
{
name: 'neighborhood',
label: 'Alphabetical (neighborhood)',
sort: { order: 'asc' }
},
{
name: 'seekingMoney',
label: 'Needs money',
sort: { order: 'desc' }
},
{
name: 'seekingVolunteers',
label: 'Needs volunteers',
sort: { order: 'desc' }
}
],
statusOptions,
onAfterUpdate: () => translator.translate()
})
// making sure to run translations after
// everything else is loaded
translator.translate()
}
// load map
map.on('load', onMapLoad)
// render key
const key = document.getElementById('key')
statusOptions.forEach(s => {
const el = document.createElement('div');
el.classList = ['legend--item'];
el.innerHTML = `<span class="legend--item--swatch" style="background-color: ${s.accessibleColor}"></span>${s.label}`
key.append(el)
})