<commit_before>// If (anyHandlesOutsideImage(evt.detail, measurementData.handles)) {
//   // Delete the measurement
//   RemoveToolState(element, this.name, measurementData);
// }

import EVENTS from '../events.js';
import external from '../externalModules.js';
import triggerEvent from '../util/triggerEvent.js';
import { clipToBox } from '../util/clip.js';

const _moveEvents = {
  mouse: [EVENTS.MOUSE_MOVE, EVENTS.MOUSE_DRAG],
  touch: [EVENTS.TOUCH_DRAG],
};

const _moveEndEvents = {
  mouse: [EVENTS.MOUSE_UP, EVENTS.MOUSE_CLICK],
  touch: [EVENTS.TOUCH_END, EVENTS.TOUCH_PINCH, EVENTS.TAP],
};

/**
 * Move a new handle
 * @public
 * @method moveNewHandle
 * @memberof Manipulators
 *
 * @param {*} evtDetail
 * @param {*} toolName
 * @param {*} toolData
 * @param {*} handle
 * @param {*} [options={}]
 * @param {*} [options.preventHandleOutsideImage]
 * @param {*} [options.doneMovingCallback]
 * @param {*} [interactionType=mouse]
 * @returns {undefined}
 */
export default function(
  evtDetail,
  toolName,
  toolData,
  handle,
  options,
  interactionType = 'mouse'
) {
  console.log('moveNewHandle');
  const element = evtDetail.element;

  handle.active = true;
  toolData.active = true;

  // I'm not sure we need either of these.
  // The other manipulators don't handle for these edge cases?
  // Const toolDeactivatedHandler = _toolDeactivatedHandler.bind(
  //   This,
  //   ToolName,
  //   MoveEndHandler
  // );
  // Const measurementRemovedHandler = _measurementRemovedHandler.bind(
  //   This,
  //   ToolData,
  //   MoveEndHandler
  // );
  const moveHandler = _moveHandler.bind(
    this,
    toolName,
    toolData,
    handle,
    options,
    interactionType
  );
  const moveEndHandler = _moveEndHandler.bind(
    this,
    toolData,
    handle,
    options,
    interactionType,
    {
      moveHandler,
      moveEndHandler,
      // MeasurementRemovedHandler,
      // ToolDeactivatedHandler,
    }
  );

  // Add event listeners
  _moveEvents[interactionType].forEach(eventType => {
    element.addEventListener(eventType, moveHandler);
  });
  _moveEndEvents[interactionType].forEach(eventType => {
    element.addEventListener(eventType, moveEndHandler);
  });
  // Element.addEventListener(
  //   EVENTS.MEASUREMENT_REMOVED,
  //   MeasurementRemovedHandler
  // );
  // Element.addEventListener(EVENTS.TOOL_DEACTIVATED, toolDeactivatedHandler);
  element.addEventListener(EVENTS.TOUCH_START, _stopImmediatePropagation);
}

function _moveHandler(
  toolName,
  toolData,
  handle,
  options,
  interactionType,
  evt
) {
  console.log('moveNewHandle: moveHandler');
  const { currentPoints, image, element } = evt.detail;
  const page = currentPoints.page;
  const fingerOffset = -57;
  const targetLocation = external.cornerstone.pageToPixel(
    element,
    interactionType === 'touch' ? page.x + fingerOffset : page.x,
    interactionType === 'touch' ? page.y + fingerOffset : page.y
  );

  handle.active = true;
  handle.x = targetLocation.x;
  handle.y = targetLocation.y;

  if (options && options.preventHandleOutsideImage) {
    clipToBox(handle, image);
  }

  external.cornerstone.updateImage(element);

  const eventType = EVENTS.MEASUREMENT_MODIFIED;
  const modifiedEventData = {
    toolName,
    element,
    measurementData: toolData,
  };

  triggerEvent(element, eventType, modifiedEventData);
}

