-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublicmeetings.html
645 lines (563 loc) · 26.7 KB
/
publicmeetings.html
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display a popup on click</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.8.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.8.1/mapbox-gl.js"></script>
<script src="https://npmcdn.com/csv2geojson@latest/csv2geojson.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<body>
<div class="sidebar">
<div class="heading">
<h1>Public Meetings in the Carolinas</h1>
</div>
<div id="counties" class="listings"></div>
<div id="listings" class="listings"></div>
</div>
<div id="map" class="map"></div>
<head>
<style>
body {
color: #444;
background-color: white;
font: 400 15px/22px 'Source Sans Pro', 'Helvetica Neue', sans-serif;
margin: 0;
padding: 0;
-webkit-font-smoothing: antialiased;
}
* {
box-sizing: border-box;
}
.sidebar {
position: absolute;
width: 33.3333%;
height: 100%;
top: 0;
left: 0;
overflow: hidden;
border-right: 1px solid rgba(0, 0, 0, 0.2);
}
.map {
position: absolute;
left: 33.3333%;
width: 66.6666%;
top: 0;
bottom: 0;
}
h1 {
font-size: 20px;
margin: 0;
font-weight: 400;
line-height: 20px;
padding: 20px 2px;
}
a {
color: #6667AB;
text-decoration: none;
}
a:hover {
color: #6667AB;
}
.heading {
background: #fff;
border-bottom: 1px solid #eee;
min-height: 60px;
line-height: 60px;
padding: 0px 10px;
background-color: #006152;
color: #fff;
}
.listings {
height: 100%;
overflow: auto;
padding-bottom: 60px;
}
.listings .item {
display: block;
border-bottom: 1px solid #eee;
padding: 10px;
text-decoration: none;
}
.listings .item:last-child {
border-bottom: none;
}
.listings .item .title {
display: block;
color: #333 ;
font-weight: 700;
}
.listings .item .title small {
font-weight: 400;
}
.listings .item.active .title,
.listings .item .title:hover {
color: #333;
}
.listings .item.active {
background-color: #f8f8f8;
}
::-webkit-scrollbar {
width: 3px;
height: 3px;
border-left: 0;
background: rgba(0, 0, 0, 0.1);
}
::-webkit-scrollbar-track {
background: none;
}
::-webkit-scrollbar-thumb {
background: #444;
border-radius: 0;
}
.marker {
border: none;
cursor: pointer;
height: 45px;
width: 45px;
background-image: url('yellow-marker-icon.svg');
}
/* Marker tweaks */
.mapboxgl-popup {
width:100000px;
padding-bottom: 50px;
}
.mapboxgl-popup-close-button {
display: none;
}
.mapboxgl-popup-content {
font: 200 15px/22px 'Source Sans Pro', 'Helvetica Neue', sans-serif;
padding: 0;
width: 500px;
}
.mapboxgl-popup-content h3 {
background: #006152;
color: #fff;
margin: 0;
padding: 10px;
border-radius: 3px 3px 0 0;
font-weight: 300;
margin-top: -15px;
}
.mapboxgl-popup-content h4 {
margin: 0;
padding: 10px;
font-weight: 400;
}
.mapboxgl-popup-content div {
padding: 10px;
}
.mapboxgl-popup-anchor-top > .mapboxgl-popup-content {
margin-top: 15px;
}
.mapboxgl-popup-anchor-top > .mapboxgl-popup-tip {
border-bottom-color: #444;
}
</style>
</head>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiY2N0aGVncmVhdCIsImEiOiJjbDI2a2lodnYwMnRnM2ZvdXVhZXNjbHd0In0.4CfhKr_VP1IDEM08Nk7PXg';
const stateCoords = [-79.8, 35.3]
const defaultZoom = 7
const spreadSheetID = '12GiMtxkEZA-TzAB0iBf3S_euXBId7HaPlWUvpOf9p-Q';
const sheetName = 'all';
const sheetURI = `https://docs.google.com/spreadsheets/d/${spreadSheetID}/gviz/tq?tqx=out:csv&sheet=${sheetName}`;
/**
* Add the map to the page
*/
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: stateCoords,
zoom: defaultZoom,
scrollZoom: false
});
const getMeetingGSheetData = async () => {
let meetingGS;
try {
const res = await fetch(sheetURI)
const data = await res.text()
csv2geojson.csv2geojson(data, {
latfield: 'Latitude',
lonfield: 'Longitude',
delimiter: ',',
}, (err, data) => {
if (err) throw err
meetingGS = data
})
return meetingGS
} catch (error) {
throw error
}
}
const getMeetingJsonData = async () => {
try {
const res = await fetch('/meetings.json')
const data = await res.json()
return data
} catch (error) {
throw error
}
}
const mergeMeetingData = async () => {
let geoJson = {
type: 'FeatureCollection',
features: []
}
try {
const meetingsGS = await getMeetingGSheetData();
const meetingsJson = await getMeetingJsonData();
meetingsJson.features.forEach(j => geoJson.features.push(j))
meetingsGS.features.forEach(f => geoJson.features.push({
geometry: {
type: f.geometry.type,
longitude: f.geometry.coordinates?.[0],
latitude: f.geometry.coordinates?.[1],
coordinates: [...f.geometry?.coordinates],
},
properties: {
address: f.properties?.Address,
end: f.properties?.End_time,
government: f.properties?.Government,
id: f.properties?.Id,
location: f.properties?.Location,
publicbody: f.properties?.Public_body,
schedule: f.properties?.Schedule,
moreInfo: f.properties?.Source,
start: f.properties?.Start_time,
remote: f.properties?.Remote_options,
},
type: f.type
}))
return geoJson
} catch (error) {
throw error
}
}
/**
* Wait until the map loads to make changes to the map.
*/
map.on('load', () => {
/**
* This is where your '.addLayer()' used to be, instead
* add only the source without styling a layer
*/
mergeMeetingData()
.then(meetings => {
map.addSource('meetingPlaces', {
'type': 'geojson',
'data': meetings
});
buildCountyList(meetings)
}).catch(function (err) {
console.log('Failed to meetings', err);
});
});
/**
* Add a marker to the map for every meeting listing.
**/
function addMarkers(meetings) {
const groupedMeetings = groupMeetingsByLocation(meetings)
/* For each feature in the GeoJSON object above: */
for (const meeting in groupedMeetings) {
const marker = groupedMeetings[meeting]
/* Add a group of meetings marker to map*/
if (marker.length > 1) {
addGroupMarker(marker)
} else {
/* Create a div element for the marker. */
const el = document.createElement('div');
/* Assign a unique `id` to the marker. */
el.id = `marker-${marker[0].properties.Id}`;
/* Assign the `marker` class to each marker for styling. */
el.className = 'marker';
/**
* Create a marker using the div element
* defined above and add it to the map.
**/
new mapboxgl.Marker(el, { offset: [0, -23] })
.setLngLat(marker[0].geometry.coordinates)
.addTo(map);
/**
* Listen to the element and when it is clicked, do three things:
* 1. Fly to the point
* 2. Close all other popups and display popup for clicked meeting
* 3. Highlight listing in sidebar (and remove highlight for all other listings)
**/
el.addEventListener('click', (e) => {
/* Fly to the point */
flytoMeeting(marker[0].geometry.coordinates, 14);
/* Close all other popups and display popup for clicked meeting */
createPopUp(marker[0]);
/* Highlight listing in sidebar */
const activeItem = document.getElementsByClassName('active');
e.stopPropagation();
if (activeItem[0]) {
activeItem[0].classList.remove('active');
}
const listing = document.getElementById(
`listing-${marker[0].properties.id}`
);
listing.classList.add('active');
});
}
}
}
function addGroupMarker(arrOfMeetings) {
const locationKey = (arrOfMeetings[0].geometry.coordinates).toString()
const el = document.createElement('div');
el.id = `marker-${locationKey}`
el.className = 'marker'
new mapboxgl.Marker(el, { offset: [0, -23] })
.setLngLat(arrOfMeetings[0].geometry.coordinates)
.addTo(map);
el.addEventListener('click', e => {
flytoMeeting(arrOfMeetings[0].geometry.coordinates, 14)
createGroupPopUp(arrOfMeetings)
}, false)
}
function groupMeetingsByLocation(meetings) {
const meetingList = {}
for(let i = 0; i < meetings.length; i++) {
// construct key using lat and long to group by
const coords = meetings[i].geometry.coordinates
const coordsKey = coords.toString()
if (meetingList[coordsKey]) {
meetingList[coordsKey].push(meetings[i])
} else {
meetingList[coordsKey] = new Array(meetings[i])
}
}
return meetingList
}
/**
* Clear the map of markers and zoom back out
* Remove popup
*/
function removeAllMarkers() {
const markers = document.querySelectorAll('.marker')
const popup = document.querySelector('.mapboxgl-popup')
if(popup) popup.remove()
for(const marker of markers) {
marker.remove()
}
flytoMeeting();
}
/**
* Builds and returns a full meeting list separated by county
**/
function buildMeetingListByCounty(meetingList) {
return meetingList.features.reduce((obj, item) => {
const county = item.properties.Government || item.properties.government;
if (county !== "") {
if (!obj[county]) {
obj[county] = [{properties: {...item.properties}, geometry: {...item.geometry}}];
} else {
obj[county].push({properties: {...item.properties}, geometry: {...item.geometry}});
}
}
return obj;
}, []);
}
/**
* Adds the County list to sidebar
**/
function buildCountyList(meetingList) {
const countyList = buildMeetingListByCounty(meetingList)
const counties = document.querySelector('#counties');
/* Sort list of counties */
const sortedCounties = Object.keys(countyList).sort((c1, c2) => c1.localeCompare(c2))
for (const countyName of sortedCounties) {
/* Add a new county listing section to the sidebar. */
const county = counties.appendChild(document.createElement('div'));
/* Assign the `item` class to each listing for styling. */
county.className = 'item';
/* Add the link to the individual listing created above. */
const link = county.appendChild(document.createElement('a'));
link.href = '#';
link.className = 'title';
link.id = `${countyName}`;
link.innerHTML = `${countyName}`
}
/**
* Uses event delegation to listen for clicks on list items
* Build meeting list for the county and add markers to map
**/
counties.addEventListener('click', function(event) {
// ignore if not clicking on link
if(!event.target.matches('.title')) return
event.preventDefault();
// hide counties list when a county is clicked
counties.style.display = 'none'
const meetingList = countyList[event.target.id]
/* Fly to general are of listings*/
flytoMeeting(meetingList[0].geometry.coordinates, 10)
buildLocationList(meetingList)
addMarkers(meetingList)
}, false)
return countyList
}
/**
* Back button handles re-showing the county list
* Removes previous meeting listings and each event handler
* Removes markers from maps
* String with selector type is required for now
* Future: pass the elements as arguments
**/
function backButton(showElem, parentElem, removeEle) {
const listings = document.querySelector(parentElem)
const backButton = listings.appendChild(document.createElement('div'))
backButton.className = 'item'
/* Make the back button a link for accessibility */
const link = backButton.appendChild(document.createElement('a'))
link.href = '#'
link.className = 'title'
link.id = 'Back'
link.innerText = '< Back to Counties'
backButton.addEventListener('click', () => {
document.querySelector(showElem).style.display = 'block'
removeAllMarkers();
const collection = listings.querySelectorAll(removeEle)
for (const elem of collection) {
elem.parentNode.removeChild(elem)
}
})
}
/**
* Add a listing for each meeting to the sidebar.
**/
function buildLocationList(meetings) {
/* Add back button to top of list to nav back to counties */
backButton('#counties', '#listings', '.item')
/* Continue to add meeting listings */
for (const meeting of (meetings)) {
const publicBody = meeting.properties.Public_body || meeting.properties.publicbody
const id = meeting.properties.Id || meeting.properties.id
const location = meeting.properties.Location || meeting.properties.location
/* Add a new listing section to the sidebar. */
const listings = document.getElementById('listings');
const listing = listings.appendChild(document.createElement('div'));
/* Assign a unique `id` to the listing. */
listing.id = `listing-${id}`;
/* Assign the `item` class to each listing for styling. */
listing.className = 'item';
/* Add the link to the individual listing created above. */
const link = listing.appendChild(document.createElement('a'));
link.href = '#';
link.className = 'title';
link.id = `link-${id}`;
link.innerHTML = `${publicBody}`;
/* Add details to the individual listing. */
const details = listing.appendChild(document.createElement('div'));
details.innerHTML = `${location}`;
if (meeting.properties.phone) {
details.innerHTML += ` · ${meeting.properties.phoneFormatted}`;
}
/**
* Listen to the element and when it is clicked, do four things:
* 1. Update the `currentFeature` to the meeting associated with the clicked link
* 2. Fly to the point
* 3. Close all other popups and display popup for clicked meeting
* 4. Highlight listing in sidebar (and remove highlight for all other listings)
**/
// TODO: use event delgation to create event listeners
link.addEventListener('click', function () {
for (const feature of meetings) {
const featId = feature.properties.Id || feature.properties.id
if (this.id === `link-${featId}`) {
flytoMeeting(feature.geometry.coordinates, 15);
createPopUp(feature);
}
}
const activeItem = document.getElementsByClassName('active');
if (activeItem[0]) {
activeItem[0].classList.remove('active');
}
this.parentNode.classList.add('active');
});
}
}
/**
* Use Mapbox GL JS's `flyTo` to move the camera smoothly
* a given center point.
* Defaults: coords = center of State, zoomLevel = state in focus
**/
function flytoMeeting(coords = stateCoords, zoomLevel = defaultZoom) {
map.flyTo({
center: coords,
zoom: zoomLevel
});
}
/**
* Create a Mapbox GL JS `Popup`.
**/
function createPopUp(currentFeature) {
const popUps = document.getElementsByClassName('mapboxgl-popup');
if (popUps[0]) popUps[0].remove();
const popup = new mapboxgl.Popup({ closeOnClick: false })
.setLngLat(currentFeature.geometry.coordinates)
.setHTML(
`<h3><center>${currentFeature.properties.publicbody}</center></h3>
<h4>
<table>
<tr>
<th><center>Government</center></th>
<th><center>Public Body</center></th>
<th><center>Location</center></th>
</tr>
<tr>
<td><center>${currentFeature.properties.government}</center></td>
<td><center>${currentFeature.properties.publicbody}</center></td>
<td><center>${currentFeature.properties.location}</center></td>
</tr>
<tr>
<th><center>Address</center></th>
<th><center>Schedule</center></th>
<th><center>Start Time</center></th>
</tr>
<tr>
<td><center>${currentFeature.properties.address}</center></td>
<td><center>${currentFeature.properties.schedule}</center></td>
<td><center>${currentFeature.properties.start}</center></td>
</tr>
<tr>
<th><center>End Time</center></th>
<th><center>Remote Options</center></th>
</tr>
<tr>
<td><center>${currentFeature.properties.end}</center></td>
<td><center>${currentFeature.properties.remote}</center></td>
</h4>`
)
.addTo(map);
}
function createGroupPopUp(arrOfMeetings) {
const html = `
<div class='listings' id='meeting-location'>
${arrOfMeetings.map((meeting, idx) => `<div class='item'><a href="#" class='meeting-link title' id=${idx}>${meeting.properties.publicbody}</a></div>`).join("")}
</div>
`;
const popUps = document.getElementsByClassName('mapboxgl-popup');
if (popUps[0]) popUps[0].remove();
const popup = new mapboxgl.Popup({ closeOnClick: false })
.setLngLat(arrOfMeetings[0].geometry.coordinates)
.setHTML(
`<h3><center>Meetings at this location</center></h3>
${html}`
)
.addTo(map);
const meetingsAtLocation = document.querySelector('#meeting-location')
meetingsAtLocation.addEventListener('click', e => {
if(!e.target.matches('.meeting-link')) return
e.preventDefault()
popUps[0].remove()
flytoMeeting(arrOfMeetings[e.target.id].geometry.coordinates, 14)
createPopUp(arrOfMeetings[e.target.id])
})
}
//adding zoom and rotation controls to map
map.addControl(new mapboxgl.NavigationControl());
</script>
</body>
</html>