<commit_before>Date.prototype.addDays = function(days) {
  this.setDate(this.getDate()+days);
  };

Date.prototype.diffInDays = function(d2) {
  var t2 = d2.getTime();
  var t1 = this.getTime();
  return parseInt((t2-t1)/(24*3600*1000));
  };

var Task = Class.create ({
  initialize: function(planning, task) {
    this.planning         = planning;
    this.name             = task.name
    this.start_date       = new Date(task.start_date);
    this.duration         = task.duration_in_day;
    this.vertTitleSpacing = 12;
    this.taskHeight       = this.planning.taskHeight-2;
    },

  draw: function (y) {
    this.planning.ctx.fillText(this.name, 2, y + this.planning.dateHeaderHeight + this.vertTitleSpacing);
    x = this.planning.getTaskX(this);
    limRight =  this.planning.canvas.width -this.planning.canvasEndBorder;
    if(x > limRight) // out of the canvas
      return;
    lim    = this.planning.taskTitleWidth
    length = this.duration*this.planning.pixelsForOneDay;
    if(x+length < lim) return; // out of the canvas
    if(x < lim) {
      length -= lim-x; // reduce length
      x = lim; // do not draw the start of the task that is out of the left limit
      }
    if(x+length > limRight) // do not draw the rest of the task that is out the canvas (on the right)
      length -= (x+length - limRight)
    this.planning.ctx.fillRect(x, y + this.planning.dateHeaderHeight, length, this.taskHeight);
    }
});