function _moveEndHandler(
  toolData,
  handle,
  options,
  interactionType,
  {
    moveHandler,
    moveEndHandler,
    // MeasurementRemovedHandler,
    // ToolDeactivatedHandler,
  },
  evt
) {
  console.log(evt, interactionType);
  console.log('moveNewHandle: moveEndHandler', moveHandler, moveEndHandler);
  const { element, currentPoints } = evt.detail;
  const page = currentPoints.page;
  const fingerOffset = -57;
  const targetLocation = external.cornerstone.pageToPixel(
    element,
    interactionType === 'touch' ? page.x + fingerOffset : page.x,<commit_msg>Try wrapping the function call<commit_after>// If (anyHandlesOutsideImage(evt.detail, measurementData.handles)) {
//   // Delete the measurement
//   RemoveToolState(element, this.name, measurementData);
// }

import EVENTS from '../events.js';
import external from '../externalModules.js';
import triggerEvent from '../util/triggerEvent.js';
import { clipToBox } from '../util/clip.js';

const _moveEvents = {
  mouse: [EVENTS.MOUSE_MOVE, EVENTS.MOUSE_DRAG],
  touch: [EVENTS.TOUCH_DRAG],
};

const _moveEndEvents = {
  mouse: [EVENTS.MOUSE_UP, EVENTS.MOUSE_CLICK],
  touch: [EVENTS.TOUCH_END, EVENTS.TOUCH_PINCH, EVENTS.TAP],
};

/**
 * Move a new handle
 * @public
 * @method moveNewHandle
 * @memberof Manipulators
 *
 * @param {*} evtDetail
 * @param {*} toolName
 * @param {*} toolData
 * @param {*} handle
 * @param {*} [options={}]
 * @param {*} [options.preventHandleOutsideImage]
 * @param {*} [options.doneMovingCallback]
 * @param {*} [interactionType=mouse]
 * @returns {undefined}
 */
export default function(
  evtDetail,
  toolName,
  toolData,
  handle,
  options,
  interactionType = 'mouse'
) {
  console.log('moveNewHandle');
  const element = evtDetail.element;

  handle.active = true;
  toolData.active = true;

  // I'm not sure we need either of these.
  // The other manipulators don't handle for these edge cases?
  // Const toolDeactivatedHandler = _toolDeactivatedHandler.bind(
  //   This,
  //   ToolName,
  //   MoveEndHandler
  // );
  // Const measurementRemovedHandler = _measurementRemovedHandler.bind(
  //   This,
  //   ToolData,
  //   MoveEndHandler
  // );
  const moveHandler = _moveHandler.bind(
    this,
    toolName,
    toolData,
    handle,
    options,
    interactionType
  );
  // So we don't need to inline the entire `moveEndEventHandler` function
  const moveEndHandler = evt => {
    _moveEndHandler(
      toolData,
      handle,
      options,
      interactionType,
      {
        moveHandler,
        moveEndHandler,
      },
      evt
    );
  };

  // Add event listeners
  _moveEvents[interactionType].forEach(eventType => {
    element.addEventListener(eventType, moveHandler);
  });
  _moveEndEvents[interactionType].forEach(eventType => {
    element.addEventListener(eventType, moveEndHandler);
  });
  // Element.addEventListener(
  //   EVENTS.MEASUREMENT_REMOVED,
  //   MeasurementRemovedHandler
  // );
  // Element.addEventListener(EVENTS.TOOL_DEACTIVATED, toolDeactivatedHandler);
  element.addEventListener(EVENTS.TOUCH_START, _stopImmediatePropagation);
}

function _moveHandler(
  toolName,
  toolData,
  handle,
  options,
  interactionType,
  evt
) {
  console.log('moveNewHandle: moveHandler');
  const { currentPoints, image, element } = evt.detail;
  const page = currentPoints.page;
  const fingerOffset = -57;
  const targetLocation = external.cornerstone.pageToPixel(
    element,
    interactionType === 'touch' ? page.x + fingerOffset : page.x,
    interactionType === 'touch' ? page.y + fingerOffset : page.y
  );

  handle.active = true;
  handle.x = targetLocation.x;
  handle.y = targetLocation.y;

  if (options && options.preventHandleOutsideImage) {
    clipToBox(handle, image);
  }

  external.cornerstone.updateImage(element);

  const eventType = EVENTS.MEASUREMENT_MODIFIED;
  const modifiedEventData = {
    toolName,
    element,
    measurementData: toolData,
  };

  triggerEvent(element, eventType, modifiedEventData);
}

function _moveEndHandler(
  toolData,
  handle,
  options,
  interactionType,
  { moveHandler, moveEndHandler },
  evt
) {
  console.log(evt, interactionType);
  console.log('moveNewHandle: moveEndHandler', moveHandler, moveEndHandler);
  const { element, currentPoints } = evt.detail;
  const page = currentPoints.page;
  const fingerOffset = -57;
  const targetLocation = external.cornerstone.pageToPixel(
    element,
    interactionType === 'touch' ? page.x + fingerOffset : page.x,
{'fraction_non_alphanumeric': 0.12334352701325178, 'fraction_numerical': 0.0010193679918450561, 'mean_word_length': 3.641632170313424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '1666325', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2501, 'n_words': 506}