All files / packages/tools/src/utilities/viewportFilters filterViewportsWithToolEnabled.ts

89.47% Statements 17/19
44.44% Branches 4/9
100% Functions 2/2
88.88% Lines 16/18

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          1x                           114x   114x   114x 162x   162x         162x       162x         162x 162x       114x                               162x 162x   162x       162x   162x    
import type { Types } from '@cornerstonejs/core';
import type { IToolGroup } from '../../types';
import { ToolGroupManager } from '../../store';
import { ToolModes } from '../../enums';
 
const { Active, Passive, Enabled } = ToolModes;
 
/**
 * Given an array of viewports, returns a list of viewports that have the the specified tool enabled.
 *
 * @param viewports - An array of viewports.
 * @param toolName - The name of the tool to filter on.
 *
 * @returns A filtered array of viewports.
 */
export default function filterViewportsWithToolEnabled(
  viewports: Array<Types.IViewport>,
  toolName: string
): Array<Types.IStackViewport | Types.IVolumeViewport> {
  const numViewports = viewports.length;
 
  const viewportsWithToolEnabled = [];
 
  for (let vp = 0; vp < numViewports; vp++) {
    const viewport = viewports[vp];
 
    const toolGroup = ToolGroupManager.getToolGroupForViewport(
      viewport.id,
      viewport.renderingEngineId
    );
 
    Iif (!toolGroup) {
      continue;
    }
 
    const hasTool = _toolGroupHasActiveEnabledOrPassiveTool(
      toolGroup,
      toolName
    );
 
    Eif (hasTool) {
      viewportsWithToolEnabled.push(viewport);
    }
  }
 
  return viewportsWithToolEnabled;
}
 
/**
 * Given a toolGroup, return true if it contains the tool with the given `toolName` and it is
 * active, passive or enabled.
 *
 * @param toolGroup - The `toolGroup` to check.
 * @param toolName - The name of the tool.
 *
 * @returns True if the tool is enabled, passive or active in the `toolGroup`.
 */
function _toolGroupHasActiveEnabledOrPassiveTool(
  toolGroup: IToolGroup,
  toolName: string
) {
  const { toolOptions } = toolGroup;
  const tool = toolOptions[toolName];
 
  Iif (!tool) {
    return false;
  }
 
  const toolMode = tool.mode;
 
  return toolMode === Active || toolMode === Passive || toolMode === Enabled;
}