All files / packages/tools/src/utilities getToolsWithModesForElement.ts

88.23% Statements 15/17
50% Branches 3/6
100% Functions 1/1
87.5% Lines 14/16

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                                      149x 149x   149x         149x       149x   149x   149x 181x 181x     181x       181x 181x 181x       149x    
import { ToolGroupManager } from '../store';
import { ToolModes } from '../enums';
import { getEnabledElement } from '@cornerstonejs/core';
 
type ModesFilter = Array<ToolModes>;
 
/**
 * Finds the enabled element, and iterates over the tools inside its
 * toolGroup. Returns the list of tool instances that are valid based
 * on the provided tool mode.
 *
 * @param element Canvas element
 * @param modesFilter tool modes: active, passive, enabled, disabled
 * @returns enabled tool instances
 */
export default function getToolsWithModesForElement(
  element: HTMLDivElement,
  modesFilter: ModesFilter
) {
  const enabledElement = getEnabledElement(element);
  const { renderingEngineId, viewportId } = enabledElement;
 
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  Iif (!toolGroup) {
    return [];
  }
 
  const enabledTools = [];
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const toolOptions = toolGroup.toolOptions[toolName];
 
    /* filter out tools that don't have options */
    Iif (!toolOptions) {
      continue;
    }
 
    Eif (modesFilter.includes(toolOptions.mode)) {
      const toolInstance = toolGroup.getToolInstance(toolName);
      enabledTools.push(toolInstance);
    }
  }
 
  return enabledTools;
}