-
Notifications
You must be signed in to change notification settings - Fork 58
/
LayerTree.tsx
353 lines (288 loc) · 9.98 KB
/
LayerTree.tsx
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
import './LayerTree.less';
import Logger from '@terrestris/base-util/dist/Logger';
import MapUtil from '@terrestris/ol-util/dist/MapUtil/MapUtil';
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import {
Tree,
TreeDataNode
} from 'antd';
import {
BasicDataNode,
DataNode,
TreeProps
} from 'antd/lib/tree';
import _isFunction from 'lodash/isFunction';
import { getUid } from 'ol';
import { EventsKey as OlEventsKey } from 'ol/events';
import OlLayerBase from 'ol/layer/Base';
import OlLayerGroup from 'ol/layer/Group';
import { unByKey } from 'ol/Observable';
import { NodeDragEventParams } from 'rc-tree/lib/contextTypes';
import { EventDataNode } from 'rc-tree/lib/interface';
import { AllowDropOptions, CheckInfo } from 'rc-tree/lib/Tree';
import React, { Key, useCallback, useEffect, useState } from 'react';
import { CSS_PREFIX } from '../constants';
interface OwnProps {
/**
* An optional array-filter function that is applied to every layer and
* subLayer. Return false to exclude this layer from the layerTree or true
* to include it.
*
* Compare MDN Docs for Array.prototype.filter: https://mdn.io/array/filter
*/
filterFunction?: (value: OlLayerBase, index: number, array: OlLayerBase[]) => boolean;
/**
* A LayerGroup the Tree should handle.
*/
layerGroup?: OlLayerGroup;
/**
* A function that can be used to pass a custom node title. It can return
* any renderable element (String, Number, Element etc.) and receives
* the layer instance of the current tree node.
*/
nodeTitleRenderer?: (layer: OlLayerBase) => React.ReactNode;
/**
* Callback function that will be called if the selection changes.
*/
onLayerVisibilityChanged?: (layer: OlLayerBase, checked: boolean) => void;
}
const defaultClassName = `${CSS_PREFIX}layertree`;
export type LayerTreeProps<T extends BasicDataNode = DataNode> = OwnProps & TreeProps<T>;
const LayerTree: React.FC<LayerTreeProps> = ({
className,
layerGroup,
nodeTitleRenderer,
filterFunction,
onLayerVisibilityChanged,
checkable = true,
draggable = true,
...passThroughProps
}) => {
const [checkedKeys, setCheckedKeys] = useState<React.Key[]>([]);
const [treeData, setTreeData] = useState<TreeDataNode[]>([]);
const map = useMap();
const getVisibleLayerKeys = useCallback(() => {
if (!map) {
return [];
}
const lGroup = layerGroup ? MapUtil.getAllLayers(layerGroup) : MapUtil.getAllLayers(map);
let visibleLayers = lGroup
.filter(layer => !(layer instanceof OlLayerGroup) && layer.getVisible());
if (filterFunction) {
visibleLayers = visibleLayers.filter(filterFunction);
}
return visibleLayers.map(getUid);
}, [filterFunction, layerGroup, map]);
const getTreeNodeTitle = useCallback((layer: OlLayerBase) => {
if (_isFunction(nodeTitleRenderer)) {
return nodeTitleRenderer(layer);
} else {
return layer.get('name');
}
}, [nodeTitleRenderer]);
const treeNodeFromLayer = useCallback((layer: OlLayerBase) => {
if (!map) {
return;
}
let childNodes: TreeDataNode[]|undefined = undefined;
if (filterFunction && [layer].filter(filterFunction).length === 0) {
return;
}
if (layer instanceof OlLayerGroup) {
let childLayers = layer.getLayers().getArray();
if (filterFunction) {
childLayers = childLayers.filter(filterFunction);
}
childNodes = childLayers
.map(childLayer => treeNodeFromLayer(childLayer))
.filter(childLayer => childLayer !== undefined)
.toReversed() as TreeDataNode[];
}
return {
key: getUid(layer),
title: <div
draggable={true}
onClick={e => e.stopPropagation()}
onDragStart={e => {e.stopPropagation(); e.preventDefault();}}
>
{getTreeNodeTitle(layer)}
</div>,
className: MapUtil.layerInResolutionRange(layer, map) ? '' : 'out-of-range',
// Required to identify a group layer/node.
children: childNodes
} as TreeDataNode;
}, [map, getTreeNodeTitle, filterFunction]);
const treeNodesFromLayerGroup = useCallback(() => {
if (!map) {
return [];
}
const lGroup = layerGroup ? layerGroup : map.getLayerGroup();
return lGroup.getLayers().getArray()
.map(l => treeNodeFromLayer(l))
.filter(n => n !== undefined)
.toReversed() as TreeDataNode[];
}, [layerGroup, map, treeNodeFromLayer]);
const onChangeResolution = useCallback(() => {
setTreeData(treeNodesFromLayerGroup());
}, [treeNodesFromLayerGroup]);
const setLayerVisibility = useCallback((layer: OlLayerBase, visible: boolean) => {
if (layer instanceof OlLayerGroup) {
layer.getLayers().forEach(subLayer => {
setLayerVisibility(subLayer, visible);
});
} else {
layer.setVisible(visible);
}
}, []);
const updateCheckedKeys = useCallback(() => {
setCheckedKeys(getVisibleLayerKeys());
}, [getVisibleLayerKeys]);
const updateTree = useCallback(() => {
setTreeData(treeNodesFromLayerGroup());
updateCheckedKeys();
}, [updateCheckedKeys, treeNodesFromLayerGroup]);
useEffect(() => {
updateTree();
}, [updateTree]);
const registerLayerListeners = useCallback((layer: OlLayerBase): OlEventsKey[] => {
if (filterFunction && [layer].filter(filterFunction).length === 0) {
return [];
}
const keys: OlEventsKey[] = [];
keys.push(layer.on('propertychange', updateTree));
if (layer instanceof OlLayerGroup) {
const layerCollection = layer.getLayers();
keys.push(layerCollection.on('add', updateTree));
keys.push(layerCollection.on('remove', updateTree));
keys.push(layer.on('change:layers', updateTree));
for (const lay of layerCollection.getArray()) {
keys.push(...registerLayerListeners(lay));
}
} else {
keys.push(layer.on('change:visible', updateCheckedKeys));
}
return keys;
}, [filterFunction, updateTree, updateCheckedKeys]);
const registerAllLayerListeners = useCallback((): OlEventsKey[] => {
if (!map) {
return [];
}
const lGroup = layerGroup ? layerGroup : map.getLayerGroup();
return registerLayerListeners(lGroup);
}, [layerGroup, map, registerLayerListeners]);
// Reregister all layer listeners if the treeData changes, this is e.g. required if a layer becomes
// a child of a layer group that isn't part of the treeData yet.
useEffect(() => {
const keys = registerAllLayerListeners();
return () => {
unByKey(keys);
};
}, [treeData, registerAllLayerListeners]);
useEffect(() => {
if (!map) {
return;
}
const key = map.getView().on('change:resolution', onChangeResolution);
return () => {
unByKey(key);
};
}, [map, onChangeResolution]);
const onCheck = useCallback((_: any, info: CheckInfo<TreeDataNode>) => {
if (!map) {
return;
}
const key = info.node.key as string;
const checked = info.checked;
if (!key) {
return;
}
const layer = MapUtil.getLayerByOlUid(map, key);
if (!layer) {
Logger.error('Layer is not defined');
return;
}
setLayerVisibility(layer, checked);
if (_isFunction(onLayerVisibilityChanged)) {
onLayerVisibilityChanged(layer, checked);
}
}, [map, setLayerVisibility, onLayerVisibilityChanged]);
const onDrop = useCallback((info: NodeDragEventParams<DataNode> & {
dragNode: EventDataNode<DataNode>;
dragNodesKeys: Key[];
dropPosition: number;
dropToGap: boolean;
}) => {
const dropKey = info.node.key as string;
const dragKey = info.dragNode.key as string;
const dropPos = info.node.pos.split('-');
const dropPosition = info.dropPosition;
// The drop position relative to the drop node, inside 0, top -1, bottom 1.
const dropPositionRelative = dropPosition - parseInt(dropPos[dropPos.length - 1], 10);
// Reorder layers
if (!map) {
return;
}
const dragLayer = MapUtil.getLayerByOlUid(map, dragKey);
if (!dragLayer) {
Logger.error('dragLayer is not defined');
return;
}
const dropLayer = MapUtil.getLayerByOlUid(map, dropKey);
if (!dropLayer) {
Logger.error('dropLayer is not defined');
return;
}
const dragInfo = MapUtil.getLayerPositionInfo(dragLayer, map);
if (!dragInfo || !dragInfo?.groupLayer) {
return;
}
const dropInfo = MapUtil.getLayerPositionInfo(dropLayer, map);
if (!dropInfo || !dropInfo?.groupLayer) {
return;
}
const dragCollection = dragInfo.groupLayer.getLayers();
const dropCollection = dropInfo.groupLayer.getLayers();
dragCollection.remove(dragLayer);
const dropLayerIndex = dropCollection.getArray().findIndex(l => l === dropLayer);
// Drop on the top of the drop node/layer.
if (dropPositionRelative === -1) {
if (dropPosition === dropCollection.getLength() - 1) {
dropCollection.push(dragLayer);
} else {
dropCollection.insertAt(dropLayerIndex + 1, dragLayer);
}
// Drop on node (= to a layer group).
} else if (dropPositionRelative === 0) {
if (dropLayer instanceof OlLayerGroup) {
dropLayer.getLayers().push(dragLayer);
}
// Drop on the bottom of the drop node/layer.
} else if (dropPositionRelative === 1) {
dropCollection.insertAt(dropLayerIndex, dragLayer);
}
}, [map]);
const allowDrop = useCallback((options: AllowDropOptions<TreeDataNode>) => {
const dropNode = options.dropNode;
const dropPositionRelative = options.dropPosition;
// Don't allow dropping on a layer node.
return !(dropPositionRelative === 0 && !dropNode.children);
}, []);
const finalClassName = className
? `${className} ${defaultClassName}`
: defaultClassName;
return (
<Tree
className={finalClassName}
checkedKeys={checkedKeys}
onCheck={onCheck}
onDrop={onDrop}
allowDrop={allowDrop}
selectable={false}
checkable={checkable}
draggable={draggable}
treeData={treeData}
{...passThroughProps}
/>
);
};
export default LayerTree;