<commit_before>import { dispatch as createDispatch } from 'd3-dispatch'
import { drag as createDrag } from 'd3-drag'
import { redispatch, rebind } from '@zambezi/d3-utils'
import { select, event } from 'd3-selection'

export function createCellDragBehaviour() {

  const cellSelector = '.zambezi-grid-cell'
      , maxParentCheckJumps = 3
      , drag = createDrag().filter(ignoreDragSelector)
      , dispatch = createDispatch('dragover', 'dragstart', 'dragend')
      , api = rebind().from(dispatch, 'on')

  let isDragging = false
    , ignoreSelector = 'input, button'
    , debugIgnoreSelector = false

  function cellDragBehaviour(s) {
    s.each(cellDragBehaviourEach)
  }

  cellDragBehaviour.debugIgnoreSelector = function(value) {
    if (!arguments.length) return debugIgnoreSelector
    debugIgnoreSelector = value
    return cellDragBehaviour
  }

  cellDragBehaviour.ignoreSelector = function(value) {
    if (!arguments.length) return ignoreSelector
    ignoreSelector = value
    return cellDragBehaviour
  }

  return api(cellDragBehaviour)

  function cellDragBehaviourEach({ dispatcher, scroll, columns }) {
    drag
        .on('start.activate', d => isDragging = true)
        .on('start.redispatch', d => dispatch.call('dragstart', null, d))
        .on('end.deactivate', () => isDragging = false)
        .on('end.redispatch', d => dispatch.call('dragend', null, d))

    dispatcher.on('cell-enter.drag-to-select', configureDragBehaviour)
  }

  function configureDragBehaviour() {
    select(this)
        .call(drag)
        .on('mouseenter.drag-to-select', onMouseEnter)
  }

  function onMouseEnter(d) {
    if (!isDragging) return
    dispatch.call('dragover', this, d)
  }

  function ignoreDragSelector() {
    const ignore = select(event.target).filter(ignoreSelector).size()
    if (debugIgnoreSelector) {
      console.log(
        ignore ? 'ignore selection on' 
      : 'accept selection on', event.target
      )
    }
    return !ignore
  }

  function findParentCell(dom, depth=0) {
    if (!dom) return null<commit_msg>Fix chrome 40 drag to select bug.<commit_after>import { dispatch as createDispatch } from 'd3-dispatch'
import { drag as createDrag } from 'd3-drag'
import { redispatch, rebind } from '@zambezi/d3-utils'
import { select, event } from 'd3-selection'

export function createCellDragBehaviour() {

  const cellSelector = '.zambezi-grid-cell'
      , maxParentCheckJumps = 3
      , drag = createDrag().filter(ignoreDragSelector)
      , dispatch = createDispatch('dragover', 'dragstart', 'dragend')
      , api = rebind().from(dispatch, 'on')

  let isDragging = false
    , ignoreSelector = 'input, button'
    , debugIgnoreSelector = false

  function cellDragBehaviour(s) {
    s.each(cellDragBehaviourEach)
  }

  cellDragBehaviour.debugIgnoreSelector = function(value) {
    if (!arguments.length) return debugIgnoreSelector
    debugIgnoreSelector = value
    return cellDragBehaviour
  }

  cellDragBehaviour.ignoreSelector = function(value) {
    if (!arguments.length) return ignoreSelector
    ignoreSelector = value
    return cellDragBehaviour
  }

  return api(cellDragBehaviour)

  function cellDragBehaviourEach({ dispatcher, scroll, columns }) {
    drag
        .on('start.activate', d => isDragging = true)
        .on('start.redispatch', d => dispatch.call('dragstart', null, d))
        .on('end.deactivate', () => isDragging = false)
        .on('end.redispatch', d => dispatch.call('dragend', null, d))

    dispatcher.on('cell-enter.drag-to-select', configureDragBehaviour)
  }

  function configureDragBehaviour() {
    select(this)
        .call(drag)
        .on('mouseover.drag-to-select', onMouseOver)
  }

  function onMouseOver(d) {
    if (!isDragging) return
    dispatch.call('dragover', this, d)
  }

  function ignoreDragSelector() {
    const ignore = select(event.target).filter(ignoreSelector).size()
    if (debugIgnoreSelector) {
      console.log(
        ignore ? 'ignore selection on' 
      : 'accept selection on', event.target
      )
    }
    return !ignore
  }

  function findParentCell(dom, depth=0) {
    if (!dom) return null
{'fraction_non_alphanumeric': 0.1202516331962255, 'fraction_numerical': 0.003387369949189451, 'mean_word_length': 3.493478260869565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4410006', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1228, 'n_words': 277}