-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathLayerSwitcher.tsx
202 lines (169 loc) · 4.93 KB
/
LayerSwitcher.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
import './LayerSwitcher.less';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import OlLayerBase from 'ol/layer/Base';
import OlLayerGroup from 'ol/layer/Group';
import OlLayerTile from 'ol/layer/Tile';
import OlMap from 'ol/Map';
import OlTileSource from 'ol/source/Tile';
import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap';
import { CSS_PREFIX } from '../constants';
import MapComponent from '../Map/MapComponent/MapComponent';
/**
* @export
* @interface LayerSwitcherProps
* @extends {React.HTMLAttributes<HTMLDivElement>}
*/
export interface OwnProps {
/**
* An optional CSS class which will be added to the wrapping div Element.
*/
className?: string;
/**
* The layers to be available in the switcher.
*/
layers: OlLayerBase[];
/**
* The property that identifies the layer.
*/
identifierProperty?: string;
/**
* The property that labels the layer.
*/
labelProperty?: string;
}
export type LayerSwitcherProps = OwnProps & React.HTMLAttributes<HTMLDivElement>;
/**
* A basic component to switch between the passed layers.
* This is most likely to be used for the backgroundlayer.
*/
export const LayerSwitcher: React.FC<LayerSwitcherProps> = ({
identifierProperty = 'name',
labelProperty = 'name',
layers,
className: classNameProp,
...passThroughProps
}) => {
const map = useMap();
const [switcherMap, setSwitcherMap] = useState<OlMap>();
/**
* The internal index of visible layer in provided layers array. If all passed
* layers are initially invisible, the first layer in array will be taken as
* default.
*/
const visibleLayerIndexRef = useRef<number>(0);
const [previewLayer, setPreviewLayer] = useState<OlLayerBase>();
const className = `${CSS_PREFIX}layer-switcher`;
/**
* Sets the visibility of the layers in the map and the switcherMap.
* Also sets the previewLayer in the state.
*/
const updateLayerVisibility = useCallback(() => {
layers.forEach((layer, i) => {
layer.setVisible(visibleLayerIndexRef.current === i);
const clone = switcherMap?.getAllLayers()
?.find(lc => lc.get(identifierProperty) === layer.get(identifierProperty));
if (!clone) {
return;
}
if ((visibleLayerIndexRef.current + 1) % layers.length === i) {
clone.setVisible(true);
setPreviewLayer(clone);
} else {
clone.setVisible(false);
}
});
}, [layers, identifierProperty, switcherMap]);
const cloneLayer = useCallback((layer: OlLayerBase): OlLayerBase => {
if (layer instanceof OlLayerGroup) {
return new OlLayerGroup({
layers: layer.getLayers().getArray().map(l => {
if (!(l instanceof OlLayerTile) || !(l instanceof OlLayerGroup)) {
throw new Error('Layer of layergroup is of unclonable type');
}
return cloneLayer(l);
}),
properties: {
originalLayer: layer
},
...layer.getProperties()
});
} else {
const clone = new OlLayerTile({
source: (layer as OlLayerTile<OlTileSource>).getSource() || undefined,
properties: {
originalLayer: layer
},
...layer.getProperties()
});
// reset reference to the map instance of original layer
clone.setMap(null);
return clone;
}
}, []);
useEffect(() => {
return () => {
if (switcherMap) {
switcherMap.getLayers().clear();
switcherMap.setTarget(undefined);
setSwitcherMap(undefined);
}
};
}, [switcherMap]);
useEffect(() => {
if (!map) {
return;
}
const mapClone = new OlMap({
view: map.getView(),
controls: []
});
setSwitcherMap(mapClone);
}, [map]);
useEffect(() => {
if (switcherMap) {
switcherMap.getLayers().clear();
layers
.map(cloneLayer)
.forEach(layer => switcherMap.addLayer(layer));
}
updateLayerVisibility();
}, [switcherMap, cloneLayer, layers, updateLayerVisibility]);
const onSwitcherClick = (evt: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
evt.stopPropagation();
const index = layers.findIndex(layer => layer.getVisible());
visibleLayerIndexRef.current = (index + 1) % layers.length;
updateLayerVisibility();
};
const finalClassName = classNameProp
? `${className} ${classNameProp}`
: className;
if (!switcherMap) {
return null;
}
return (
<div
className={finalClassName}
role="menu"
{...passThroughProps}
>
<div
className="clip"
onClick={onSwitcherClick}
role="button"
>
<MapComponent
mapDivId="layer-switcher-map"
map={switcherMap}
role="presentation"
/>
{
previewLayer &&
<span className="layer-title">
{previewLayer?.get(labelProperty)}
</span>
}
</div>
</div>
);
};
export default LayerSwitcher;