var Planning = Class.create({
  initialize: function(canvas_id,tasks) {
    // Actually start initializing defaults etc.
    window.Planning         = this; // define this class global, so it is accessible from event handlers
    this.canvas_id          = canvas_id || "planning";
    this.canvas             = $(this.canvas_id);
    this.ctx                = this.canvas.getContext("2d");
    this.taskTitleWidth     = 150;
    this.canvasEndBorder    = 25;
    this.taskBarMaxWidth    = this.canvas.width - this.taskTitleWidth - this.canvasEndBorder
    this.dateHeaderHeight   = 30;
    this.start_date         = new Date();
    this.setPlanningWidthInDay(60);
    this.months             = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    this.mouseCoords        = null; // current mouse coords
    this.fromCoords         = null; // down event ouse coords
    this.mouseState         = null;
    this.taskHeight         = 20;
    HTMLCanvasElement.prototype.relMouseCoords = this.relMouseCoords;

    // tasks
    this.tasks = new Array();
    for(var i=0; i < tasks.length; i++) {
      this.tasks.push(new Task(this, tasks[i].task));
      }

    // Draw the grid
    this.draw();

    // listen to the mouse clicks
    this.canvas.addEventListener("mousedown", this.onMouseDown, false);
    this.canvas.addEventListener("mouseup",   this.onMouseUp, false);
    this.canvas.addEventListener("mousemove", this.onMouseMove, false);
    this.canvas.addEventListener("mouseout",  this.onMouseOut, false);
    },

  setPlanningWidthInDay: function(days) {
    if(days < 10 || days > 90) return;
    this.planningWidthInDay = days;
    this.end_date           = new Date(this.start_date.valueOf());
    this.end_date.addDays(this.planningWidthInDay);
    this.pixelsForOneDay    = this.taskBarMaxWidth / this.planningWidthInDay;
    },

  draw: function() {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    this.ctx.fillStyle   = "rgba(100, 100, 255, 1.0)";
    this.ctx.font        = "12px Helvetica";
    this.ctx.lineWidth   = 1;
    this.ctx.strokeStyle = "black";
    this.drawGrid();
    for(var i=0; i < this.tasks.length; i++) {
      this.tasks[i].draw(1+i*this.taskHeight);
      }
    },

  drawGrid: function() {
    this.drawDateHeader();
    this.ctx.beginPath();
    this.ctx.moveTo(0,this.dateHeaderHeight-0.5);
    this.ctx.lineTo(this.canvas.width-this.canvasEndBorder, this.dateHeaderHeight-0.5);
    this.ctx.stroke();
    this.ctx.beginPath();
    this.ctx.moveTo(0,this.canvas.height-0.5);
    this.ctx.lineTo(this.canvas.width-this.canvasEndBorder, this.canvas.height-0.5);
    this.ctx.stroke();
    for(var i=0; i <= this.planningWidthInDay; i++) {
      this.ctx.beginPath();
      this.ctx.moveTo(this.taskTitleWidth+i*this.pixelsForOneDay-0.5,this.dateHeaderHeight);
      this.ctx.lineTo(this.taskTitleWidth+i*this.pixelsForOneDay-0.5,this.canvas.height);
      this.ctx.stroke();
      }
    },

  drawDateHeader: function() {
    this.ctx.fillText(this.myDateFormat(this.start_date), this.taskTitleWidth-15, 20);
    this.ctx.fillText(this.myDateFormat(this.end_date), this.canvas.width-this.canvasEndBorder - 15, 20);
    },

  myDateFormat: function(date) {
    return date.getDate() + '-' + this.months[date.getMonth()];
    },

  onMouseDown: function(event) {
    window.Planning.fromCoords = this.relMouseCoords(event);
    window.Planning.mouseState = 'down';
    },
  onMouseUp: function(event) {
    window.Planning.mouseCoords = this.relMouseCoords(event);
    window.Planning.mouseState = 'up';
    },<commit_msg>save the coordinates (to be able to get the tasks under the click, for example)<commit_after>Date.prototype.addDays = function(days) {
  this.setDate(this.getDate()+days);
  };

Date.prototype.diffInDays = function(d2) {
  var t2 = d2.getTime();
  var t1 = this.getTime();
  return parseInt((t2-t1)/(24*3600*1000));
  };

var Task = Class.create ({
  initialize: function(planning, task) {
    this.planning         = planning;
    this.name             = task.name
    this.start_date       = new Date(task.start_date);
    this.duration         = task.duration_in_day;
    this.vertTitleSpacing = 12;
    this.taskHeight       = this.planning.taskHeight-2;
    this.leftCoords       = {x: null, y: null}
    this.rightCoords      = {x: null, y: null}
    },

  draw: function (y) {
    this.planning.ctx.fillText(this.name, 2, y + this.planning.dateHeaderHeight + this.vertTitleSpacing);
    x = this.planning.getTaskX(this);
    limRight =  this.planning.canvas.width -this.planning.canvasEndBorder;
    if(x > limRight) // out of the canvas
      return;
    lim    = this.planning.taskTitleWidth
    length = this.duration*this.planning.pixelsForOneDay;
    if(x+length < lim) return; // out of the canvas
    if(x < lim) {
      length -= lim-x; // reduce length
      x = lim; // do not draw the start of the task that is out of the left limit
      }
    if(x+length > limRight) // do not draw the rest of the task that is out the canvas (on the right)
      length -= (x+length - limRight)
    // save the coordinates (to be able to get the tasks under the click, for example)
    this.leftCoords.x  = x;
    this.leftCoords.y  = y + this.planning.dateHeaderHeight;
    this.rightCoords.x = x + length;
    this.rightCoords.y = y + this.planning.dateHeaderHeight + this.taskHeight;
    this.planning.ctx.fillRect(x, y + this.planning.dateHeaderHeight, length, this.taskHeight);
    }
});

var Planning = Class.create({
  initialize: function(canvas_id,tasks) {
    // Actually start initializing defaults etc.
    window.Planning         = this; // define this class global, so it is accessible from event handlers
    this.canvas_id          = canvas_id || "planning";
    this.canvas             = $(this.canvas_id);
    this.ctx                = this.canvas.getContext("2d");
    this.taskTitleWidth     = 150;
    this.canvasEndBorder    = 25;
    this.taskBarMaxWidth    = this.canvas.width - this.taskTitleWidth - this.canvasEndBorder
    this.dateHeaderHeight   = 30;
    this.start_date         = new Date();
    this.setPlanningWidthInDay(60);
    this.months             = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    this.mouseCoords        = null; // current mouse coords
    this.fromCoords         = null; // down event ouse coords
    this.mouseState         = null;
    this.taskHeight         = 20;
    HTMLCanvasElement.prototype.relMouseCoords = this.relMouseCoords;

    // tasks
    this.tasks = new Array();
    for(var i=0; i < tasks.length; i++) {
      this.tasks.push(new Task(this, tasks[i].task));
      }

    // Draw the grid
    this.draw();

    // listen to the mouse clicks
    this.canvas.addEventListener("mousedown", this.onMouseDown, false);
    this.canvas.addEventListener("mouseup",   this.onMouseUp, false);
    this.canvas.addEventListener("mousemove", this.onMouseMove, false);
    this.canvas.addEventListener("mouseout",  this.onMouseOut, false);
    },

  setPlanningWidthInDay: function(days) {
    if(days < 10 || days > 90) return;
    this.planningWidthInDay = days;
    this.end_date           = new Date(this.start_date.valueOf());
    this.end_date.addDays(this.planningWidthInDay);
    this.pixelsForOneDay    = this.taskBarMaxWidth / this.planningWidthInDay;
    },

  draw: function() {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    this.ctx.fillStyle   = "rgba(100, 100, 255, 1.0)";
    this.ctx.font        = "12px Helvetica";
    this.ctx.lineWidth   = 1;
    this.ctx.strokeStyle = "black";
    this.drawGrid();
    for(var i=0; i < this.tasks.length; i++) {
      this.tasks[i].draw(1+i*this.taskHeight);
      }
    },

  drawGrid: function() {
    this.drawDateHeader();
    // top horizontal line
    this.ctx.beginPath();
    this.ctx.moveTo(0,this.dateHeaderHeight-0.5);
    this.ctx.lineTo(this.canvas.width-this.canvasEndBorder, this.dateHeaderHeight-0.5);
    this.ctx.stroke();
    // bottom horizontal line
    this.ctx.beginPath();
    this.ctx.moveTo(0,this.canvas.height-0.5);
    this.ctx.lineTo(this.canvas.width-this.canvasEndBorder, this.canvas.height-0.5);
    this.ctx.stroke();
    // vertical lines for days
    for(var i=0; i <= this.planningWidthInDay; i++) {
      this.ctx.beginPath();
      this.ctx.moveTo(this.taskTitleWidth+i*this.pixelsForOneDay-0.5,this.dateHeaderHeight);
      this.ctx.lineTo(this.taskTitleWidth+i*this.pixelsForOneDay-0.5,this.canvas.height);
      this.ctx.stroke();
      }
    },

  drawDateHeader: function() {
    this.ctx.fillText(this.myDateFormat(this.start_date), this.taskTitleWidth-15, 20);
    this.ctx.fillText(this.myDateFormat(this.end_date), this.canvas.width-this.canvasEndBorder - 15, 20);
    },

  myDateFormat: function(date) {
    return date.getDate() + '-' + this.months[date.getMonth()];
    },

  onMouseDown: function(event) {
    window.Planning.fromCoords = this.relMouseCoords(event);
    window.Planning.mouseState = 'down';
    },
  onMouseUp: function(event) {
    window.Planning.mouseCoords = this.relMouseCoords(event);
    window.Planning.mouseState = 'up';
    },
{'fraction_non_alphanumeric': 0.14, 'fraction_numerical': 0.014647887323943662, 'mean_word_length': 2.870276162790698, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, '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': '7034082', 'n_tokens_mistral': 3649, 'n_tokens_neox': 3519, 'n_words': 677}