/
annotationLocking.ts
178 lines (159 loc) · 4.47 KB
/
annotationLocking.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
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
import { Events } from '../../enums';
import { Annotation } from '../../types';
import { AnnotationLockChangeEventDetail } from '../../types/EventTypes';
/*
* Constants
*/
const globalLockedAnnotationsSet: Set<Annotation> = new Set();
/*
* Interface (Public API)
*/
/**
* Set the "Locked" state of a given annotation instance.
*
* @triggers ANNOTATION_LOCK_CHANGE
*
* @param annotation - The annotation instance which will have
* its locked state changed. An event will only be triggered if the locked state
* of the given annotation instance changed.
* @param locked - A boolean value indicating if the instance should
* be locked (true) or not (false)
*/
function setAnnotationLocked(annotation: Annotation, locked = true): void {
const detail = makeEventDetail();
if (annotation) {
if (locked) {
lock(annotation, globalLockedAnnotationsSet, detail);
} else {
unlock(annotation, globalLockedAnnotationsSet, detail);
}
}
publish(detail, globalLockedAnnotationsSet);
}
/**
* Clears all the locked annotation
*
*/
function unlockAllAnnotations(): void {
const detail = makeEventDetail();
clearLockedAnnotationsSet(globalLockedAnnotationsSet, detail);
publish(detail, globalLockedAnnotationsSet);
}
/**
* Returns an array of all the annotation that is currently locked
* @returns An array of tool specific annotation objects.
*
*/
function getAnnotationsLocked(): Array<Annotation> {
return Array.from(globalLockedAnnotationsSet);
}
/**
* Given a Annotation object, return true if it is locked.
* @param annotation - Annotation
* @returns A boolean value.
*/
function isAnnotationLocked(annotation: Annotation): boolean {
return globalLockedAnnotationsSet.has(annotation);
}
/**
* Get the number of locked annotation objects in the global set of locked annotation
* objects.
* @returns The number of locked annotation objects.
*
*/
function getAnnotationsLockedCount(): number {
return globalLockedAnnotationsSet.size;
}
/**
* Properly initialize the isLocked on annotation, and set it as locked if
* isLocked is true.
* @param annotation - The annotation object to be checked.
*/
function checkAndDefineIsLockedProperty(annotation: Annotation): void {
if (annotation) {
const isLocked = !!annotation.isLocked;
if (shouldDefineIsLockedProperty(annotation)) {
Object.defineProperty(annotation, 'isLocked', {
configurable: false,
enumerable: true,
set: setIsLocked,
get: getIsLocked,
});
}
setAnnotationLocked(annotation, isLocked);
}
}
/*
* Private Helpers
*/
function makeEventDetail(): AnnotationLockChangeEventDetail {
return Object.freeze({
added: [],
removed: [],
locked: [],
});
}
function lock(
annotation: Annotation,
lockedAnnotationsSet: Set<Annotation>,
detail: AnnotationLockChangeEventDetail
): void {
if (!lockedAnnotationsSet.has(annotation)) {
lockedAnnotationsSet.add(annotation);
detail.added.push(annotation);
}
}
function unlock(
annotation: Annotation,
lockedAnnotationsSet: Set<Annotation>,
detail: AnnotationLockChangeEventDetail
): void {
if (lockedAnnotationsSet.delete(annotation)) {
detail.removed.push(annotation);
}
}
function clearLockedAnnotationsSet(
lockedAnnotationsSet: Set<Annotation>,
detail: AnnotationLockChangeEventDetail
): void {
lockedAnnotationsSet.forEach((annotation) => {
unlock(annotation, lockedAnnotationsSet, detail);
});
}
function publish(
detail: AnnotationLockChangeEventDetail,
lockedAnnotationsSet: Set<Annotation>
) {
if (detail.added.length > 0 || detail.removed.length > 0) {
lockedAnnotationsSet.forEach((item) => void detail.locked.push(item));
triggerEvent(eventTarget, Events.ANNOTATION_LOCK_CHANGE, detail);
}
}
function shouldDefineIsLockedProperty(annotation: Annotation): boolean {
const descriptor = Object.getOwnPropertyDescriptor(annotation, 'isLocked');
if (descriptor) {
return (
descriptor.configurable &&
(descriptor.set !== setIsLocked || descriptor.get !== getIsLocked)
);
}
return Object.isExtensible(annotation);
}
function setIsLocked(locked: boolean) {
setAnnotationLocked(this as Annotation, locked);
}
function getIsLocked() {
return isAnnotationLocked(this as Annotation);
}
/*
* Exports
*/
export {
setAnnotationLocked,
getAnnotationsLocked,
getAnnotationsLockedCount,
unlockAllAnnotations,
isAnnotationLocked,
checkAndDefineIsLockedProperty,
};