Skip to content

Commit

Permalink
feat(imageIdSpecificStateManager.js): Expose methods for managing too…
Browse files Browse the repository at this point in the history
…lState via imageId (#1151)

Expose [get|add|clear]ImageIdToolstate methods which allow managing imageId specific toolState
without needing an element with the image loaded.

#1150

Co-authored-by: Danny Brown <[email protected]>
  • Loading branch information
mikehazell and dannyrb committed Jan 21, 2020
1 parent b43d373 commit eac36e6
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/stateManagement/imageIdSpecificStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down

0 comments on commit eac36e6

Please sign in to comment.