All files / packages/tools/src/store addTool.ts

33.33% Statements 5/15
20% Branches 2/10
33.33% Functions 1/3
33.33% Lines 5/15

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                                  119x 119x   119x       119x         119x                                                                            
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 {
  // Check if tool exists and name is not undefined
  const toolName = ToolClass.toolName;
  const toolAlreadyAdded = state.tools[toolName] !== undefined;
 
  Iif (!toolName) {
    throw new Error(`No Tool Found for the ToolClass ${ToolClass.name}`);
  }
 
  Iif (toolAlreadyAdded) {
    throw new Error(`${toolName} has already been added globally`);
  }
 
  // 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 alredy registered or false otherwise
 */
export function hasTool(ToolClass): boolean {
  const toolName = ToolClass.toolName;
 
  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;