All files / tools/src/store addTool.ts

28.57% Statements 4/14
16.66% Branches 2/12
25% Functions 1/4
28.57% Lines 4/14

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                                  1350x   1350x         1350x   1350x                                                                                      
import { state } from './state';
 
/**
 * Adds the tool class to the cornerstoneTools to be used later. This function
 * should be called before creating the toolGroups and adding tools and setting their mode.
 * The flow is:
 * ```
 * - addTool(ToolClass) // where ToolClass is the tool constructor imported from CornerstoneTools or created by a 3rd party
 * - createToolGroup(toolGroupId)
 * - toolGroup.addTool(${toolName}) // NOT THE TOOL CLASS
 * - toolGroup.setToolActive(${toolName})
 *```
 * @param ToolClass - A tool calls to instantiate.
 * @param toolOptions - The tool-specific configuration options for the tool.
 * @returns
 */
export function addTool(ToolClass): void {
  const toolName = ToolClass.toolName;
 
  Iif (!toolName) {
    throw new Error(`No Tool Found for the ToolClass ${ToolClass.name}`);
  }
 
  // Only add the tool if it hasn't been added already
  Eif (!state.tools[toolName]) {
    // Stores the toolNames and ToolClass to be instantiated in the toolGroup on `toolGroup.addTool`
    state.tools[toolName] = {
      toolClass: ToolClass,
    };
  }
}
 
/**
 * Check if a given tool is already registered
 * @param ToolClass - A tool class to check
 * @returns True if the tool is already registered or false otherwise
 */
export function hasTool(ToolClass): boolean {
  const toolName = ToolClass.toolName;
 
  return !!(toolName && state.tools[toolName]);
}
 
export function hasToolByName(toolName: string): boolean {
  return !!(toolName && state.tools[toolName]);
}
 
/**
 * Removes the tool class from the cornerstoneTools.
 *
 * @param ToolClass - A tool calls to instantiate.
 */
export function removeTool(ToolClass): void {
  const toolName = ToolClass.toolName;
 
  if (!toolName) {
    throw new Error(`No tool found for: ${ToolClass.name}`);
  }
 
  if (!state.tools[toolName] !== undefined) {
    delete state.tools[toolName];
  } else {
    throw new Error(
      `${toolName} cannot be removed because it has not been added`
    );
  }
}
 
export default addTool;