<commit_before>/**
 * Manage files associated with datasets
 * save RELATIVE path
 *
 * @param collectionId
 * @param data
 * @param field - JSON data key
 * @param idField - HTML id for the template
 */

function addSupplementaryFile(collectionId, data, field, idField) {
  var list = data[field];
  var downloadExtensions;
  var dataTemplate = {list: list, idField: idField};
  var html = templates.editorSupplementaryFiles(dataTemplate);
  $('#' + idField).replaceWith(html);
  var uriUpload;

  $(".workspace-edit").scrollTop(Florence.globalVars.pagePos);

  //Edit saved from editor

  // Delete
  if (!data[field] || data[field].length === 0) {
    var lastIndex = 0;
  } else {
    $(data[field]).each(function (index) {
      // Delete
      $('#' + idField + '-delete_' + index).click(function () {
        var result = confirm("Are you sure you want to delete this file?");
        if (result === true) {
          var position = $(".workspace-edit").scrollTop();
          var fileToDelete = data.uri + data[field][index].file
          Florence.globalVars.pagePos = position;
          $(this).parent().remove();
          $.ajax({
            url: "/zebedee/content/" + collectionId + "?uri=" + fileToDelete,
            type: "DELETE",
            success: function (res) {
              console.log(res);
            },
            error: function (res) {
              console.log(res);
            }
          });
          data[field].splice(index, 1);
          updateContent(collectionId, data.uri, JSON.stringify(data));
        }
      });
    });
  }

  //Add
  if (data.type === 'dataset') {
    downloadExtensions = /\.csv$|.xls$|.zip$/;
  } else if (data.type === 'timeseries_dataset') {
    downloadExtensions = /\.csdb$/;
  } else {
    alert('This file type is not valid. Contact an administrator to add this type of file in this document');
  }

  $('#add-' + idField).one('click', function () {
      var position = $(".workspace-edit").scrollTop();
      Florence.globalVars.pagePos = position + 200;
      $('#sortable-' + idField).append(
        '<div id="' + lastIndex + '" class="edit-section__item">' +
        '  <form id="UploadForm">' +
        '    <input type="file" title="Select a file and click Submit" name="files">' +
        '    <br>' +
        '    <button type="submit" form="UploadForm" value="submit">Submit</button>' +
        '    <button class="btn-page-cancel" id="file-cancel">Cancel</button>' +
        '  </form>' +
        '  <div id="response"></div>' +
        '  <ul id="list"></ul>' +
        '</div>');

      $('#file-cancel').one('click', function (e) {
        e.preventDefault();
        $('#' + lastIndex).remove();
        addFile(collectionId, data, field, idField);
      });

      $('#UploadForm').submit(function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();

        var formdata = new FormData();

        function showUploadedItem(source) {
          $('#list').append(source);
        }

        var file = this[0].files[0];
        if(!file) {
          alert('Please select a file to upload.');
          return;
        }

        document.getElementById("response").innerHTML = "Uploading . . .";

        var fileNameNoSpace = file.name.replace(/\s*/g, "").toLowerCase();
        uriUpload = data.uri + "/" + fileNameNoSpace;
        var safeUriUpload = checkPathSlashes(uriUpload);

        if (data[field] && data[field].length > 0) {
          $(data[field]).each(function (i, filesUploaded) {
            if (filesUploaded.file == safeUriUpload) {
              alert('This file already exists');
              $('#' + lastIndex).remove();
              addFile(collectionId, data, field, idField);
              return;
            }
          });
        }
        if (!!file.name.match(downloadExtensions)) {
          showUploadedItem(fileNameNoSpace);
          if (formdata) {
            formdata.append("name", file);
          }
        } else {
          alert('This file type is not supported');
          $('#' + lastIndex).remove();
          addFile(collectionId, data, field, idField);
          return;
        }

        if (formdata) {
          $.ajax({
            url: "/zebedee/content/" + collectionId + "?uri=" + safeUriUpload,
            type: 'POST',
            data: formdata,
            cache: false,
            processData: false,
            contentType: false,
            success: function () {
              document.getElementById("response").innerHTML = "File uploaded successfully";
              if (!data[field]) {
                data[field] = [];
              }
              data[field].push({title: '', file: '/' + fileNameNoSpace});
              updateContent(collectionId, data.uri, JSON.stringify(data));
            }
          });
        }
      });
    }
  );

  $(function () {
    $('.add-tooltip').tooltip({
      items: '.add-tooltip',
      content: 'Type title here and click Save to add it to the page',
      show: "slideDown", // show immediately
      open: function (event, ui) {
        ui.tooltip.hover(
          function () {
            $(this).fadeTo("slow", 0.5);
          });
      }
    });
  });

  function sortable() {
    $('#sortable-' + idField).sortable();
  }

  sortable();
}

