-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
288 lines (239 loc) · 8.29 KB
/
index.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
<html>
<head>
<title>Massachusetts Legislature</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base target="_blank">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin="" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet-search.min.css" />
<style>
:root {
--color--district: rgba(0, 0, 0, 0.2);
--color--district--active: rgba(0, 0, 0, 0.4);
--color--Democrat: rgba(0, 0, 255, 0.5);
--color--Democrat--active: rgba(0, 0, 255, 0.9);
--color--Republican: rgba(255, 0, 0, 0.5);
--color--Republican--active: rgba(255, 0, 0, 0.9);
}
body {
margin: 0;
/* Matching .leaflet-container */
background: #ddd;
}
#map:not(.leaflet-container)::after {
content: '…loading map…';
display: block;
padding: 1rem;
text-align: center;
/* Matching .leaflet-container */
font: oblique 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
}
a {
color: #00529A !important;
}
.legend {
background: white;
border: 2px solid rgba(0, 0, 0, 0.2);
border-radius: 4px;
padding: 0 0.5rem;
}
.legend__item {
display: flex;
align-items: center;
margin: 0.5rem 0;
}
.legend__item::before {
background: var(--color--district);
border-radius: 2px;
content: '';
display: block;
margin-right: 0.5rem;
height: 1rem;
width: 1rem;
}
.legend__item--Democrat::before {
background: var(--color--Democrat);
}
.legend__item--Republican::before {
background: var(--color--Republican);
}
.district {
fill: var(--color--district);
fill-opacity: 1;
stroke: var(--color--district);
stroke-width: 1;
}
.district:hover {
stroke-width: 4;
}
.district--active {
fill: var(--color--district--active);
}
.district--Democrat {
fill: var(--color--Democrat);
}
.district--Democrat.district--active {
fill: var(--color--Democrat--active);
}
.district--Republican {
fill: var(--color--Republican);
}
.district--Republican.district--active {
fill: var(--color--Republican--active);
}
.leaflet-control-search .search-input {
outline: none;
width: 15rem;
}
.leaflet-control-search .search-tooltip {
margin: 2px 0 0;
width: 100%;
border-radius: 4px;
}
.leaflet-control-search .search-tip {
margin: 0;
border-radius: 0;
}
table {
border-collapse: collapse;
border-spacing: 0;
font: inherit;
width: 100%;
}
th,
td {
text-align: left;
vertical-align: baseline;
white-space: nowrap;
}
</style>
</head>
<body>
<!--
Center the map with a constrained width and a responsive 64% aspect ratio (same as Massachusetts)
https://css-tricks.com/aspect-ratio-boxes/
Temporarily(?) using `style` attribute to keep page layout separate from map styling in <style>
-->
<div style="max-width: 800px; margin: 0 auto;">
<div style="position: relative; overflow: hidden; padding-top: 64%;">
<div id="map" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0;"></div>
</div>
</div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script src="https://unpkg.com/[email protected]/leaflet-providers.js"></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet-search.min.js"></script>
<script>
/* global L */
/* Load the legislative district boundaries and rep data */
Promise.all([
/* The GeoJSON contains basic contact information for each rep */
fetch('dist/ma_house.geojson').then((response) => response.json()),
fetch('dist/ma_senate.geojson').then((response) => response.json()),
/**
* To add additional data about each rep/district, uncomment this `fetch`
* and set the URL to a JSON data source with a `district` field.
*
* For a CSV, use https://www.papaparse.com/; see an example at:
* https://github.com/actonmass/campaign-map/blob/main/index.html
*/
// fetch('https://example.com/rep_data.json').then((response) => response.json()),
])
.then(([houseFeatures, senateFeatures, repData = []]) => {
/* Build a rep info object, e.g. `rep.first_name`, `rep.extra_data` */
const repDataByDistrict = repData.reduce((acc, cur) => {
acc[cur.district] = cur;
return acc;
}, {});
const repProperties = (feature) => {
const data = repDataByDistrict[feature.properties.district] || {};
return { ...feature.properties, ...data };
};
/* Templates for map elements */
const districtLegend = () => /* html */`
<div class="legend__item legend__item--Democrat">
Democrat
</div>
<div class="legend__item legend__item--Republican">
Republican
</div>
<div class="legend__item legend__item">
Other
</div>
`;
const districtStyle = (rep) => ({
className: `district district--${rep.party}`,
});
const districtPopup = (rep) => `
<p>
<strong>${rep.first_name} ${rep.last_name}</strong>
${rep.party ? `<br />${rep.party}` : ''}
<br />${rep.district}
${rep.url ? `<br /><a href="${rep.url}">Contact</a>` : ''}
</p>
`;
const onPopup = (e) => {
const active = e.type === 'popupopen';
e.target.getElement().classList.toggle('district--active', active);
};
/* Build the district layers */
const districtLayer = (features) => L.geoJson(features, {
style: (feature) => districtStyle(repProperties(feature)),
onEachFeature: (feature, layer) => {
const rep = repProperties(feature);
layer.bindPopup(districtPopup(rep));
layer.on('popupopen', onPopup);
layer.on('popupclose', onPopup);
// Enable searching by name or district; inspired by:
// https://github.com/stefanocudini/leaflet-search/issues/52#issuecomment-266168224
// eslint-disable-next-line no-param-reassign
feature.properties.index = `${rep.first_name} ${rep.last_name} - ${rep.district}`;
},
});
const districtSearch = (layer) => new L.Control.Search({
layer,
propertyName: 'index',
initial: false,
marker: false,
textPlaceholder: 'Search legislators and districts',
moveToLocation(latlng, title, map) {
map.fitBounds(latlng.layer.getBounds());
latlng.layer.openPopup();
},
});
const layers = {
House: districtLayer(houseFeatures),
Senate: districtLayer(senateFeatures),
};
const searchControls = {
House: districtSearch(layers.House),
Senate: districtSearch(layers.Senate),
};
/* Build the map */
const map = L.map('map').addLayer(L.tileLayer.provider('CartoDB.Positron'));
Object.keys(layers).forEach((chamber) => {
layers[chamber]
.on('add', () => searchControls[chamber].addTo(map))
.on('remove', () => searchControls[chamber].remove());
});
map.addLayer(layers.House)
.fitBounds(layers.House.getBounds())
// Avoid accidental excessive zoom out
.setMinZoom(map.getZoom());
const layerControl = L.control.layers(layers, {}, {
collapsed: false,
});
layerControl.addTo(map);
const legendControl = L.control({ position: 'topright' });
legendControl.onAdd = () => {
const div = L.DomUtil.create('div', 'legend');
div.innerHTML = districtLegend();
return div;
};
legendControl.addTo(map);
});
</script>
</body>
</html>