All files / tools/src/store addEnabledElement.ts

100% Statements 56/56
100% Branches 0/0
100% Functions 4/4
100% Lines 56/56

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                                                          702x 702x     702x 702x     702x     702x 702x 702x 702x 702x     702x 702x 702x 702x   702x 702x 702x       702x             702x 702x   702x 702x 702x 702x 702x 702x 702x 702x         702x 702x 702x 702x 702x     702x 702x     702x 702x 702x 702x     702x 702x 702x 702x           702x 702x 702x   702x 702x 702x 702x 702x   702x         702x 702x       702x                       702x    
import type { Types } from '@cornerstonejs/core';
import {
  mouseEventListeners,
  wheelEventListener,
  touchEventListeners,
  keyEventListener,
  imageChangeEventListener,
} from '../eventListeners';
import {
  imageRenderedEventDispatcher,
  cameraModifiedEventDispatcher,
  mouseToolEventDispatcher,
  touchToolEventDispatcher,
  keyboardToolEventDispatcher,
  imageSpacingCalibratedEventDispatcher,
  cameraResetEventDispatcher,
} from '../eventDispatchers';
import { state } from './state';
import { annotationRenderingEngine } from '../stateManagement/annotation/AnnotationRenderingEngine';
 
/**
 * When an element is "enabled", add event listeners and dispatchers to it
 * so we can use interactions to affect tool behaviors
 *
 * @param evt - The ELEMENT_ENABLED event
 */
export default function addEnabledElement(
  evt: Types.EventTypes.ElementEnabledEvent
): void {
  const { element, viewportId } = evt.detail;
  const svgLayer = _createSvgAnnotationLayer(viewportId);
 
  // Reset/Create svgNodeCache for element
  _setSvgNodeCache(element);
  _appendChild(svgLayer, element);
 
  // Add this element to the annotation rendering engine
  annotationRenderingEngine.addViewportElement(viewportId, element);
 
  // Listeners
  mouseEventListeners.enable(element);
  wheelEventListener.enable(element);
  touchEventListeners.enable(element);
  keyEventListener.enable(element);
  imageChangeEventListener.enable(element);
 
  // Dispatchers: renderer
  imageRenderedEventDispatcher.enable(element);
  cameraModifiedEventDispatcher.enable(element);
  imageSpacingCalibratedEventDispatcher.enable(element);
  cameraResetEventDispatcher.enable(element);
  // Dispatchers: interaction
  mouseToolEventDispatcher.enable(element);
  keyboardToolEventDispatcher.enable(element);
  touchToolEventDispatcher.enable(element);
 
  // labelmap
  // State
  state.enabledElements.push(element);
}
 
/**
 *
 */
function _createSvgAnnotationLayer(viewportId: string): SVGElement {
  const svgns = 'http://www.w3.org/2000/svg';
  const svgLayer = document.createElementNS(svgns, 'svg');
 
  const svgLayerId = `svg-layer-${viewportId}`;
  svgLayer.classList.add('svg-layer');
  svgLayer.setAttribute('id', svgLayerId);
  svgLayer.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  svgLayer.style.width = '100%';
  svgLayer.style.height = '100%';
  svgLayer.style.pointerEvents = 'none';
  svgLayer.style.position = 'absolute';
  // TODO: we should test this on high-res monitors
  //svgLayer.style.textRendering = 'optimizeSpeed'
 
  // Single dropshadow config for now
  const defs = document.createElementNS(svgns, 'defs');
  const filter = document.createElementNS(svgns, 'filter');
  const feOffset = document.createElementNS(svgns, 'feOffset');
  const feColorMatrix = document.createElementNS(svgns, 'feColorMatrix');
  const feBlend = document.createElementNS(svgns, 'feBlend');
 
  //
  filter.setAttribute('id', `shadow-${svgLayerId}`);
  filter.setAttribute('filterUnits', 'userSpaceOnUse');
 
  //
  feOffset.setAttribute('result', 'offOut');
  feOffset.setAttribute('in', 'SourceGraphic');
  feOffset.setAttribute('dx', '0.5');
  feOffset.setAttribute('dy', '0.5');
 
  //
  feColorMatrix.setAttribute('result', 'matrixOut');
  feColorMatrix.setAttribute('in', 'offOut');
  feColorMatrix.setAttribute('in2', 'matrix');
  feColorMatrix.setAttribute(
    'values',
    '0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0'
  );
 
  //
  feBlend.setAttribute('in', 'SourceGraphic');
  feBlend.setAttribute('in2', 'matrixOut');
  feBlend.setAttribute('mode', 'normal');
 
  filter.appendChild(feOffset);
  filter.appendChild(feColorMatrix);
  filter.appendChild(feBlend);
  defs.appendChild(filter);
  svgLayer.appendChild(defs);
 
  return svgLayer;
}
 
function _setSvgNodeCache(element) {
  const { viewportUid: viewportId, renderingEngineUid: renderingEngineId } =
    element.dataset;
  const elementHash = `${viewportId}:${renderingEngineId}`;
 
  // Create or reset
  // TODO: If... Reset, we should blow out any nodes in DOM
  state.svgNodeCache[elementHash] = {};
}
 
/**
 *
 * @param newNode
 * @param referenceNode
 */
function _appendChild(
  newNode: SVGElement,
  referenceNode: HTMLDivElement
): void {
  referenceNode.querySelector('div.viewport-element').appendChild(newNode);
}