All files / packages/tools/src/store removeEnabledElement.ts

86.79% Statements 46/53
50% Branches 3/6
63.63% Functions 7/11
87.23% Lines 41/47

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                                                    1x           4x   4x 4x     4x     4x 4x 4x 4x       4x 4x 4x   4x 4x 4x       4x 4x     4x     1x 4x   4x       4x         1x 4x   4x   4x 4x       1x                             4x 4x 4x   4x       4x 4x 4x 4x               1x 4x 4x     4x 4x          
import { getEnabledElement, Types } from '@cornerstonejs/core';
import {
  mouseEventListeners,
  wheelEventListener,
  touchEventListeners,
  keyEventListener,
} from '../eventListeners';
import {
  imageRenderedEventDispatcher,
  cameraModifiedEventDispatcher,
  mouseToolEventDispatcher,
  keyboardToolEventDispatcher,
  imageSpacingCalibratedEventDispatcher,
  touchToolEventDispatcher,
} from '../eventDispatchers';
// ~~
 
import filterToolsWithAnnotationsForElement from './filterToolsWithAnnotationsForElement';
import { state } from './state';
import getToolsWithModesForElement from '../utilities/getToolsWithModesForElement';
import { ToolModes } from '../enums';
import { removeAnnotation } from '../stateManagement';
import getSynchronizersForViewport from './SynchronizerManager/getSynchronizersForViewport';
import getToolGroupForViewport from './ToolGroupManager/getToolGroupForViewport';
import { annotationRenderingEngine } from '../utilities/triggerAnnotationRender';
 
const VIEWPORT_ELEMENT = 'viewport-element';
 
function removeEnabledElement(
  elementDisabledEvt: Types.EventTypes.ElementDisabledEvent
): void {
  // Is DOM element
  const { element, viewportId } = elementDisabledEvt.detail;
 
  _resetSvgNodeCache(element);
  _removeSvgNode(element);
 
  // Remove this element from the annotation rendering engine
  annotationRenderingEngine.removeViewportElement(viewportId, element);
 
  // Listeners
  mouseEventListeners.disable(element);
  wheelEventListener.disable(element);
  touchEventListeners.disable(element);
  keyEventListener.disable(element);
  // labelmap
 
  // Dispatchers: renderer
  imageRenderedEventDispatcher.disable(element);
  cameraModifiedEventDispatcher.disable(element);
  imageSpacingCalibratedEventDispatcher.disable(element);
  // Dispatchers: interaction
  mouseToolEventDispatcher.disable(element);
  keyboardToolEventDispatcher.disable(element);
  touchToolEventDispatcher.disable(element);
 
  // State
  // @TODO: We used to "disable" the tool before removal. Should we preserve the hook that would call on tools?
  _removeViewportFromSynchronizers(element);
  _removeViewportFromToolGroup(element);
 
  // _removeAllToolsForElement(canvas)
  _removeEnabledElement(element);
}
 
const _removeViewportFromSynchronizers = (element: HTMLDivElement) => {
  const enabledElement = getEnabledElement(element);
 
  const synchronizers = getSynchronizersForViewport(
    enabledElement.viewportId,
    enabledElement.renderingEngineId
  );
  synchronizers.forEach((sync) => {
    sync.remove(enabledElement);
  });
};
 
const _removeViewportFromToolGroup = (element: HTMLDivElement) => {
  const { renderingEngineId, viewportId } = getEnabledElement(element);
 
  const toolGroup = getToolGroupForViewport(viewportId, renderingEngineId);
 
  Eif (toolGroup) {
    toolGroup.removeViewports(renderingEngineId, viewportId);
  }
};
 
const _removeAllToolsForElement = function (element) {
  const tools = getToolsWithModesForElement(element, [
    ToolModes.Active,
    ToolModes.Passive,
  ]);
 
  const toolsWithData = filterToolsWithAnnotationsForElement(element, tools);
  toolsWithData.forEach(({ annotations }) => {
    annotations.forEach((annotation) => {
      removeAnnotation(annotation.annotationUID, element);
    });
  });
};
 
function _resetSvgNodeCache(element: HTMLDivElement) {
  const { viewportUid: viewportId, renderingEngineUid: renderingEngineId } =
    element.dataset;
  const elementHash = `${viewportId}:${renderingEngineId}`;
 
  delete state.svgNodeCache[elementHash];
}
 
function _removeSvgNode(element: HTMLDivElement) {
  const internalViewportNode = element.querySelector(`div.${VIEWPORT_ELEMENT}`);
  const svgLayer = internalViewportNode.querySelector('svg');
  Eif (svgLayer) {
    internalViewportNode.removeChild(svgLayer);
  }
}
 
/**
 * @private
 * @param enabledElement
 */
const _removeEnabledElement = function (element: HTMLDivElement) {
  const foundElementIndex = state.enabledElements.findIndex(
    (el) => el === element
  );
 
  Eif (foundElementIndex > -1) {
    state.enabledElements.splice(foundElementIndex, 1);
  }
};
 
export default removeEnabledElement;