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

78.94% Statements 15/19
70% Branches 7/10
100% Functions 1/1
77.77% Lines 14/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        428x                                           411x 156x     255x 255x   255x                       255x   255x 1271x 1271x   1271x   1271x                       255x 255x          
import { state } from '../../store/state';
import ToolModes from '../../enums/ToolModes';
import { getToolGroupForViewport } from '../../store/ToolGroupManager';
 
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 = 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);
 
    Iif (
      // 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;
    }
  }
 
  Eif (!activeTool) {
    return;
  }
 
  activeTool[customFunction](evt);
}