Sha256: d44dac9b0874dd99c0418a597e994a60537a1dbc2ae71ac9787b5f803ac96a7c

Contents?: true

Size: 1.69 KB

Versions: 199

Compression:

Stored size: 1.69 KB

Contents

import { DirectUpload } from "./direct_upload"
import { dispatchEvent } from "./helpers"

export class DirectUploadController {
  constructor(input, file) {
    this.input = input
    this.file = file
    this.directUpload = new DirectUpload(this.file, this.url, this)
    this.dispatch("initialize")
  }

  start(callback) {
    const hiddenInput = document.createElement("input")
    hiddenInput.type = "hidden"
    hiddenInput.name = this.input.name
    this.input.insertAdjacentElement("beforebegin", hiddenInput)

    this.dispatch("start")

    this.directUpload.create((error, attributes) => {
      if (error) {
        hiddenInput.parentNode.removeChild(hiddenInput)
        this.dispatchError(error)
      } else {
        hiddenInput.value = attributes.signed_id
      }

      this.dispatch("end")
      callback(error)
    })
  }

  uploadRequestDidProgress(event) {
    const progress = event.loaded / event.total * 100
    if (progress) {
      this.dispatch("progress", { progress })
    }
  }

  get url() {
    return this.input.getAttribute("data-direct-upload-url")
  }

  dispatch(name, detail = {}) {
    detail.file = this.file
    detail.id = this.directUpload.id
    return dispatchEvent(this.input, `direct-upload:${name}`, { detail })
  }

  dispatchError(error) {
    const event = this.dispatch("error", { error })
    if (!event.defaultPrevented) {
      alert(error)
    }
  }

  // DirectUpload delegate

  directUploadWillCreateBlobWithXHR(xhr) {
    this.dispatch("before-blob-request", { xhr })
  }

  directUploadWillStoreFileWithXHR(xhr) {
    this.dispatch("before-storage-request", { xhr })
    xhr.upload.addEventListener("progress", event => this.uploadRequestDidProgress(event))
  }
}

Version data entries

199 entries across 194 versions & 20 rubygems

Version Path
activestorage-7.1.3 app/javascript/activestorage/direct_upload_controller.js
activestorage_legacy-0.2.1 app/javascript/activestorage/direct_upload_controller.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.3.1/app/javascript/activestorage/direct_upload_controller.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/app/javascript/activestorage/direct_upload_controller.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.2 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.1 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.0 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.0.rc2 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.0.rc1 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.0.beta1 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.0.8 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.0.7.2 app/javascript/activestorage/direct_upload_controller.js
activestorage-6.1.7.6 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.0.7.1 app/javascript/activestorage/direct_upload_controller.js
activestorage-6.1.7.5 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.0.7 app/javascript/activestorage/direct_upload_controller.js
activestorage_legacy-0.2.0 app/javascript/activestorage/direct_upload_controller.js
activestorage_legacy-0.1.3 app/javascript/activestorage/direct_upload_controller.js
activestorage_legacy-0.1.2 app/javascript/activestorage/direct_upload_controller.js