Sha256: a0881795782293d69ba6a4a99eda0c7460b9b5f986377d8a58d817debf4f9504

Contents?: true

Size: 1.82 KB

Versions: 39

Compression:

Stored size: 1.82 KB

Contents

mumuki.upload = (() => {
  class FileUploader {
    constructor(file) {
      this.file = file;
    }

    uploadFileIfValid() {
      if (!this.file) return;

      let maxFileSize = $('#mu-upload-input').attr("mu-upload-file-limit");
      if (this.file.size > maxFileSize) {
        return mumuki.upload.ui.showFileExceedsMaxSize();
      }

      mumuki.upload.ui.allowSubmissionFor(this.file.name);

      const reader = new FileReader();
      reader.onload = function (e) {
        const contents = e.target.result;
        $('#solution_content').html(contents);
      };
      reader.readAsText(this.file);

      return reader;
    }
  }

  class UI {
    constructor() {
      this.$uploadFileLimitExceeded = $('#mu-upload-file-limit-exceeded');
      this.$uploadLabel = $('#mu-upload-label span');
      this.$uploadLabelText = this.$uploadLabel.text();
      this.$uploadIcon = $('#mu-upload-icon');
      this.$btnSubmit = $('.btn-submit');
    }

    showFileExceedsMaxSize() {
      this.$uploadFileLimitExceeded.removeClass('d-none');
      this.$uploadLabel.text(this.$uploadLabelText);
      this.$uploadIcon.addClass('fa-upload').removeClass('fa-file-alt');
      this.$btnSubmit.addClass('disabled');
    }

    allowSubmissionFor(filename) {
      this.$uploadFileLimitExceeded.addClass('d-none');
      this.$uploadLabel.text(filename);
      this.$uploadIcon.removeClass('fa-upload').addClass('fa-file-alt');
      this.$btnSubmit.removeClass('disabled');
    }
  }

  function _setUI() {
    mumuki.upload.ui = new UI();
  }

  return {
    FileUploader,
    UI,

    _setUI,

    /** @type {UI} */
    ui: null
  };
})();

mumuki.load(() => {
  $('#mu-upload-input').change(function (evt) {
    if (!mumuki.upload.ui) mumuki.upload._setUI();
    return new mumuki.upload.FileUploader(evt.target.files[0]).uploadFileIfValid();
  });
});

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
mumuki-laboratory-9.23.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.22.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.21.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.20.1 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.20.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.19.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.18.1 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.18.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.17.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.16.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.15.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.14.1 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.14.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.13.2 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.13.1 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.13.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.12.1 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.12.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.11.0 app/assets/javascripts/mumuki_laboratory/application/upload.js
mumuki-laboratory-9.10.0 app/assets/javascripts/mumuki_laboratory/application/upload.js