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

44.44% Statements 16/36
12.5% Branches 1/8
55.55% Functions 5/9
44.44% Lines 16/36

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                                                  104x 104x   104x   104x           104x                                                                                                             69x 69x 69x 69x             69x             66x       66x                     2x         2x                   68x 68x                    
import {
  getEnabledElement,
  triggerEvent,
  eventTarget,
  getEnabledElementByIds,
} from '@cornerstonejs/core';
import { Events, ChangeTypes } from '../../../enums';
import { Annotation } from '../../../types/AnnotationTypes';
import { getToolGroupsWithToolName } from '../../../store/ToolGroupManager';
import {
  AnnotationAddedEventDetail,
  AnnotationModifiedEventDetail,
  AnnotationCompletedEventDetail,
  ContourAnnotationCompletedEventDetail,
} 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);
      }
    });
  });
 
  const eventType = Events.ANNOTATION_ADDED;
  const eventDetail: AnnotationAddedEventDetail = { annotation };
 
  if (!viewportsToRender.length) {
    triggerEvent(eventTarget, eventType, eventDetail);
    return;
  }
 
  viewportsToRender.forEach(({ renderingEngineId, viewportId }) => {
    eventDetail.viewportId = viewportId;
    eventDetail.renderingEngineId = renderingEngineId;
    triggerEvent(eventTarget, eventType, eventDetail);
  });
}
 
/**
 * Triggers an annotation modified event.
 */
function triggerAnnotationModified(
  annotation: Annotation,
  element: HTMLDivElement,
  changeType = ChangeTypes.HandlesUpdated
): void {
  const enabledElement = getEnabledElement(element);
  const { viewportId, renderingEngineId } = enabledElement;
  const eventType = Events.ANNOTATION_MODIFIED;
  const eventDetail: AnnotationModifiedEventDetail = {
    annotation,
    viewportId,
    renderingEngineId,
    changeType,
  };
 
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
/**
 * Triggers an annotation completed event.
 */
function triggerAnnotationCompleted(annotation: Annotation): void {
  const eventDetail: AnnotationCompletedEventDetail = {
    annotation,
  };
 
  _triggerAnnotationCompleted(eventDetail);
}
 
/**
 * Triggers an annotation completed event for contours (same annotation completed
 * event but with more specific details).
 */
function triggerContourAnnotationCompleted(
  annotation: Annotation,
  contourHoleProcessingEnabled = false
): void {
  const eventDetail: ContourAnnotationCompletedEventDetail = {
    annotation,
    contourHoleProcessingEnabled,
  };
 
  _triggerAnnotationCompleted(eventDetail);
}
 
/**
 * Triggers an annotation completed event for the `detail` provided
 * @param eventDetail - Event detail
 */
function _triggerAnnotationCompleted(
  eventDetail: AnnotationCompletedEventDetail
) {
  const eventType = Events.ANNOTATION_COMPLETED;
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
export {
  triggerAnnotationAddedForElement,
  triggerAnnotationAddedForFOR,
  triggerAnnotationModified,
  triggerAnnotationCompleted,
  triggerContourAnnotationCompleted,
};