<commit_before>%h1
  Upload a file
%hr

= form_for @course, url: upload_course_path, method: :post, html: { multipart: true, id: "fileupload" } do |f|
  .row
    %h3
      Course name
    .padding-left
      = @course.name
  
  .row.empty-space

  .row 
    - unless @course.status == 'uploading'
      .alert.alert-success.smaller-text
        To begin an upload, please first select a file from your computer.
    - else
      .alert.alert-warning.smaller-text
        The system detected an unfinished upload. To resume it, please select the same file and press the green button. <br />
        Resume not working or receiving error messages? Then please click #{link_to 'here', reset_upload_course_path(@course)} to delete the temoporary file and start over.

  .row.fileupload-buttonbar
    #js-data{ data: { course: @course.id } }
    .col-lg-7
      .btn.btn-primary.fileinput-button
        %i.glyphicon.glyphicon-plus
        Select a file
        = f.file_field :upload

  %table.table.table-striped#js-file-container{ role: "presentation" }
    %tbody.files

:javascript
  $(document).ready(function () {
    var course_id = $('#js-data').data('course');
    var controller_name = $('body').data('controller');
    var model_instance_path = '/' + controller_name + '/' + course_id;
    var resume_path = model_instance_path + '/resume_upload';
    var update_status_path = model_instance_path + '/update_status';

    $('#fileupload').fileupload({
      maxNumberOfFiles: 1,
      maxChunkSize: 10000000, // 10 MB
      add: function (e, data) {
        var that = this;
        $.getJSON(resume_path, { file: data.files[0].name }, function (result) {
            var file = result.file;
            data.uploadedBytes = file && file.size;
            $.blueimp.fileupload.prototype
                .options.add.call(that, e, data);
        });
      }
    });

    $('#fileupload')
      .bind('fileuploadchunkdone', function (e, data) {
        var perc = parseInt(data.loaded / data.total * 100, 10);
        $('#js-completed').html(perc + "%");
      })
      .bind('fileuploadsubmit', function (e, data) {
        $.ajax({
          url: update_status_path,
          type: "PATCH",
          data: { status: 'uploading' }
        });
      })
      .bind('fileuploaddone', function (e, data) {
        $.ajax({
          url: update_status_path,
          type: "PATCH",
          data: { status: 'uploaded'},
          success: function(data) {
            window.location.replace(model_instance_path);
          } 
        });
      });
    });

:plain
  <script id="template-upload" type="text/x-tmpl">
    {% for (var i=0, file; file=o.files[i]; i++) { %}
        <tr class="template-upload fade">

            <td width="45%">
                <p class="name"><h3>{%=file.name%}</h3></p>
                <p class="size">Processing...</p>
                <strong class="error text-danger"></strong>

                {% if (!i && !o.options.autoUpload) { %}<commit_msg>Remove upload submit callback, use controller action to update status instead<commit_after>%h1
  Upload a file
%hr

= form_for @course, url: upload_course_path, method: :post, html: { multipart: true, id: "fileupload" } do |f|
  .row
    %h3
      Course name
    .padding-left
      = @course.name
  
  .row.empty-space

  .row 
    - unless @course.status == 'uploading'
      .alert.alert-success.smaller-text
        To begin an upload, please first select a file from your computer.
    - else
      .alert.alert-warning.smaller-text
        The system detected an unfinished upload. To resume it, please select the same file and press the green button. <br />
        Resume not working or receiving error messages? Then please click #{link_to 'here', reset_upload_course_path(@course)} to delete the temoporary file and start over.

  .row.fileupload-buttonbar
    #js-data{ data: { course: @course.id } }
    .col-lg-7
      .btn.btn-primary.fileinput-button
        %i.glyphicon.glyphicon-plus
        Select a file
        = f.file_field :upload

  %table.table.table-striped#js-file-container{ role: "presentation" }
    %tbody.files

:javascript
  $(document).ready(function () {
    var course_id = $('#js-data').data('course');
    var controller_name = $('body').data('controller');
    var model_instance_path = '/' + controller_name + '/' + course_id;
    var resume_path = model_instance_path + '/resume_upload';
    var update_status_path = model_instance_path + '/update_status';

    $('#fileupload').fileupload({
      maxNumberOfFiles: 1,
      maxChunkSize: 10000000, // 10 MB
      type: 'PATCH',
      add: function (e, data) {
        var that = this;
        $.getJSON(resume_path, { file: data.files[0].name }, function (result) {
            var file = result.file;
            data.uploadedBytes = file && file.size;
            $.blueimp.fileupload.prototype
                .options.add.call(that, e, data);
        });
      }
    });

    $('#fileupload')
      .bind('fileuploadchunkdone', function (e, data) {
        var perc = parseInt(data.loaded / data.total * 100, 10);
        $('#js-completed').html(perc + "%");
      })
      .bind('fileuploaddone', function (e, data) {
        $.ajax({
          url: update_status_path,
          type: "PATCH",
          data: { status: 'uploaded'},
          success: function(data) {
            window.location.replace(model_instance_path);
          } 
        });
      });
    });

:plain
  <script id="template-upload" type="text/x-tmpl">
    {% for (var i=0, file; file=o.files[i]; i++) { %}
        <tr class="template-upload fade">

            <td width="45%">
                <p class="name"><h3>{%=file.name%}</h3></p>
                <p class="size">Processing...</p>
                <strong class="error text-danger"></strong>

                {% if (!i && !o.options.autoUpload) { %}
{'fraction_non_alphanumeric': 0.14735945485519591, 'fraction_numerical': 0.008517887563884156, 'mean_word_length': 2.309470124013529, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '5088827', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1780, 'n_words': 430}