<commit_before>/**
 * Direct file uploads to AWS S3
 *
 * Example Form inputs
 * -------------------
 * <input type="hidden" name="product[image_ids][]" value="1">
 * <input type="file" data-giman-file-upload data-giman-param="product_images" multiple="multiple" data-supported-types="image/jpeg,image/jpg,image/png,image/gif">
 * <div class="file-upload-dropbox" style="height: 120px; border: dashed 2px #ccc;" data-giman-drop-upload data-giman-param="product_images"></div>
 *
 */
$(function() {
  var directUploadId = 0;

  $(document).on("directUpload:started", function(ev, args) {
    console.log("directUpload:started", args);
  });

  $(document).on("directUpload:failed", function(ev, args) {
    console.error("directUpload:failed", args.message);
  });

  $(document).on("directUpload:progress", function(ev, args) {
    console.log("directUpload:progress", args.percentLoaded+"%");
  });

  $(document).on("directUpload:complete", function(ev, args) {
    console.log("directUpload:complete", args.fileData);
  });


  $(document).on("change", "[data-giman-file-upload]", function(ev) {
    var $input = $(ev.currentTarget),
        targetParam = $input.attr("data-giman-param"),
        files = ev.target.files;

    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      directUploadFile(file, $input, targetParam);
    };
  });

  // Disable standard file dragging navigation
  $(document).on("dragenter", function(e) {
    e.stopPropagation();
    e.preventDefault();
  });

  $(document).on("dragover", function(e) {
    e.stopPropagation();
    e.preventDefault();
  });

  $(document).on("click", "[data-giman-drop-upload]", function(ev) {
    ev.preventDefault();
    ev.stopPropagation();

    $(ev.currentTarget).siblings("input[type=file]").click();
  });

  $(document).on("drop", "[data-giman-drop-upload]", function(ev) {
    ev.preventDefault();
    ev.stopPropagation();

    var $input = $(ev.currentTarget),
        targetParam = $input.attr("data-giman-param"),
        dataTransfer = ev.originalEvent.dataTransfer,
        files = dataTransfer.files;

    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      directUploadFile(file, $input, targetParam);
    };
  });

  $(document).on("directUpload:removeFile", function(ev, args) {
    removeHiddenFieldForUpload(args.$form, args.targetParam, args.serverId);
  });

  function getHiddenFieldFor($form, targetParam, serverId) {
    var found = null;

    $form.find('input[name="'+targetParam+'"]').each(function() {
      if (String($(this).val()) === String(serverId)) {
        found = $(this);
      }
    });

    return found;
  }

  function addHiddenFieldForUpload($form, targetParam, serverId) {
    var exists = getHiddenFieldFor($form, targetParam, serverId);

    if (! exists) {
      $form.prepend('<input type="hidden" name="'+targetParam+'" value="'+serverId+'">');
    }
  }

  function removeHiddenFieldForUpload($form, targetParam, serverId) {
    var exists = getHiddenFieldFor($form, targetParam, serverId);
    if (exists) exists.remove();
  }

  function directUploadFile(file, $input, targetParam) {
    var supportedTypes = $input.attr("data-supported-types"),
        uniqueFileId = "directUpload:"+(directUploadId++);

    if (supportedTypes && supportedTypes.split(",").indexOf(file.type) < 0) {
      return $(document).trigger("directUpload:failed", {
        uniqueFileId: uniqueFileId,
        file: file,
        $input: $input,
        message: "Unsupported file type: "+ file.type
      });
    }

    $(document).trigger("directUpload:started", {
      uniqueFileId: uniqueFileId,
      file: file,
      $input: $input
    });

    var objectUrl = URL.createObjectURL(file);

    $(document).trigger("directUpload:previewReady", {
      uniqueFileId: uniqueFileId,
      file: file,
      $input: $input,<commit_msg>Support multiple and single uploads<commit_after>/**
 * Direct file uploads to AWS S3
 *
 * Example Form inputs
 * -------------------
 * <input type="hidden" name="product[image_ids][]" value="1">
 * <input type="file" data-giman-file-upload data-giman-param="product_images" multiple="multiple" data-supported-types="image/jpeg,image/jpg,image/png,image/gif">
 * <div class="file-upload-dropbox" style="height: 120px; border: dashed 2px #ccc;" data-giman-drop-upload data-giman-param="product_images"></div>
 *
 */
$(function() {
  var directUploadId = 0;

  $(document).on("directUpload:started", function(ev, args) {
    console.log("directUpload:started", args);
  });

  $(document).on("directUpload:failed", function(ev, args) {
    console.error("directUpload:failed", args.message);
  });

  $(document).on("directUpload:progress", function(ev, args) {
    console.log("directUpload:progress", args.percentLoaded+"%");
  });

  $(document).on("directUpload:complete", function(ev, args) {
    console.log("directUpload:complete", args.fileData);
  });


  $(document).on("change", "[data-giman-file-upload]", function(ev) {
    var $input = $(ev.currentTarget),
        targetParam = $input.attr("data-giman-param"),
        files = ev.target.files;

    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      directUploadFile(file, $input, targetParam);
    };
  });

  // Disable standard file dragging navigation
  $(document).on("dragenter", function(e) {
    e.stopPropagation();
    e.preventDefault();
  });

  $(document).on("dragover", function(e) {
    e.stopPropagation();
    e.preventDefault();
  });

  $(document).on("click", "[data-giman-drop-upload]", function(ev) {
    ev.preventDefault();
    ev.stopPropagation();

    $(ev.currentTarget).siblings("input[type=file]").click();
  });

  $(document).on("drop", "[data-giman-drop-upload]", function(ev) {
    ev.preventDefault();
    ev.stopPropagation();

    var $input = $(ev.currentTarget),
        targetParam = $input.attr("data-giman-param"),
        dataTransfer = ev.originalEvent.dataTransfer,
        files = dataTransfer.files;

    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      directUploadFile(file, $input, targetParam);
    };
  });

  $(document).on("directUpload:removeFile", function(ev, args) {
    removeHiddenFieldForUpload(args.$form, args.targetParam, args.serverId);
  });

  function getHiddenFieldFor($form, targetParam, serverId) {
    var found = null;

    $form.find('input[name="'+targetParam+'"]').each(function() {
      if (String($(this).val()) === String(serverId)) {
        found = $(this);
      }
    });

    return found;
  }

  function addHiddenFieldForUpload($form, targetParam, serverId) {
    var exists = getHiddenFieldFor($form, targetParam, serverId);

    if (! exists) {
      $form.prepend('<input type="hidden" name="'+targetParam+'" value="'+serverId+'">');
    }
  }

  function removeHiddenFieldForUpload($form, targetParam, serverId) {
    var exists = getHiddenFieldFor($form, targetParam, serverId);
    if (exists) exists.remove();
  }

  function directUploadFile(file, $input, targetParam) {
    var supportedTypes = $input.attr("data-supported-types"),
        uniqueFileId = "directUpload:"+(directUploadId++);

    if (supportedTypes && supportedTypes.split(",").indexOf(file.type) < 0) {
      return $(document).trigger("directUpload:failed", {
        uniqueFileId: uniqueFileId,
        file: file,
        $input: $input,
        message: "Unsupported file type: "+ file.type
      });
    }

    if ( ! $input.attr("multiple")) {
      // Remove all previous IDs
      $input.closest("form").find('input[name="'+targetParam+'"]').remove();
    }

    $(document).trigger("directUpload:started", {
      uniqueFileId: uniqueFileId,
      file: file,
      $input: $input
    });

    var objectUrl = URL.createObjectURL(file);

    $(document).trigger("directUpload:previewReady", {
      uniqueFileId: uniqueFileId,
      file: file,
      $input: $input,
{'fraction_non_alphanumeric': 0.18254068373911947, 'fraction_numerical': 0.0025230225810521003, 'mean_word_length': 3.6911242603550294, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '4542178', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2433, 'n_words': 438}