<commit_before>// The functions in this file manages all the behaviour uploads / cancels to trim.
// in both the new and in progress tables on the dashboard
'use strict';

var trimLink = {};



trimLink.setUpTrimControls = function() {
  var $fileField, $status, $choices;

  // check if we're on the details page
  if (document.getElementById('pq-details')) {
    trimLink.setUpDetailsPage();
  }

  // check if we're on the dashboard
  if (document.getElementById('dashboard')) {
    trimLink.setUpDashBoard();
  }
};


/*
 * sets up trim file upload controls on a PQ details page
 */
trimLink.setUpDetailsPage = function() {
  var $fileField = $('input[type=file]');
  var $status = $('#status');
  var $choices = $('#choices');

  $status.hide();

  $choices.on('click', function () {
    $('#pq_trim_link_attributes_file').click();
  });

  $('#cancel').on('click', function () {
    $('form.progress-menu-form').trigger('reset');
    $status.hide();
    $choices.show();
  });

  $fileField.on('change', function(){
    var selectedPath = $fileField.val();
    var selectedFileName;
    var $messageContainer = $('#status');
    var $uploadMessage = $('#tr5-message');
    var $chooseBtn = $('#choices');
    if(selectedPath) {

      // If a file is selected successfully, show a message
      selectedFileName = selectedPath.split(/[\\/]/).pop();
      $chooseBtn.hide();
      $uploadMessage.text('File selected: ' + selectedFileName);
      $messageContainer.show();
    }
  });
}


/*
 * sets up trim file upload controls on the dashboard
 */
