Skip to content

Commit c96c353

Browse files
committed
refactor(defaultTheme): refine enhanceUpload() mode switch
1 parent 8f42568 commit c96c353

File tree

3 files changed

+65
-57
lines changed

3 files changed

+65
-57
lines changed

src/tpl/defaultTheme/frontend/index.css

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,33 +159,39 @@ html.dragging::before {
159159

160160
.tab {
161161
display: flex;
162+
gap: 0.5em;
162163
white-space: nowrap;
163164
margin: 1em 1em -1em 1em;
164165
}
165166

166-
.tab label {
167+
.tab h3, .tab a {
167168
flex: 0 0 auto;
168-
margin-right: 0.5em;
169169
padding: 1em;
170+
}
171+
172+
.tab h3 {
173+
margin: 0;
174+
font-size: inherit;
175+
}
176+
177+
.tab a {
170178
cursor: pointer;
171179
}
172180

173-
.tab label:focus {
174-
outline: 0;
175-
text-decoration: underline;
176-
text-decoration-style: dotted;
181+
.tab a:focus {
182+
text-decoration: underline 1px dotted;
177183
}
178184

179-
.tab label:hover {
185+
.tab a:hover {
180186
background: #fbfbfb;
181187
}
182188

183-
.tab label.active {
189+
.tab a.active {
184190
color: #000;
185191
background: #f7f7f7;
186192
}
187193

188-
.tab label:last-child {
194+
.tab a:last-child {
189195
margin-right: 0;
190196
}
191197

@@ -532,11 +538,11 @@ html.dragging::before {
532538
border-color: #555 #555 transparent transparent;
533539
}
534540

535-
.tab label:hover {
541+
.tab a:hover {
536542
background-color: #181818;
537543
}
538544

539-
.tab label.active {
545+
.tab a.active {
540546
color: #fff;
541547
background-color: #222;
542548
}

src/tpl/defaultTheme/frontend/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@
5454
}
5555
</script>
5656
<div class="tab upload-type">
57-
<label class="file active" tabindex="0" role="button" title="{{.Trans.UploadFilesHint}}">{{.Trans.UploadFilesLabel}}</label>
58-
{{if .CanMkdir}}<label class="dirfile hidden" tabindex="0" role="button" title="{{.Trans.UploadDirHint}}">{{.Trans.UploadDirLabel}}</label>
59-
<label class="innerdirfile hidden" tabindex="0" role="button" title="{{.Trans.UploadDirContentsHint}}">{{.Trans.UploadDirContentsLabel}}</label>{{end}}
57+
<h3>{{.Trans.UploadLabel}}</h3>
58+
<a class="file active" tabindex="0" role="button" title="{{.Trans.UploadFilesHint}}">{{.Trans.UploadFilesLabel}}</a>
59+
{{if .CanMkdir}}<a class="dirfile" tabindex="0" role="button" title="{{.Trans.UploadDirHint}}">{{.Trans.UploadDirLabel}}</a>
60+
<a class="innerdirfile" tabindex="0" role="button" title="{{.Trans.UploadDirContentsHint}}">{{.Trans.UploadDirContentsLabel}}</a>{{end}}
6061
</div>
6162
<div class="panel upload">
6263
<form method="POST" action="{{.SubItemPrefix}}?upload" enctype="multipart/form-data">

src/tpl/defaultTheme/frontend/index.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
(function () {
2-
function noop() {
3-
}
4-
52
function logError(err) {
63
console.error(err);
74
}
@@ -439,6 +436,9 @@
439436
var fileInput = form.querySelector('input[type=file]');
440437
if (!fileInput) return;
441438

439+
var submitButton = form.querySelector('[type=submit]');
440+
if (submitButton) submitButton.classList.add(classNone);
441+
442442
var uploadType = document.body.querySelector('.upload-type');
443443
if (!uploadType) return;
444444

@@ -597,16 +597,13 @@
597597
}
598598
}
599599

600-
var switchToFileMode = noop;
601-
var switchToDirMode = noop;
602-
603-
function enableAddDirFile() {
600+
function enableFileDirModeSwitch() {
604601
var classHidden = 'hidden';
605602
var classActive = 'active';
606603

607604
function onClickOpt(optTarget, clearInput) {
608605
if (optTarget === optActive) {
609-
return;
606+
return false;
610607
}
611608
optActive.classList.remove(classActive);
612609

@@ -657,12 +654,28 @@
657654
}
658655
}
659656

660-
optDirFile && optDirFile.classList.remove(classHidden);
661-
optInnerDirFile && optInnerDirFile.classList.remove(classHidden);
662-
663657
if (optFile) {
664658
optFile.addEventListener('click', onClickOptFile);
665659
optFile.addEventListener('keydown', onKeydownOpt);
660+
661+
fileInput.addEventListener('change', function (e) {
662+
// workaround fix for old browsers, select dir not work but still act like select files
663+
// switch back to file
664+
if (optActive === optFile) {
665+
return;
666+
}
667+
var files = e.target.files;
668+
if (!files.length) {
669+
return;
670+
}
671+
672+
var nodir = Array.prototype.slice.call(files).every(function (file) {
673+
return file.webkitRelativePath.indexOf('/') < 0;
674+
});
675+
if (nodir) {
676+
onClickOptFile(); // prevent clear input files
677+
}
678+
});
666679
}
667680
if (optDirFile) {
668681
optDirFile.addEventListener('click', onClickOptDirFile);
@@ -676,49 +689,32 @@
676689
if (hasStorage) {
677690
var uploadTypeField = 'upload-type';
678691
var prevUploadType = sessionStorage.getItem(uploadTypeField);
679-
sessionStorage.removeItem(uploadTypeField);
680-
681-
window.addEventListener(leavingEvent, function () {
682-
var activeUploadType = fileInput.name;
683-
if (activeUploadType !== file) {
684-
sessionStorage.setItem(uploadTypeField, activeUploadType)
685-
}
686-
}, false);
687-
688692
if (prevUploadType === dirFile) {
689693
optDirFile && optDirFile.click();
690694
} else if (prevUploadType === innerDirFile) {
691695
optInnerDirFile && optInnerDirFile.click();
692696
}
693-
}
694697

695-
optFile && fileInput.addEventListener('change', function (e) {
696-
// workaround fix for mobile device, select dir not work but still act like select files
697-
// switch back to file
698-
if (optActive === optFile) {
699-
return;
700-
}
701-
var files = e.target.files;
702-
if (!files.length) {
703-
return;
698+
if (prevUploadType !== null) {
699+
sessionStorage.removeItem(uploadTypeField);
704700
}
705701

706-
var nodir = Array.prototype.slice.call(files).every(function (file) {
707-
return file.webkitRelativePath.indexOf('/') < 0;
702+
window.addEventListener(leavingEvent, function () {
703+
var activeUploadType = fileInput.name;
704+
if (activeUploadType !== file) {
705+
sessionStorage.setItem(uploadTypeField, activeUploadType)
706+
}
708707
});
709-
if (nodir) {
710-
onClickOptFile(); // prevent clear input files
711-
}
712-
});
708+
}
713709

714-
switchToFileMode = function () {
710+
function switchToFileMode() {
715711
if (optFile && optActive !== optFile) {
716712
optFile.focus();
717713
onClickOptFile(true);
718714
}
719715
}
720716

721-
switchToDirMode = function () {
717+
function switchToDirMode() {
722718
if (optDirFile) {
723719
if (optActive !== optDirFile) {
724720
optDirFile.focus();
@@ -731,6 +727,11 @@
731727
}
732728
}
733729
}
730+
731+
return {
732+
switchToFileMode: switchToFileMode,
733+
switchToDirMode: switchToDirMode
734+
};
734735
}
735736

736737
function enableUploadProgress() { // also fix Safari upload filename has no path info
@@ -850,7 +851,7 @@
850851
});
851852
}
852853

853-
function enableAddDragDrop(uploadProgressively) {
854+
function enableAddDragDrop(uploadProgressively, switchToFileMode, switchToDirMode) {
854855
var isSelfDragging = false;
855856
var classDragging = 'dragging';
856857

@@ -911,7 +912,7 @@
911912
dragDropEl.addEventListener('drop', onDrop);
912913
}
913914

914-
function enableAddPasteProgressively(uploadProgressively) {
915+
function enableAddPasteProgressively(uploadProgressively, switchToFileMode, switchToDirMode) {
915916
var typeTextPlain = 'text/plain';
916917
var nonTextInputTypes = ['hidden', 'radio', 'checkbox', 'button', 'reset', 'submit', 'image'];
917918

@@ -1020,11 +1021,11 @@
10201021
});
10211022
}
10221023

1023-
enableAddDirFile();
1024+
var modes = enableFileDirModeSwitch();
10241025
var uploadProgressively = enableUploadProgress();
10251026
enableFormUploadProgress(uploadProgressively);
1026-
enableAddPasteProgressively(uploadProgressively);
1027-
enableAddDragDrop(uploadProgressively);
1027+
enableAddPasteProgressively(uploadProgressively, modes.switchToFileMode, modes.switchToDirMode);
1028+
enableAddDragDrop(uploadProgressively, modes.switchToFileMode, modes.switchToDirMode);
10281029
}
10291030

10301031
function enableNonRefreshDelete() {

0 commit comments

Comments
 (0)