-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLayerToggle.jsx
More file actions
438 lines (403 loc) · 13.5 KB
/
Copy pathLayerToggle.jsx
File metadata and controls
438 lines (403 loc) · 13.5 KB
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
import {
DndContext,
KeyboardSensor,
PointerSensor,
closestCenter,
useSensor,
useSensors,
} from "@dnd-kit/core";
import {
SortableContext,
arrayMove,
sortableKeyboardCoordinates,
useSortable,
verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import classNames from "classnames";
import _filter from "lodash/filter";
import _isEmpty from "lodash/isEmpty";
import _map from "lodash/map";
import _sortBy from "lodash/sortBy";
import PropTypes from "prop-types";
import { Component, Fragment } from "react";
import { FormattedMessage } from "react-intl";
import AsEndUser from "../../../interactions/User/AsEndUser";
import { DEFAULT_OVERLAY_ORDER } from "../../../services/VisibleLayer/LayerSources";
import Dropdown from "../../Dropdown/Dropdown";
import WithLayerSources from "../../HOCs/WithLayerSources/WithLayerSources";
import WithVisibleLayer from "../../HOCs/WithVisibleLayer/WithVisibleLayer";
import SvgSymbol from "../../SvgSymbol/SvgSymbol";
import messages from "./Messages";
// SortableItem component for draggable overlays
const SortableOverlayItem = ({ overlay, canReorderLayers }) => {
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({
id: overlay.id,
});
const style = {
transform: CSS.Transform.toString(transform),
transition,
};
return (
<div ref={setNodeRef} style={style}>
<div className="mr-relative mr-my-2 mr-flex mr-justify-between">
<div>{overlay.component}</div>
{canReorderLayers && (
<div {...attributes} {...listeners}>
<SvgSymbol
sym="reorder-icon"
className="mr-mt-6px mr-w-6 mr-h-6 mr-fill-green-light"
viewBox="0 0 96 96"
/>
</div>
)}
</div>
</div>
);
};
/**
* LayerToggle presents a control for selecting the desired map layer/tiles.
* The required `changeLayer` prop function will be invoked with the new layer
* name whenever the user selects a new layer.
*
* @author [Neil Rotstan](https://github.com/nrotstan)
*/
export class LayerToggle extends Component {
overlayVisible = (layerId) => {
const visibleOverlays = this.props.visibleOverlays || [];
return visibleOverlays.indexOf(layerId) !== -1;
};
toggleOverlay = (layerId) => {
this.overlayVisible(layerId)
? this.props.removeVisibleOverlay(layerId)
: this.props.addVisibleOverlay(layerId);
};
orderedOverlays = () => {
let overlays = overlayToggles({
...this.props,
overlayVisible: this.overlayVisible,
toggleOverlay: this.toggleOverlay,
});
const overlayOrder = _isEmpty(this.props.overlayOrder)
? DEFAULT_OVERLAY_ORDER
: this.props.overlayOrder;
if (overlayOrder && overlayOrder.length > 0) {
overlays = _sortBy(overlays, (layer) => {
const position = overlayOrder.indexOf(layer.id);
return position === -1 ? Number.MAX_SAFE_INTEGER : position;
});
}
return overlays;
};
handleDragEnd = (event) => {
if (!this.props.user || !this.props.updateUserAppSetting) {
return;
}
const { active, over } = event;
// dropped outside the list or on itself
if (!over || active.id === over.id) {
return;
}
const overlays = this.orderedOverlays();
// Find indexes for the source and destination
let sourceIndex = -1;
let destinationIndex = -1;
overlays.forEach((layer, index) => {
if (layer.id === active.id) sourceIndex = index;
if (layer.id === over.id) destinationIndex = index;
});
if (sourceIndex !== -1 && destinationIndex !== -1) {
const newOverlays = arrayMove(overlays, sourceIndex, destinationIndex);
this.props.updateUserAppSetting(this.props.user.id, {
mapOverlayOrder: _map(newOverlays, "id"),
});
}
};
render() {
const baseSources = _filter(this.props.layerSources, (source) => !source.overlay);
const layerListItems = _map(baseSources, (layer) => (
<li key={layer.id}>
<button
className={
this.props.source.id === layer.id
? "mr-text-current"
: "mr-text-green-lighter hover:mr-text-current"
}
onClick={() => this.props.changeLayer(layer.id, this.props.mapType)}
>
{layer.name}
</button>
</li>
));
const overlays = this.orderedOverlays();
const canReorderLayers =
AsEndUser(this.props.user).isLoggedIn() && this.props.updateUserAppSetting;
// Wrap DndContext in a function component since hooks can't be used in class components
const DraggableOverlayList = () => {
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
}),
);
return (
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragEnd={this.handleDragEnd}
>
<SortableContext
items={overlays.map((overlay) => overlay.id)}
strategy={verticalListSortingStrategy}
>
{_map(overlays, (overlay) => (
<SortableOverlayItem
key={overlay.id}
overlay={overlay}
canReorderLayers={canReorderLayers}
/>
))}
</SortableContext>
</DndContext>
);
};
return (
<Dropdown
className="mr-dropdown--right mr-absolute mr-z-10 mr-right-0 mr-top-0 mr-mr-2 mr-mt-2"
dropdownButton={(dropdown) => (
<button
onClick={dropdown.toggleDropdownVisible}
className="mr-leading-none mr-p-2 mr-bg-black-50 mr-text-white mr-w-8 mr-h-8 mr-flex mr-items-center mr-shadow mr-rounded-sm mr-transition-normal-in-out-quad hover:mr-text-green-lighter"
aria-haspopup="true"
aria-controls="dropdown-menu"
>
<SvgSymbol
sym="layers-icon"
className="mr-w-4 mr-h-4 mr-fill-current"
viewBox="0 0 20 20"
/>
</button>
)}
dropdownContent={() => (
<Fragment>
{layerListItems.length > 0 && <ol className="mr-o-2">{layerListItems}</ol>}
{layerListItems.length > 0 && overlays.length > 0 && (
<hr className="mr-h-px mr-my-4 mr-bg-white-15" />
)}
<div>
<DraggableOverlayList />
</div>
</Fragment>
)}
/>
);
}
}
LayerToggle.propTypes = {
/** Array of layer sources to present as options */
layerSources: PropTypes.array.isRequired,
/** Current active layer source */
source: PropTypes.object,
/** Invoked when the user chooses a new layer source */
changeLayer: PropTypes.func.isRequired,
/** Array of overlay layers currently visible */
visibleOverlays: PropTypes.array,
/** Invoked to add an overlay layer to the visible overlays */
addVisibleOverlay: PropTypes.func.isRequired,
/** Invoked to remove an overlay layer from the visible overlays */
removeVisibleOverlay: PropTypes.func.isRequired,
/** Set to true if task features are shown on the map */
showTaskFeatures: PropTypes.bool,
/** Invoked when the user toggles visibility of task features */
toggleTaskFeatures: PropTypes.func,
/** Set to true if Mapillary layer is to be shown on the map */
showMapillary: PropTypes.bool,
/** Set to the number of Mapillary markers available in layer */
mapillaryCount: PropTypes.number,
/** Invoked when the user toggles visibility of Mapillary layer */
toggleMapillary: PropTypes.func,
};
const overlayToggles = (props) => {
const toggles = _map(props.intersectingOverlays, (layer) => ({
id: layer.id,
label: layer.name,
component: (
<SimpleLayerToggle
key={layer.id}
toggleLayerActive={() => props.toggleOverlay(layer.id)}
isLayerActive={props.overlayVisible(layer.id)}
layerLabel={layer.name}
/>
),
}));
if (props.challenge) {
const { highPriorityBounds, mediumPriorityBounds, lowPriorityBounds } = props.challenge;
if (
props.togglePriorityBounds &&
(props.priorityBounds?.length > 0 ||
highPriorityBounds?.length > 0 ||
mediumPriorityBounds?.length > 0 ||
lowPriorityBounds?.length > 0)
) {
toggles.push({
id: "priority-bounds",
label: <FormattedMessage {...messages.showPriorityBoundsLabel} />,
component: (
<SimpleLayerToggle
key="priority-bounds"
toggleLayerActive={props.togglePriorityBounds}
isLayerActive={props.showPriorityBounds}
layerLabel={<FormattedMessage {...messages.showPriorityBoundsLabel} />}
/>
),
});
}
}
if (props.toggleTaskFeatures) {
toggles.push({
id: "task-features",
label: <FormattedMessage {...messages.showTaskFeaturesLabel} />,
component: (
<SimpleLayerToggle
key="task-features"
toggleLayerActive={props.toggleTaskFeatures}
isLayerActive={props.showTaskFeatures}
layerLabel={<FormattedMessage {...messages.showTaskFeaturesLabel} />}
/>
),
});
}
if (props.toggleOSMData && (window.env?.REACT_APP_OSM_DATA_OVERLAY ?? "enabled") !== "disabled") {
toggles.push({
id: "osm-data",
label: <FormattedMessage {...messages.showOSMDataLabel} />,
component: <OSMDataLayerToggle key="osm-data" {...props} />,
});
}
if (props.toggleMapillary) {
toggles.push({
id: "mapillary",
label: <FormattedMessage {...messages.showMapillaryLabel} />,
component: <MapillaryLayerToggle key="mapillary" {...props} />,
});
}
if (props.toggleOpenStreetCam) {
toggles.push({
id: "openstreetcam",
label: <FormattedMessage {...messages.showOpenStreetCamLabel} />,
component: <OpenStreetCamLayerToggle key="openstreetcam" {...props} />,
});
}
return toggles;
};
const SimpleLayerToggle = (props) => {
return (
<div
className={classNames(
"mr-my-2 mr-flex mr-items-center mr-leading-none",
props.toggleClassName,
)}
>
<label className="mr-text-orange mr-cursor-pointer">
<input
type="checkbox"
className="mr-checkbox-toggle mr-mr-3"
checked={props.isLayerActive}
onChange={props.toggleLayerActive}
/>
{props.layerLabel}
</label>
</div>
);
};
const OSMDataLayerToggle = (props) => {
return (
<Fragment>
<SimpleLayerToggle
toggleLayerActive={props.toggleOSMData}
isLayerActive={props.showOSMData}
layerLabel={
<Fragment>
<FormattedMessage {...messages.showOSMDataLabel} />{" "}
{props.osmDataLoading && <FormattedMessage {...messages.loading} />}
</Fragment>
}
/>
{props.showOSMData && !props.osmDataLoading && props.toggleOSMElements && (
<Fragment>
{["nodes", "ways", "areas"].map((element) => (
<SimpleLayerToggle
key={`osm-element-toggle-${element}`}
toggleClassName="mr-ml-4"
toggleLayerActive={() => props.toggleOSMElements(element)}
isLayerActive={props.showOSMElements[element]}
layerLabel={element}
/>
))}
</Fragment>
)}
</Fragment>
);
};
const MapillaryLayerToggle = (props) => {
return (
<SimpleLayerToggle
toggleLayerActive={() => props.toggleMapillary()}
isLayerActive={props.showMapillary || false}
layerLabel={
<Fragment>
<FormattedMessage {...messages.showMapillaryLabel} />{" "}
{props.showMapillary && !props.mapillaryLoading && (
<FormattedMessage {...messages.imageCount} values={{ count: props.mapillaryCount }} />
)}{" "}
{props.mapillaryLoading && <FormattedMessage {...messages.loading} />}{" "}
{props.showMapillary && props.hasMoreMapillaryImagery && !props.mapillaryLoading && (
<button
className="mr-button mr-button--xsmall mr-ml-2"
onClick={(e) => {
e.stopPropagation();
props.fetchMoreMapillaryImagery();
}}
>
<FormattedMessage {...messages.moreLabel} />
</button>
)}
</Fragment>
}
/>
);
};
const OpenStreetCamLayerToggle = (props) => {
return (
<SimpleLayerToggle
toggleLayerActive={() => props.toggleOpenStreetCam()}
isLayerActive={props.showOpenStreetCam || false}
layerLabel={
<Fragment>
<FormattedMessage {...messages.showOpenStreetCamLabel} />{" "}
{props.showOpenStreetCam && !props.openStreetCamLoading && (
<FormattedMessage
{...messages.imageCount}
values={{ count: props.openStreetCamCount }}
/>
)}{" "}
{props.openStreetCamLoading && <FormattedMessage {...messages.loading} />}{" "}
{props.showOpenStreetCam &&
props.hasMoreOpenStreetCamImagery &&
!props.openStreetCamLoading && (
<button
className="mr-button mr-button--xsmall mr-ml-2"
onClick={(e) => {
e.stopPropagation();
props.fetchMoreOpenStreetCamImagery();
}}
>
<FormattedMessage {...messages.moreLabel} />
</button>
)}
</Fragment>
}
/>
);
};
export default WithVisibleLayer(WithLayerSources(LayerToggle));