<commit_before>'use strict';
var __ = require('underscore'), $ = require('jquery-browserify');

function ControlsHandler(elementSelector, cb, options) {
  var defaults = {
    keyboard: true,
    mouse: true,
    touch: false
  };
  options = __(defaults).extend(options || {});

  this.cb = cb;
  this.send.bind( this );

  this.selector = elementSelector;
  this.el = false;

  var that = this;

  $(document).ready(function() {
    that.el = $(elementSelector)[0];
    if (that.el === undefined){
      throw 'NoSuchElementError';
    }

    // Disable right click
    $(that.el).bind('contextmenu', function() {
      return false;
    });

    if (options.keyboard) {
      that.keyHandler = new KeyboardHandler(that.el, that.send.bind(that));
    }

    if (options.mouse) {
      that.mouseHandler = new MouseHandler(that.el, that.send.bind(that));
    }

    if (options.touch) {
      // Init touch event handler
      // that.touchHandler = new TouchHandler(that.el, that.send);
    }

  });

}

ControlsHandler.prototype.setCallback = function( cb ){
  this.cb = cb;
};

ControlsHandler.prototype.send = function( message ) {
  if (this.cb){
    //console.log('Sending event', message);
    window.me = message;
    this.cb( message );
  } else {
    console.log('CB not set', message);
  }
};

function MouseHandler(element, cb) { // jshint ignore:line
  // this.send = cb;
  this.send = __.throttle(cb, 50);
  this.el = element;
  this.buttons = [false, false, false, false];

  this.offset = $(this.el).offset();
  this.height = $(this.el).height();
  this.width = $(this.el).width();

  $(this.el).mousedown( this.mousedown.bind(this) );
  $(this.el).dblclick( this.dblclick.bind(this) );
  $(document).mouseup( this.mouseup.bind(this) );
}

MouseHandler.prototype.mousedown = function( event ) {
  console.log('Button', event.which, event.button, event);
  this.buttons[ event.which ] = true;
  $(this.el).mousemove( this.mousemove.bind(this) );

  this.send( this.buildMessage( 'press', event ) );
};

MouseHandler.prototype.mouseup = function( event ) {
  this.buttons[ event.which ] = false;

  this.send( this.buildMessage( 'release', event ) );

  //Unbind move binding if no buttons are pressed;
  if ( this.buttons.indexOf( true ) === -1 ) {
    $(this.el).unbind('mousemove');
  }
};<commit_msg>Fix mouse controls<commit_after>'use strict';
var __ = require('underscore'), $ = require('jquery-browserify');

function ControlsHandler(elementSelector, cb, options) {
  var defaults = {
    keyboard: true,
    mouse: true,
    touch: false
  };
  options = __(defaults).extend(options || {});

  this.cb = cb;
  this.send.bind( this );

  this.selector = elementSelector;
  this.el = false;

  var that = this;

  $(document).ready(function() {
    that.el = $(elementSelector)[0];
    if (that.el === undefined){
      throw 'NoSuchElementError';
    }

    // Disable right click
    $(that.el).bind('contextmenu', function() {
      return false;
    });

    if (options.keyboard) {
      that.keyHandler = new KeyboardHandler(that.el, that.send.bind(that));
    }

    if (options.mouse) {
      that.mouseHandler = new MouseHandler(that.el, that.send.bind(that));
    }

    if (options.touch) {
      // Init touch event handler
      // that.touchHandler = new TouchHandler(that.el, that.send);
    }

  });

}

ControlsHandler.prototype.setCallback = function( cb ){
  this.cb = cb;
};

ControlsHandler.prototype.send = function( message ) {
  if (this.cb){
    //console.log('Sending event', message);
    window.me = message;
    this.cb( message );
  } else {
    console.log('CB not set', message);
  }
};

function MouseHandler(element, cb) { // jshint ignore:line
  // this.send = cb;
  this.send = __.throttle(cb, 50);
  this.el = element;
  this.buttons = [false, false, false, false];

  $(this.el).mousedown( this.mousedown.bind(this) );
  $(this.el).dblclick( this.dblclick.bind(this) );
  $(document).mouseup( this.mouseup.bind(this) );

  this.setUnits();
  $(window).resize( this.setUnits.bind(this) );
};

MouseHandler.prototype.setUnits = function(){
  this.offset = $(this.el).offset();
  this.height = $(this.el).height();
  this.width = $(this.el).width();
};

MouseHandler.prototype.mousedown = function( event ) {
  console.log('Button', event.which, event.button, event);
  this.buttons[ event.which ] = true;
  $(this.el).mousemove( this.mousemove.bind(this) );

  this.send( this.buildMessage( 'press', event ) );
};

MouseHandler.prototype.mouseup = function( event ) {
  this.buttons[ event.which ] = false;

  this.send( this.buildMessage( 'release', event ) );

  //Unbind move binding if no buttons are pressed;
  if ( this.buttons.indexOf( true ) === -1 ) {
    $(this.el).unbind('mousemove');
  }
};
{'fraction_non_alphanumeric': 0.1730728838479311, 'fraction_numerical': 0.0016803192606595252, 'mean_word_length': 3.2900900900900902, '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': '5805300', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1569, 'n_words': 321}