This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
/
handleChildMeasurementAdded.js
98 lines (78 loc) · 2.89 KB
/
handleChildMeasurementAdded.js
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
import cornerstone from 'cornerstone-core';
import { MeasurementApi } from '../classes';
import log from '../../log';
import user from '../../user';
import getImageAttributes from '../lib/getImageAttributes';
import getLabel from '../lib/getLabel';
export default function({ eventData, tool, toolGroupId, toolGroup }) {
const measurementApi = MeasurementApi.Instance;
if (!measurementApi) {
log.warn('Measurement API is not initialized');
}
const { measurementData } = eventData;
const collection = measurementApi.tools[tool.parentTool];
// Stop here if the tool data shall not be persisted (e.g. temp tools)
if (!collection) return;
// Stop here if there's no measurement data or if it was cancelled
if (!measurementData || measurementData.cancelled) return;
log.info('CornerstoneToolsMeasurementAdded');
const imageAttributes = getImageAttributes(eventData.element);
const additionalProperties = Object.assign(imageAttributes, {
userId: user.getUserId(),
});
const childMeasurement = Object.assign(
{},
measurementData,
additionalProperties
);
const parentMeasurement = collection.find(
t =>
t.toolType === tool.parentTool &&
t.patientId === imageAttributes.patientId &&
t[tool.attribute] === null
);
// Check if a measurement to fit this child tool already exists
if (parentMeasurement) {
const key = tool.attribute;
// Add the createdAt attribute
childMeasurement.createdAt = new Date();
// Update the parent measurement
parentMeasurement[key] = childMeasurement;
parentMeasurement.childToolsCount =
(parentMeasurement.childToolsCount || 0) + 1;
measurementApi.updateMeasurement(tool.parentTool, parentMeasurement);
// Update the measurementData ID and lesionNamingNumber
measurementData._id = parentMeasurement._id;
measurementData.lesionNamingNumber = parentMeasurement.lesionNamingNumber;
} else {
const measurement = {
toolType: tool.parentTool,
lesionNamingNumber: measurementData.lesionNamingNumber,
userId: user.getUserId(),
patientId: imageAttributes.patientId,
studyInstanceUid: imageAttributes.studyInstanceUid,
};
measurement[tool.attribute] = Object.assign(
{},
measurementData,
additionalProperties
);
const addedMeasurement = measurementApi.addMeasurement(
tool.parentTool,
measurement
);
Object.assign(measurementData, addedMeasurement);
}
const measurementLabel = getLabel(measurementData);
if (measurementLabel) {
measurementData.labels = [measurementLabel];
}
// TODO: This is very hacky, but will work for now
cornerstone.getEnabledElements().forEach(enabledElement => {
cornerstone.updateImage(enabledElement.element);
});
// TODO: Notify about the last activated measurement
if (MeasurementApi.isToolIncluded(tool)) {
// TODO: Notify that viewer suffered changes
}
}