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

87.87% Statements 29/33
50% Branches 4/8
85.71% Functions 6/7
87.87% Lines 29/33

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 165 166 167                                          147x                                   533x                             230x   230x 230x   230x                     62x   62x 62x     62x   62x 62x 62x   62x   62x           62x                       67x 67x 67x     67x     67x 8x     59x     59x   59x         59x                         174x 174x   174x                                                    
import {
  getEnabledElement,
  triggerEvent,
  eventTarget,
  utilities as csUtils,
} from '@cornerstonejs/core';
import { Events } from '../../enums';
import { Types } from '@cornerstonejs/core';
import { defaultFrameOfReferenceSpecificAnnotationManager } from './FrameOfReferenceSpecificAnnotationManager';
import { Annotations, Annotation } from '../../types/AnnotationTypes';
 
import {
  AnnotationAddedEventDetail,
  AnnotationRemovedEventDetail,
} from '../../types/EventTypes';
 
/**
 * It returns the default annotations manager.
 * @returns the singleton default annotations manager.
 */
function getDefaultAnnotationManager() {
  return defaultFrameOfReferenceSpecificAnnotationManager;
}
 
/**
 * Given an element, return the FrameOfReferenceSpecificStateManager for that
 * element
 * @param element - The element that the state manager is managing the state of.
 * @returns The default state manager
 */
function getViewportSpecificAnnotationManager(
  element?: Types.IEnabledElement | HTMLDivElement
) {
  // TODO:
  // We may want multiple FrameOfReferenceSpecificStateManagers.
  // E.g. displaying two different radiologists annotations on the same underlying data/FoR.
 
  // Just return the default for now.
 
  return defaultFrameOfReferenceSpecificAnnotationManager;
}
 
/**
 * Returns the annotations for the `FrameOfReference` of the `Viewport`
 * being viewed by the cornerstone3D enabled `element`.
 *
 * @param element - The HTML element.
 * @param toolName - The name of the tool.
 * @returns The annotations corresponding to the Frame of Reference and the toolName.
 */
function getAnnotations(
  element: HTMLDivElement,
  toolName: string
): Annotations {
  const enabledElement = getEnabledElement(element);
  const annotationManager =
    getViewportSpecificAnnotationManager(enabledElement);
  const { FrameOfReferenceUID } = enabledElement;
 
  return annotationManager.get(FrameOfReferenceUID, toolName);
}
 
/**
 * Add the annotation to the annotations for the `FrameOfReference` of the `Viewport`
 * being viewed by the cornerstone3D enabled `element`.
 *
 * @param element - HTMLDivElement
 * @param annotation - The annotation that is being added to the annotations manager.
 */
function addAnnotation(element: HTMLDivElement, annotation: Annotation): void {
  const annotationManager = getViewportSpecificAnnotationManager(element);
 
  Eif (annotation.annotationUID === undefined) {
    annotation.annotationUID = csUtils.uuidv4() as string;
  }
 
  annotationManager.addAnnotation(annotation);
 
  const enabledElement = getEnabledElement(element);
  const { renderingEngine } = enabledElement;
  const { viewportId } = enabledElement;
 
  const eventType = Events.ANNOTATION_ADDED;
 
  const eventDetail: AnnotationAddedEventDetail = {
    annotation,
    viewportId,
    renderingEngineId: renderingEngine.id,
  };
 
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
/**
 * Remove the annotation by UID of the annotation.
 * @param element - HTMLDivElement
 * @param annotationUID - The unique identifier for the annotation.
 */
function removeAnnotation(
  annotationUID: string,
  element?: HTMLDivElement
): void {
  let annotationManager = getDefaultAnnotationManager();
  Eif (element) {
    annotationManager = getViewportSpecificAnnotationManager(element);
  }
 
  const annotation = annotationManager.getAnnotation(annotationUID);
 
  // no need to continue in case there is no annotation.
  if (!annotation) {
    return;
  }
 
  annotationManager.removeAnnotation(annotationUID);
 
  // trigger annotation removed
  const eventType = Events.ANNOTATION_REMOVED;
 
  const eventDetail: AnnotationRemovedEventDetail = {
    annotation,
    annotationManagerUID: annotationManager.uid,
  };
 
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
/**
 * Get the Annotation object by its UID
 * @param annotationUID - The unique identifier of the annotation.
 * @param element - The element that the tool is being used on.
 * @returns A Annotation object
 */
function getAnnotation(
  annotationUID: string,
  element?: HTMLDivElement
): Annotation {
  const annotationManager = getViewportSpecificAnnotationManager(element);
  const annotation = annotationManager.getAnnotation(annotationUID);
 
  return annotation;
}
 
/**
 * It removes all annotations from the default annotation manager
 * @param element - Optional element to get the annotation manager from, if not
 * specified it will use the default annotation manager.
 */
function removeAllAnnotations(element?: HTMLDivElement): void {
  let annotationManager = getDefaultAnnotationManager();
  if (element) {
    annotationManager = getViewportSpecificAnnotationManager(element);
  }
 
  annotationManager.removeAllAnnotations();
}
 
export {
  getAnnotations,
  addAnnotation,
  getAnnotation,
  removeAnnotation,
  removeAllAnnotations,
  getViewportSpecificAnnotationManager,
  getDefaultAnnotationManager,
};