<commit_before>/*global $,cyberDojo*/

var cyberDojo = (function(cd, $) {
  "use strict";

  // Builds the diff filenames click handlers for a given kata-id,
  // given animal-name, and given traffic-light number. Clicking
  // on the filename brings it into view by hiding the previous
  // one and showing the selected one.
  
  cd.buildDiffFilenameHandlers = function(diffs) {
    
    var bindLineNumbers = function(filename) {
      var content = cd.fileContentFor(filename);
      var numbers = cd.lineNumbersFor(filename);
      
      function setLine() {
        numbers.scrollTop(content.scrollTop());   
      }
      
      content.bind({
        keydown   : function(ev) { setLine(); },
        scroll    : function(ev) { setLine(); },
        mousewheel: function(ev) { setLine(); },
        mousemove : function(ev) { setLine(); },
        mousedown : function(ev) { setLine(); },
        mouseup   : function(ev) { setLine(); }
      });
    };

    var previousFilenameNode;
    
    var loadFrom = function(filenameNode, diff) {
      // assert filename.val() == diff[:filename]
      
      var diffSheet = cd.fileContentFor(filenameNode.val());
      var sectionIndex = 0;
      var sectionCount = diff.section_count;
      var id = diff.id;
      
      if (sectionCount > 0) {
          filenameNode.parent().attr('title', 'Auto-scroll through diffs');
      }
      
      return function() {
        
        // .hide() and .show() calls restore the cursorPos and scrollPos
        // correctly (automatically) on files that have no diffs.
        // However, if the file has a diff it will auto-scroll to the
        // first diff-section (when opened) rather than leaving the
        // position where it was. This could be refactored so autoscroll
        // only happens when a file is selected for the first time
        // or when the current file is explicitly reselected.
        
        cd.radioEntrySwitch(previousFilenameNode, filenameNode);
        if (previousFilenameNode !== undefined) {
          cd.fileDiv(previousFilenameNode.val()).hide();          
        }
        cd.fileDiv(filenameNode.val()).show();
        previousFilenameNode = filenameNode;
        // some files have no diffs
        if (sectionCount > 0) {
          var section = $('#' + id + '_section_' + sectionIndex);           
          var downFromTop = 100;
          var halfSecond = 500;          
          diffSheet.animate({
            scrollTop: section.offset().top - diffSheet.offset().top - downFromTop
            }, halfSecond);
          sectionIndex += 1;
          sectionIndex %= sectionCount;
        }        
      };
    };
    
    $.each(diffs, function(n, diff) {
      // _filenames.html.erb contains an
      // <input type="radio" id="radio_<%= diff[:id] %>" />
      // for each file in the current diff.
      var filenameNode = $('#radio_' + diff.id);
      filenameNode.parent().click(loadFrom(filenameNode, diff));
      bindLineNumbers(diff.filename);<commit_msg>diff-view autoscroll improvement<commit_after>/*global $,cyberDojo*/

var cyberDojo = (function(cd, $) {
  "use strict";

  // Builds the diff filenames click handlers for a given kata-id,
  // given animal-name, and given traffic-light number. Clicking
  // on the filename brings it into view by hiding the previous
  // one and showing the selected one.
  
  cd.buildDiffFilenameHandlers = function(diffs) {
    
    var bindLineNumbers = function(filename) {
      var content = cd.fileContentFor(filename);
      var numbers = cd.lineNumbersFor(filename);
      
      function setLine() {
        numbers.scrollTop(content.scrollTop());   
      }
      
      content.bind({
        keydown   : function(ev) { setLine(); },
        scroll    : function(ev) { setLine(); },
        mousewheel: function(ev) { setLine(); },
        mousemove : function(ev) { setLine(); },
        mousedown : function(ev) { setLine(); },
        mouseup   : function(ev) { setLine(); }
      });
    };

    var previousFilenameNode;
    var alreadyOpened = [ ];
    
    var loadFrom = function(filenameNode, id, sectionCount) {
      
      var diffSheet = cd.fileContentFor(filenameNode.val());
      var sectionIndex = 0;
      
      if (sectionCount > 0) {
          filenameNode.parent().attr('title', 'Auto-scroll through diffs');
      }
      
      return function() {
        var filename = filenameNode.val();
        var reselected =
          previousFilenameNode !== undefined && previousFilenameNode.val() === filename;
        
        cd.radioEntrySwitch(previousFilenameNode, filenameNode);
        if (previousFilenameNode !== undefined) {
          cd.fileDiv(previousFilenameNode.val()).hide();          
        }
        cd.fileDiv(filenameNode.val()).show();
        previousFilenameNode = filenameNode;
        
        if (sectionCount > 0 && (reselected || !cd.inArray(filename, alreadyOpened))) {
          var section = $('#' + id + '_section_' + sectionIndex);           
          var downFromTop = 150;
          var halfSecond = 500;
          diffSheet.animate({
            scrollTop: section.offset().top - diffSheet.offset().top - downFromTop
            }, halfSecond);
          sectionIndex += 1;
          sectionIndex %= sectionCount;
        }
        alreadyOpened.push(filename);                
      };
    };
    
    $.each(diffs, function(n, diff) {
      // _filenames.html.erb contains an
      // <input id="radio_<%= diff[:id] %>" ... />
      // for each file in the current diff.
      var filenameNode = $('#radio_' + diff.id);
      filenameNode.parent().click(loadFrom(filenameNode, diff.id, diff.section_count));
      // assert filenameNode.val() == diff.filename
      bindLineNumbers(diff.filename);
{'fraction_non_alphanumeric': 0.12246313963573287, 'fraction_numerical': 0.003469210754553339, 'mean_word_length': 2.1803640375068944, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6717300', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1569, 'n_words': 404}