-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
401 lines (358 loc) · 11 KB
/
index.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
398
399
400
401
import React from 'react';
import PropTypes from 'prop-types';
import { find, filter } from 'lodash';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
import Point from './Point';
import MapInset from './MapInset';
import './style.scss';
import './popover.scss';
import './popovertip.scss';
import './popover_implementation.scss';
import { LAYER_NAME, accessToken } from './constants';
import { REQUEST_SUPPORT, OFFER_SUPPORT, GENERAL } from '../../state/constants';
const mapboxgl = window.mapboxgl;
class MapView extends React.Component {
constructor(props) {
super(props);
this.filterDistrict = ['any'];
this.includedStates = ['in', 'NAME'];
this.addPopups = this.addPopups.bind(this);
this.addClickListener = this.addClickListener.bind(this);
this.addLayer = this.addLayer.bind(this);
this.createFeatures = this.createFeatures.bind(this);
this.updateData = this.updateData.bind(this);
this.handleReset = this.handleReset.bind(this);
this.insetOnClickEvent = this.insetOnClickEvent.bind(this);
this.handleClickOnInset = this.handleClickOnInset.bind(this);
this.initializeMap = this.initializeMap.bind(this);
this.state = {
popoverColor: 'popover-general-icon',
bbox: null,
};
}
componentDidMount() {
const { networks } = this.props;
const featuresHome = this.createFeatures(networks);
this.initializeMap(featuresHome);
}
componentDidUpdate(prevProps) {
const {
hoveredPointId,
viewState,
networks
} = this.props;
this.map.resize();
// changed filters
if (networks.length !== prevProps.networks.length) {
this.updateData(this.props.networks);
}
// toggled view between full map and zoom
if (prevProps.viewState !== viewState) {
this.hoveredPopup.remove(); //close any open popup
if (viewState === 'default') {
this.setInitialState();
}
}
// if a bounding box has been set before render, fix the bounds and clear
// for changing map zoom and size
if (this.state.bbox && viewState === 'list') {
this.map.fitBounds(this.state.bbox);
this.setState({
bbox: null
})
}
if (hoveredPointId) {
this.hoverPoint(hoveredPointId)
}
if (prevProps.hoveredPointId && prevProps.hoveredPointId !== hoveredPointId) {
this.unHoverPoint(prevProps.hoveredPointId);
}
}
insetOnClickEvent(e) {
this.setState({ inset: false });
const dataBounds = e.target.parentNode.parentNode.getAttribute('data-bounds').split(',');
const boundsOne = [Number(dataBounds[0]), Number(dataBounds[1])];
const boundsTwo = [Number(dataBounds[2]), Number(dataBounds[3])];
const bounds = boundsOne.concat(boundsTwo);
this.map.fitBounds(bounds);
}
updateData(networks) {
const featuresHome = this.createFeatures(networks);
this.map.fitBounds([[-128.8, 23.6], [-65.4, 50.2]]);
if (!this.map.getSource(LAYER_NAME)) {
return;
}
this.map.getSource(LAYER_NAME).setData(featuresHome);
}
createFeatures(networks) {
const featuresHome = {
features: [],
type: 'FeatureCollection',
};
featuresHome.features = networks.map((network) => {
const newFeature = new Point(network);
return newFeature;
});
return featuresHome;
}
addPopups(layer) {
const { map } = this;
this.hoveredPopup = new mapboxgl.Popup({
closeButton: true,
closeOnClick: true,
});
map.on('mousemove', (e) => {
const features = map.queryRenderedFeatures(e.point, { layers: [layer] });
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
if (features.length) {
const feature = features[0];
const { properties } = feature;
const popoverClassName = `popover-${feature.properties.category.split(' ').join('-').toLowerCase()}`
this.setState({
popoverColor: popoverClassName
});
this.props.setHoveredPoint(features[0].id);
let link;
if (properties.generalForm) {
link = `<a target="_blank" href=${properties.generalForm}>Link to form</a>`
} else if (properties.supportOfferForm && properties.supportRequestForm) {
link = `<a class="side-by-side" target="_blank" href=${properties.supportOfferForm}>Offer support</a><a class="side-by-side" target="_blank" href=${properties.supportRequestForm}>Request support</a>`
} else if (properties.supportOfferForm) {
link = `<a href=${properties.supportOfferForm}>Offer support</a>`;
} else if (properties.supportRequestForm) {
link = `<a href=${properties.supportRequestForm}>Request support</a>`;
} else {
link = `<a href=${properties.facebookPage}>Link to group</a>`;
}
return this.hoveredPopup.setLngLat(feature.geometry.coordinates)
.setHTML(`
<h4>${properties.title}</h4>
<div>${link}</div>`)
.addTo(map);
}
return undefined;
});
}
addClickListener() {
const { map } = this;
const {
setLatLng
} = this.props;
map.on('click', (e) => {
const features = map.queryRenderedFeatures(
e.point,
{
layers: [LAYER_NAME],
},
);
if (features.length > 0) {
let bbox = JSON.parse(features[0].properties.bbox);
setLatLng({lat: features[0].properties.lat, lng: features[0].properties.lng});
this.setState({bbox});
}
});
}
hoverPoint(hoveredPinId) {
this.map.setFeatureState({
source: LAYER_NAME,
id: hoveredPinId
}, {
hover: true
});
};
unHoverPoint(hoveredPinId) {
this.map.setFeatureState({
source: LAYER_NAME,
id: hoveredPinId
}, {
hover: false
});
};
addLayer(featuresHome) {
this.map.addLayer(
{
id: LAYER_NAME,
paint: {
// 'circle-opacity': 0.5,
'circle-opacity': [
'case',
['boolean', ['feature-state', 'hover'], false],
1,
0.5
],
'circle-radius': 8
// [
// 'interpolate', ['linear'],
// ['number', ['get', 'scale'], 5],
// 1,
// 5,
// 70,
// 70
// ]
,
'circle-stroke-color': '#fff',
'circle-stroke-width': [
'case',
['boolean', ['feature-state', 'hover'], false],
2,
1
],
'circle-color': [
'match',
['get', 'category'],
REQUEST_SUPPORT,
'#ef4822',
OFFER_SUPPORT,
'#6ac1e5',
GENERAL,
'#8048f3',
/* other */
'#057A8F'
]
},
source: {
data: featuresHome,
type: 'geojson',
},
type: 'circle',
},
'district_interactive',
);
}
handleReset() {
const {
setLatLng
} = this.props;
setLatLng({});
}
setInitialState() {
this.map.fitBounds([
[-128.8, 23.6],
[-65.4, 50.2]
]);
document.getElementsByClassName('mapboxgl-ctrl-geocoder--input');
Array.from(document.getElementsByClassName('mapboxgl-ctrl-geocoder--input')).forEach(ele => {
ele.value = '';
})
this.map.resize();
}
handleClickOnInset(bounds) {
// this is for clicking on a state inset
this.setState({bbox: bounds})
const mbBounds = new mapboxgl.LngLatBounds(bounds);
const center = mbBounds.getCenter();
this.props.setLatLng(center);
}
// Creates the button in our zoom controls to go to the national view
makeZoomToNationalButton() {
document.querySelector('.mapboxgl-ctrl-compass').remove();
if (document.querySelector('.mapboxgl-ctrl-usa')) {
document.querySelector('.mapboxgl-ctrl-usa').remove();
}
const usaButton = document.createElement('button');
usaButton.className = 'mapboxgl-ctrl-icon mapboxgl-ctrl-usa';
usaButton.innerHTML = '<span class="usa-icon"></span>';
usaButton.addEventListener('click', this.handleReset);
document.querySelector('.mapboxgl-ctrl-group').appendChild(usaButton);
}
initializeMap(featuresHome) {
const {
setLatLng
} = this.props;
mapboxgl.accessToken = accessToken;
const styleUrl = 'mapbox://styles/townhallproject/cjgr7qoqr00012ro4hnwlvsyp';
this.map = new mapboxgl.Map({
container: 'map',
style: styleUrl,
});
// Set Mapbox map controls
this.map.addControl(new mapboxgl.NavigationControl());
this.map.scrollZoom.disable();
this.map.dragRotate.disable();
this.map.touchZoomRotate.disableRotation();
this.makeZoomToNationalButton();
const { map } = this;
const me = this;
this.map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
countries: 'us',
marker: false,
zoom: 12,
flyTo: false,
})
.on('clear', function (result) {
setLatLng({});
})
.on('result', function (returned) {
map.resize();
setLatLng({
lat: returned.result.center[1],
lng: returned.result.center[0]
});
me.setState({
bbox: returned.result.bbox
})
}),
'top-left'
);
// map on 'load'
this.map.on('load', () => {
this.map.fitBounds([[-128.8, 23.6], [-65.4, 50.2]]);
this.addClickListener();
this.addLayer(featuresHome);
this.addPopups(LAYER_NAME);
// this.map.getSource(LAYER_NAME).setData(featuresHome);
});
}
render() {
const {
center,
resetSelections,
setLatLng,
viewState,
networks,
} = this.props;
return (
<React.Fragment>
<div id="map" className={this.state.popoverColor}>
<div className="map-overlay" id="legend">
<MapInset
networks={filter(networks, { state: 'AK' })}
center={center}
stateName="AK"
viewState={viewState}
resetSelections={resetSelections}
setLatLng={setLatLng}
setBounds={this.handleClickOnInset}
mapId="map-overlay-alaska"
bounds={[[-170.15625, 51.72702815704774], [-127.61718749999999, 71.85622888185527]]}
/>
<MapInset
networks={filter(networks, { state: 'HI' })}
stateName="HI"
center={center}
viewState={viewState}
resetSelections={resetSelections}
setLatLng={setLatLng}
setBounds={this.handleClickOnInset}
mapId="map-overlay-hawaii"
bounds={[
[-161.03759765625, 18.542116654448996],
[-154.22607421875, 22.573438264572406]]}
/>
</div>
</div>
</React.Fragment>
);
}
}
MapView.propTypes = {
networks: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
setLatLng: PropTypes.func.isRequired,
};
MapView.defaultProps = {
};
export default MapView;