<commit_before>var session_id = Math.floor(0x80000000 * Math.random()).toString(36);
var new_squad = null;
var creating = {};
var needs_update_after_creation = {};

(function() {
  var current_numeric_date = new Date().getDate();
  $('td.day#' + current_numeric_date).addClass('current-day');
})();

if (show === 'reset_password') {
  $('input#reset-new-password').focus();
} else {
  // No way to escape the reset password prompt
  $(window).click(function(event) {
    if ($(event.target).hasClass('modal-overlay')) {
      $('div.modal-overlay').hide();
    }
  });
  $('span.modal-close').click(function() {
    $('div.modal-overlay').hide();
  });
  $(document).keyup(function(e) {
    if (e.keyCode == 27) { // esc key
      $('div.modal-overlay').hide();
    }
  });
}

var original_values = {};
$('textarea').each(function(i, element) {
  original_values[element.id] = element.value;
});

$('textarea').each(function() {
  this.setAttribute('style', 'height: ' + (this.scrollHeight) + 'px');
});
$('table').on('input', 'textarea', function() {
  this.style.height = 'auto';
  this.style.height = (this.scrollHeight) + 'px';
});

var save_attempt_index = 0;
var latest_save_attempt_index = {}; // map from numeric date to save index
function save_entry(textarea_element) {
  if (textarea_element.value.trim() === '') {
    return;
  }
  var id_parts = textarea_element.id.split("_");
  var numeric_date = id_parts[0];
  var entry_id = id_parts[1];
  if (entry_id === "-1") {
    if (creating[numeric_date]) {
      needs_update_after_creation[numeric_date] = true;
      return;
    } else {
      creating[numeric_date] = true;
    }
  }
  var cur_save_attempt_index = save_attempt_index++;
  latest_save_attempt_index[numeric_date] = cur_save_attempt_index;
  var entry = $(textarea_element).closest('div.entry');
  entry.find('img.entry-loading').show();
  $.post(
    'save.php',
    {
      'text': textarea_element.value,
      'day': numeric_date,
      'month': month,
      'year': year,
      'squad': squad,
      'prev_text': original_values[textarea_element.id],
      'session_id': session_id,
      'timestamp': Date.now(),
      'entry_id': entry_id,
    },
    function(data) {
      console.log(data);
      if (data.error === 'concurrent_modification') {
        $('div#concurrent-modification-modal-overlay').show();
        return;
      }
      if (latest_save_attempt_index[numeric_date] === cur_save_attempt_index) {
        entry.find('img.entry-loading').hide();
        if (data.success) {
          entry.find('span.save-error').hide();
        } else {
          entry.find('span.save-error').show();
        }
      }
      if (entry_id === "-1" && data.entry_id) {
        var needs_update = needs_update_after_creation[numeric_date];
        var textarea = $("textarea#" + textarea_element.id);
        if (needs_update && textarea.length === 0) {
          delete_entry(data.entry_id);
        } else {
          textarea_element.id = numeric_date + "_" + data.entry_id;
          textarea.closest('div.entry').find('a.delete-entry-button').after(
            "<a href='#' class='entry-history-button'>" +
            "  <span class='history'>≡</span>" +
            "  <span class='action-links-text'>History</span>" +
            "</a>"
          );
        }
        creating[numeric_date] = false;
        needs_update_after_creation[numeric_date] = false;
        if (needs_update && $("textarea#" + textarea_element.id).length !== 0) {
          save_entry(textarea_element);
        }
      }
    }
  );
}
$('table').on('input', 'textarea', function(event) {
  save_entry(event.target);
});
$('input#refresh-button').click(function() {
  window.location.href = this_url;
});

