<commit_before>import external from './../../externalModules.js';
// State
import { getters, state } from './../../store/index.js';
import { getToolState } from './../../stateManagement/toolState.js';
// Manipulators
import getHandleNearImagePoint from './../../manipulators/getHandleNearImagePoint.js';
import moveAllHandles from './../../manipulators/moveAllHandles.js';
import moveHandle from './../../manipulators/moveHandle.js';
// Util
import isMouseButtonEnabled from './../../util/isMouseButtonEnabled.js';
// Todo: Where should these live?
import getInteractiveToolsForElement from './../../store/getInteractiveToolsForElement.js';
import getToolsWithDataForElement from './../../store/getToolsWithDataForElement.js';

const cornerstone = external.cornerstone;

/**
 * MouseDown is called before MouseDownActivate. If MouseDown
 * finds an existing tool to interact with, it can prevent the
 * event from bubbling to MouseDownActivate.
 *
 * TODO: Set that a tool is active to prevent multiple event fires
 * TODO: Handles should trigger image update when released
 * TODO: Handles should handle delting out of bound data by setting
 *
 * @param {*} evt
 * @returns
 */
export default function (evt) {
  if (state.isToolLocked) {
    return;
  }

  let tools;
  const eventData = evt.detail;
  const element = evt.detail.element;
  const coords = evt.detail.currentPoints.canvas;

  // High level filtering
  tools = getInteractiveToolsForElement(element, getters.mouseTools());
  tools = tools.filter((tool) =>
    isMouseButtonEnabled(eventData.which, tool.options.mouseButtonMask)
  );

  const activeTools = tools.filter((tool) => tool.mode === 'active');

  // If any tools are active, check if they have a special reason for dealing with the event.
  if (activeTools.length > 0) {
    // TODO: If length > 1, you could assess fitness and select the ideal tool
    // TODO: But because we're locking this to 'active' tools, that should rarely be an issue
    const firstActiveTool = activeTools[0];

    if (typeof firstActiveTool.activeMouseDownCallback === 'function') {
      const shouldPreventPropagation = firstActiveTool.activeMouseDownCallback(evt);

      if (shouldPreventPropagation) {
        preventPropagation(evt);

        return;
      }
    }
  }

  // Annotation tool specific
  const annotationTools = getToolsWithDataForElement(element, tools);
  const annotationToolsWithMoveableHandles = getToolsWithMovableHandles(
    element,
    annotationTools,
    coords
  );

  // HANDLES
  if (annotationToolsWithMoveableHandles.length > 0) {
    const firstToolWithMoveableHandles = annotationToolsWithMoveableHandles[0];
    const toolState = getToolState(element, firstToolWithMoveableHandles.name);

    if (typeof firstToolWithMoveableHandles.handleSelectedCallback === 'function') {
      const handle = findHandleNearImagePoint(
        element,
        evt,
        toolState,
        firstToolWithMoveableHandles.name,<commit_msg>Return true to "claim the event" and stop other tools from having their handler triggered.<commit_after>import external from './../../externalModules.js';
// State
import { getters, state } from './../../store/index.js';
import { getToolState } from './../../stateManagement/toolState.js';
// Manipulators
import getHandleNearImagePoint from './../../manipulators/getHandleNearImagePoint.js';
import moveAllHandles from './../../manipulators/moveAllHandles.js';
import moveHandle from './../../manipulators/moveHandle.js';
// Util
import isMouseButtonEnabled from './../../util/isMouseButtonEnabled.js';
// Todo: Where should these live?
import getInteractiveToolsForElement from './../../store/getInteractiveToolsForElement.js';
import getToolsWithDataForElement from './../../store/getToolsWithDataForElement.js';

const cornerstone = external.cornerstone;

/**
 * MouseDown is called before MouseDownActivate. If MouseDown
 * finds an existing tool to interact with, it can prevent the
 * event from bubbling to MouseDownActivate.
 *
 * TODO: Set that a tool is active to prevent multiple event fires
 * TODO: Handles should trigger image update when released
 * TODO: Handles should handle delting out of bound data by setting
 *
 * @param {*} evt
 * @returns
 */
export default function (evt) {
  if (state.isToolLocked) {
    return;
  }

  let tools;
  const eventData = evt.detail;
  const element = evt.detail.element;
  const coords = evt.detail.currentPoints.canvas;

  // High level filtering
  tools = getInteractiveToolsForElement(element, getters.mouseTools());
  tools = tools.filter((tool) =>
    isMouseButtonEnabled(eventData.which, tool.options.mouseButtonMask)
  );

  const activeTools = tools.filter((tool) => tool.mode === 'active');

  // If any tools are active, check if they have a special reason for dealing with the event.
  if (activeTools.length > 0) {
    // TODO: If length > 1, you could assess fitness and select the ideal tool
    // TODO: But because we're locking this to 'active' tools, that should rarely be an issue
    const firstActiveTool = activeTools[0];

    if (typeof firstActiveTool.activeMouseDownCallback === 'function') {
      const claimEvent = firstActiveTool.activeMouseDownCallback(evt);

      if (claimEvent) {
        return;
      }
    }
  }

  // Annotation tool specific
  const annotationTools = getToolsWithDataForElement(element, tools);
  const annotationToolsWithMoveableHandles = getToolsWithMovableHandles(
    element,
    annotationTools,
    coords
  );

  // HANDLES
  if (annotationToolsWithMoveableHandles.length > 0) {
    const firstToolWithMoveableHandles = annotationToolsWithMoveableHandles[0];
    const toolState = getToolState(element, firstToolWithMoveableHandles.name);

    if (typeof firstToolWithMoveableHandles.handleSelectedCallback === 'function') {
      const handle = findHandleNearImagePoint(
        element,
        evt,
        toolState,
        firstToolWithMoveableHandles.name,
{'fraction_non_alphanumeric': 0.10754495042849942, 'fraction_numerical': 0.0016803898504453034, 'mean_word_length': 3.784565916398714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3950339', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1612, 'n_words': 482}