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-8.0.0 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.2.2 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.5 app/javascript/activestorage/direct_upload_controller.js
activestorage-8.0.0.rc2 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.2.1.2 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.4.2 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.0.8.6 app/javascript/activestorage/direct_upload_controller.js
activestorage-6.1.7.10 app/javascript/activestorage/direct_upload_controller.js
activestorage-8.0.0.rc1 app/javascript/activestorage/direct_upload_controller.js
activestorage-6.1.7.9 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.2.1.1 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.4.1 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.0.8.5 app/javascript/activestorage/direct_upload_controller.js
activestorage-8.0.0.beta1 app/javascript/activestorage/direct_upload_controller.js
omg-activestorage-8.0.0.alpha9 app/javascript/activestorage/direct_upload_controller.js
omg-activestorage-8.0.0.alpha8 app/javascript/activestorage/direct_upload_controller.js
omg-activestorage-8.0.0.alpha7 app/javascript/activestorage/direct_upload_controller.js
omg-activestorage-8.0.0.alpha4 app/javascript/activestorage/direct_upload_controller.js
omg-activestorage-8.0.0.alpha3 app/javascript/activestorage/direct_upload_controller.js
activestorage-7.1.4 app/javascript/activestorage/direct_upload_controller.js