Last active
August 18, 2020 19:57
-
-
Save dannyrb/9909233c2325d77d3e1bc544c0a13282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import EVENTS from '../events.js'; | |
import external from '../externalModules.js'; | |
import { getToolState } from '../stateManagement/toolState.js'; | |
import requestPoolManager from '../requestPool/requestPoolManager.js'; | |
import loadHandlerManager from '../stateManagement/loadHandlerManager.js'; | |
import triggerEvent from '../util/triggerEvent.js'; | |
/** | |
* Scrolls through the stack to the image index requested. | |
* @export @public @method | |
* @name scrollToIndex | |
* | |
* @param {type} element The element to scroll through. | |
* @param {type} newImageIdIndex The target image index. | |
* @returns {void} | |
*/ | |
export default async function(element, newImageIdIndex, isSilent = false) { | |
const cornerstone = external.cornerstone; | |
const toolData = getToolState(element, 'stack'); | |
const stackData = toolData?.data[0] | |
if (!stackData) { | |
return; | |
} | |
// Allow for negative indexing | |
if (newImageIdIndex < 0) { | |
newImageIdIndex += stackData.imageIds.length; | |
} | |
const isSameImageIdIndex = newImageIdIndex === stackData.currentImageIdIndex | |
if (isSameImageIdIndex) { | |
return; | |
} | |
const startLoadingHandler = loadHandlerManager.getStartLoadHandler(element) || _noop; | |
const endLoadingHandler = loadHandlerManager.getEndLoadHandler(element) || _noop; | |
const errorLoadingHandler = loadHandlerManager.getErrorLoadingHandler( | |
element | |
) || _noop; | |
const oldImageIdIndex = stackData.currentImageIdIndex; | |
const newImageId = stackData.imageIds[newImageIdIndex]; | |
const shouldPreventCache = Boolean(stackData.preventCache); | |
const imagePromise = shouldPreventCache | |
? cornerstone.loadImage(newImageId) | |
: cornerstone.loadAndCacheImage(newImageId); | |
try { | |
startLoadingHandler(element); | |
stackData.currentImageIdIndex = newImageIdIndex; | |
const image = await imagePromise; | |
if (stackData.currentImageIdIndex !== newImageIdIndex) { | |
return; | |
} | |
try { | |
// TODO: Add 'isElementEnabled' to Cornerstone? | |
cornerstone.getEnabledElement(element); | |
} catch (error) { | |
return; | |
} | |
// If we have more than one stack, check if we have a stack renderer defined | |
const stackRenderer = getToolState(element, 'stackRenderer')?.data[0] | |
// Display Image | |
if (stackRenderer !== undefined) { | |
stackRenderer.currentImageIdIndex = newImageIdIndex; | |
stackRenderer.render(element, toolData.data); | |
} else { | |
cornerstone.displayImage(element, image); | |
} | |
endLoadingHandler(element, image); | |
} catch(error) { | |
errorLoadingHandler(element, newImageId, error); | |
} | |
// Make sure we kick off any changed download request pools | |
requestPoolManager.startGrabbing(); | |
if(!isSilent) { | |
const eventData = { | |
newImageIdIndex, | |
direction: newImageIdIndex - oldImageIdIndex, | |
}; | |
triggerEvent(element, EVENTS.STACK_SCROLL, eventData); | |
} | |
} | |
function _noop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment