-
Notifications
You must be signed in to change notification settings - Fork 456
/
toolState.js
executable file
·157 lines (136 loc) · 4.46 KB
/
toolState.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
import EVENTS from '../events.js';
import external from '../externalModules.js';
import { globalImageIdSpecificToolStateManager } from './imageIdSpecificStateManager.js';
import triggerEvent from '../util/triggerEvent.js';
import uuidv4 from '../util/uuidv4.js';
/**
* Returns the toolstate for a specific element.
* @public
* @function getElementToolStateManager
*
* @param {HTMLElement} element The element.
* @returns {Object} The toolState.
*/
function getElementToolStateManager(element) {
const enabledElement = external.cornerstone.getEnabledElement(element);
// If the enabledElement has no toolStateManager, create a default one for it
// NOTE: This makes state management element specific
if (enabledElement.toolStateManager === undefined) {
enabledElement.toolStateManager = globalImageIdSpecificToolStateManager;
}
return enabledElement.toolStateManager;
}
/**
* Adds tool state to the toolStateManager, this is done by tools as well
* as modules that restore saved state.
* @public
* @method addToolState
*
* @param {HTMLElement} element The element.
* @param {string} toolName The name of the tool the state belongs to.
* @param {Object} measurementData The data to store in the state.
* @returns {undefined}
*/
function addToolState(element, toolName, measurementData) {
const toolStateManager = getElementToolStateManager(element);
measurementData.uuid = measurementData.uuid || uuidv4();
toolStateManager.add(element, toolName, measurementData);
const eventType = EVENTS.MEASUREMENT_ADDED;
const eventData = {
toolName,
toolType: toolName, // Deprecation notice: toolType will be replaced by toolName
element,
measurementData,
};
triggerEvent(element, eventType, eventData);
}
/**
* Returns tool specific state of an element. Used by tools as well as modules
* that save state persistently
* @export
* @public
* @method
* @name getToolState
*
* @param {HTMLElement} element The element.
* @param {string} toolName The name of the tool the state belongs to.
* @returns {Object} The element's state for the given toolName.
*/
function getToolState(element, toolName) {
const toolStateManager = getElementToolStateManager(element);
return toolStateManager.get(element, toolName);
}
/**
* Removes specific tool state from the toolStateManager.
* @public
* @method removeToolState
*
* @param {HTMLElement} element The element.
* @param {string} toolName The name of the tool the state belongs to.
* @param {Object} data The data to remove from the toolStateManager.
* @returns {undefined}
*/
function removeToolState(element, toolName, data) {
const toolStateManager = getElementToolStateManager(element);
const toolData = toolStateManager.get(element, toolName);
if (!toolData || !toolData.data || !toolData.data.length) {
return;
}
// Find this tool data
let indexOfData = -1;
for (let i = 0; i < toolData.data.length; i++) {
if (toolData.data[i] === data) {
indexOfData = i;
}
}
if (indexOfData !== -1) {
toolData.data.splice(indexOfData, 1);
const eventType = EVENTS.MEASUREMENT_REMOVED;
const eventData = {
toolName,
toolType: toolName, // Deprecation notice: toolType will be replaced by toolName
element,
measurementData: data,
};
triggerEvent(element, eventType, eventData);
}
}
/**
* Removes all toolState from the toolStateManager corresponding to
* the toolName and element.
* @public
* @method clearToolState
*
* @param {HTMLElement} element The element.
* @param {string} toolName The name of the tool the state belongs to.
* @returns {undefined}
*/
function clearToolState(element, toolName) {
const toolStateManager = getElementToolStateManager(element);
const toolData = toolStateManager.get(element, toolName);
// If any toolData actually exists, clear it
if (toolData !== undefined) {
toolData.data = [];
}
}
/**
* Sets the tool state manager for an element
* @public
* @method setElementToolStateManager
*
* @param {HTMLElement} element The element.
* @param {Object} toolStateManager The toolStateManager.
* @returns {undefined}
*/
function setElementToolStateManager(element, toolStateManager) {
const enabledElement = external.cornerstone.getEnabledElement(element);
enabledElement.toolStateManager = toolStateManager;
}
export {
addToolState,
getToolState,
removeToolState,
clearToolState,
setElementToolStateManager,
getElementToolStateManager,
};