From e3429a87e8f9be54f206c2ca529df5f1910104b4 Mon Sep 17 00:00:00 2001 From: timoxa Date: Wed, 11 Apr 2018 12:47:39 +0300 Subject: [PATCH] queue file uploads --- src/components/fields/file.vue | 58 +++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/src/components/fields/file.vue b/src/components/fields/file.vue index f89b755..628e20b 100644 --- a/src/components/fields/file.vue +++ b/src/components/fields/file.vue @@ -2,6 +2,43 @@ import http from 'src/http'; import { httpErrorModalData } from 'src/utils'; + const uploads = []; + let uploading = false; + + function upload({ data, vm }) { + vm.uploading = true; + return http.post('upload/files', data, { onUploadProgress: vm.progressUpload }) + .then(res => { + vm.$emit('input', res.data[0]); + }) + .catch(err => { + vm.$modal.open('error', httpErrorModalData(err)); + }) + .then(() => { + vm.uploading = false; + vm.uploadProgress = 0; + }); + } + + function uploadNext() { + if (uploads[0]) { + if (uploading) uploads[0].vm.uploadInQueue = true; + else { + uploading = true; + uploads[0].vm.uploadInQueue = false; + upload(uploads.shift()).then(() => { + uploading = false; + uploadNext(); + }); + } + } + } + + function queueUpload(data, vm) { + uploads.push({ data, vm }); + uploadNext(); + } + export default { props: { placeholder: String, @@ -24,6 +61,7 @@ }, data: () => ({ uploading: false, + uploadInQueue: false, uploadProgress: 0 }), computed: { @@ -38,20 +76,9 @@ onFileChange(e) { if (this.disabled) return; if (this.ajaxMode) { - this.uploading = true; const data = new FormData(); data.append('files[]', e.target.files[0]); - http.post('upload/files', data, { onUploadProgress: this.progressUpload }) - .then(res => { - this.$emit('input', res.data[0]); - }) - .catch(err => { - this.$modal.open('error', httpErrorModalData(err)); - }) - .then(() => { - this.uploading = false; - this.uploadProgress = 0; - }); + queueUpload(data, this); } else this.$emit('input', e.target.files[0]); }, @@ -63,9 +90,10 @@ };