Skip to content

Commit 957beca

Browse files
committed
feat(tpl): use JavaScript code to perform upload if possible
- Disable upload button when uploading - Show upload progress if possible - Fix Safari browser that file input with `webkitdirectory` uploading will not carry file path
1 parent 8b7a76b commit 957beca

File tree

6 files changed

+133
-8
lines changed

6 files changed

+133
-8
lines changed

src/tpl/asset/main.css

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ em {
7474
visibility: hidden;
7575
}
7676

77+
.if-disabled {
78+
display: none;
79+
}
80+
81+
:disabled .if-disabled {
82+
display: inherit;
83+
}
84+
85+
:disabled .if-enabled {
86+
display: none;
87+
}
88+
89+
7790
.path-list {
7891
font-size: 1.5em;
7992
overflow: hidden;
@@ -174,13 +187,14 @@ em {
174187
display: block;
175188
}
176189

177-
.upload input {
190+
.upload input,
191+
.upload button {
178192
display: block;
179193
width: 100%;
180194
box-sizing: border-box;
181195
}
182196

183-
.upload input + input {
197+
.upload button {
184198
margin-top: 0.5em;
185199
}
186200

src/tpl/asset/main.css.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ display: none;
6363
.hidden {
6464
visibility: hidden;
6565
}
66+
.if-disabled {
67+
display: none;
68+
}
69+
:disabled .if-disabled {
70+
display: inherit;
71+
}
72+
:disabled .if-enabled {
73+
display: none;
74+
}
6675
.path-list {
6776
font-size: 1.5em;
6877
overflow: hidden;
@@ -147,12 +156,13 @@ background: #c9c;
147156
.upload.dragging::before {
148157
display: block;
149158
}
150-
.upload input {
159+
.upload input,
160+
.upload button {
151161
display: block;
152162
width: 100%;
153163
box-sizing: border-box;
154164
}
155-
.upload input + input {
165+
.upload button {
156166
margin-top: 0.5em;
157167
}
158168
.archive {

src/tpl/asset/main.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@
377377
if (!upload) {
378378
return;
379379
}
380-
var fileInput = upload.querySelector('.file');
380+
var form = upload.querySelector('form');
381+
if (!form) {
382+
return;
383+
}
384+
var fileInput = form.querySelector('.file');
381385
if (!fileInput) {
382386
return;
383387
}
@@ -516,8 +520,57 @@
516520
upload.addEventListener('drop', onDrop, false);
517521
}
518522

523+
function enableUploadProgress() { // also fix Safari upload filename has no path info
524+
if (!FormData) {
525+
return;
526+
}
527+
528+
var btnSubmit = form.querySelector('.submit');
529+
if (!btnSubmit) {
530+
return;
531+
}
532+
533+
function onComplete() {
534+
btnSubmit.disabled = false;
535+
}
536+
537+
function onLoad() {
538+
location.reload();
539+
}
540+
541+
form.addEventListener('submit', function (e) {
542+
e.stopPropagation();
543+
e.preventDefault();
544+
545+
var files = Array.prototype.slice.call(fileInput.files);
546+
if (!files.length) {
547+
return;
548+
}
549+
550+
var formName = fileInput.name;
551+
var parts = new FormData();
552+
files.forEach(function (file) {
553+
if (file.webkitRelativePath) {
554+
parts.append(formName, file, file.webkitRelativePath);
555+
} else {
556+
parts.append(formName, file);
557+
}
558+
});
559+
560+
var xhr = new XMLHttpRequest();
561+
xhr.addEventListener('error', onComplete);
562+
xhr.addEventListener('load', onComplete);
563+
xhr.addEventListener('load', onLoad);
564+
565+
xhr.open(form.method, form.action);
566+
xhr.send(parts);
567+
btnSubmit.disabled = true;
568+
});
569+
}
570+
519571
enableAddDir();
520572
enableAddDragDrop();
573+
enableUploadProgress();
521574
}
522575

523576
function enableNonRefreshDelete() {

src/tpl/asset/main.js.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ var upload = document.body.querySelector('.upload');
342342
if (!upload) {
343343
return;
344344
}
345-
var fileInput = upload.querySelector('.file');
345+
var form = upload.querySelector('form');
346+
if (!form) {
347+
return;
348+
}
349+
var fileInput = form.querySelector('.file');
346350
if (!fileInput) {
347351
return;
348352
}
@@ -458,8 +462,48 @@ upload.addEventListener('dragover', onDragEnterOver, false);
458462
upload.addEventListener('dragleave', onDragLeave, false);
459463
upload.addEventListener('drop', onDrop, false);
460464
}
465+
function enableUploadProgress() { // also fix Safari upload filename has no path info
466+
if (!FormData) {
467+
return;
468+
}
469+
var btnSubmit = form.querySelector('.submit');
470+
if (!btnSubmit) {
471+
return;
472+
}
473+
function onComplete() {
474+
btnSubmit.disabled = false;
475+
}
476+
function onLoad() {
477+
location.reload();
478+
}
479+
form.addEventListener('submit', function (e) {
480+
e.stopPropagation();
481+
e.preventDefault();
482+
var files = Array.prototype.slice.call(fileInput.files);
483+
if (!files.length) {
484+
return;
485+
}
486+
var formName = fileInput.name;
487+
var parts = new FormData();
488+
files.forEach(function (file) {
489+
if (file.webkitRelativePath) {
490+
parts.append(formName, file, file.webkitRelativePath);
491+
} else {
492+
parts.append(formName, file);
493+
}
494+
});
495+
var xhr = new XMLHttpRequest();
496+
xhr.addEventListener('error', onComplete);
497+
xhr.addEventListener('load', onComplete);
498+
xhr.addEventListener('load', onLoad);
499+
xhr.open(form.method, form.action);
500+
xhr.send(parts);
501+
btnSubmit.disabled = true;
502+
});
503+
}
461504
enableAddDir();
462505
enableAddDragDrop();
506+
enableUploadProgress();
463507
}
464508
function enableNonRefreshDelete() {
465509
if (!document.querySelector) {

src/tpl/page.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
<div class="panel upload">
3737
<form method="POST" action="{{.SubItemPrefix}}?upload" enctype="multipart/form-data">
3838
<input type="file" name="file" multiple="multiple" class="file"/>
39-
<input type="submit" value="Upload" class="submit"/>
39+
<button type="submit" class="submit">
40+
<span class="if-enabled">Upload</span><span class="if-disabled">Uploading ...</span>
41+
</button>
4042
</form>
4143
</div>
4244
{{end}}

src/tpl/page.html.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ const pageTplStr = `
4444
<div class="panel upload">
4545
<form method="POST" action="{{.SubItemPrefix}}?upload" enctype="multipart/form-data">
4646
<input type="file" name="file" multiple="multiple" class="file"/>
47-
<input type="submit" value="Upload" class="submit"/>
47+
<button type="submit" class="submit">
48+
<span class="if-enabled">Upload</span><span class="if-disabled">Uploading ...</span>
49+
</button>
4850
</form>
4951
</div>
5052
{{end}}

0 commit comments

Comments
 (0)