Skip to content

Commit 40b6a0c

Browse files
committed
refactor(defaultTheme): simplify script syntax
1 parent 9061b95 commit 40b6a0c

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

src/tpl/defaultTheme/frontend/index.js

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
const classHeader = 'header';
1111

1212
const selectorIsNone = '.' + classNone;
13-
const selectorNotNone = ':not(' + selectorIsNone + ')';
13+
const selectorNotNone = `:not(${selectorIsNone})`;
1414
const selectorPathList = '.path-list';
1515
const selectorItemList = '.item-list';
16-
const selectorItem = 'li:not(.' + classHeader + '):not(.parent)';
16+
const selectorItem = `li:not(.${classHeader}):not(.parent)`;
1717
const selectorItemIsNone = selectorItem + selectorIsNone;
1818
const selectorItemNotNone = selectorItem + selectorNotNone;
1919

20-
const leavingEvent = typeof window.onpagehide !== strUndef ? 'pagehide' : 'beforeunload';
21-
2220
const Enter = 'Enter';
2321
const Escape = 'Escape';
2422
const Esc = 'Esc';
@@ -33,7 +31,7 @@
3331
let filteredText = '';
3432

3533
function matchFilter(input) {
36-
return input.toLowerCase().indexOf(filteredText) >= 0;
34+
return input.toLowerCase().includes(filteredText);
3735
}
3836

3937
let lastFocused;
@@ -47,8 +45,7 @@
4745
const input = filter.querySelector('input');
4846
if (!input) return;
4947

50-
let clear = filter.querySelector('button');
51-
if (!clear) clear = document.createElement('button');
48+
const clear = filter.querySelector('button') || document.createElement('button');
5249

5350
const itemList = document.querySelector(selectorItemList)
5451

@@ -62,18 +59,17 @@
6259
clear.style.display = 'block';
6360

