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