<commit_before>(function($, exports) {
  if (typeof exports.Dexter == 'undefined') exports.Dexter = {};
  var Dexter = exports.Dexter;

  // view when editing the document analysis
  Dexter.EditDocumentAnalysisView = function() {
    var self = this;

    self.init = function() {
      self.$form = $('form.edit-analysis');
      if (self.$form.length === 0) {
        return;
      }

      $('button.submit').on('click', function(e) {
        self.$form.submit();
      });

      // flag functionality
      $('label.article-flag input[type=checkbox]').on('change', function(e) {
        var checked = $(this).prop("checked");

        $('.article-flag i.fa').toggleClass('flag-set', checked);
        self.$form.find('[name="notes"]')
          .removeClass('hidden')
          .toggle(checked);
      });

      self.$form
        .on('ajax:success', function(e, data, status, xhr) {
          // success, reload the page
          $('input[data-disable-with]', self.$form).removeAttr('data-disable-with');
          document.location = document.location;
        })
        .on('ajax:error', function(e, xhr, status, error) {
          console.log(xhr.status);
          if (xhr.status == 500) {
            $('#error-box')
              .text('Hmm, something went wrong, please try again. (' + xhr.status + ': ' + error + ')')
              .show();
            $('html, body').animate({
              scrollTop: 0,
            }, 300);
          } else {
            // problem, do a non-ajax submit
            $('input[data-disable-with]', self.$form).removeAttr('data-disable-with');
            self.$form.removeData('remote').removeAttr('data-remote').submit();
          }
        });

      // source person name autocomplete
      self.personHound = new Bloodhound({
        name: 'people',
        remote: {
          url: '/api/people?limit=5&q=%QUERY',
          ajax: {beforeSend: function(xrh) { self.ttShowSpinner(); }},
          filter: function(resp) { self.ttHideSpinner(); return resp.people; },
        },
        datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
        queryTokenizer: Bloodhound.tokenizers.whitespace
      });
      self.personHound.initialize();

      self.newSourceCount = $('.sources tr.new', self.$form).length;

      $('.btn.add-source').on('click', self.addSource);
      $('table.sources', self.$form).
        on('click', '.btn.delete', self.deleteSource).
        on('click', '.btn.undo-delete', self.undoDeleteSource).
        on('change', 'input:radio[name$="-source_type"]', self.toggleSourceType).
        on('change', 'input:checkbox[name$="-named"]', self.toggleAnonymous);

      self.newFairnessCount = $('.fairness tr.new', self.$form).length;
      $('table.fairness', self.$form).
        on('change', '.template select', self.addNewFairness).
        on('click', '.btn.delete', self.deleteFairness).
        on('click', '.btn.undo-delete', self.undoDeleteFairness);

      $('.suggested-sources .source').on('click', self.addSuggestedSource);
    };
     
    self.addSuggestedSource = function(e) {
      e.preventDefault();

      var $row = self.addSource(e);
      var person = {
        gender: $(this).data('gender'),
        race: $(this).data('race'),
        name: $(this).data('name'),
        quoted: $(this).data('quoted') == '1',
        affiliation: '',
      };

      var $name = $('input[name$="-name"]', $row);
      $name
        .typeahead('val', person.name)
        .typeahead('close');

      $(this).closest('li').hide();

      self.personSourceChosen.call($name, e, person);
    };

    self.addSource = function(e) {
      e.preventDefault();<commit_msg>Make form submission more tolerant of failure.<commit_after>(function($, exports) {
  if (typeof exports.Dexter == 'undefined') exports.Dexter = {};
  var Dexter = exports.Dexter;

  // view when editing the document analysis
  Dexter.EditDocumentAnalysisView = function() {
    var self = this;

    self.init = function() {
      self.$form = $('form.edit-analysis');
      if (self.$form.length === 0) {
        return;
      }

      self.$form.on('submit', self.submitForm);
      self.$submitButton = $('button.submit');
      self.$submitButton.on('click', function(e) {
        self.$form.submit();
      });

      // flag functionality
      $('label.article-flag input[type=checkbox]').on('change', self.toggleFlag);
      
      self.$form
        .on('ajax:success', self.formSubmitSuccess)
        .on('ajax:error', self.formSubmitFail);
        
      // source person name autocomplete
      self.personHound = new Bloodhound({
        name: 'people',
        remote: {
          url: '/api/people?limit=5&q=%QUERY',
          ajax: {beforeSend: function(xrh) { self.ttShowSpinner(); }},
          filter: function(resp) { self.ttHideSpinner(); return resp.people; },
        },
        datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
        queryTokenizer: Bloodhound.tokenizers.whitespace
      });
      self.personHound.initialize();

      self.newSourceCount = $('.sources tr.new', self.$form).length;

      $('.btn.add-source').on('click', self.addSource);
      $('table.sources', self.$form).
        on('click', '.btn.delete', self.deleteSource).
        on('click', '.btn.undo-delete', self.undoDeleteSource).
        on('change', 'input:radio[name$="-source_type"]', self.toggleSourceType).
        on('change', 'input:checkbox[name$="-named"]', self.toggleAnonymous);

      self.newFairnessCount = $('.fairness tr.new', self.$form).length;
      $('table.fairness', self.$form).
        on('change', '.template select', self.addNewFairness).
        on('click', '.btn.delete', self.deleteFairness).
        on('click', '.btn.undo-delete', self.undoDeleteFairness);

      $('.suggested-sources .source').on('click', self.addSuggestedSource);
    };

    self.submitForm = function(e) {
      // disable the button
      self.$submitButton
        .data('enable-with', self.$submitButton.html())
        .prop('disabled', true)
        .html(self.$submitButton.data('disable-with'));
    };

    self.formSubmitSuccess = function(e, data, status, xhr) {
      // success, reload the page
      $('input[data-disable-with]', self.$form).removeAttr('data-disable-with');
      document.location = document.location;
    };

    self.formSubmitFail = function(e, xhr, status, error) {
      console.log(xhr.status);

      if (xhr.status == 500) {
        $('#error-box')
          .text('Hmm, something went wrong, please try again. (' + xhr.status + ': ' + error + ')')
          .show();

        $('html, body').animate({
          scrollTop: 0,
        }, 300);

        // re-enable the button
        self.$submitButton
          .prop('disabled', false)
          .html(self.$submitButton.data('enable-with'));

      } else {
        // problem, do a non-ajax submit
        $('input[data-disable-with]', self.$form).removeAttr('data-disable-with');
        self.$form.removeData('remote').removeAttr('data-remote').submit();
      }
    };

    self.toggleFlag = function(e) {
      var checked = $(this).prop("checked");

      $('.article-flag i.fa').toggleClass('flag-set', checked);
      self.$form.find('[name="notes"]')
        .removeClass('hidden')
        .toggle(checked);
    };
     
    self.addSuggestedSource = function(e) {
      e.preventDefault();

      var $row = self.addSource(e);
      var person = {
        gender: $(this).data('gender'),
        race: $(this).data('race'),
        name: $(this).data('name'),
        quoted: $(this).data('quoted') == '1',
        affiliation: '',
      };

      var $name = $('input[name$="-name"]', $row);
      $name
        .typeahead('val', person.name)
        .typeahead('close');

      $(this).closest('li').hide();

      self.personSourceChosen.call($name, e, person);
    };

    self.addSource = function(e) {
      e.preventDefault();
{'fraction_non_alphanumeric': 0.18561746987951808, 'fraction_numerical': 0.0025100401606425703, 'mean_word_length': 2.461772371850565, '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': '3474451', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2359, 'n_words': 421}