All files / tools/src/utilities getToolsWithModesForElement.ts

94.11% Statements 16/17
83.33% Branches 5/6
100% Functions 1/1
93.75% Lines 15/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                                      8816x 8816x   8816x   8816x 716x     8100x   8100x   8100x 29965x 29965x     29965x       29965x 28551x 28551x       8100x    
import type { ToolModes } from '../enums';
import { getEnabledElement } from '@cornerstonejs/core';
import { getToolGroupForViewport } from '../store/ToolGroupManager';
 
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 = getToolGroupForViewport(viewportId, renderingEngineId);
 
  if (!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;
    }
 
    if (modesFilter.includes(toolOptions.mode)) {
      const toolInstance = toolGroup.getToolInstance(toolName);
      enabledTools.push(toolInstance);
    }
  }
 
  return enabledTools;
}