All files / packages/tools/src/eventDispatchers/shared customCallbackHandler.ts

94.73% Statements 18/19
90% Branches 9/10
100% Functions 1/1
94.44% Lines 17/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 74      1x                                           114x 98x     16x 16x         16x                       16x   16x 25x 25x   25x   25x             4x 4x       16x 12x     4x    
import { state, ToolGroupManager } from '../../store';
import ToolModes from '../../enums/ToolModes';
 
const { Active } = ToolModes;
 
/**
 * @function customCallbackHandler This is used as a generic event handler for tool events
 * on viewports. It:
 *
 * - Finds an "active" tool with:
 *    - A matching `handlerType`
 *    - A matching `customFunction` on its tool instance
 *
 * Then calls that custom function with raised event.
 *
 * @param handlerType - 'Mouse' | 'Touch' | 'MouseWheel'
 * @param customFunction - Function name that's expected to live on implementing
 *   (and event handling) active tool ex. 'doubleClickCallback'
 * @param evt
 */
export default function customCallbackHandler(
  handlerType: string,
  customFunction: string,
  evt
) {
  if (state.isInteractingWithTool) {
    return false;
  }
 
  const { renderingEngineId, viewportId } = evt.detail;
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  Iif (!toolGroup) {
    return false;
  }
 
  // TODO: Filter tools by interaction type?
  /**
   * Iterate tool group tools until we find a tool that is:
   * - active
   * - has the custom callback function
   *
   */
  let activeTool;
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const tool = toolGroup.toolOptions[toolName];
    // TODO: Should be getter
    const toolInstance = toolGroup.getToolInstance(toolName);
 
    if (
      // TODO: Should be enum?
      tool.mode === Active &&
      // TODO: Should be implements interface?
      // Weird that we need concrete instance. Other options to filter / get callback?
      typeof toolInstance[customFunction] === 'function'
    ) {
      activeTool = toolGroup.getToolInstance(toolName);
      break;
    }
  }
 
  if (!activeTool) {
    return;
  }
 
  activeTool[customFunction](evt);
}