Sha256: 97d7809b59525360487243bec75296915a7040dc5a14256d56b6545974459dda
Contents?: true
Size: 1.6 KB
Versions: 12
Compression:
Stored size: 1.6 KB
Contents
import { DirectUpload, dispatchEvent } from "@rails/activestorage" export class AttachmentUpload { constructor(attachment, element) { this.attachment = attachment this.element = element this.directUpload = new DirectUpload(attachment.file, this.directUploadUrl, this) } start() { this.directUpload.create(this.directUploadDidComplete.bind(this)) this.dispatch("start") } directUploadWillStoreFileWithXHR(xhr) { xhr.upload.addEventListener("progress", event => { const progress = event.loaded / event.total * 100 this.attachment.setUploadProgress(progress) if (progress) { this.dispatch("progress", { progress: progress }) } }) } directUploadDidComplete(error, attributes) { if (error) { this.dispatchError(error) } else { this.attachment.setAttributes({ sgid: attributes.attachable_sgid, url: this.createBlobUrl(attributes.signed_id, attributes.filename) }) this.dispatch("end") } } createBlobUrl(signedId, filename) { return this.blobUrlTemplate .replace(":signed_id", signedId) .replace(":filename", encodeURIComponent(filename)) } dispatch(name, detail = {}) { detail.attachment = this.attachment return dispatchEvent(this.element, `direct-upload:${name}`, { detail }) } dispatchError(error) { const event = this.dispatch("error", { error }) if (!event.defaultPrevented) { alert(error); } } get directUploadUrl() { return this.element.dataset.directUploadUrl } get blobUrlTemplate() { return this.element.dataset.blobUrlTemplate } }
Version data entries
12 entries across 12 versions & 2 rubygems