-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathannotationState.ts
180 lines (157 loc) · 5.76 KB
/
annotationState.ts
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
import {
triggerEvent,
eventTarget,
utilities as csUtils,
} from '@cornerstonejs/core';
import { Events } from '../../enums';
import { defaultFrameOfReferenceSpecificAnnotationManager } from './FrameOfReferenceSpecificAnnotationManager';
import { Annotations, Annotation } from '../../types/AnnotationTypes';
import { AnnotationRemovedEventDetail } from '../../types/EventTypes';
import { AnnotationGroupSelector } from '../../types';
import {
triggerAnnotationAddedForElement,
triggerAnnotationAddedForFOR,
} from './helpers/state';
// our default annotation manager
let defaultManager = defaultFrameOfReferenceSpecificAnnotationManager;
/**
* It returns the default annotations manager.
* @returns the singleton default annotations manager.
*/
function getAnnotationManager() {
return defaultManager;
}
/**
* Set the annotation manager to be used for rendering, adding, removing, etc.
* @param annotationManager - The annotation manager to be used
*/
function setAnnotationManager(annotationManager) {
defaultManager = annotationManager;
}
// set back to default frameOfReferenceSpecificAnnotationManager
function resetAnnotationManager() {
defaultManager = defaultFrameOfReferenceSpecificAnnotationManager;
}
/**
* Returns the annotations for a given tool with the provided options that is
* used to filter annotations based on the annotation manager.
*
* In our default implementation, the options are the element and/or the FrameOfReferenceUID.
* Hence, the getAnnotations function will return the annotations for the given tool
* that are associated with the FrameOfReferenceUID.
*
* @param toolName - The name of the tool.
* @param annotationGroupSelector - element or FrameOfReferenceUID that is used
* to group annotations in the annotation manager.
* @returns The annotations corresponding to the Frame of Reference and the toolName.
*/
function getAnnotations(
toolName: string,
annotationGroupSelector: AnnotationGroupSelector
): Annotations {
const manager = getAnnotationManager();
const groupKey = manager.getGroupKey(annotationGroupSelector);
return manager.getAnnotations(groupKey, toolName) as Annotations;
}
/**
* Add the annotation to the annotation manager along with the options that is
* used to filter the annotation manager and the annotation group that
* the annotation belongs to.
*
* As a result, our default implementation will add the annotation to the
* default manager using the FrameOfReferenceUID as the group key.
*
* @param annotation - The annotation that is being added to the annotations manager.
* @param annotationGroupSelector - element or FrameOfReferenceUID that is used
* to group annotations in the annotation manager.
*/
function addAnnotation(
annotation: Annotation,
annotationGroupSelector: AnnotationGroupSelector
): string {
if (annotation.annotationUID === undefined) {
annotation.annotationUID = csUtils.uuidv4() as string;
}
const manager = getAnnotationManager();
const groupKey = manager.getGroupKey(annotationGroupSelector);
manager.addAnnotation(annotation, groupKey);
// if the annotation manager selector is an element, trigger the
// annotation added event for that element.
if (annotationGroupSelector instanceof HTMLDivElement) {
triggerAnnotationAddedForElement(annotation, annotationGroupSelector);
}
// if no element is provided, render all viewports that have the
// same frame of reference.
// Todo: we should do something else here for other types of annotation managers.
triggerAnnotationAddedForFOR(annotation);
return annotation.annotationUID;
}
/**
* Get the number of annotations for a given tool with the provided options that is
* used to filter annotations based on the annotation manager.
*
* In our default implementation, the options are the element and/or the FrameOfReferenceUID.
* Hence, the getNumberOfAnnotations function will return the number of annotations for the given tool
* that are associated with the FrameOfReferenceUID.
*
* @param toolName - The name of the tool
* @param annotationGroupSelector - element or FrameOfReferenceUID that is used
* to group annotations in the annotation manager.
*
*/
function getNumberOfAnnotations(
toolName: string,
annotationGroupSelector: AnnotationGroupSelector
): number {
const manager = getAnnotationManager();
const groupKey = manager.getGroupKey(annotationGroupSelector);
return manager.getNumberOfAnnotations(groupKey, toolName);
}
/**
* Remove the annotation by UID of the annotation.
* @param annotationUID - The unique identifier for the annotation.
*/
function removeAnnotation(annotationUID: string): void {
const manager = getAnnotationManager();
const annotation = manager.getAnnotation(annotationUID);
// no need to continue in case there is no annotation.
if (!annotation) {
return;
}
manager.removeAnnotation(annotationUID);
// trigger annotation removed
const eventType = Events.ANNOTATION_REMOVED;
const eventDetail: AnnotationRemovedEventDetail = {
annotation,
annotationManagerUID: manager.uid,
};
triggerEvent(eventTarget, eventType, eventDetail);
}
/**
* Get the Annotation object by its UID
* @param annotationUID - The unique identifier of the annotation.
*/
function getAnnotation(annotationUID: string): Annotation {
const manager = getAnnotationManager();
const annotation = manager.getAnnotation(annotationUID);
return annotation;
}
/**
* It removes all annotations from the default annotation manager
*/
function removeAllAnnotations(): void {
const manager = getAnnotationManager();
manager.removeAllAnnotations();
}
export {
getAnnotations,
getNumberOfAnnotations,
addAnnotation,
getAnnotation,
removeAnnotation,
removeAllAnnotations,
// annotation manager
setAnnotationManager,
getAnnotationManager,
resetAnnotationManager,
};