<commit_before>import { Dragger } from '@ephox/dragster';
import { Fun } from '@ephox/katamari';
import { Option } from '@ephox/katamari';
import { Event } from '@ephox/porkbun';
import { Events } from '@ephox/porkbun';
import BarMutation from './BarMutation';
import Bars from './Bars';
import Styles from '../style/Styles';
import CellUtils from '../util/CellUtils';
import { Compare } from '@ephox/sugar';
import { DomEvent } from '@ephox/sugar';
import { Body } from '@ephox/sugar';
import { Node } from '@ephox/sugar';
import { Attr } from '@ephox/sugar';
import { Class } from '@ephox/sugar';
import { Css } from '@ephox/sugar';
import { SelectorExists } from '@ephox/sugar';
import { SelectorFind } from '@ephox/sugar';

var resizeBarDragging = Styles.resolve('resizer-bar-dragging');

export default <any> function (wire, direction, hdirection) {
  var mutation = BarMutation();
  var resizing = Dragger.transform(mutation, {});

  var hoverTable = Option.none();

  var getResizer = function (element, type) {
    return Option.from(Attr.get(element, type));
  };

  /* Reposition the bar as the user drags */
  mutation.events.drag.bind(function (event) {
    getResizer(event.target(), 'data-row').each(function (_dataRow) {
      var currentRow = CellUtils.getInt(event.target(), 'top');
      Css.set(event.target(), 'top', currentRow + event.yDelta() + 'px');
    });

    getResizer(event.target(), 'data-column').each(function (_dataCol) {
      var currentCol = CellUtils.getInt(event.target(), 'left');
      Css.set(event.target(), 'left', currentCol + event.xDelta() + 'px');
    });
  });

  var getDelta = function (target, direction) {
    var newX = CellUtils.getInt(target, direction);
    var oldX = parseInt(Attr.get(target, 'data-initial-' + direction), 10);
    return newX - oldX;
  };

  /* Resize the column once the user releases the mouse */
  resizing.events.stop.bind(function () {
    mutation.get().each(function (target) {
      hoverTable.each(function (table) {
        getResizer(target, 'data-row').each(function (row) {
          var delta = getDelta(target, 'top');
          Attr.remove(target, 'data-initial-top');
          events.trigger.adjustHeight(table, delta, parseInt(row, 10));
        });

        getResizer(target, 'data-column').each(function (column) {
          var delta = getDelta(target, 'left');
          Attr.remove(target, 'data-initial-left');
          events.trigger.adjustWidth(table, delta, parseInt(column, 10));
        });

        Bars.refresh(wire, table, hdirection, direction);
      });
    });

  });

  var handler = function (target, direction) {
    events.trigger.startAdjust();
    mutation.assign(target);
    Attr.set(target, 'data-initial-' + direction, parseInt(Css.get(target, direction), 10));
    Class.add(target, resizeBarDragging);
    Css.set(target, 'opacity', '0.2');
    resizing.go(wire.parent());
  };

  /* mousedown on resize bar: start dragging when the bar is clicked, storing the initial position. */
  var mousedown = DomEvent.bind(wire.parent(), 'mousedown', function (event) {
    if (Bars.isRowBar(event.target())) handler(event.target(), 'top');

    if (Bars.isColBar(event.target())) handler(event.target(), 'left');
  });

  var isRoot = function (e) { return Compare.eq(e, wire.view()); };

  /* mouseover on table: When the mouse moves within the CONTENT AREA (NOT THE TABLE), refresh the bars. */
  var mouseover = DomEvent.bind(wire.view(), 'mouseover', function (event) {
    if (Node.name(event.target()) === 'table' || SelectorExists.ancestor(event.target(), 'table', isRoot)) {
      hoverTable = Node.name(event.target()) === 'table' ? Option.some(event.target()) : SelectorFind.ancestor(event.target(), 'table', isRoot);
      hoverTable.each(function (ht) {
        Bars.refresh(wire, ht, hdirection, direction);<commit_msg>TINY-1578: check isRoot before bind in BarManager mouseover<commit_after>import { Dragger } from '@ephox/dragster';
import { Fun } from '@ephox/katamari';
import { Option } from '@ephox/katamari';
import { Event } from '@ephox/porkbun';
import { Events } from '@ephox/porkbun';
import BarMutation from './BarMutation';
import Bars from './Bars';
import Styles from '../style/Styles';
import CellUtils from '../util/CellUtils';
import { Compare } from '@ephox/sugar';
import { DomEvent } from '@ephox/sugar';
import { Body } from '@ephox/sugar';
import { Node } from '@ephox/sugar';
import { Attr } from '@ephox/sugar';
import { Class } from '@ephox/sugar';
import { Css } from '@ephox/sugar';
import { SelectorExists } from '@ephox/sugar';
import { SelectorFind } from '@ephox/sugar';

var resizeBarDragging = Styles.resolve('resizer-bar-dragging');

export default <any> function (wire, direction, hdirection) {
  var mutation = BarMutation();
  var resizing = Dragger.transform(mutation, {});

  var hoverTable = Option.none();

  var getResizer = function (element, type) {
    return Option.from(Attr.get(element, type));
  };

  /* Reposition the bar as the user drags */
  mutation.events.drag.bind(function (event) {
    getResizer(event.target(), 'data-row').each(function (_dataRow) {
      var currentRow = CellUtils.getInt(event.target(), 'top');
      Css.set(event.target(), 'top', currentRow + event.yDelta() + 'px');
    });

    getResizer(event.target(), 'data-column').each(function (_dataCol) {
      var currentCol = CellUtils.getInt(event.target(), 'left');
      Css.set(event.target(), 'left', currentCol + event.xDelta() + 'px');
    });
  });

  var getDelta = function (target, direction) {
    var newX = CellUtils.getInt(target, direction);
    var oldX = parseInt(Attr.get(target, 'data-initial-' + direction), 10);
    return newX - oldX;
  };

  /* Resize the column once the user releases the mouse */
  resizing.events.stop.bind(function () {
    mutation.get().each(function (target) {
      hoverTable.each(function (table) {
        getResizer(target, 'data-row').each(function (row) {
          var delta = getDelta(target, 'top');
          Attr.remove(target, 'data-initial-top');
          events.trigger.adjustHeight(table, delta, parseInt(row, 10));
        });

        getResizer(target, 'data-column').each(function (column) {
          var delta = getDelta(target, 'left');
          Attr.remove(target, 'data-initial-left');
          events.trigger.adjustWidth(table, delta, parseInt(column, 10));
        });

        Bars.refresh(wire, table, hdirection, direction);
      });
    });

  });

  var handler = function (target, direction) {
    events.trigger.startAdjust();
    mutation.assign(target);
    Attr.set(target, 'data-initial-' + direction, parseInt(Css.get(target, direction), 10));
    Class.add(target, resizeBarDragging);
    Css.set(target, 'opacity', '0.2');
    resizing.go(wire.parent());
  };

  /* mousedown on resize bar: start dragging when the bar is clicked, storing the initial position. */
  var mousedown = DomEvent.bind(wire.parent(), 'mousedown', function (event) {
    if (Bars.isRowBar(event.target())) handler(event.target(), 'top');

    if (Bars.isColBar(event.target())) handler(event.target(), 'left');
  });

  var isRoot = function (e) { return Compare.eq(e, wire.view()); };

  var hasTableAncestor = function (element) {
    return !isRoot(element) && SelectorExists.ancestor(element, 'table', isRoot)
  }

  /* mouseover on table: When the mouse moves within the CONTENT AREA (NOT THE TABLE), refresh the bars. */
  var mouseover = DomEvent.bind(wire.view(), 'mouseover', function (event) {
    if (Node.name(event.target()) === 'table' || hasTableAncestor(event.target())) {
      hoverTable = Node.name(event.target()) === 'table' ? Option.some(event.target()) : SelectorFind.ancestor(event.target(), 'table', isRoot);
      hoverTable.each(function (ht) {
        Bars.refresh(wire, ht, hdirection, direction);
{'fraction_non_alphanumeric': 0.16645456859251717, 'fraction_numerical': 0.003054212267752609, 'mean_word_length': 3.818516247700797, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3217746', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2481, 'n_words': 595}