All files / packages/tools/src init.ts

97.61% Statements 41/42
50% Branches 3/6
100% Functions 6/6
97.61% Lines 41/42

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                          1x               79x 79x       79x 79x           79x   79x                 79x 79x   79x       79x     79x     79x 79x   79x 79x 79x                     79x   79x 79x   79x 79x                 158x 158x   158x 158x                 79x         79x         79x         79x               79x         79x       79x         79x                         158x         158x         158x                 158x         158x       158x         158x              
import { eventTarget, Enums } from '@cornerstonejs/core';
import { getDefaultAnnotationManager } from './stateManagement/annotation/annotationState';
import { getDefaultSegmentationStateManager } from './stateManagement/segmentation/segmentationState';
import { Events as TOOLS_EVENTS } from './enums';
import { addEnabledElement, removeEnabledElement } from './store';
import { resetCornerstoneToolsState } from './store/state';
import {
  annotationSelectionListener,
  segmentationDataModifiedEventListener,
  segmentationRepresentationModifiedEventListener,
  segmentationRepresentationRemovedEventListener,
  segmentationModifiedListener,
  annotationModifiedListener,
} from './eventListeners';
 
import * as ToolGroupManager from './store/ToolGroupManager';
import {
  addIgnoreDoubleClickCaptureListener,
  removeIgnoreDoubleClickCaptureListener,
} from './eventListeners/mouse/mouseDownListener';
 
let csToolsInitialized = false;
 
/**
 * Initialize the cornerstoneTools package. It will add event listeners for mouse
 * and keyboard events.
 * @param defaultConfiguration - A configuration object that will be used to
 * initialize the tool.
 */
export function init(defaultConfiguration = {}): void {
  Iif (csToolsInitialized) {
    return;
  }
 
  _addCornerstoneEventListeners();
  _addCornerstoneToolsEventListeners();
 
  // A separate double click listener at the document root. Separate
  // from the {@link mouseDoubleClickListener} because...
  // - it listens on the capture phase (and not the typical bubble phase)
  // - the data used to ignore the double click is private to the mouseDownListener module
  addIgnoreDoubleClickCaptureListener();
 
  csToolsInitialized = true;
}
 
/**
 * It destroys and cleanup state for cornerstone3DTools. It removes all the tools
 * that were added to the tool groups and restore states. It also removes all
 * event listeners.
 */
export function destroy(): void {
  _removeCornerstoneEventListeners();
  _removeCornerstoneToolsEventListeners();
 
  removeIgnoreDoubleClickCaptureListener();
 
  // Important: destroy ToolGroups first, in order for cleanup to work correctly for the
  // added tools.
  ToolGroupManager.destroy();
 
  // Remove all tools
  resetCornerstoneToolsState();
 
  // remove all annotation.
  const annotationManager = getDefaultAnnotationManager();
  const segmentationStateManager = getDefaultSegmentationStateManager();
 
  annotationManager.restoreAnnotations({});
  segmentationStateManager.resetState();
  csToolsInitialized = false;
}
 
/**
 * Wires up event listeners for the Cornerstone#ElementDisabled and
 * Cornerstone#ElementEnabled events.
 *
 * @internal
 */
function _addCornerstoneEventListeners(): void {
  // Clear any listeners that may already be set
  _removeCornerstoneEventListeners();
 
  const elementEnabledEvent = Enums.Events.ELEMENT_ENABLED;
  const elementDisabledEvent = Enums.Events.ELEMENT_DISABLED;
 
  eventTarget.addEventListener(elementEnabledEvent, addEnabledElement);
  eventTarget.addEventListener(elementDisabledEvent, removeEnabledElement);
}
 
/**
 * Removes event listeners for the Cornerstone#ElementDisabled and
 * Cornerstone#ElementEnabled events.
 *
 */
function _removeCornerstoneEventListeners(): void {
  const elementEnabledEvent = Enums.Events.ELEMENT_ENABLED;
  const elementDisabledEvent = Enums.Events.ELEMENT_DISABLED;
 
  eventTarget.removeEventListener(elementEnabledEvent, addEnabledElement);
  eventTarget.removeEventListener(elementDisabledEvent, removeEnabledElement);
}
 
/**
 * It adds an event listener to the event target (the cornerstoneTools object) for
 * the annotation selected and annotation modified events.
 */
function _addCornerstoneToolsEventListeners() {
  // Clear any listeners that may already be set
  _removeCornerstoneToolsEventListeners();
 
  /**
   * Annotation
   */
  eventTarget.addEventListener(
    TOOLS_EVENTS.ANNOTATION_MODIFIED,
    annotationModifiedListener
  );
 
  eventTarget.addEventListener(
    TOOLS_EVENTS.ANNOTATION_SELECTION_CHANGE,
    annotationSelectionListener
  );
 
  eventTarget.addEventListener(
    TOOLS_EVENTS.ANNOTATION_SELECTION_CHANGE,
    annotationSelectionListener
  );
 
  /**
   * Segmentation
   */
  eventTarget.addEventListener(
    TOOLS_EVENTS.SEGMENTATION_MODIFIED,
    segmentationModifiedListener
  );
 
  eventTarget.addEventListener(
    TOOLS_EVENTS.SEGMENTATION_DATA_MODIFIED,
    segmentationDataModifiedEventListener
  );
  eventTarget.addEventListener(
    TOOLS_EVENTS.SEGMENTATION_REPRESENTATION_MODIFIED,
    segmentationRepresentationModifiedEventListener
  );
 
  eventTarget.addEventListener(
    TOOLS_EVENTS.SEGMENTATION_REPRESENTATION_REMOVED,
    segmentationRepresentationRemovedEventListener
  );
}
 
/**
 * Remove the event listener for the the annotation selected and annotation modified events.
 */
function _removeCornerstoneToolsEventListeners() {
  /**
   * Annotation
   */
  eventTarget.removeEventListener(
    TOOLS_EVENTS.ANNOTATION_MODIFIED,
    annotationModifiedListener
  );
 
  eventTarget.removeEventListener(
    TOOLS_EVENTS.ANNOTATION_SELECTION_CHANGE,
    annotationSelectionListener
  );
 
  eventTarget.removeEventListener(
    TOOLS_EVENTS.ANNOTATION_SELECTION_CHANGE,
    annotationSelectionListener
  );
 
  /**
   * Segmentation
   */
 
  eventTarget.removeEventListener(
    TOOLS_EVENTS.SEGMENTATION_MODIFIED,
    segmentationModifiedListener
  );
 
  eventTarget.removeEventListener(
    TOOLS_EVENTS.SEGMENTATION_DATA_MODIFIED,
    segmentationDataModifiedEventListener
  );
  eventTarget.removeEventListener(
    TOOLS_EVENTS.SEGMENTATION_REPRESENTATION_MODIFIED,
    segmentationRepresentationModifiedEventListener
  );
 
  eventTarget.removeEventListener(
    TOOLS_EVENTS.SEGMENTATION_REPRESENTATION_REMOVED,
    segmentationRepresentationRemovedEventListener
  );
}
 
export default init;