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

42.1% Statements 16/38
33.33% Branches 4/12
50% Functions 5/10
42.1% Lines 16/38

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                                                    164x 164x   164x   164x           164x                                                                                                           72x 72x                         61x 61x 61x 61x             61x             16x       16x                                                   16x 16x                      
import {
  getEnabledElement,
  triggerEvent,
  eventTarget,
  getEnabledElementByIds,
} from '@cornerstonejs/core';
import { Events, ChangeTypes } from '../../../enums';
import type { Annotation } from '../../../types/AnnotationTypes';
import { getToolGroupsWithToolName } from '../../../store/ToolGroupManager';
import type {
  AnnotationAddedEventDetail,
  AnnotationModifiedEventDetail,
  AnnotationCompletedEventDetail,
  ContourAnnotationCompletedEventDetail,
  AnnotationRemovedEventDetail,
} 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 removed event.
 * @param eventDetail - Event detail
 */
function triggerAnnotationRemoved(
  eventDetail: AnnotationRemovedEventDetail
): void {
  const eventType = Events.ANNOTATION_REMOVED;
  triggerEvent(eventTarget, eventType, eventDetail);
}
 
/**
 * Triggers an annotation modified event.
 * Note this no longer requires the element, which should be handled by testing
 * to see which viewports this element is shown on.
 */
function triggerAnnotationModified(
  annotation: Annotation,
  element?: HTMLDivElement,
  changeType = ChangeTypes.HandlesUpdated
): void {
  const enabledElement = element && 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,
  triggerAnnotationRemoved,
  triggerAnnotationModified,
  triggerAnnotationCompleted,
  triggerContourAnnotationCompleted,
};