-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvehicle-feature-manager.ts
More file actions
346 lines (322 loc) · 12.1 KB
/
vehicle-feature-manager.ts
File metadata and controls
346 lines (322 loc) · 12.1 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
import { createSelector, type Store } from '@ngrx/store';
import { normalZoom } from 'fuesim-digital-shared';
import type {
UUID,
Vehicle,
// eslint-disable-next-line @typescript-eslint/no-shadow
Element,
PatientStatus,
} from 'fuesim-digital-shared';
import type { Feature, MapBrowserEvent } from 'ol';
import type Point from 'ol/geom/Point';
import type { TranslateEvent } from 'ol/interaction/Translate';
import type OlMap from 'ol/Map';
import { pairwise, startWith, takeUntil, type Subject } from 'rxjs';
import { Fill, Stroke, Style, Text as OlText } from 'ol/style';
import { VehiclePopupComponent } from '../shared/vehicle-popup/vehicle-popup.component';
import type { OlMapInteractionsManager } from '../utility/ol-map-interactions-manager';
import { PointGeometryHelper } from '../utility/point-geometry-helper';
import { ImagePopupHelper } from '../utility/popup-helper';
import { ImageStyleHelper } from '../utility/style-helper/image-style-helper';
import { NameStyleHelper } from '../utility/style-helper/name-style-helper';
import type { PopupService } from '../utility/popup.service';
import { CircleStyleHelper } from '../utility/style-helper/circle-style-helper';
import type { ExerciseService } from '../../../../../../core/exercise.service';
import type { AppState } from '../../../../../../state/app.state';
import {
selectConfiguration,
selectVehicles,
selectExerciseState,
} from '../../../../../../state/application/selectors/exercise.selectors';
import { selectVisibleVehicles } from '../../../../../../state/application/selectors/shared.selectors';
import { selectStateSnapshot } from '../../../../../../state/get-state-snapshot';
import { MoveableFeatureManager } from './moveable-feature-manager';
type PossibleVehicleStatus = Exclude<PatientStatus, 'white'>;
interface StatusbarColor {
backgroundColor: string;
backgroundStroke: string;
color: string;
}
const statusPriorities = [
'red',
'yellow',
'green',
'blue',
'black',
] as const satisfies PossibleVehicleStatus[];
const patientStatusToStatusbarColors = {
red: {
backgroundColor: 'rgba(220, 53, 69, 0.85)',
backgroundStroke: 'rgb(220, 53, 69)',
color: 'white',
},
yellow: {
backgroundColor: 'rgba(255, 193, 7, 0.85)',
backgroundStroke: 'rgb(255, 193, 7)',
color: 'black',
},
green: {
backgroundColor: 'rgba(40, 167, 69, 0.85)',
backgroundStroke: 'rgb(40, 167, 69)',
color: 'white',
},
black: {
backgroundColor: 'rgba(15, 15, 15, 0.85)',
backgroundStroke: 'rgb(15, 15, 15)',
color: 'white',
},
blue: {
backgroundColor: 'rgba(0, 123, 255, 0.85)',
backgroundStroke: 'rgb(0, 123, 255)',
color: 'white',
},
} as const satisfies {
[key in PossibleVehicleStatus]: StatusbarColor;
};
export class VehicleFeatureManager extends MoveableFeatureManager<Vehicle> {
public register(
destroy$: Subject<void>,
mapInteractionsManager: OlMapInteractionsManager
): void {
super.registerFeatureElementManager(
this.store.select(selectVisibleVehicles),
destroy$,
mapInteractionsManager
);
// Register change handlers to show/hide vehicle status indicators if configuration was changed
this.store
.select(
createSelector(
selectConfiguration,
selectVehicles,
(configuration, vehicles) => ({
vehicleStatusHighlight:
configuration.vehicleStatusHighlight,
vehicleStatusInPatientStatusColor:
configuration.vehicleStatusInPatientStatusColor,
vehicles,
})
)
)
.pipe(
startWith({
vehicleStatusHighlight: false,
vehicleStatusInPatientStatusColor: false,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
vehicles: {} as { [key: UUID]: Vehicle },
}),
pairwise(),
takeUntil(destroy$)
)
.subscribe(([oldData, newData]) => {
if (
oldData.vehicleStatusHighlight !==
newData.vehicleStatusHighlight ||
oldData.vehicleStatusInPatientStatusColor !==
newData.vehicleStatusInPatientStatusColor
) {
Object.values(newData.vehicles).forEach((newVehicle) => {
const oldVehicle = oldData.vehicles[newVehicle.id];
if (oldVehicle)
this.onElementChanged(oldVehicle, newVehicle);
});
}
});
}
private readonly imageStyleHelper = new ImageStyleHelper(
(feature) => (this.getElementFromFeature(feature) as Vehicle).image
);
private readonly nameStyleHelper = new NameStyleHelper(
(feature) => {
const vehicle = this.getElementFromFeature(feature) as Vehicle;
return {
name: vehicle.name,
offsetY: vehicle.image.height / 2 / normalZoom,
};
},
0.1,
'top'
);
private readonly popupHelper = new ImagePopupHelper(this.olMap, this.layer);
private readonly openPopupCircleStyleHelper = new CircleStyleHelper(
(feature) => ({
radius: Math.max(
(this.getElementFromFeature(feature) as Vehicle).image.height,
(this.getElementFromFeature(feature) as Vehicle).image.height *
(this.getElementFromFeature(feature) as Vehicle).image
.aspectRatio
),
fill: new Fill({
color: '#00000000',
}),
stroke: new Stroke({
color: 'orange',
width: 10,
}),
}),
0.025,
(_) => [0, 0]
);
constructor(
olMap: OlMap,
private readonly store: Store<AppState>,
private readonly exerciseService: ExerciseService,
private readonly popupService: PopupService
) {
super(
olMap,
async (targetPosition, vehicle) =>
exerciseService.proposeAction(
{
type: '[Vehicle] Move vehicle',
vehicleId: vehicle.id,
targetPosition,
},
true
),
new PointGeometryHelper(),
1000
);
this.layer.setStyle((feature, resolution) => {
const styles = [
this.nameStyleHelper.getStyle(feature as Feature, resolution),
this.imageStyleHelper.getStyle(feature as Feature, resolution),
];
const statusBarStyle = this.statusBarStyleHelper(
feature as Feature
);
if (statusBarStyle) {
styles.push(...statusBarStyle);
}
this.addMarking(
feature,
styles,
this.popupService,
this.store,
this.openPopupCircleStyleHelper.getStyle(
feature as Feature,
resolution
)
);
return styles;
});
}
public override onFeatureDrop(
droppedElement: Element | undefined,
droppedOnFeature: Feature<Point>,
dropEvent?: TranslateEvent
) {
const droppedOnVehicle = this.getElementFromFeature(
droppedOnFeature
) as Vehicle | undefined;
if (!droppedElement || !droppedOnVehicle) {
console.error('Could not find element for the features');
return false;
}
if (
(droppedElement.type === 'personnel' &&
droppedOnVehicle.personnelIds[droppedElement.id]) ||
(droppedElement.type === 'material' &&
droppedOnVehicle.materialIds[droppedElement.id]) ||
(droppedElement.type === 'patient' &&
Object.keys(droppedOnVehicle.patientIds).length <
droppedOnVehicle.patientCapacity)
) {
// TODO: user feedback (e.g. toast)
this.exerciseService.proposeAction(
{
type: '[Vehicle] Load vehicle',
vehicleId: droppedOnVehicle.id,
elementToBeLoadedId: droppedElement.id,
elementToBeLoadedType: droppedElement.type,
},
true
);
return true;
}
return false;
}
public override onFeatureClicked(
event: MapBrowserEvent<any>,
feature: Feature<any>
): void {
super.onFeatureClicked(event, feature);
const vehicle = this.getElementFromFeature(feature) as Vehicle;
this.popupService.openPopup(
this.popupHelper.getPopupOptions(
VehiclePopupComponent,
feature,
[feature.getId() as UUID],
[
...Object.keys(vehicle.materialIds),
...Object.keys(vehicle.personnelIds),
feature.getId() as UUID,
],
[feature.getId() as UUID],
['vehicle', 'personnel', 'material'],
{
vehicleId: feature.getId() as UUID,
}
)
);
}
/**
* Creates statusbar styles for a vehicle feature.
* The statusbar shows the number of occupied/all patient seats and is, optionally, colored by the patient's status color.
*/
private readonly statusBarStyleHelper = (
feature: Feature<any>
): Style[] | undefined => {
const config = selectStateSnapshot(selectConfiguration, this.store);
if (!config.vehicleStatusHighlight) {
return undefined;
}
const vehicle = this.getElementFromFeature(feature) as Vehicle;
if (vehicle.patientCapacity <= 0) {
return undefined;
}
const patientCount = Object.keys(vehicle.patientIds).length;
const text = `${patientCount}/${vehicle.patientCapacity}`;
let statusbarColor: StatusbarColor = {
backgroundColor: 'rgba(255, 255, 255, 0.85)',
backgroundStroke: 'rgb(255, 255, 255)',
color: 'black',
};
if (config.vehicleStatusInPatientStatusColor && patientCount > 0) {
const state = selectStateSnapshot(selectExerciseState, this.store);
const patients = Object.keys(vehicle.patientIds)
.map((id) => state.patients[id])
.filter(Boolean);
const getStatus = (p: any) =>
config.pretriageEnabled ? p.pretriageStatus : p.realStatus;
const vehicleStatusColor = statusPriorities.find((s) =>
patients.some((p) => p && getStatus(p) === s)
);
if (vehicleStatusColor)
statusbarColor =
patientStatusToStatusbarColors[vehicleStatusColor];
}
const resolution = this.olMap.getView().getResolution() ?? 1;
const scale = 1 / resolution;
const fontPx = 2 * scale;
const textStyle = new Style({
text: new OlText({
text,
font: `${fontPx}px sans-serif`,
fill: new Fill({ color: statusbarColor.color }),
backgroundFill: new Fill({
color: statusbarColor.backgroundColor,
}),
backgroundStroke: new Stroke({
color: statusbarColor.backgroundStroke,
width: 0.5 * scale,
}),
offsetY: -5 * scale,
padding: [0.3 * scale, 2 * scale, 0 * scale, 2 * scale],
textAlign: 'center',
textBaseline: 'middle',
}),
});
return [textStyle];
};
}