From 5473465c191949f0a0aad44a9ce3deb58447c18a Mon Sep 17 00:00:00 2001 From: Mike Hazell Date: Thu, 19 Dec 2019 14:23:21 +1100 Subject: [PATCH] feat(imageIdSpecificStateManager.js): Expose methods for managing toolState via imageId Expose [get|add|clear]ImageIdToolstate methods which allow managing imageId specific toolState without needing an element with the image loaded. #1150 --- .../imageIdSpecificStateManager.js | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/src/stateManagement/imageIdSpecificStateManager.js b/src/stateManagement/imageIdSpecificStateManager.js index 7b9bf21c0..06413fd31 100755 --- a/src/stateManagement/imageIdSpecificStateManager.js +++ b/src/stateManagement/imageIdSpecificStateManager.js @@ -34,20 +34,23 @@ function newImageIdSpecificToolStateManager() { // Here we add tool state, this is done by tools as well // As modules that restore saved state - function addImageIdSpecificToolState(element, toolType, data) { + function addElementToolState(element, toolType, data) { const enabledElement = external.cornerstone.getEnabledElement(element); // If we don't have an image for this element exit early if (!enabledElement.image) { return; } + addImageIdToolState(enabledElement.image.imageId, toolType, data); + } + function addImageIdToolState(imageId, toolType, data) { // If we don't have any tool state for this imageId, add an empty object - if (toolState.hasOwnProperty(enabledElement.image.imageId) === false) { - toolState[enabledElement.image.imageId] = {}; + if (toolState.hasOwnProperty(imageId) === false) { + toolState[imageId] = {}; } - const imageIdToolState = toolState[enabledElement.image.imageId]; + const imageIdToolState = toolState[imageId]; // If we don't have tool state for this type of tool, add an empty object if (imageIdToolState.hasOwnProperty(toolType) === false) { @@ -62,49 +65,60 @@ function newImageIdSpecificToolStateManager() { toolData.data.push(data); } + function getElementToolState(element, toolType) { + const enabledElement = external.cornerstone.getEnabledElement(element); + + // If the element does not have an image return undefined. + if (!enabledElement.image) { + return; + } + + return getImageIdToolState(enabledElement.image.imageId, toolType); + } + // Here you can get state - used by tools as well as modules // That save state persistently - function getImageIdSpecificToolState(element, toolType) { - const enabledElement = external.cornerstone.getEnabledElement(element); + function getImageIdToolState(imageId, toolType) { // If we don't have any tool state for this imageId, return undefined - - if ( - !enabledElement.image || - toolState.hasOwnProperty(enabledElement.image.imageId) === false - ) { + if (toolState.hasOwnProperty(imageId) === false) { return; } - const imageIdToolState = toolState[enabledElement.image.imageId]; + const imageIdToolState = toolState[imageId]; // If we don't have tool state for this type of tool, return undefined if (imageIdToolState.hasOwnProperty(toolType) === false) { return; } - const toolData = imageIdToolState[toolType]; - - return toolData; + return imageIdToolState[toolType]; } // Clears all tool data from this toolStateManager. - function clearImageIdSpecificToolStateManager(element) { + function clearElementToolState(element) { const enabledElement = external.cornerstone.getEnabledElement(element); - if ( - !enabledElement.image || - toolState.hasOwnProperty(enabledElement.image.imageId) === false - ) { + if (!enabledElement.image) { + return; + } + clearImageIdToolState(enabledElement.imageId); + } + + function clearImageIdToolState(imageId) { + if (toolState.hasOwnProperty(imageId) === false) { return; } - delete toolState[enabledElement.image.imageId]; + delete toolState[imageId]; } return { - get: getImageIdSpecificToolState, - add: addImageIdSpecificToolState, - clear: clearImageIdSpecificToolStateManager, + get: getElementToolState, + add: addElementToolState, + clear: clearElementToolState, + getImageIdToolState, + addImageIdToolState, + clearImageIdToolState, saveImageIdToolState, restoreImageIdToolState, saveToolState,