trimLink.setUpDashBoard = function() {

  // loop through all the trimlink upload controls visible on the dashboard
  $('.trim-links-form').each(function(i, area) {
    var $container = $(area);
    var $messageContainer = $container.find('.tr5-message');
    var $messageIcon = $messageContainer.find('.fa');
    var $uploadMessage = $messageIcon.siblings('.message');
    var $actions = $container.find('.tr5-actions');
    var $chooseButton = $container.find('.button-choose');
    var $form = $container.find('form');
    var $fileField = $form.find('.trim-file-chooser');
    var $cancelButton = $container.find('.button-cancel');
    var statusMessages = {
      selected : {
        message : 'File selected',
        classname : 'fa fa-file-o'
      },
      uploading : {
        message : 'Uploading',
        classname : 'fa fa-spin fa-circle-o-notch'
      },
      success : {
        classname : 'fa fa-check-circle'
      },
        failure : {
        classname : 'fa fa-warning'
      }
    };

    // called when a file has been chosen by the user
    function fileSelectedCallback() {
      var chosen = $fileField.val();
      var selectedFileName;
      if(chosen) {
        // If a file is selected successfully, show a message
        selectedFileName = chosen.split(/[\\/]/).pop();
        $chooseButton.hide();
        $messageIcon[0].className = statusMessages.selected.classname;
        $uploadMessage.text(statusMessages.selected.message +': '+selectedFileName);
        $messageContainer.show();
        $actions.show();
      }
    }

    $cancelButton.on('click', function () {
      $form.trigger('reset');
      $chooseButton.show();
      $actions.hide();
      $messageContainer.hide();
      $actions.find('.button-upload').show();
    });

    // File selector behaviour
    $fileField.on('change', fileSelectedCallback);

    //clicking on the "choose trim file" button
    $chooseButton.on('click', function () {
      $fileField.click();
    });

    // trim file async upload callbacks
    $form
      .on('ajax:error', function(e, response) {
        var json = JSON.parse(response.responseText);

        if (json.status === 200) {
          $messageIcon.attr('class', statusMessages.success.classname);
          $actions
            .hide()
            .after('<a href="'+ json.link +'" rel="external">Open trim link</a>');
        } else {
          $messageIcon.attr('class', statusMessages.failure.classname);<commit_msg>added logic to trap files over 50KB<commit_after>// The functions in this file manages all the behaviour uploads / cancels to trim.
// in both the new and in progress tables on the dashboard
'use strict';

var trimLink = {};



trimLink.setUpTrimControls = function() {
  var $fileField, $status, $choices;

  // check if we're on the details page
  if (document.getElementById('pq-details')) {
    trimLink.setUpDetailsPage();
  }

  // check if we're on the dashboard
  if (document.getElementById('dashboard')) {
    trimLink.setUpDashBoard();
  }
};


/*
 * sets up trim file upload controls on a PQ details page
 */
trimLink.setUpDetailsPage = function() {
  var $fileField = $('input[type=file]');
  var $status = $('#status');
  var $choices = $('#choices');

  $status.hide();

  $choices.on('click', function () {
    $('#pq_trim_link_attributes_file').click();
  });

  $('#cancel').on('click', function () {
    $('form.progress-menu-form').trigger('reset');
    $status.hide();
    $choices.show();
  });

  $fileField.on('change', function(){
    var selectedPath = $fileField.val();
    var selectedFileName;
    var $messageContainer = $('#status');
    var $uploadMessage = $('#tr5-message');
    var $chooseBtn = $('#choices');
    var filesize = this.files[0].size; //(In Bytes)
    

    if(selectedPath) {

      // If a file is selected successfully, show a message
      selectedFileName = selectedPath.split(/[\\/]/).pop();
      $chooseBtn.hide();

      if(filesize > 50000) {
        $('.fa-file-o').toggleClass('fa-warning');
        $uploadMessage.text('This file is too large: ' + selectedFileName);
      } else {
        $uploadMessage.text('File selected: ' + selectedFileName);
      }
      $messageContainer.show();
    }
  });
}


/*
 * sets up trim file upload controls on the dashboard
 */
trimLink.setUpDashBoard = function() {

  // loop through all the trimlink upload controls visible on the dashboard
  $('.trim-links-form').each(function(i, area) {
    var $container = $(area);
    var $messageContainer = $container.find('.tr5-message');
    var $messageIcon = $messageContainer.find('.fa');
    var $uploadMessage = $messageIcon.siblings('.message');
    var $actions = $container.find('.tr5-actions');
    var $chooseButton = $container.find('.button-choose');
    var $form = $container.find('form');
    var $fileField = $form.find('.trim-file-chooser');
    var $cancelButton = $container.find('.button-cancel');
    var statusMessages = {
      selected : {
        message : 'File selected',
        classname : 'fa fa-file-o'
      },
      uploading : {
        message : 'Uploading',
        classname : 'fa fa-spin fa-circle-o-notch'
      },
      success : {
        classname : 'fa fa-check-circle'
      },
      failure : {
        classname : 'fa fa-warning'
      },
      filesize : {
        message : 'This file is too large',
        classname : 'fa fa-warning'
      }
    };

    // called when a file has been chosen by the user
    function fileSelectedCallback() {
      var chosen = $fileField.val();
      var selectedFileName;
      var filesize = this.files[0].size; //(In Bytes)
      var statusMessage;

      if(chosen) {
        // If a file is selected successfully, show a message
        selectedFileName = chosen.split(/[\\/]/).pop();
        $chooseButton.hide();
       
        if(filesize > 50000) {
          statusMessage = statusMessages.filesize;
        } else {
          statusMessage = statusMessages.selected;
        }

        $messageIcon[0].className = statusMessage.classname;
        $uploadMessage.text(statusMessage.message +': '+selectedFileName);
        $actions.find('.button-upload').show();
        $messageContainer.show();
        $actions.show(); 
      }
    }

    $cancelButton.on('click', function () {
      $form.trigger('reset');
      $chooseButton.show();
      $actions.hide();
      $messageContainer.hide();
      $actions.find('.button-upload').show();
    });

    // File selector behaviour
    $fileField.on('change', fileSelectedCallback);

    //clicking on the "choose trim file" button
    $chooseButton.on('click', function () {
      $fileField.click();
    });

    // trim file async upload callbacks
    $form
      .on('ajax:error', function(e, response) {
        var json = JSON.parse(response.responseText);

        if (json.status === 200) {
          $messageIcon.attr('class', statusMessages.success.classname);
          $actions
            .hide()
            .after('<a href="'+ json.link +'" rel="external">Open trim link</a>');
        } else {
          $messageIcon.attr('class', statusMessages.failure.classname);
{'fraction_non_alphanumeric': 0.1581550494593973, 'fraction_numerical': 0.00322061191626409, 'mean_word_length': 2.720581942661532, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3610714', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2496, 'n_words': 603}