-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
141 lines (127 loc) · 4.79 KB
/
index.tsx
File metadata and controls
141 lines (127 loc) · 4.79 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
import * as turf from '@turf/turf'
import { Feature, MultiPolygon, Position } from 'geojson'
import { LatLngExpression, LeafletMouseEvent } from 'leaflet'
import { Polygon as ReactPolygon, Tooltip } from 'react-leaflet'
import { useMemo } from 'react'
import { useDocContext } from '../../../state/DocContext'
import { featureIsVisibleInPeriod } from '../../../helpers/featureIsVisibleAtTime'
import './index.css'
import { mouseOut, mouseOver } from '../commonHandlers'
export interface MultiZoneProps {
feature: Feature<MultiPolygon>
onClickHandler: { (id: string, modifier: boolean): void }
}
const MultiZone: React.FC<MultiZoneProps> = ({ feature, onClickHandler }) => {
const { selection, time } = useDocContext()
const { start: timeStart, end: timeEnd, filterApplied } = time
const isSelected = selection.includes(feature.id as string)
const isVisible = useMemo(() => {
return filterApplied
? featureIsVisibleInPeriod(feature, timeStart, timeEnd)
: true
}, [feature, timeStart, timeEnd, filterApplied])
const color = useMemo(() => {
return feature.properties?.stroke || '#F0F'
}, [feature])
const fill = useMemo(() => {
return feature.properties?.fill || feature.properties?.stroke || '#F0F'
}, [feature])
const lineWeight = useMemo(() => {
return feature.properties?.['stroke-width'] || 2
}, [feature])
const opacity = useMemo(() => {
return feature.properties?.['fill-opacity'] || 0.15
}, [feature])
const polygons = useMemo((): React.ReactElement[] => {
const onclick = (evt: LeafletMouseEvent) => {
onClickHandler(
feature.id as string,
evt.originalEvent.altKey || evt.originalEvent.ctrlKey
)
}
const eventHandlers = {
click: onclick,
mouseover: mouseOver,
mouseout: (evt: LeafletMouseEvent) => mouseOut(evt, isSelected),
}
const polys = feature.geometry.coordinates
return polys.map((poly, index) => {
const points = turf.featureCollection([
turf.polygon(poly),
])
const centre = turf
.center(points)
.geometry.coordinates.reverse() as LatLngExpression
const trackCoords = (poly as unknown as Position[][]).map((line) => line.map((item): LatLngExpression => [item[1], item[0]]))
return <ReactPolygon
key={feature.id + '-polygon-' + isSelected + lineWeight + color + fill + index}
fill={true}
positions={trackCoords}
interactive={false}
weight={lineWeight}
color={color}
fillOpacity={opacity}
eventHandlers={eventHandlers}
>
<Tooltip
className={'zone-label'}
position={centre}
direction='center'
opacity={1}
permanent
>
{feature.properties?.names[index]}
</Tooltip>
</ReactPolygon>
})
}, [feature, isSelected, lineWeight, color, fill, opacity, onClickHandler])
// if (coords.length === 0 || coords[0].length === 0) return null
// const points = turf.featureCollection([
// turf.polygon((feature.geometry as MultiPolygon).coordinates),
// ])
// const centre = turf
// .center(points)
// .geometry.coordinates.reverse() as LatLngExpression
// const polyCoords = (feature.geometry as MultiPolygon).coordinates
// // our coordinates may be a single line (polygon, circle) or multiple lines (shape with a hole).
// const trackCoords = (polyCoords as unknown as Position[][]).map((line) => line.map((item): LatLngExpression => [item[1], item[0]]))
// return (
// <>
// <ReactPolygon
// key={feature.id + '-polygon-' + isSelected + lineWeight + color + fill}
// fill={true}
// positions={trackCoords}
// interactive={false}
// weight={lineWeight}
// color={color}
// fillColor={fill}
// eventHandlers={eventHandlers}
// fillOpacity={ isSelected ? 0.65 : opacity}
// >
// <Tooltip
// className={'zone-label'}
// position={centre}
// direction='center'
// opacity={1}
// permanent
// >
// {feature.properties?.name}
// </Tooltip>
// </ReactPolygon>
// {/* we only allow clicking on the edge, by preventing the polygon from being clicked,
// but allowing this polyline to be clicked */}
// <Polyline
// key={feature.id + '-edge-' + isSelected}
// positions={trackCoords}
// interactive={true}
// weight={lineWeight}
// color={color}
// eventHandlers={eventHandlers}
// >
// </Polyline>
// </>
// )
// }, [feature, isSelected, lineWeight, onClickHandler, opacity, color, fill])
return <>{isVisible && polygons}</>
}
export default MultiZone