All files / extensions/cornerstone/src/utils segmentUtils.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 2/2
100% Lines 17/17

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    204x                         10x   10x   10x     10x 10x   10x 23x     10x     10x 1x 9x 1x         10x   10x 10x 10x    
import SegmentationServiceType from '../services/SegmentationService';
 
export const handleSegmentChange = ({
  direction,
  segmentationId,
  viewportId,
  selectedSegmentObjectIndex,
  segmentationService,
}: {
  direction: number;
  segmentationId: string;
  viewportId: string;
  selectedSegmentObjectIndex: number;
  segmentationService: SegmentationServiceType;
}) => {
  const segmentation = segmentationService.getSegmentation(segmentationId);
 
  const { segments } = segmentation;
 
  const numberOfSegments = Object.keys(segments).length;
 
  // Get activeSegment each time because the user can select any segment from the list and thus the index should be updated
  const activeSegment = segmentationService.getActiveSegment(viewportId);
  if (activeSegment) {
    // from the activeSegment get the actual object array index to be used
    selectedSegmentObjectIndex = Object.values(segments).findIndex(
      segment => segment.segmentIndex === activeSegment.segmentIndex
    );
  }
  let newSelectedSegmentIndex = selectedSegmentObjectIndex + direction;
 
  // Handle looping through list of segments
  if (newSelectedSegmentIndex > numberOfSegments - 1) {
    newSelectedSegmentIndex = 0;
  } else if (newSelectedSegmentIndex < 0) {
    newSelectedSegmentIndex = numberOfSegments - 1;
  }
 
  // Convert segmentationId from object array index to property value of type Segment
  // Functions below use the segmentIndex object attribute so we have to do the conversion
  const segmentIndex = Object.values(segments)[newSelectedSegmentIndex]?.segmentIndex;
 
  segmentationService.setActiveSegment(segmentationId, segmentIndex);
  segmentationService.jumpToSegmentNext(segmentationId, segmentIndex, undefined, direction);
  selectedSegmentObjectIndex = newSelectedSegmentIndex;
};