6461
let selector
65-
if (filteringText.indexOf(filteredText) >= 0) { // increment search, find in visible items
62+
if (filteringText.includes(filteredText)) { // increment search, find in visible items
6663
selector = selectorItemNotNone;
67-
} else if (filteredText.indexOf(filteringText) >= 0) { // decrement search, find in hidden items
64+
} else if (filteredText.includes(filteringText)) { // decrement search, find in hidden items
6865
selector = selectorItemIsNone;
6966
} else {
7067
selector = selectorItem;
7168
}
7269
filteredText = filteringText;
7370

7471
items = itemList.querySelectorAll(selector);
75-
if (!items.forEach) items = Array.prototype.slice.call(items); // IE9+/ClassicEdge
76-
items.forEach(function (item) {
72+
items.forEach(item => {
7773
const name = item.querySelector('.name');
7874
if (matchFilter(name.textContent)) {
7975
if (selector !== selectorItemNotNone) {
@@ -90,10 +86,7 @@
9086
filteredText = '';
9187

9288
items = itemList.querySelectorAll(selectorItemIsNone);
93-
if (!items.forEach) items = Array.prototype.slice.call(items); // IE9+/ClassicEdge
94-
items.forEach(function (item) {
95-
item.classList.remove(classNone);
96-
});
89+
items.forEach(item => item.classList.remove(classNone));
9790
}
9891
};
9992

@@ -143,12 +136,11 @@
143136
sessionStorage.removeItem(location.pathname);
144137
}
145138

146-
window.addEventListener(leavingEvent, function () {
139+
window.addEventListener('pagehide', function () {
147140
if (input.value) {
148141
sessionStorage.setItem(location.pathname, input.value);
149142
}
150143
});
151-
152144
}
153145
if (input.value) {
154146
doFilter();
@@ -200,8 +192,7 @@
200192
prevChildName = decodeURIComponent(prevChildName);
201193
if (!matchFilter(prevChildName)) return;
202194

203-
let items = document.body.querySelectorAll(selectorItemList + '>' + selectorItemNotNone);
204-
items = Array.prototype.slice.call(items);
195+
const items = Array.from(document.body.querySelectorAll(selectorItemList + '>' + selectorItemNotNone));
205196
const selectorName = '.field.name';
206197
const selectorLink = 'a';
207198
for (let i = 0; i < items.length; i++) {
@@ -237,11 +228,7 @@
237228
}
238229
let startLI = startA && startA.closest('li');
239230
if (!startLI) {
240-
if (isBackward) {
241-
startLI = container.firstElementChild;
242-
} else {
243-
startLI = container.lastElementChild;
244-
}
231+
startLI = isBackward ? container.firstElementChild : container.lastElementChild;
245232
}
246233
if (!startLI) {
247234
return;
@@ -266,7 +253,7 @@
266253
}
267254

268255
function getFirstFocusableSibling(container) {
269-
const a = container.querySelector('li:not(.' + classNone + '):not(.' + classHeader + ') a');
256+
const a = container.querySelector(`li:not(.${classNone}):not(.${classHeader}) a`);
270257
return a;
271258
}
272259

@@ -278,8 +265,8 @@
278265

279266
function getMatchedFocusableSibling(container, isBackward, startA, buf) {
280267
let skipRound = buf.length === 1; // find next single-char prefix
281-
let firstCheckA;
282-
let secondCheckA;
268+
let firstCheckedA;
269+
let secondCheckedA;
283270
let a = startA;
284271
do {
285272
if (skipRound) {
@@ -290,15 +277,15 @@
290277
continue;
291278
}
292279

293-
// firstCheckA maybe a focused a that not belongs to the list
294-
// secondCheckA must be in the list
295-
if (!firstCheckA) {
296-
firstCheckA = a;
297-
} else if (firstCheckA === a) {
280+
// firstCheckedA maybe a focused a that not belongs to the list
281+
// secondCheckedA must be in the list
282+
if (!firstCheckedA) {
283+
firstCheckedA = a;
284+
} else if (firstCheckedA === a) {
298285
return;
299-
} else if (!secondCheckA) {
300-
secondCheckA = a;
301-
} else if (secondCheckA === a) {
286+
} else if (!secondCheckedA) {
287+
secondCheckedA = a;
288+
} else if (secondCheckedA === a) {
302289
return;
303290
}
304291

@@ -317,7 +304,7 @@
317304
const SKIP_TAGS = ['INPUT', 'BUTTON', 'TEXTAREA'];
318305

319306
const PLATFORM = navigator.platform || navigator.userAgent;
320-
const IS_MAC_PLATFORM = PLATFORM.indexOf('Mac') >= 0 || PLATFORM.indexOf('iPhone') >= 0 || PLATFORM.indexOf('iPad') >= 0 || PLATFORM.indexOf('iPod') >= 0
307+
const IS_MAC_PLATFORM = PLATFORM.includes('Mac') || PLATFORM.includes('iPhone') || PLATFORM.includes('iPad') || PLATFORM.includes('iPod')
321308

322309
let lookupKey;
323310
let lookupBuffer;
@@ -380,7 +367,7 @@
380367
}
381368

382369
function getFocusItemByKeyPress(e) {
383-
if (SKIP_TAGS.indexOf(e.target.tagName) >= 0) {
370+
if (SKIP_TAGS.includes(e.target.tagName)) {
384371
return;
385372
}
386373

@@ -598,7 +585,6 @@
598585
}
599586

600587
function enableFileDirModeSwitch() {
601-
const classHidden = 'hidden';
602588
const classActive = 'active';
603589

604590
function onClickOpt(optTarget, clearInput) {
@@ -669,10 +655,10 @@
669655
return;
670656
}
671657

672-
const nodir = Array.prototype.slice.call(files).every(function (file) {
673-
return file.webkitRelativePath.indexOf('/') < 0;
674-
});
675-
if (nodir) {
658+
const noDir = Array.prototype.slice.call(files).every(file =>
659+
file.webkitRelativePath.includes('/')
660+
);
661+
if (noDir) {
676662
onClickOptFile(); // prevent clear input files
677663
}
678664
});
@@ -699,7 +685,7 @@
699685
sessionStorage.removeItem(uploadTypeField);
700686
}
701687

702-
window.addEventListener(leavingEvent, function () {
688+
window.addEventListener('pagehide', function () {
703689
const activeUploadType = fileInput.name;
704690
if (activeUploadType !== file) {
705691
sessionStorage.setItem(uploadTypeField, activeUploadType)
@@ -923,7 +909,7 @@
923909
const tagName = e.target.tagName;
924910
if (tagName === 'TEXTAREA') {
925911
return;
926-
} else if (tagName === 'INPUT' && nonTextInputTypes.indexOf(e.target.type) < 0) {
912+
} else if (tagName === 'INPUT' && !nonTextInputTypes.includes(e.target.type)) {
927913
return;
928914
}
929915

@@ -967,11 +953,11 @@
967953
});
968954
}
969955

970-
const modes = enableFileDirModeSwitch();
956+
const {switchToFileMode, switchToDirMode} = enableFileDirModeSwitch();
971957
const uploadProgressively = enableUploadProgress();
972958
enableFormUploadProgress(uploadProgressively);
973-
enableDndUploadProgress(uploadProgressively, modes.switchToFileMode, modes.switchToDirMode);
974-
enablePasteUploadProgress(uploadProgressively, modes.switchToFileMode, modes.switchToDirMode);
959+
enableDndUploadProgress(uploadProgressively, switchToFileMode, switchToDirMode);
960+
enablePasteUploadProgress(uploadProgressively, switchToFileMode, switchToDirMode);
975961
}
976962

977963
function enableNonRefreshDelete() {

0 commit comments

Comments
 (0)