/
SearchMapWithMapbox.js
520 lines (458 loc) · 17.1 KB
/
SearchMapWithMapbox.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
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
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { arrayOf, func, node, number, shape, string } from 'prop-types';
import differenceBy from 'lodash/differenceBy';
import isEqual from 'lodash/isEqual';
import classNames from 'classnames';
import { types as sdkTypes } from '../../util/sdkLoader';
import { parse } from '../../util/urlHelpers';
import { propTypes } from '../../util/types';
import { ensureListing } from '../../util/data';
import { sdkBoundsToFixedCoordinates, hasSameSDKBounds } from '../../util/maps';
import { SearchMapInfoCard, SearchMapPriceLabel, SearchMapGroupLabel } from '../../components';
import { groupedByCoordinates, reducedToArray } from './SearchMap.helpers.js';
import css from './SearchMapWithMapbox.css';
export const LABEL_HANDLE = 'SearchMapLabel';
export const INFO_CARD_HANDLE = 'SearchMapInfoCard';
export const SOURCE_AUTOCOMPLETE = 'autocomplete';
const BOUNDS_FIXED_PRECISION = 8;
const { LatLng: SDKLatLng, LatLngBounds: SDKLatLngBounds } = sdkTypes;
/**
* Fit part of map (descriped with bounds) to visible map-viewport
*
* @param {Object} map - map that needs to be centered with given bounds
* @param {SDK.LatLngBounds} bounds - the area that needs to be visible when map loads.
*/
export const fitMapToBounds = (map, bounds, options) => {
const { padding = 0, isAutocompleteSearch = false } = options;
// map bounds as string literal for google.maps
const mapBounds = sdkBoundsToMapboxBounds(bounds);
const paddingOptionMaybe = padding == null ? { padding } : {};
const eventData = isAutocompleteSearch ? { searchSource: SOURCE_AUTOCOMPLETE } : {};
// If bounds are given, use it (defaults to center & zoom).
if (map && mapBounds) {
map.fitBounds(mapBounds, { ...paddingOptionMaybe, linear: true, duration: 0 }, eventData);
}
};
/**
* Convert Mapbox formatted LatLng object to Sharetribe SDK's LatLng coordinate format
* Longitudes > 180 and < -180 are converted to the correct corresponding value
* between -180 and 180.
*
* @param {LngLat} mapboxLngLat - Mapbox LngLat
*
* @return {SDKLatLng} - Converted latLng coordinate
*/
export const mapboxLngLatToSDKLatLng = lngLat => {
const mapboxLng = lngLat.lng;
// For bounding boxes that overlap the antimeridian Mapbox sometimes gives
// longitude values outside -180 and 180 degrees.Those values are converted
// so that longitude is always between -180 and 180.
const lng = mapboxLng > 180 ? mapboxLng - 360 : mapboxLng < -180 ? mapboxLng + 360 : mapboxLng;
return new SDKLatLng(lngLat.lat, lng);
};
/**
* Convert Mapbox formatted bounds object to Sharetribe SDK's bounds format
*
* @param {LngLatBounds} mapboxBounds - Mapbox LngLatBounds
*
* @return {SDKLatLngBounds} - Converted bounds
*/
export const mapboxBoundsToSDKBounds = mapboxBounds => {
if (!mapboxBounds) {
return null;
}
const ne = mapboxBounds.getNorthEast();
const sw = mapboxBounds.getSouthWest();
return new SDKLatLngBounds(mapboxLngLatToSDKLatLng(ne), mapboxLngLatToSDKLatLng(sw));
};
/**
* Convert sdk bounds that overlap the antimeridian into values that can
* be passed to Mapbox. This is achieved by converting the SW longitude into
* a value less than -180 that flows over the antimeridian.
*
* @param {SDKLatLng} bounds - bounds passed to the map
*
* @return {LngLatBoundsLike} a bounding box that is compatible with Mapbox
*/
const sdkBoundsToMapboxBounds = bounds => {
if (!bounds) {
return null;
}
const { ne, sw } = bounds;
// if sw lng is > ne lng => the bounds overlap antimeridian
// => flip the nw lng to the negative side so that the value
// is less than -180
const swLng = sw.lng > ne.lng ? -360 + sw.lng : sw.lng;
return [[swLng, sw.lat], [ne.lng, ne.lat]];
};
/**
* Return map bounds as SDKBounds
*
* @param {Mapbox} map - Mapbox map from where the bounds are asked
*
* @return {SDKLatLngBounds} - Converted bounds of given map
*/
export const getMapBounds = map => mapboxBoundsToSDKBounds(map.getBounds());
/**
* Return map center as SDKLatLng
*
* @param {Mapbox} map - Mapbox map from where the center is asked
*
* @return {SDKLatLng} - Converted center of given map
*/
export const getMapCenter = map => mapboxLngLatToSDKLatLng(map.getCenter());
/**
* Check if map library is loaded
*/
export const isMapsLibLoaded = () =>
typeof window !== 'undefined' && window.mapboxgl && window.mapboxgl.accessToken;
/**
* Return price labels grouped by listing locations.
* This is a helper function for SearchMapWithMapbox component.
*/
const priceLabelsInLocations = (
listings,
activeListingId,
infoCardOpen,
onListingClicked,
mapComponentRefreshToken
) => {
const listingArraysInLocations = reducedToArray(groupedByCoordinates(listings));
const priceLabels = listingArraysInLocations.reverse().map(listingArr => {
const isActive = activeListingId
? !!listingArr.find(l => activeListingId.uuid === l.id.uuid)
: false;
// If location contains only one listing, print price label
if (listingArr.length === 1) {
const listing = listingArr[0];
const infoCardOpenIds = Array.isArray(infoCardOpen)
? infoCardOpen.map(l => l.id.uuid)
: infoCardOpen
? [infoCardOpen.id.uuid]
: [];
// if the listing is open, don't print price label
if (infoCardOpen != null && infoCardOpenIds.includes(listing.id.uuid)) {
return null;
}
// Explicit type change to object literal for Google OverlayViews (geolocation is SDK type)
const { geolocation } = listing.attributes;
const key = listing.id.uuid;
return {
markerId: `price_${key}`,
location: geolocation,
type: 'price',
componentProps: {
key,
isActive,
className: LABEL_HANDLE,
listing,
onListingClicked,
mapComponentRefreshToken,
},
};
}
// Explicit type change to object literal for Google OverlayViews (geolocation is SDK type)
const firstListing = ensureListing(listingArr[0]);
const geolocation = firstListing.attributes.geolocation;
const key = listingArr[0].id.uuid;
return {
markerId: `group_${key}`,
location: geolocation,
type: 'group',
componentProps: {
key,
isActive,
className: LABEL_HANDLE,
listings: listingArr,
onListingClicked,
mapComponentRefreshToken,
},
};
});
return priceLabels;
};
/**
* Return info card. This is a helper function for SearchMapWithMapbox component.
*/
const infoCardComponent = (
infoCardOpen,
onListingInfoCardClicked,
createURLToListing,
mapComponentRefreshToken
) => {
const listingsArray = Array.isArray(infoCardOpen) ? infoCardOpen : [infoCardOpen];
if (!infoCardOpen) {
return null;
}
const firstListing = ensureListing(listingsArray[0]);
const key = firstListing.id.uuid;
const geolocation = firstListing.attributes.geolocation;
return {
markerId: `infoCard_${key}`,
location: geolocation,
componentProps: {
key,
mapComponentRefreshToken,
className: INFO_CARD_HANDLE,
listings: listingsArray,
onListingInfoCardClicked,
createURLToListing,
},
};
};
/**
* SearchMap component using Mapbox as map provider
*/
class SearchMapWithMapbox extends Component {
constructor(props) {
super(props);
this.map = typeof window !== 'undefined' && window.mapboxMap ? window.mapboxMap : null;
this.currentMarkers = [];
this.currentInfoCard = null;
this.state = { mapContainer: null, isMapReady: false };
this.viewportBounds = null;
this.onMount = this.onMount.bind(this);
this.onMoveend = this.onMoveend.bind(this);
this.initializeMap = this.initializeMap.bind(this);
this.handleDoubleClickOnInfoCard = this.handleDoubleClickOnInfoCard.bind(this);
this.handleMobilePinchZoom = this.handleMobilePinchZoom.bind(this);
}
componentWillReceiveProps(nextProps) {
if (!isEqual(this.props.location, nextProps.location)) {
// If no mapSearch url parameter is given, this is original location search
const { mapSearch } = parse(nextProps.location.search, {
latlng: ['origin'],
latlngBounds: ['bounds'],
});
if (!mapSearch) {
this.viewportBounds = null;
}
}
if (this.map) {
const currentBounds = getMapBounds(this.map);
// Do not call fitMapToBounds if bounds are the same.
// Our bounds are viewport bounds, and fitBounds will try to add margins around those bounds
// that would result to zoom-loop (bound change -> fitmap -> bounds change -> ...)
if (!isEqual(nextProps.bounds, currentBounds) && !this.viewportBounds) {
fitMapToBounds(this.map, nextProps.bounds, { padding: 0, isAutocompleteSearch: true });
}
}
}
componentDidUpdate(prevProps) {
if (!this.map && this.state.mapContainer) {
this.initializeMap();
/* Notify parent component that Mapbox map is loaded */
this.props.onMapLoad(this.map);
} else if (prevProps.mapComponentRefreshToken !== this.props.mapComponentRefreshToken) {
/* Notify parent component that Mapbox map is loaded */
this.props.onMapLoad(this.map);
}
}
componentWillUnmount() {
this.currentInfoCard.markerContainer.removeEventListener(
'dblclick',
this.handleDoubleClickOnInfoCard
);
document.removeEventListener('gesturestart', this.handleMobilePinchZoom, false);
document.removeEventListener('gesturechange', this.handleMobilePinchZoom, false);
document.removeEventListener('gestureend', this.handleMobilePinchZoom, false);
}
onMount(element) {
// This prevents pinch zoom to affect whole page on mobile Safari.
document.addEventListener('gesturestart', this.handleMobilePinchZoom, false);
document.addEventListener('gesturechange', this.handleMobilePinchZoom, false);
document.addEventListener('gestureend', this.handleMobilePinchZoom, false);
this.setState({ mapContainer: element });
}
onMoveend(e) {
if (this.map) {
// If reusableMapHiddenHandle is given and parent element has that class,
// we don't listen moveend events.
// This fixes mobile Chrome bug that sends map events to invisible map components.
const isHiddenByReusableMap =
this.props.reusableMapHiddenHandle &&
this.state.mapContainer.parentElement.classList.contains(
this.props.reusableMapHiddenHandle
);
if (!isHiddenByReusableMap) {
const viewportMapBounds = getMapBounds(this.map);
const viewportMapCenter = getMapCenter(this.map);
const viewportBounds = sdkBoundsToFixedCoordinates(
viewportMapBounds,
BOUNDS_FIXED_PRECISION
);
// ViewportBounds from (previous) rendering differ from viewportBounds currently set to map
// I.e. user has changed the map somehow: moved, panned, zoomed, resized
const viewportBoundsChanged =
this.viewportBounds && !hasSameSDKBounds(this.viewportBounds, viewportBounds);
this.props.onMapMoveEnd(viewportBoundsChanged, { viewportBounds, viewportMapCenter });
this.viewportBounds = viewportBounds;
}
}
}
initializeMap() {
const { offsetHeight, offsetWidth } = this.state.mapContainer;
const hasDimensions = offsetHeight > 0 && offsetWidth > 0;
if (hasDimensions) {
this.map = new window.mapboxgl.Map({
container: this.state.mapContainer,
style: 'mapbox://styles/mapbox/streets-v10',
scrollZoom: false,
});
window.mapboxMap = this.map;
var nav = new window.mapboxgl.NavigationControl({ showCompass: false });
this.map.addControl(nav, 'top-left');
this.map.on('moveend', this.onMoveend);
// Introduce rerendering after map is ready (to include labels),
// but keep the map out of state life cycle.
this.setState({ isMapReady: true });
}
}
handleMobilePinchZoom(e) {
e.preventDefault();
// A hack to prevent pinch zoom gesture in mobile Safari
// Otherwise, pinch zoom would zoom both map and the document.
document.body.style.zoom = 0.99;
}
handleDoubleClickOnInfoCard(e) {
e.stopPropagation();
}
render() {
const {
className,
listings,
activeListingId,
infoCardOpen,
onListingClicked,
onListingInfoCardClicked,
createURLToListing,
mapComponentRefreshToken,
} = this.props;
if (this.map) {
// Create markers out of price labels and grouped labels
const labels = priceLabelsInLocations(
listings,
activeListingId,
infoCardOpen,
onListingClicked,
mapComponentRefreshToken
);
// If map has moved or info card opened, unnecessary markers need to be removed
const removableMarkers = differenceBy(this.currentMarkers, labels, 'markerId');
removableMarkers.forEach(rm => rm.marker.remove());
// Helper function to create markers to given container
const createMarker = (data, markerContainer) =>
new window.mapboxgl.Marker(markerContainer, { anchor: 'bottom' })
.setLngLat([data.location.lng, data.location.lat])
.addTo(this.map);
// SearchMapPriceLabel and SearchMapGroupLabel:
// create a new marker or use existing one if markerId is among previously rendered markers
this.currentMarkers = labels
.filter(v => v != null)
.map(m => {
const existingMarkerId = this.currentMarkers.findIndex(
marker => m.markerId === marker.markerId && marker.marker
);
if (existingMarkerId >= 0) {
const { marker, markerContainer, ...rest } = this.currentMarkers[existingMarkerId];
return { ...rest, ...m, markerContainer, marker };
} else {
const markerContainer = document.createElement('div');
markerContainer.setAttribute('id', m.markerId);
markerContainer.classList.add(css.labelContainer);
const marker = createMarker(m, markerContainer);
return { ...m, markerContainer, marker };
}
});
/* Create marker for SearchMapInfoCard component */
if (infoCardOpen) {
const infoCard = infoCardComponent(
infoCardOpen,
onListingInfoCardClicked,
createURLToListing,
mapComponentRefreshToken
);
// marker container and its styles
const infoCardContainer = document.createElement('div');
infoCardContainer.setAttribute('id', infoCard.markerId);
infoCardContainer.classList.add(css.infoCardContainer);
infoCardContainer.addEventListener('dblclick', this.handleDoubleClickOnInfoCard, false);
this.currentInfoCard = {
...infoCard,
markerContainer: infoCardContainer,
marker: infoCard ? createMarker(infoCard, infoCardContainer) : null,
};
} else {
if (this.currentInfoCard) {
this.currentInfoCard.markerContainer.removeEventListener(
'dblclick',
this.handleDoubleClickOnInfoCard
);
}
this.currentInfoCard = null;
}
}
return (
<div
id="map"
ref={this.onMount}
className={classNames(className, css.fullArea)}
onClick={this.props.onClick}
>
{this.currentMarkers.map(m => {
// Remove existing activeLabel classes and add it only to the correct container
m.markerContainer.classList.remove(css.activeLabel);
if (activeListingId && activeListingId.uuid === m.componentProps.key) {
m.markerContainer.classList.add(css.activeLabel);
}
const isMapReadyForMarkers = this.map && m.markerContainer;
// DOM node that should be used as portal's root
const portalDOMContainer = isMapReadyForMarkers
? document.getElementById(m.markerContainer.id)
: null;
// Create component portals for correct marker containers
if (isMapReadyForMarkers && m.type === 'price') {
return ReactDOM.createPortal(
<SearchMapPriceLabel {...m.componentProps} />,
portalDOMContainer
);
} else if (isMapReadyForMarkers && m.type === 'group') {
return ReactDOM.createPortal(
<SearchMapGroupLabel {...m.componentProps} />,
portalDOMContainer
);
}
return null;
})}
{this.state.mapContainer && this.currentInfoCard
? ReactDOM.createPortal(
<SearchMapInfoCard {...this.currentInfoCard.componentProps} />,
this.currentInfoCard.markerContainer
)
: null}
</div>
);
}
}
SearchMapWithMapbox.defaultProps = {
center: null,
priceLabels: [],
infoCard: null,
zoom: 11,
reusableMapHiddenHandle: null,
};
SearchMapWithMapbox.propTypes = {
center: propTypes.latlng,
location: shape({
search: string.isRequired,
}).isRequired,
priceLabels: arrayOf(node),
infoCard: node,
onClick: func.isRequired,
onMapMoveEnd: func.isRequired,
onMapLoad: func.isRequired,
zoom: number,
reusableMapHiddenHandle: string,
};
export default SearchMapWithMapbox;