All files / packages/tools/src/stateManagement/annotation annotationSelection.ts

64.7% Statements 22/34
60.71% Branches 17/28
61.53% Functions 8/13
62.5% Lines 20/32

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                  1x                                         75x 75x 75x                                   75x 75x 75x 75x   75x 75x 75x   75x                                                                                               119x                               75x                     75x 74x 74x                 75x 75x 75x                                
import { eventTarget, triggerEvent } from '@cornerstonejs/core';
import { Events } from '../../enums';
import { AnnotationSelectionChangeEventDetail } from '../../types/EventTypes';
import { getAnnotation } from './annotationState';
 
/*
 * Constants
 */
 
const selectedAnnotationUIDs: Set<string> = new Set();
 
/*
 * Interface (Public API)
 */
 
/**
 * Set a given annotationUID as selected or deselected based on the provided
 * selected value.
 *
 * @param annotationUID - The annotation UID to be selected
 * @param selected - When true, the annotation is selected. When false, the annotation is deselected.
 * @param preserveSelected - When true, preserves existing
 *  selections (i.e., the given annotation is appended to the selection set).
 *  When false (the default behavior) the currently selected items are discarded
 *  (i.e., the given annotation instance replaces the currently selected ones).
 */
function setAnnotationSelected(
  annotationUID: string,
  selected = true,
  preserveSelected = false
): void {
  Eif (selected) {
    selectAnnotation(annotationUID, preserveSelected);
  } else {
    deselectAnnotation(annotationUID);
  }
}
 
/**
 * Set a given annotation as selected.
 *
 * @param annotationUID - The annotation UID to be selected
 * @param preserveSelected - When true, preserves existing
 *  selections (i.e., the given annotation is appended to the selection set).
 *  When false (the default behavior) the currently selected items are discarded
 *  (i.e., the given annotation instance replaces the currently selected ones).
 */
function selectAnnotation(
  annotationUID: string,
  preserveSelected = false
): void {
  const detail = makeEventDetail();
  Eif (!preserveSelected) {
    clearSelectionSet(selectedAnnotationUIDs, detail);
  }
  Eif (annotationUID && !selectedAnnotationUIDs.has(annotationUID)) {
    selectedAnnotationUIDs.add(annotationUID);
    detail.added.push(annotationUID);
  }
  publish(detail, selectedAnnotationUIDs);
}
 
/**
 * Deselect one or all annotations.
 *
 * @param annotationUID - If an annotation is provided that instance will be removed from
 * the internal selection set. If none is given, ALL selections will be cleared.
 */
function deselectAnnotation(annotationUID?: string): void {
  const detail = makeEventDetail();
  if (annotationUID) {
    if (selectedAnnotationUIDs.delete(annotationUID)) {
      detail.removed.push(annotationUID);
    }
  } else {
    clearSelectionSet(selectedAnnotationUIDs, detail);
  }
  publish(detail, selectedAnnotationUIDs);
}
 
/**
 * Return an array of ALL the selected annotationUIDs
 * @returns An array of Annotation UIDs
 */
function getAnnotationsSelected(): Array<string> {
  return Array.from(selectedAnnotationUIDs);
}
 
/**
 * Given a tool name, return ALL the annotationUIDs for that tool that are selected
 * @param toolName - The name of the tool you want to get the selected annotation for
 * @returns An array of annotationUIDs
 */
function getAnnotationsSelectedByToolName(toolName: string): Array<string> {
  return getAnnotationsSelected().filter((annotationUID) => {
    const annotation = getAnnotation(annotationUID);
    return annotation.metadata.toolName === toolName;
  });
}
 
/**
 * Given an annotationUID, return true if it is selected, false
 * otherwise.
 * @param annotationUID - Annotation UID
 * @returns A boolean value.
 */
function isAnnotationSelected(annotationUID: string): boolean {
  return selectedAnnotationUIDs.has(annotationUID);
}
 
/**
 * Return the number of the selected annotation
 * @returns The size of the selected annotation set
 */
function getAnnotationsSelectedCount(): number {
  return selectedAnnotationUIDs.size;
}
 
/*
 * Private Helpers
 */
 
function makeEventDetail(): AnnotationSelectionChangeEventDetail {
  return Object.freeze({
    added: [],
    removed: [],
    selection: [],
  });
}
 
function clearSelectionSet(
  selectionSet: Set<string>,
  detail: AnnotationSelectionChangeEventDetail
): void {
  selectionSet.forEach((value) => {
    Eif (selectionSet.delete(value)) {
      detail.removed.push(value);
    }
  });
}
 
function publish(
  detail: AnnotationSelectionChangeEventDetail,
  selectionSet: Set<string>
) {
  Eif (detail.added.length > 0 || detail.removed.length > 0) {
    selectionSet.forEach((item) => void detail.selection.push(item));
    triggerEvent(eventTarget, Events.ANNOTATION_SELECTION_CHANGE, detail);
  }
}
 
/*
 * Exports
 */
 
export {
  setAnnotationSelected,
  getAnnotationsSelected,
  getAnnotationsSelectedByToolName,
  getAnnotationsSelectedCount,
  deselectAnnotation,
  isAnnotationSelected,
};