<commit_before>var mercury = require('../../index.js')
var extend = require("xtend")

module.exports = DragEventHandler

function DragEventHandler(fn, value) {
    if (!(this instanceof DragEventHandler)) {
        return new DragEventHandler(fn, value)
    }

    this.fn = fn
    this.value = value || {}
    this.delegator = mercury.Delegator()
}

DragEventHandler.prototype.handleEvent = function (ev) {
    var fn = this.fn
    var value = this.value
    var delegator = this.delegator

    var currentX = ev.offsetX || ev.layerX
    var currentY = ev.offsetY || ev.layerY

    function onmove(ev) {
        var delta = {
            x: ev.clientX - currentX,
            y: ev.clientY - currentY
        }

        fn(extend(value, delta))

        currentX = ev.clientX
        currentY = ev.clientY
    }

    function onup(ev) {
        delegator.unlistenTo("mousemove")
        delegator.removeGlobalEventListener("mousemove", onmove)
        delegator.removeGlobalEventListener("mouseup", onup)
    }

    delegator.listenTo("mousemove")
    delegator.addGlobalEventListener("mousemove", onmove)
    delegator.addGlobalEventListener("mouseup", onup)
}<commit_msg>clean up drag event handler<commit_after>var mercury = require('../../index.js')
var extend = require("xtend")

module.exports = DragEventHandler

function DragEventHandler(fn, value) {
    if (!(this instanceof DragEventHandler)) {
        return new DragEventHandler(fn, value)
    }

    this.fn = fn
    this.value = value || {}
    this.delegator = mercury.Delegator()
}

DragEventHandler.prototype.handleEvent = function (ev) {
    var fn = this.fn
    var value = this.value
    var delegator = this.delegator

    var current = {
        x: ev.offsetX || ev.layerX,
        y: ev.offsetY || ev.layerY
    }

    function onmove(ev) {

        var previous = current

        current = {
            x: ev.offsetX || ev.layerX,
            y: ev.offsetY || ev.layerY
        }

        var delta = {
            x: current.x - previous.x,
            y: current.y - previous.y
        }

        fn(extend(value, delta))
    }

    function onup(ev) {
        delegator.unlistenTo("mousemove")
        delegator.removeGlobalEventListener("mousemove", onmove)
        delegator.removeGlobalEventListener("mouseup", onup)
    }

    delegator.listenTo("mousemove")
    delegator.addGlobalEventListener("mousemove", onmove)
    delegator.addGlobalEventListener("mouseup", onup)
}
{'fraction_non_alphanumeric': 0.115995115995116, 'fraction_numerical': 0.0, 'mean_word_length': 2.5417867435158503, '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': '3083062', 'n_tokens_mistral': 736, 'n_tokens_neox': 740, 'n_words': 156}