<commit_before>/*http://bl.ocks.org/mbostock/4062045 used as reference
 *As well as http://bl.ocks.org/mbostock/3750558
 *and http://bl.ocks.org/mbostock/950642
 *and http://bl.ocks.org/mbostock/1153292
 */

var drawGraph = function (nodes, links, positiveWeights, negativeWeights, sheetType, warnings, sliderController) {

  var $container = $(".grnsight-container");
  d3.selectAll("svg").remove();

  var width = $container.width(),
      height = $container.height(),
      nodeHeight = 30,
      gridWidth = 300,
      colorOptimal = true;

  $('#warningMessage').html(warnings.length != 0 ? "Click here in order to view warnings." : "");

  var getNodeWidth = function (node) {
    return node.name.length * 15;
  };

  // If colorOptimal is false, then weighting is ignored, and the lines are all drawn as if it was an unweighted sheet
  if(!$("#colorEdges").hasClass('active')) {
    colorOptimal = false;
  }

  var adaptive = $("input[name='viewport']:checked").val() === "viewportAdapt";

  var MIN_SCALE = 0.25;
  var MAX_SCALE = (adaptive) ? 10 : 1;

  var allWeights = positiveWeights.concat(negativeWeights);

  if(!colorOptimal) {
    for(var i = 0; i < allWeights.length; i++) {
      if( allWeights[i] != 0 ) {
        allWeights[i] = 1;
      }
    }
  } else {
    for(var i = 0; i < allWeights.length; i++ ) {
      allWeights[i] = Math.abs((allWeights[i]).toPrecision(4));
    }
  }

  //normalization all weights b/w 2-14
  var totalScale = d3.scale.linear()
        .domain(d3.extent(allWeights))
        .range([2, 14]),

      normalizedScale = d3.scale.linear()
        .domain(d3.extent(allWeights)),

      unweighted = false;

  //if unweighted, weight is 2
  if (sheetType === 'unweighted') {
    totalScale = d3.scale.quantile()
      .domain(d3.extent(allWeights))
      .range(["2"]);

    normalizedScale = normalizedScale.range(["2"]);
    unweighted = true;
    $(".normalization-form").append("placeholder='unweighted'");
  } else if (sheetType === 'weighted') {
    $(".normalization-form").append("placeholder='weighted'");
  }

  var getEdgeThickness = function (edge) {
        return Math.round(totalScale(Math.abs(edge.value)));
      };

  var force = d3.layout.force()
      .size([width, height])
      .on("tick", tick)
      .linkDistance($("#linkDistInput").val())
      .charge($("#chargeInput").val())
      .chargeDistance($("#chargeDistInput").val())
      .gravity($("#gravityInput").val());

  var drag = force.drag()
      .origin(function(d) { return d; })
      .on("dragstart", dragstart);

  var zoom = d3.behavior.zoom()
    .center([width / 2, height / 2])
    .scaleExtent([MIN_SCALE, MAX_SCALE])
    .on("zoom", zoomed);

  var svg = d3.select($container[0]).append("svg")
        .attr("width", width)
        .attr("height", height)
        .call(zoom)
      .append("g") // required for zoom to work

  // This rectangle catches all of the mousewheel and pan events, without letting
  // them bubble up to the body.
  var innerRect = svg.append("rect")
                     .attr("width", width)
                     .attr("height", height)
                     .style("fill", "none")
                     .style("pointer-events", "all")
                     .attr("stroke", adaptive ? "none" : "#9A9A9A")
                     .append("g")


  function zoomed() {
    // These comments are for when we restrict the zoom to the available space.
    // var tx = Math.min(0, d3.event.translate[0]);
    // var ty = Math.min(0, d3.event.translate[1]);
    // svg.attr("transform", "translate(" + [tx, ty] + ")scale(" + d3.event.scale + ")");
    svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
  }

  d3.selectAll(".scrollBtn").on("click", null); // Remove event handlers, if there were any.

  // TODO: Make this less bad
  d3.select(".scrollUp").on("click", function () {
    move("up", true);
  });

  d3.select(".scrollLeft").on("click", function () {
    move("left", true);
  });

  d3.select(".scrollRight").on("click", function () {
    move("right", true);
  });

  d3.select(".scrollDown").on("click", function () {
    move("down", true);
  });

  d3.select("#zoomIn").on("click", function () {
    move("in", false);
  });

  d3.select("#zoomOut").on("click", function () {
    move("out", false);
  });

  d3.selectAll(".boundBoxSize").on("click", function () {
    var newWidth = d3.select(".grnsight-container").style("width");
    var newHeight = d3.select(".grnsight-container").style("height");
    var BORDER_OFFSET = 4;

    // Remove trailing "px"
    newWidth = newWidth.substring(0, newWidth.length - 2) - BORDER_OFFSET;
    newHeight = newHeight.substring(0, newHeight.length - 2) - BORDER_OFFSET;

    if (adaptive) {
      width = (width < newWidth) ? newWidth : width;
      height = (height < newHeight) ? newHeight : height;
    } else {
      width = newWidth;
      height = newHeight;
    }

    d3.select("svg").attr("width", width).attr("height", height);
    d3.select("rect").attr("width", width).attr("height", height);
    force.size([width, height]).resume();
  });

  /* Credit to https://bl.ocks.org/mbostock/7ec977c95910dd026812 */
  function move(direction, isMove) {
    svg.call(zoom.event);
    var currentTransform = d3.transform(svg.attr("transform"));
    var currentTranslate = [0, 0];
    var currentScale = 1;
    if (currentTransform) {
      currentTranslate = d3.transform(currentTransform).translate;
      currentScale = d3.transform(currentTransform).scale[0]; // x and y scale will always be equal
    }
    if (isMove) {
      currentTranslate[0] += (direction === "left" ? 50 : (direction === "right" ? -50 : 0));
      currentTranslate[1] += (direction === "up" ? 50 : (direction === "down" ? -50 : 0));
    } else {
      currentScale += (direction === "in" ? 0.05 : -0.05)
      if (currentScale > MAX_SCALE) {
        currentScale = MAX_SCALE;
      } else if (currentScale < MIN_SCALE) {
        currentScale = MIN_SCALE;
      }
    }
    zoom.scale(currentScale);
    zoom.translate(currentTranslate);
    svg.transition().call(zoom.event);
  }

  var defs = svg.append("defs");
<commit_msg>Allow changing between adaptive and fixed.<commit_after>/*http://bl.ocks.org/mbostock/4062045 used as reference
 *As well as http://bl.ocks.org/mbostock/3750558
 *and http://bl.ocks.org/mbostock/950642
 *and http://bl.ocks.org/mbostock/1153292
 */

var drawGraph = function (nodes, links, positiveWeights, negativeWeights, sheetType, warnings, sliderController) {

  var $container = $(".grnsight-container");
  d3.selectAll("svg").remove();

  var width = $container.width(),
      height = $container.height(),
      nodeHeight = 30,
      gridWidth = 300,
      colorOptimal = true;

  $('#warningMessage').html(warnings.length != 0 ? "Click here in order to view warnings." : "");

  var getNodeWidth = function (node) {
    return node.name.length * 15;
  };

  // If colorOptimal is false, then weighting is ignored, and the lines are all drawn as if it was an unweighted sheet
  if(!$("#colorEdges").hasClass('active')) {
    colorOptimal = false;
  }

  var adaptive = $("input[name='viewport']:checked").val() === "viewportAdapt";

  var MIN_SCALE = 0.25;
  var MAX_SCALE = (adaptive) ? 10 : 1;

  var allWeights = positiveWeights.concat(negativeWeights);

  if(!colorOptimal) {
    for(var i = 0; i < allWeights.length; i++) {
      if( allWeights[i] != 0 ) {
        allWeights[i] = 1;
      }
    }
  } else {
    for(var i = 0; i < allWeights.length; i++ ) {
      allWeights[i] = Math.abs((allWeights[i]).toPrecision(4));
    }
  }

  //normalization all weights b/w 2-14
  var totalScale = d3.scale.linear()
        .domain(d3.extent(allWeights))
        .range([2, 14]),

      normalizedScale = d3.scale.linear()
        .domain(d3.extent(allWeights)),

      unweighted = false;

  //if unweighted, weight is 2
  if (sheetType === 'unweighted') {
    totalScale = d3.scale.quantile()
      .domain(d3.extent(allWeights))
      .range(["2"]);

    normalizedScale = normalizedScale.range(["2"]);
    unweighted = true;
    $(".normalization-form").append("placeholder='unweighted'");
  } else if (sheetType === 'weighted') {
    $(".normalization-form").append("placeholder='weighted'");
  }

  var getEdgeThickness = function (edge) {
        return Math.round(totalScale(Math.abs(edge.value)));
      };

  var force = d3.layout.force()
      .size([width, height])
      .on("tick", tick)
      .linkDistance($("#linkDistInput").val())
      .charge($("#chargeInput").val())
      .chargeDistance($("#chargeDistInput").val())
      .gravity($("#gravityInput").val());

  var drag = force.drag()
      .origin(function(d) { return d; })
      .on("dragstart", dragstart);

  var zoom = d3.behavior.zoom()
    .center([width / 2, height / 2])
    .scaleExtent([MIN_SCALE, MAX_SCALE])
    .on("zoom", zoomed);

  var svg = d3.select($container[0]).append("svg")
        .attr("width", width)
        .attr("height", height)
        .call(zoom)
      .append("g") // required for zoom to work

  // This rectangle catches all of the mousewheel and pan events, without letting
  // them bubble up to the body.
  var innerRect = svg.append("rect")
                     .attr("width", width)
                     .attr("height", height)
                     .style("fill", "none")
                     .style("pointer-events", "all")
                     .attr("stroke", adaptive ? "none" : "#9A9A9A")
                     .append("g")


  function zoomed() {
    // These comments are for when we restrict the zoom to the available space.
    // var tx = Math.min(0, d3.event.translate[0]);
    // var ty = Math.min(0, d3.event.translate[1]);
    // svg.attr("transform", "translate(" + [tx, ty] + ")scale(" + d3.event.scale + ")");
    svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
  }

  d3.selectAll(".scrollBtn").on("click", null); // Remove event handlers, if there were any.

  // TODO: Make this less bad
  d3.select(".scrollUp").on("click", function () {
    move("up", true);
  });

  d3.select(".scrollLeft").on("click", function () {
    move("left", true);
  });

  d3.select(".scrollRight").on("click", function () {
    move("right", true);
  });

  d3.select(".scrollDown").on("click", function () {
    move("down", true);
  });

  d3.select("#zoomIn").on("click", function () {
    move("in", false);
  });

  d3.select("#zoomOut").on("click", function () {
    move("out", false);
  });

  d3.selectAll(".boundBoxSize").on("click", function () {
    var newWidth = d3.select(".grnsight-container").style("width");
    var newHeight = d3.select(".grnsight-container").style("height");
    var BORDER_OFFSET = 4;

    // Remove trailing "px"
    newWidth = newWidth.substring(0, newWidth.length - 2) - BORDER_OFFSET;
    newHeight = newHeight.substring(0, newHeight.length - 2) - BORDER_OFFSET;

    if (adaptive) {
      width = (width < newWidth) ? newWidth : width;
      height = (height < newHeight) ? newHeight : height;
    } else {
      width = newWidth;
      height = newHeight;
    }

    d3.select("svg").attr("width", width).attr("height", height);
    d3.select("rect").attr("width", width).attr("height", height);
    force.size([width, height]).resume();
  });

  d3.selectAll("input[name=viewport]").on("change", function () {
    let value = $(this).attr("value");
    if (!adaptive && value === "viewportAdapt") {
      adaptive = true;
      d3.select("rect").attr("stroke", "none");
    } else if (adaptive && value === "viewportHard") {
      var BORDER_OFFSET = 4;
      var newWidth = d3.select(".grnsight-container").style("width");
      var newHeight = d3.select(".grnsight-container").style("height");
      adaptive = false;
      d3.select("rect").attr("stroke", "#9A9A9A");
      width = newWidth.substring(0, newWidth.length - 2) - BORDER_OFFSET;
      height = newHeight.substring(0, newHeight.length - 2) - BORDER_OFFSET;
      force.size([width, height]).resume();
    }
  });

  /* Credit to https://bl.ocks.org/mbostock/7ec977c95910dd026812 */
  function move(direction, isMove) {
    svg.call(zoom.event);
    var currentTransform = d3.transform(svg.attr("transform"));
    var currentTranslate = [0, 0];
    var currentScale = 1;
    if (currentTransform) {
      currentTranslate = d3.transform(currentTransform).translate;
      currentScale = d3.transform(currentTransform).scale[0]; // x and y scale will always be equal
    }
    if (isMove) {
      currentTranslate[0] += (direction === "left" ? 50 : (direction === "right" ? -50 : 0));
      currentTranslate[1] += (direction === "up" ? 50 : (direction === "down" ? -50 : 0));
    } else {
      currentScale += (direction === "in" ? 0.05 : -0.05)
      if (currentScale > MAX_SCALE) {
        currentScale = MAX_SCALE;
      } else if (currentScale < MIN_SCALE) {
        currentScale = MIN_SCALE;
      }
    }
    zoom.scale(currentScale);
    zoom.translate(currentTranslate);
    svg.transition().call(zoom.event);
  }

  var defs = svg.append("defs");

{'fraction_non_alphanumeric': 0.18045801526717556, 'fraction_numerical': 0.021908396946564886, 'mean_word_length': 2.9212810535767733, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '218850', 'n_tokens_mistral': 4456, 'n_tokens_neox': 4203, 'n_words': 904}