<commit_msg>Add more extensions to supplementary files<commit_after>/**
 * Manage files associated with datasets
 * save RELATIVE path
 *
 * @param collectionId
 * @param data
 * @param field - JSON data key
 * @param idField - HTML id for the template
 */

function addSupplementaryFile(collectionId, data, field, idField) {
  var list = data[field];
  var downloadExtensions;
  var dataTemplate = {list: list, idField: idField};
  var html = templates.editorSupplementaryFiles(dataTemplate);
  $('#' + idField).replaceWith(html);
  var uriUpload;

  $(".workspace-edit").scrollTop(Florence.globalVars.pagePos);

  //Edit saved from editor

  // Delete
  if (!data[field] || data[field].length === 0) {
    var lastIndex = 0;
  } else {
    $(data[field]).each(function (index) {
      // Delete
      $('#' + idField + '-delete_' + index).click(function () {
        var result = confirm("Are you sure you want to delete this file?");
        if (result === true) {
          var position = $(".workspace-edit").scrollTop();
          var fileToDelete = data.uri + data[field][index].file
          Florence.globalVars.pagePos = position;
          $(this).parent().remove();
          $.ajax({
            url: "/zebedee/content/" + collectionId + "?uri=" + fileToDelete,
            type: "DELETE",
            success: function (res) {
              console.log(res);
            },
            error: function (res) {
              console.log(res);
            }
          });
          data[field].splice(index, 1);
          updateContent(collectionId, data.uri, JSON.stringify(data));
        }
      });
    });
  }

  //Add
  downloadExtensions = /\.csv$|.xls$|.doc$|.pdf$|.zip$/;

  $('#add-' + idField).one('click', function () {
      var position = $(".workspace-edit").scrollTop();
      Florence.globalVars.pagePos = position + 200;
      $('#sortable-' + idField).append(
        '<div id="' + lastIndex + '" class="edit-section__item">' +
        '  <form id="UploadForm">' +
        '    <input type="file" title="Select a file and click Submit" name="files">' +
        '    <br>' +
        '    <button type="submit" form="UploadForm" value="submit">Submit</button>' +
        '    <button class="btn-page-cancel" id="file-cancel">Cancel</button>' +
        '  </form>' +
        '  <div id="response"></div>' +
        '  <ul id="list"></ul>' +
        '</div>');

      $('#file-cancel').one('click', function (e) {
        e.preventDefault();
        $('#' + lastIndex).remove();
        addFile(collectionId, data, field, idField);
      });

      $('#UploadForm').submit(function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();

        var formdata = new FormData();

        function showUploadedItem(source) {
          $('#list').append(source);
        }

        var file = this[0].files[0];
        if(!file) {
          alert('Please select a file to upload.');
          return;
        }

        document.getElementById("response").innerHTML = "Uploading . . .";

        var fileNameNoSpace = file.name.replace(/\s*/g, "").toLowerCase();
        uriUpload = data.uri + "/" + fileNameNoSpace;
        var safeUriUpload = checkPathSlashes(uriUpload);

        if (data[field] && data[field].length > 0) {
          $(data[field]).each(function (i, filesUploaded) {
            if (filesUploaded.file == safeUriUpload) {
              alert('This file already exists');
              $('#' + lastIndex).remove();
              addFile(collectionId, data, field, idField);
              return;
            }
          });
        }
        if (!!file.name.match(downloadExtensions)) {
          showUploadedItem(fileNameNoSpace);
          if (formdata) {
            formdata.append("name", file);
          }
        } else {
          alert('This file type is not supported');
          $('#' + lastIndex).remove();
          addFile(collectionId, data, field, idField);
          return;
        }

        if (formdata) {
          $.ajax({
            url: "/zebedee/content/" + collectionId + "?uri=" + safeUriUpload,
            type: 'POST',
            data: formdata,
            cache: false,
            processData: false,
            contentType: false,
            success: function () {
              document.getElementById("response").innerHTML = "File uploaded successfully";
              if (!data[field]) {
                data[field] = [];
              }
              data[field].push({title: '', file: '/' + fileNameNoSpace});
              updateContent(collectionId, data.uri, JSON.stringify(data));
            }
          });
        }
      });
    }
  );

  $(function () {
    $('.add-tooltip').tooltip({
      items: '.add-tooltip',
      content: 'Type title here and click Save to add it to the page',
      show: "slideDown", // show immediately
      open: function (event, ui) {
        ui.tooltip.hover(
          function () {
            $(this).fadeTo("slow", 0.5);
          });
      }
    });
  });

  function sortable() {
    $('#sortable-' + idField).sortable();
  }

  sortable();
}


{'fraction_non_alphanumeric': 0.1631649961449499, 'fraction_numerical': 0.002120277563608327, 'mean_word_length': 2.0008675534991323, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415703', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2952, 'n_words': 643}