Skip to content

Commit 575b153

Browse files
committed
feat(tpl): queue upload tasks
When uploading, will not block new uploads. Upload button is always enabled now.
1 parent bcfb77a commit 575b153

File tree

3 files changed

+21
-40
lines changed

3 files changed

+21
-40
lines changed

src/tpl/frontend/index.css

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,6 @@ em {
9292
visibility: hidden;
9393
}
9494

95-
.if-disabled {
96-
display: none;
97-
}
98-
99-
:disabled .if-disabled {
100-
display: inherit;
101-
}
102-
103-
:disabled .if-enabled {
104-
display: none;
105-
}
106-
10795

10896
html::before {
10997
display: none;
@@ -225,7 +213,7 @@ html.dragging::before {
225213
position: relative;
226214
}
227215

228-
.upload button .progress {
216+
.upload .progress {
229217
position: fixed;
230218
left: 0;
231219
top: 0;

src/tpl/frontend/index.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@
3939
<div class="panel upload">
4040
<form method="POST" action="{{.SubItemPrefix}}?upload" enctype="multipart/form-data">
4141
<input type="file" name="file" multiple="multiple" class="file"/>
42-
<button type="submit" class="submit">
43-
<span class="progress"></span>
44-
<span class="if-enabled">{{.Trans.UploadLabel}}</span><span class="if-disabled">{{.Trans.UploadingLabel}}</span>
45-
</button>
42+
<button type="submit" class="submit">{{.Trans.UploadLabel}}</button>
4643
</form>
44+
<span class="progress"></span>
4745
</div>
4846
{{end}}
4947

src/tpl/frontend/index.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -433,18 +433,11 @@
433433
return;
434434
}
435435

436-
var btnSubmit = form.querySelector('.submit') || form.querySelector('input[type=submit]');
437-
if (!btnSubmit) {
438-
return;
439-
}
440-
441436
var uploadType = document.body.querySelector('.upload-type');
442437
if (!uploadType) {
443438
return;
444439
}
445440

446-
var classUploading = 'uploading';
447-
448441
var file = 'file';
449442
var dirFile = 'dirfile';
450443
var innerDirFile = 'innerdirfile';
@@ -462,10 +455,6 @@
462455
ele && ele.classList.remove(className);
463456
}
464457

465-
function hasClass(ele, className) {
466-
return ele && ele.classList.contains(className);
467-
}
468-
469458
var padStart = String.prototype.padStart ? function (sourceString, targetLength, padTemplate) {
470459
return sourceString.padStart(targetLength, padTemplate);
471460
} : function (sourceString, targetLength, padTemplate) {
@@ -749,19 +738,23 @@
749738
return;
750739
}
751740

752-
var elProgress = btnSubmit.querySelector('.progress');
741+
var uploading = false;
742+
var batches = [];
743+
var elProgress = upload.querySelector('.progress');
753744

754745
function onComplete() {
755746
if (elProgress) {
756747
elProgress.style.width = '';
757748
}
758-
fileInput.disabled = false;
759-
btnSubmit.disabled = false;
760-
removeClass(upload, classUploading);
749+
if (batches.length) {
750+
uploadBatch(batches.shift());
751+
} else {
752+
uploading = false;
753+
}
761754
}
762755

763756
function onLoad() {
764-
location.reload();
757+
!uploading && location.reload();
765758
}
766759

767760
function onProgress(e) {
@@ -776,6 +769,15 @@
776769
return;
777770
}
778771

772+
if (uploading) {
773+
batches.push(files);
774+
} else {
775+
uploading = true;
776+
uploadBatch(files);
777+
}
778+
}
779+
780+
function uploadBatch(files) {
779781
var formName = fileInput.name;
780782
var parts = new FormData();
781783
files.forEach(function (file) {
@@ -805,9 +807,6 @@
805807

806808
xhr.open(form.method, form.action);
807809
xhr.send(parts);
808-
addClass(upload, classUploading);
809-
fileInput.disabled = true;
810-
btnSubmit.disabled = true;
811810
}
812811

813812
return uploadProgressively;
@@ -847,9 +846,6 @@
847846
e.stopPropagation();
848847
e.preventDefault();
849848
removeClass(e.currentTarget, classDragging);
850-
if (hasClass(e.currentTarget, classUploading)) {
851-
return;
852-
}
853849
fileInput.value = '';
854850

855851
if (!e.dataTransfer || !e.dataTransfer.files || !e.dataTransfer.files.length) {
@@ -862,7 +858,6 @@
862858
// must use progressive upload by JS if has directory
863859
return;
864860
}
865-
btnSubmit.disabled = true; // disable earlier
866861
var itemsCount = items.length; // save items count earlier, items will be lost after calling FileSystemFileEntry.file()
867862
itemsToFiles(items, function (files) {
868863
itemsCount > 1 ? switchToDirMode() : switchToAnyDirMode();

0 commit comments

Comments
 (0)