All files / packages/tools/src/stateManagement/annotation/helpers state.ts

22.22% Statements 6/27
0% Branches 0/6
20% Functions 1/5
22.72% Lines 5/22

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                                        67x 67x   67x   67x           67x                                                                                                        
import {
  getEnabledElement,
  triggerEvent,
  eventTarget,
  getEnabledElementByIds,
} from '@cornerstonejs/core';
import { Events } from '../../../enums';
import { Annotation } from '../../../types/AnnotationTypes';
import { getToolGroupsWithToolName } from '../../../store/ToolGroupManager';
import { AnnotationAddedEventDetail } from '../../../types/EventTypes';
 
/**
 * It triggers an event for the element when an annotation is added
 * @param annotation - Annotation - The annotation that was added.
 * @param element - The element that the annotation was added to.
 */
function triggerAnnotationAddedForElement(
  annotation: Annotation,
  element: HTMLDivElement
) {
  const enabledElement = getEnabledElement(element);
  const { renderingEngine, viewportId } = enabledElement;
 
  const eventType = Events.ANNOTATION_ADDED;
 
  const eventDetail: AnnotationAddedEventDetail = {
    annotation,
    viewportId,
    renderingEngineId: renderingEngine.id,
  };
 
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
/**
 * If the annotation has a FrameOfReferenceUID, it triggers the ANNOTATION_ADDED
 * event for all the viewports that has the same FrameOfReferenceUID.
 * @param annotation -  Annotation - The annotation that was added
 */
function triggerAnnotationAddedForFOR(annotation: Annotation) {
  const { toolName } = annotation.metadata;
 
  const toolGroups = getToolGroupsWithToolName(toolName);
 
  if (!toolGroups.length) {
    return;
  }
 
  // Find the viewports in the toolGroups who has the same FrameOfReferenceUID
  const viewportsToRender = [];
 
  toolGroups.forEach((toolGroup) => {
    toolGroup.viewportsInfo.forEach((viewportInfo) => {
      const { renderingEngineId, viewportId } = viewportInfo;
      const { FrameOfReferenceUID } = getEnabledElementByIds(
        viewportId,
        renderingEngineId
      );
 
      if (annotation.metadata.FrameOfReferenceUID === FrameOfReferenceUID) {
        viewportsToRender.push(viewportInfo);
      }
    });
  });
 
  if (!viewportsToRender.length) {
    return;
  }
 
  const eventType = Events.ANNOTATION_ADDED;
 
  viewportsToRender.forEach(({ renderingEngineId, viewportId }) => {
    const eventDetail: AnnotationAddedEventDetail = {
      annotation,
      viewportId,
      renderingEngineId,
    };
 
    triggerEvent(eventTarget, eventType, eventDetail);
  });
}
 
export { triggerAnnotationAddedForElement, triggerAnnotationAddedForFOR };