-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathdrawController.js
179 lines (162 loc) · 4.55 KB
/
drawController.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
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
import {AnnotationGroup} from '../image/annotationGroup';
import {
RemoveAnnotationCommand,
UpdateAnnotationCommand
} from '../tools/drawCommands';
import {logger} from '../utils/logger';
// doc imports
/* eslint-disable no-unused-vars */
import {Annotation} from '../image/annotation';
/* eslint-enable no-unused-vars */
/**
* Draw controller.
*/
export class DrawController {
/**
* The annotation group.
*
* @type {AnnotationGroup}
*/
#annotationGroup;
/**
* Get an annotation.
*
* @param {string} id The annotation id.
* @returns {Annotation|undefined} The annotation.
*/
getAnnotation(id) {
return this.#annotationGroup.find(id);
}
/**
* Get the annotation group.
*
* @returns {AnnotationGroup} The list.
*/
getAnnotationGroup() {
return this.#annotationGroup;
}
/**
* Check if the annotation group is editable.
*
* @returns {boolean} True if editable.
*/
isAnnotationGroupEditable() {
return this.#annotationGroup.isEditable();
}
/**
* Set the annotation group editability.
*
* @param {boolean} flag True to make the annotation group editable.
*/
setAnnotationGroupEditable(flag) {
this.#annotationGroup.setEditable(flag);
}
/**
* Add an annotation.
*
* @param {Annotation} annotation The annotation to add.
*/
addAnnotation(annotation) {
this.#annotationGroup.add(annotation);
}
/**
* Update an anotation from the list.
*
* @param {Annotation} annotation The annotation to update.
* @param {string[]} [propKeys] Optional properties that got updated.
*/
updateAnnotation(annotation, propKeys) {
this.#annotationGroup.update(annotation, propKeys);
}
/**
* Remove an anotation for the list.
*
* @param {string} id The id of the annotation to remove.
*/
removeAnnotation(id) {
this.#annotationGroup.remove(id);
}
/**
* Remove an annotation via a remove command (triggers draw actions).
*
* @param {string} id The annotation id.
* @param {Function} exeCallback The undo stack callback.
*/
removeAnnotationWithCommand(id, exeCallback) {
const annotation = this.getAnnotation(id);
if (typeof annotation === 'undefined') {
logger.warn(
'Cannot create remove command for undefined annotation: ' + id);
return;
}
// create remove annotation command
const command = new RemoveAnnotationCommand(annotation, this);
// add command to undo stack
exeCallback(command);
// execute command: triggers draw remove
command.execute();
}
/**
* Update an annotation via an update command (triggers draw actions).
*
* @param {string} id The annotation id.
* @param {object} originalProps The original annotation properties
* that will be updated.
* @param {object} newProps The new annotation properties
* that will replace the original ones.
* @param {Function} exeCallback The undo stack callback.
*/
updateAnnotationWithCommand(id, originalProps, newProps, exeCallback) {
const annotation = this.getAnnotation(id);
if (typeof annotation === 'undefined') {
logger.warn(
'Cannot create update command for undefined annotation: ' + id);
return;
}
// create remove annotation command
const command = new UpdateAnnotationCommand(
annotation, originalProps, newProps, this);
// add command to undo stack
exeCallback(command);
// execute command: triggers draw remove
command.execute();
}
/**
* Remove all annotations via remove commands (triggers draw actions).
*
* @param {Function} exeCallback The undo stack callback.
*/
removeAllAnnotationsWithCommand(exeCallback) {
for (const annotation of this.#annotationGroup.getList()) {
this.removeAnnotationWithCommand(annotation.id, exeCallback);
}
}
/**
* @param {AnnotationGroup} [group] Optional annotation group.
*/
constructor(group) {
if (typeof group !== 'undefined') {
this.#annotationGroup = group;
} else {
this.#annotationGroup = new AnnotationGroup();
}
}
/**
* Check if the annotation group contains a meta data value.
*
* @param {string} key The key to check.
* @returns {boolean} True if the meta data is present.
*/
hasAnnotationMeta(key) {
return this.#annotationGroup.hasMeta(key);
}
/**
* Set an annotation meta data.
*
* @param {string} key The meta data to set.
* @param {string} value The value of the meta data.
*/
setAnnotationMeta(key, value) {
this.#annotationGroup.setMetaValue(key, value);
}
} // class DrawController