Sha256: fc71006a9f52ed8aab3e081e1cc307239bc05e69bdd180205241af24b17a7c2f

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

(function() {
  var HOST = "<%= Bongo::Engine.routes.url_helpers.files_path %>"

  addEventListener("trix-attachment-add", function(event) {
    if (event.attachment.file) {
      uploadFileAttachment(event.attachment)
    }
  })

  function uploadFileAttachment(attachment) {
    uploadFile(attachment.file, setProgress, setAttributes)

    function setProgress(progress) {
      attachment.setUploadProgress(progress)
    }

    function setAttributes(attributes) {
      attachment.setAttributes(attributes)
    }
  }

  function uploadFile(file, progressCallback, successCallback) {
    var formData = createFormData(file)
    var xhr = new XMLHttpRequest()

    xhr.open("POST", HOST, true)

    xhr.upload.addEventListener("progress", function(event) {
      var progress = event.loaded / event.total * 100
      progressCallback(progress)
    })

    xhr.addEventListener("load", function(event) {
      if (xhr.status == 200) {
        var attributes = {
          url: xhr.response
        }
        successCallback(attributes)
      }
    })

    xhr.send(formData)
  }

  function createFormData(file) {
    var data = new FormData()
    data.append("Content-Type", file.type)
    data.append("file", file)
    return data
  }
})();

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bongo-0.2.0 app/assets/javascripts/bongo/attachments.js.erb
bongo-0.1.1 app/assets/javascripts/bongo/attachments.js.erb
bongo-0.1.0 app/assets/javascripts/bongo/attachments.js.erb
bongo-0.0.4 app/assets/javascripts/bongo/attachments.js.erb