-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
284 lines (255 loc) · 7.9 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
import {GeoSearchControl} from 'leaflet-geosearch';
import isEqual from 'lodash/isEqual';
import PropTypes from 'prop-types';
import React, {useCallback, useContext, useEffect} from 'react';
import {defineMessages, useIntl} from 'react-intl';
import {MapContainer, Marker, TileLayer, useMap, useMapEvent} from 'react-leaflet';
import {useGeolocation} from 'react-use';
import {ConfigContext} from 'Context';
import {CRS_RD, DEFAULT_LAT_LNG, DEFAULT_ZOOM, TILE_LAYER_RD} from 'map/constants';
import {getBEMClassName} from 'utils';
import NearestAddress from './NearestAddress';
import OpenFormsProvider from './provider';
const searchControlMessages = defineMessages({
buttonLabel: {
description: "The leaflet map's search button areaLabel text.",
defaultMessage: 'Map component search button',
},
searchLabel: {
description: "The leaflet map's input fields placeholder message.",
defaultMessage: 'Enter address, please',
},
notFound: {
description: "The leaflet map's location not found message.",
defaultMessage: 'Sorry, that address could not be found.',
},
});
const leafletGestureHandlingText = defineMessages({
touch: {
description: 'Gesturehandeling phone touch message.',
defaultMessage: 'Use two fingers to move the map',
},
scroll: {
description: 'Gesturehandeling pc scroll message.',
defaultMessage: 'Use ctrl + scroll to zoom the map',
},
scrollMac: {
description: 'Gesturehandeling mac scroll message.',
defaultMessage: 'Use \u2318 + scroll to zoom the map',
},
});
const useDefaultCoordinates = () => {
// FIXME: can't call hooks conditionally
const {loading, latitude, longitude, error} = useGeolocation();
// it's possible the user declined permissions (error.code === 1) to access the
// location, or the location could not be determined. In that case, fall back to the
// hardcoded default. See Github issue
// https://github.com/open-formulieren/open-forms/issues/864 and the docs on
// GeolocationPositionError:
// https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError
if (error) {
return null;
}
if (!navigator.geolocation) return null;
if (loading) return null;
return [latitude, longitude];
};
const LeaftletMap = ({
markerCoordinates,
onMarkerSet,
defaultCenter = DEFAULT_LAT_LNG,
defaultZoomLevel = DEFAULT_ZOOM,
disabled = false,
}) => {
const intl = useIntl();
const defaultCoordinates = useDefaultCoordinates();
const coordinates = markerCoordinates || defaultCoordinates;
const onWrapperMarkerSet = coordinates => {
const coordinatesChanged = !isEqual(markerCoordinates, coordinates);
if (!coordinatesChanged) return;
onMarkerSet(coordinates);
};
const modifiers = disabled ? ['disabled'] : [];
const className = getBEMClassName('leaflet-map', modifiers);
return (
<>
<MapContainer
center={defaultCenter}
zoom={defaultZoomLevel}
crs={CRS_RD}
attributionControl
className={className}
searchControl
gestureHandling
gestureHandlingOptions={{
text: {
touch: intl.formatMessage(leafletGestureHandlingText.touch),
scroll: intl.formatMessage(leafletGestureHandlingText.scroll),
scrollMac: intl.formatMessage(leafletGestureHandlingText.scrollMac),
},
duration: 3000,
}}
>
<TileLayer {...TILE_LAYER_RD} />
{coordinates ? (
<>
<MapView coordinates={coordinates} />
<MarkerWrapper position={coordinates} onMarkerSet={onWrapperMarkerSet} />
</>
) : null}
<SearchControl
onMarkerSet={onMarkerSet}
options={{
showMarker: false,
showPopup: false,
retainZoomLevel: false,
animateZoom: true,
autoClose: false,
searchLabel: intl.formatMessage(searchControlMessages.searchLabel),
keepResult: true,
updateMap: true,
notFoundMessage: intl.formatMessage(searchControlMessages.notFound),
}}
/>
{disabled ? <DisabledMapControls /> : <CaptureClick setMarker={onMarkerSet} />}
</MapContainer>
{markerCoordinates && markerCoordinates.length && (
<NearestAddress coordinates={markerCoordinates} />
)}
</>
);
};
LeaftletMap.propTypes = {
markerCoordinates: PropTypes.arrayOf(PropTypes.number),
onMarkerSet: PropTypes.func,
disabled: PropTypes.bool,
};
// Set the map view if coordinates are provided
const MapView = ({coordinates = null}) => {
const map = useMap();
useEffect(() => {
if (!coordinates || coordinates.length !== 2) return;
if (!coordinates.filter(value => isFinite(value)).length === 2) return;
map.setView(coordinates);
}, [map, coordinates]);
// rendering is done by leaflet, so just return null
return null;
};
MapView.propTypes = {
coordinates: PropTypes.arrayOf(PropTypes.number),
};
const SearchControl = ({onMarkerSet, options}) => {
const {baseUrl} = useContext(ConfigContext);
const map = useMap();
const intl = useIntl();
const {
showMarker,
showPopup,
retainZoomLevel,
animateZoom,
autoClose,
searchLabel,
keepResult,
updateMap,
notFoundMessage,
} = options;
const buttonLabel = intl.formatMessage(searchControlMessages.buttonLabel);
const setMarker = useCallback(
result => {
if (result.location) {
onMarkerSet([result.location.y, result.location.x]);
}
},
[onMarkerSet]
);
useEffect(() => {
const provider = new OpenFormsProvider(baseUrl);
const searchControl = new GeoSearchControl({
provider: provider,
style: 'button',
showMarker,
showPopup,
retainZoomLevel,
animateZoom,
autoClose,
searchLabel,
keepResult,
updateMap,
notFoundMessage,
});
searchControl.button.setAttribute('aria-label', buttonLabel);
map.addControl(searchControl);
map.on('geosearch/showlocation', setMarker);
return () => {
map.off('geosearch/showlocation', setMarker);
map.removeControl(searchControl);
};
}, [
map,
setMarker,
baseUrl,
showMarker,
showPopup,
retainZoomLevel,
animateZoom,
autoClose,
searchLabel,
keepResult,
updateMap,
notFoundMessage,
buttonLabel,
]);
return null;
};
SearchControl.propTypes = {
onMarkerSet: PropTypes.func.isRequired,
options: PropTypes.shape({
showMarker: PropTypes.bool.isRequired,
showPopup: PropTypes.bool.isRequired,
retainZoomLevel: PropTypes.bool.isRequired,
animateZoom: PropTypes.bool.isRequired,
autoClose: PropTypes.bool.isRequired,
searchLabel: PropTypes.string.isRequired,
keepResult: PropTypes.bool.isRequired,
updateMap: PropTypes.bool.isRequired,
notFoundMessage: PropTypes.string.isRequired,
}),
};
const MarkerWrapper = ({position, onMarkerSet, ...props}) => {
const shouldSetMarker = !!(position && position.length === 2);
useEffect(() => {
if (!shouldSetMarker) return;
if (!onMarkerSet) return;
onMarkerSet(position);
});
// only render a marker if we explicitly have a marker
return shouldSetMarker ? <Marker position={position} {...props} /> : null;
};
MarkerWrapper.propTypes = {
position: PropTypes.arrayOf(PropTypes.number),
onMarkerSet: PropTypes.func,
};
const DisabledMapControls = () => {
const map = useMap();
useEffect(() => {
map.dragging.disable();
map.touchZoom.disable();
map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
map.boxZoom.disable();
map.keyboard.disable();
if (map.tap) map.tap.disable();
}, [map]);
return null;
};
const CaptureClick = ({setMarker}) => {
useMapEvent('click', event => {
const newLatLng = [event.latlng.lat, event.latlng.lng];
setMarker(newLatLng);
});
return null;
};
CaptureClick.propTypes = {
setMarker: PropTypes.func.isRequired,
};
export default LeaftletMap;