<commit_before>image_ids = []
$(document).ready( function() {
  fixButtons();

  $(".add-gallery-icon").click(function() {
    $(this).toggleClass('selected-icon');
    if ($(this).hasClass('selected-icon')) {
      image_ids.push(this.dataset['id']);
    } else {
      image_ids.pop(this.dataset['id']);
    }
  });

  $("#add-gallery-icons").submit(function() {
    if (image_ids.length < 1) { return false; }
    $("#image_ids").val(image_ids);
    return true;
  });
});

function bindAdd() { 
  $(".icon-row-add").click(function () {
    var new_row = $(".icon-row:last").clone();
    var index = new_row.data('index') + 1;
    var inputs = new_row.find('input');
    var urlField = inputs.first();
    var fileField = $(inputs.get(1));

    new_row.attr('data-index', index);
    inputs.val('');

    new_row.find('.conf').remove();
    urlField.show();
    urlField.attr('id', 'icons_'+index+'_url');

    fileField.attr('id', 'icons_'+index+'_file');
    fileField.attr('data-index', index);
    bindFileInput(inputs.get(1));

    new_row.insertBefore($(".submit-row"));
    fixButtons();
  });
};

function bindRem() {
  $(".icon-row-rem").click(function () {
    var rem_row = $(this).parent().parent();
    rem_row.remove();
    fixButtons();
  });
};

function fixButtons() {
  $(".icon-row-add").hide().unbind();
  $(".icon-row-add").last().show();
  $(".icon-row-rem").show();
  $(".icon-row-rem").first().hide();
  bindAdd();
  bindRem();
  $("#icon-table tr.icon-row:odd td").removeClass('even').addClass("odd");
  $("#icon-table tr.icon-row:even td").removeClass('odd').addClass("even");
};

$(function() {
  $('.icon-upload').find("input:file").each(function(i, elem) {
    bindFileInput(elem);
  });
});

function bindFileInput(elem) {
    var fileInput    = $(elem);
    var form         = $('form.icon-upload');
    var submitButton = form.find('input[type="submit"]');
    var iconIndex    = $(elem).data('index');
    var formData     = form.data('form-data');

    fileInput.fileupload({
      fileInput:       fileInput,
      url:             form.data('url'),
      type:            'POST',
      autoUpload:       true,
      formData:         formData,
      paramName:        'file', // S3 does not like nested name fields i.e. name="user[avatar_url]"
      dataType:         'XML',  // S3 returns XML if success_action_status is set to 201
      replaceFileInput: false,
      add: function (e, data) {
        formData["Content-Type"] = data.files[0].type; 
        data.formData = formData;
        data.submit();
      },
      start: function (e) {
        submitButton.prop('disabled', true);
      },
      done: function(e, data) {
        submitButton.prop('disabled', false);<commit_msg>Improve pre-processing on image types to detect non-images and .tiff images<commit_after>image_ids = []
$(document).ready( function() {
  fixButtons();

  $(".add-gallery-icon").click(function() {
    $(this).toggleClass('selected-icon');
    if ($(this).hasClass('selected-icon')) {
      image_ids.push(this.dataset['id']);
    } else {
      image_ids.pop(this.dataset['id']);
    }
  });

  $("#add-gallery-icons").submit(function() {
    if (image_ids.length < 1) { return false; }
    $("#image_ids").val(image_ids);
    return true;
  });
});

function bindAdd() { 
  $(".icon-row-add").click(function () {
    var new_row = $(".icon-row:last").clone();
    var index = new_row.data('index') + 1;
    var inputs = new_row.find('input');
    var urlField = inputs.first();
    var fileField = $(inputs.get(1));

    new_row.attr('data-index', index);
    inputs.val('');

    new_row.find('.conf').remove();
    urlField.show();
    urlField.attr('id', 'icons_'+index+'_url');

    fileField.attr('id', 'icons_'+index+'_file');
    fileField.attr('data-index', index);
    bindFileInput(inputs.get(1));

    new_row.insertBefore($(".submit-row"));
    fixButtons();
  });
};

function bindRem() {
  $(".icon-row-rem").click(function () {
    var rem_row = $(this).parent().parent();
    rem_row.remove();
    fixButtons();
  });
};

function fixButtons() {
  $(".icon-row-add").hide().unbind();
  $(".icon-row-add").last().show();
  $(".icon-row-rem").show();
  $(".icon-row-rem").first().hide();
  bindAdd();
  bindRem();
  $("#icon-table tr.icon-row:odd td").removeClass('even').addClass("odd");
  $("#icon-table tr.icon-row:even td").removeClass('odd').addClass("even");
};

$(function() {
  $('.icon-upload').find("input:file").each(function(i, elem) {
    bindFileInput(elem);
  });
});

function bindFileInput(elem) {
    var fileInput    = $(elem);
    var form         = $('form.icon-upload');
    var submitButton = form.find('input[type="submit"]');
    var iconIndex    = $(elem).data('index');
    var formData     = form.data('form-data');

    fileInput.fileupload({
      fileInput:       fileInput,
      url:             form.data('url'),
      type:            'POST',
      autoUpload:       true,
      formData:         formData,
      paramName:        'file', // S3 does not like nested name fields i.e. name="user[avatar_url]"
      dataType:         'XML',  // S3 returns XML if success_action_status is set to 201
      replaceFileInput: false,
      add: function (e, data) {
        var fileType = data.files[0].type;
        if (!fileType.startsWith('image/')) {
          alert("You must upload files with an image filetype such as .png or .jpg - please retry with a valid file.");
          return;
        } else if (fileType === 'image/tiff') {
          alert("Unfortunately, .tiff files are only supported by Safari - please retry with a valid file.");
          return
        }
        formData["Content-Type"] = fileType;
        data.formData = formData;
        data.submit();
      },
      start: function (e) {
        submitButton.prop('disabled', true);
      },
      done: function(e, data) {
        submitButton.prop('disabled', false);
{'fraction_non_alphanumeric': 0.20256800135157965, 'fraction_numerical': 0.0033789491468153403, 'mean_word_length': 2.8566775244299674, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3855126', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1902, 'n_words': 353}