-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDataBuilder.ts
More file actions
264 lines (243 loc) · 9.09 KB
/
TreeDataBuilder.ts
File metadata and controls
264 lines (243 loc) · 9.09 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
import { Feature } from 'geojson'
import { TreeDataNode } from 'antd'
import { symbolOptions } from '../../helpers/symbolTypes'
import { EnvOptions, FeatureTypes } from '../../types'
import { featureIsVisibleInPeriod } from '../../helpers/featureIsVisibleAtTime'
import { BUOY_FIELD_TYPE, ZONE_TYPE, REFERENCE_POINT_TYPE, BACKDROP_TYPE } from '../../constants'
// Import node constants from the constants file
import {
NODE_TRACKS,
NODE_FIELDS,
NODE_ZONES,
NODE_POINTS,
NODE_BACKDROPS
} from './constants'
type FieldDataNode = {
title: string
key: string
children: FieldDataNode[]
}
// Import React for type definitions
import React from 'react'
// Using React.MouseEvent for the event type
export type HandleAddFunction = (e: React.MouseEvent, key: string, title: string) => void
// Interface for icon creators to be passed from Layers component
export interface IconCreators {
createFolderIcon: () => React.ReactNode
createFeatureIcon: (dataType: string, color?: string, environment?: string) => React.ReactNode
createAddIcon: (key: string, title: string, handleAdd: HandleAddFunction) => React.ReactNode
createTitleElement: (title: string) => React.ReactNode
}
export class TreeDataBuilder {
/**
* Get the name for a feature
* @param feature The feature to get the name for
* @returns The name of the feature
*/
static nameFor(feature: Feature): string {
return (feature.properties?.name || feature.id)
}
/**
* Get the ID for a feature
* @param feature The feature to get the ID for
* @returns The ID of the feature
*/
static idFor(feature: Feature): string {
return `${feature.id || 'unknown'}`
}
/**
* Find children of a specific type
* @param features The features to search
* @param dType The data type to filter by
* @returns An array of FieldDataNode objects
*/
static findChildrenOfType(features: Feature[], dType: string): FieldDataNode[] {
const items = features.filter(
(feature) => feature.properties?.dataType === dType
)
return items.map((item) => ({
title: this.nameFor(item),
key: this.idFor(item),
children: [],
}))
}
/**
* Get the label for an add icon
* @param key The node key
* @param title The node title
* @returns The label for the add icon
*/
// Map of node types to their add icon labels
private static readonly nodeLabels = [
{
key: NODE_TRACKS,
getLabel: (title: string) => {
// special case - get the name for the env
const env = title as EnvOptions
return `Create new ${symbolOptions.find(e => e.value === env)?.label} track`
}
},
{ key: NODE_FIELDS, getLabel: () => 'Create new buoy field' },
{ key: NODE_ZONES, getLabel: () => 'Create new zone' },
{ key: NODE_POINTS, getLabel: () => 'Create new reference point' },
{ key: NODE_BACKDROPS, getLabel: () => 'Create new backdrop' }
]
static addIconLabelFor(key: string, title: string): string {
const nodeType = this.nodeLabels.find(node => node.key === key)
return nodeType ? nodeType.getLabel(title) : `ERROR - node type not handled: ${key}`
}
/**
* Get the appropriate icon for a node using the provided icon creators
* @param feature The feature (if any)
* @param key The node key
* @param title The node title
* @param handleAdd The add handler function
* @param iconCreators The icon creator functions
* @param button Optional custom button
* @returns The icon React node
*/
static getIcon(
feature: Feature | undefined,
key: string,
title: string,
handleAdd: HandleAddFunction | undefined,
iconCreators: IconCreators,
button?: React.ReactNode
): React.ReactNode {
// If no feature is provided, this is a parent node - show plus icon
if (!feature) {
if (!handleAdd) return null
if (button) return button
return iconCreators.createAddIcon(key, title, handleAdd)
}
// For leaf nodes, show type-specific icon based on dataType
const dataType = feature.properties?.dataType
const color = feature.properties?.stroke || feature.properties?.color || feature.properties?.['marker-color']
const environment = feature.properties?.env
return dataType
? iconCreators.createFeatureIcon(dataType, color, environment)
: iconCreators.createFolderIcon()
}
/**
* Build a track node
* @param features The features to include
* @param handleAdd The add handler function
* @param iconCreators The icon creator functions
* @param useTimeFilter Whether to filter by time
* @returns A TreeDataNode for tracks
*/
static buildTrackNode(
features: Feature[],
handleAdd: HandleAddFunction,
iconCreators: IconCreators,
useTimeFilter: boolean
): TreeDataNode {
// generate new root
const root: TreeDataNode = {
title: 'Units',
key: NODE_TRACKS,
icon: iconCreators.createFolderIcon(),
children: [],
}
const environments = symbolOptions.map((env): TreeDataNode => ({
title: env.label,
key: env.value,
icon: this.getIcon(undefined, NODE_TRACKS, env.value, handleAdd, iconCreators),
children: features
.filter(feature => feature.properties?.env === env.value)
.map((feature): TreeDataNode => ({
title: this.nameFor(feature),
key: this.idFor(feature),
icon: this.getIcon(feature, this.idFor(feature), this.nameFor(feature), undefined, iconCreators),
children: [],
}))
}))
// if time filter is applied, only include environments that contain features
const validEnvironments = useTimeFilter ? environments.filter(env => !!env.children?.length) : environments
root.children = root.children ? root.children.concat(...validEnvironments) : [...validEnvironments]
return root
}
/**
* Build a node for a specific feature type
* @param features The features to include
* @param title The node title
* @param key The node key
* @param dType The data type to filter by
* @param handleAdd The add handler function
* @param iconCreators The icon creator functions
* @param useTimeFilter Whether to filter by time
* @returns A TreeDataNode for the specified type
*/
static buildTypeNode(
features: Feature[],
title: string,
key: string,
dType: FeatureTypes,
handleAdd: HandleAddFunction,
iconCreators: IconCreators,
useTimeFilter: boolean,
iconOverride?: React.ReactNode
): TreeDataNode | null {
const children = features
? this.findChildrenOfType(features, dType).map(child => {
// Find the corresponding feature for this child
const feature = features.find(f => this.idFor(f) === child.key)
return {
...child,
icon: this.getIcon(feature, child.key, child.title, handleAdd, iconCreators),
}
})
: []
if (useTimeFilter && !children.length) return null
return {
title: iconCreators.createTitleElement(title),
key,
icon: iconOverride || this.getIcon(undefined, key, title, handleAdd, iconCreators), // Parent node gets plus icon
children,
}
}
/**
* Build the complete tree data model
* @param features The features to include
* @param handleAdd The add handler function
* @param iconCreators The icon creator functions
* @param useTimeFilter Whether to filter features by time
* @param timeStart The start time for filtering (if useTimeFilter is true)
* @param timeEnd The end time for filtering (if useTimeFilter is true)
* @returns An array of TreeDataNode objects representing the complete tree
*/
static buildTreeModel(
features: Feature[],
handleAdd: HandleAddFunction,
iconCreators: IconCreators,
useTimeFilter: boolean = false,
timeStart: number,
timeEnd: number,
zonesIcon: React.ReactNode
): Array<TreeDataNode | null> {
// If time filtering is enabled, filter the features
let filteredFeatures = features
if (useTimeFilter && timeStart !== 0 && timeEnd !== 0) {
// Filter features based on time properties
filteredFeatures = features.filter(feature =>
// Include features that are visible in the current time period (or don't have time)
featureIsVisibleInPeriod(feature, timeStart, timeEnd)
)
}
return [
this.buildTrackNode(filteredFeatures, handleAdd, iconCreators, useTimeFilter),
this.buildTypeNode(filteredFeatures, 'Buoy Fields', NODE_FIELDS, BUOY_FIELD_TYPE, handleAdd, iconCreators, useTimeFilter, undefined),
this.buildTypeNode(filteredFeatures, 'Zones', NODE_ZONES, ZONE_TYPE, handleAdd, iconCreators, useTimeFilter, zonesIcon),
this.buildTypeNode(filteredFeatures, 'Reference Points', NODE_POINTS, REFERENCE_POINT_TYPE, handleAdd, iconCreators, useTimeFilter, undefined),
this.buildTypeNode(filteredFeatures, 'Backdrops', NODE_BACKDROPS, BACKDROP_TYPE, handleAdd, iconCreators, useTimeFilter, undefined),
]
}
/**
* Check if an ID is a real feature ID (not a node ID)
* @param id The ID to check
* @returns True if the ID is a real feature ID
*/
static isRealId(id: string): boolean {
return !id.startsWith('node-')
}
}