$('select#squad-nav').change(function(event) {
  new_squad = event.target.value;
  if (new_squad === "0") {
    if (email) {
      $('div#new-squad-modal-overlay').show();
      $('div#new-squad-modal input:visible')
        .filter(function() { return this.value === ""; })
        .first()
        .focus();
    } else {
      $('select#squad-nav').val(squad);
      $('div#login-to-create-squad-modal-overlay').show();
    }
  } else if (authorized_squads[new_squad] !== true) {
    var new_squad_name = $(event.target)
      .find("[value='"+new_squad+"']").text();
    $('div#squad-login-name > div.form-content').text(new_squad_name);<commit_msg>Use a fail function to correctly handle 500s<commit_after>var session_id = Math.floor(0x80000000 * Math.random()).toString(36);
var new_squad = null;
var creating = {};
var needs_update_after_creation = {};

(function() {
  var current_numeric_date = new Date().getDate();
  $('td.day#' + current_numeric_date).addClass('current-day');
})();

if (show === 'reset_password') {
  $('input#reset-new-password').focus();
} else {
  // No way to escape the reset password prompt
  $(window).click(function(event) {
    if ($(event.target).hasClass('modal-overlay')) {
      $('div.modal-overlay').hide();
    }
  });
  $('span.modal-close').click(function() {
    $('div.modal-overlay').hide();
  });
  $(document).keyup(function(e) {
    if (e.keyCode == 27) { // esc key
      $('div.modal-overlay').hide();
    }
  });
}

var original_values = {};
$('textarea').each(function(i, element) {
  original_values[element.id] = element.value;
});

$('textarea').each(function() {
  this.setAttribute('style', 'height: ' + (this.scrollHeight) + 'px');
});
$('table').on('input', 'textarea', function() {
  this.style.height = 'auto';
  this.style.height = (this.scrollHeight) + 'px';
});

var save_attempt_index = 0;
var latest_save_attempt_index = {}; // map from numeric date to save index
function save_entry(textarea_element) {
  if (textarea_element.value.trim() === '') {
    return;
  }
  var id_parts = textarea_element.id.split("_");
  var numeric_date = id_parts[0];
  var entry_id = id_parts[1];
  if (entry_id === "-1") {
    if (creating[numeric_date]) {
      needs_update_after_creation[numeric_date] = true;
      return;
    } else {
      creating[numeric_date] = true;
    }
  }
  var cur_save_attempt_index = save_attempt_index++;
  latest_save_attempt_index[numeric_date] = cur_save_attempt_index;
  var entry = $(textarea_element).closest('div.entry');
  entry.find('img.entry-loading').show();
  $.post(
    'save.php',
    {
      'text': textarea_element.value,
      'day': numeric_date,
      'month': month,
      'year': year,
      'squad': squad,
      'prev_text': original_values[textarea_element.id],
      'session_id': session_id,
      'timestamp': Date.now(),
      'entry_id': entry_id,
    }
  ).done(function(data) {
    console.log(data);
    if (data.error === 'concurrent_modification') {
      $('div#concurrent-modification-modal-overlay').show();
      return;
    }
    if (latest_save_attempt_index[numeric_date] === cur_save_attempt_index) {
      entry.find('img.entry-loading').hide();
      if (data.success) {
        entry.find('span.save-error').hide();
      } else {
        entry.find('span.save-error').show();
      }
    }
    if (entry_id === "-1" && data.entry_id) {
      var needs_update = needs_update_after_creation[numeric_date];
      var textarea = $("textarea#" + textarea_element.id);
      if (needs_update && textarea.length === 0) {
        delete_entry(data.entry_id);
      } else {
        textarea_element.id = numeric_date + "_" + data.entry_id;
        textarea.closest('div.entry').find('a.delete-entry-button').after(
          "<a href='#' class='entry-history-button'>" +
          "  <span class='history'>≡</span>" +
          "  <span class='action-links-text'>History</span>" +
          "</a>"
        );
      }
      creating[numeric_date] = false;
      needs_update_after_creation[numeric_date] = false;
      if (needs_update && $("textarea#" + textarea_element.id).length !== 0) {
        save_entry(textarea_element);
      }
    }
  }).fail(function() {
    if (latest_save_attempt_index[numeric_date] === cur_save_attempt_index) {
      entry.find('img.entry-loading').hide();
      entry.find('span.save-error').show();
    }
  });
}
$('table').on('input', 'textarea', function(event) {
  save_entry(event.target);
});
$('input#refresh-button').click(function() {
  window.location.href = this_url;
});

$('select#squad-nav').change(function(event) {
  new_squad = event.target.value;
  if (new_squad === "0") {
    if (email) {
      $('div#new-squad-modal-overlay').show();
      $('div#new-squad-modal input:visible')
        .filter(function() { return this.value === ""; })
        .first()
        .focus();
    } else {
      $('select#squad-nav').val(squad);
      $('div#login-to-create-squad-modal-overlay').show();
    }
  } else if (authorized_squads[new_squad] !== true) {
    var new_squad_name = $(event.target)
      .find("[value='"+new_squad+"']").text();
    $('div#squad-login-name > div.form-content').text(new_squad_name);
{'fraction_non_alphanumeric': 0.1903265626753451, 'fraction_numerical': 0.005049938278532152, 'mean_word_length': 3.2458313482610768, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6553649', 'n_tokens_mistral': 3067, 'n_tokens_neox': 2983, 'n_words': 452}