<commit_before>import $ from '../jquery.js';
import * as cornerstone from '../cornerstone-core.js';

// This object is responsible for synchronizing target elements when an event fires on a source
// Element
function Synchronizer (event, handler) {

  const that = this;
  const sourceElements = []; // Source elements fire the events we want to synchronize to
  const targetElements = []; // Target elements we want to synchronize to source elements

  let ignoreFiredEvents = false;
  const initialData = {};
  let eventHandler = handler;

  this.setHandler = function (handler) {
    eventHandler = handler;
  };

  this.getHandler = function () {
    return eventHandler;
  };

  this.getDistances = function () {
    if (!sourceElements.length || !targetElements.length) {
      return;
    }

    initialData.distances = {};
    initialData.imageIds = {
      sourceElements: [],
      targetElements: []
    };

    sourceElements.forEach(function (sourceElement) {
      const sourceEnabledElement = cornerstone.getEnabledElement(sourceElement);

      if (!sourceEnabledElement || !sourceEnabledElement.image) {
        return;
      }

      const sourceImageId = sourceEnabledElement.image.imageId;
      const sourceImagePlane = cornerstone.metaData.get('imagePlane', sourceImageId);

      if (!sourceImagePlane || !sourceImagePlane.imagePositionPatient) {
        return;
      }

      const sourceImagePosition = sourceImagePlane.imagePositionPatient;

      if (initialData.hasOwnProperty(sourceEnabledElement)) {
        return;
      }
      initialData.distances[sourceImageId] = {};


      initialData.imageIds.sourceElements.push(sourceImageId);

      targetElements.forEach(function (targetElement) {
        const targetEnabledElement = cornerstone.getEnabledElement(targetElement);

        if (!targetEnabledElement || !targetEnabledElement.image) {
          return;
        }

        const targetImageId = targetEnabledElement.image.imageId;

        initialData.imageIds.targetElements.push(targetImageId);

        if (sourceElement === targetElement) {
          return;
        }

        if (sourceImageId === targetImageId) {
          return;
        }

        if (initialData.distances[sourceImageId].hasOwnProperty(targetImageId)) {
          return;
        }

        const targetImagePlane = cornerstone.metaData.get('imagePlane', targetImageId);

        if (!targetImagePlane || !targetImagePlane.imagePositionPatient) {
          return;
        }

        const targetImagePosition = targetImagePlane.imagePositionPatient;

        initialData.distances[sourceImageId][targetImageId] = targetImagePosition.clone().sub(sourceImagePosition);
      });

      if (!Object.keys(initialData.distances[sourceImageId]).length) {
        delete initialData.distances[sourceImageId];
      }
    });
  };

  function fireEvent (sourceElement, eventData) {
        // Broadcast an event that something changed
    if (!sourceElements.length || !targetElements.length) {
      return;
    }

    ignoreFiredEvents = true;
    targetElements.forEach(function (targetElement) {
      const targetIndex = targetElements.indexOf(targetElement);

      if (targetIndex === -1) {
        return;
      }

      const targetImageId = initialData.imageIds.targetElements[targetIndex];
      const sourceIndex = sourceElements.indexOf(sourceElement);

      if (sourceIndex === -1) {
        return;
      }

      const sourceImageId = initialData.imageIds.sourceElements[sourceIndex];

      let positionDifference;

      if (sourceImageId === targetImageId) {
        positionDifference = 0;
      } else {
        positionDifference = initialData.distances[sourceImageId][targetImageId];
      }

      eventHandler(that, sourceElement, targetElement, eventData, positionDifference);
    });
    ignoreFiredEvents = false;
  }

  function onEvent (e, eventData) {
    if (ignoreFiredEvents === true) {
      return;
    }
<commit_msg>Feature/position difference undefined instead of crash (#241)<commit_after>import $ from '../jquery.js';
import * as cornerstone from '../cornerstone-core.js';

// This object is responsible for synchronizing target elements when an event fires on a source
// Element
function Synchronizer (event, handler) {

  const that = this;
  const sourceElements = []; // Source elements fire the events we want to synchronize to
  const targetElements = []; // Target elements we want to synchronize to source elements

  let ignoreFiredEvents = false;
  const initialData = {};
  let eventHandler = handler;

  this.setHandler = function (handler) {
    eventHandler = handler;
  };

  this.getHandler = function () {
    return eventHandler;
  };

  this.getDistances = function () {
    if (!sourceElements.length || !targetElements.length) {
      return;
    }

    initialData.distances = {};
    initialData.imageIds = {
      sourceElements: [],
      targetElements: []
    };

    sourceElements.forEach(function (sourceElement) {
      const sourceEnabledElement = cornerstone.getEnabledElement(sourceElement);

      if (!sourceEnabledElement || !sourceEnabledElement.image) {
        return;
      }

      const sourceImageId = sourceEnabledElement.image.imageId;
      const sourceImagePlane = cornerstone.metaData.get('imagePlane', sourceImageId);

      if (!sourceImagePlane || !sourceImagePlane.imagePositionPatient) {
        return;
      }

      const sourceImagePosition = sourceImagePlane.imagePositionPatient;

      if (initialData.hasOwnProperty(sourceEnabledElement)) {
        return;
      }
      initialData.distances[sourceImageId] = {};


      initialData.imageIds.sourceElements.push(sourceImageId);

      targetElements.forEach(function (targetElement) {
        const targetEnabledElement = cornerstone.getEnabledElement(targetElement);

        if (!targetEnabledElement || !targetEnabledElement.image) {
          return;
        }

        const targetImageId = targetEnabledElement.image.imageId;

        initialData.imageIds.targetElements.push(targetImageId);

        if (sourceElement === targetElement) {
          return;
        }

        if (sourceImageId === targetImageId) {
          return;
        }

        if (initialData.distances[sourceImageId].hasOwnProperty(targetImageId)) {
          return;
        }

        const targetImagePlane = cornerstone.metaData.get('imagePlane', targetImageId);

        if (!targetImagePlane || !targetImagePlane.imagePositionPatient) {
          return;
        }

        const targetImagePosition = targetImagePlane.imagePositionPatient;

        initialData.distances[sourceImageId][targetImageId] = targetImagePosition.clone().sub(sourceImagePosition);
      });

      if (!Object.keys(initialData.distances[sourceImageId]).length) {
        delete initialData.distances[sourceImageId];
      }
    });
  };

  function fireEvent (sourceElement, eventData) {
        // Broadcast an event that something changed
    if (!sourceElements.length || !targetElements.length) {
      return;
    }

    ignoreFiredEvents = true;
    targetElements.forEach(function (targetElement) {
      const targetIndex = targetElements.indexOf(targetElement);

      if (targetIndex === -1) {
        return;
      }

      const targetImageId = initialData.imageIds.targetElements[targetIndex];
      const sourceIndex = sourceElements.indexOf(sourceElement);

      if (sourceIndex === -1) {
        return;
      }

      const sourceImageId = initialData.imageIds.sourceElements[sourceIndex];

      let positionDifference;

      if (sourceImageId === targetImageId) {
        positionDifference = 0;
      } else if (initialData.distances[sourceImageId] !== undefined) {
        positionDifference = initialData.distances[sourceImageId][targetImageId];
      }

      eventHandler(that, sourceElement, targetElement, eventData, positionDifference);
    });
    ignoreFiredEvents = false;
  }

  function onEvent (e, eventData) {
    if (ignoreFiredEvents === true) {
      return;
    }

{'fraction_non_alphanumeric': 0.09878079124160238, 'fraction_numerical': 0.001119681512814133, 'mean_word_length': 3.062152602324406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6628421', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2064, 'n_words': 435}