All files / core/src/utilities jumpToSlice.ts

85.71% Statements 12/14
28.57% Branches 2/7
100% Functions 3/3
85.71% Lines 12/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                16x 16x   16x       16x   16x         16x 16x   16x             16x               16x                   16x   16x        
import clip from './clip';
import type {
  ImageSliceData,
  IStackViewport,
  IVolumeViewport,
  JumpToSliceOptions,
} from '../types';
import scroll from './scroll';
import getEnabledElement from '../getEnabledElement';
import StackViewport from '../RenderingEngine/StackViewport';
/**
 * It uses the imageIndex in the Options to scroll to the slice that is intended.
 * It works for both Stack and Volume viewports. In VolumeViewports, the imageIndex
 * should be given with respect to the index in the 3D image in the view direction
 * (i.e. the index of the slice in Axial, Sagittal, Coronal, or Oblique).
 *
 * @param element - the HTML Div element scrolling inside
 * @param options - the options used for jumping to a slice
 * @returns Promise that resolves to ImageIdIndex
 */
async function jumpToSlice(
  element: HTMLDivElement,
  options = {} as JumpToSliceOptions
): Promise<void> {
  const { imageIndex, debounceLoading, volumeId } = options;
  const enabledElement = getEnabledElement(element);
 
  Iif (!enabledElement) {
    throw new Error('Element has been disabled');
  }
 
  const { viewport } = enabledElement;
 
  const { imageIndex: currentImageIndex, numberOfSlices } = _getImageSliceData(
    viewport,
    debounceLoading
  );
 
  const imageIndexToJump = _getImageIndexToJump(numberOfSlices, imageIndex);
  const delta = imageIndexToJump - currentImageIndex;
 
  scroll(viewport, { delta, debounceLoading, volumeId });
}
 
function _getImageSliceData(
  viewport: IStackViewport | IVolumeViewport,
  debounceLoading?: boolean
): ImageSliceData {
  Iif (viewport instanceof StackViewport) {
    return {
      numberOfSlices: viewport.getImageIds().length,
      imageIndex: debounceLoading
        ? viewport.getTargetImageIdIndex()
        : viewport.getCurrentImageIdIndex(),
    };
  }
  return {
    numberOfSlices: viewport.getNumberOfSlices(),
    imageIndex: viewport.getSliceIndex(),
  };
}
 
function _getImageIndexToJump(
  numberOfSlices: number,
  imageIndex: number
): number {
  const lastSliceIndex = numberOfSlices - 1;
 
  return clip(imageIndex, 0, lastSliceIndex);
}
 
export { jumpToSlice };