Skip to content

Commit 79e93fd

Browse files
committed
refactor(defaultTheme): simplify script logic
1 parent bd35806 commit 79e93fd

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

src/tpl/defaultTheme/frontend/index.js

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
const Enter = 'Enter';
2121
const Escape = 'Escape';
22-
const Esc = 'Esc';
2322
const Space = ' ';
2423

2524
let hasStorage = false;
@@ -109,16 +108,12 @@
109108
};
110109

111110
input.addEventListener('keydown', function (e) {
112-
switch (e.key) {
113-
case Enter:
114-
onEnter();
115-
e.preventDefault();
116-
break;
117-
case Escape:
118-
case Esc:
119-
onEscape();
120-
e.preventDefault();
121-
break;
111+
if (e.key === Enter) {
112+
onEnter();
113+
e.preventDefault();
114+
} else if (e.key === Escape) {
115+
onEscape();
116+
e.preventDefault();
122117
}
123118
});
124119
clear.addEventListener('click', function () {
@@ -137,8 +132,9 @@
137132
}
138133

139134
window.addEventListener('pagehide', function () {
140-
if (input.value) {
141-
sessionStorage.setItem(location.pathname, input.value);
135+
const inputValue = input.value;
136+
if (inputValue) {
137+
sessionStorage.setItem(location.pathname, inputValue);
142138
}
143139
});
144140
}
@@ -150,8 +146,7 @@
150146
function keepFocusOnBackwardForward() {
151147
function onFocus(e) {
152148
const link = e.target.closest('a');
153-
if (!link || link === lastFocused) return;
154-
lastFocused = link;
149+
if (link && link !== lastFocused) lastFocused = link;
155150
}
156151

157152
const itemList = document.body.querySelector(selectorItemList);
@@ -212,6 +207,7 @@
212207
lastFocused = elLink;
213208
elLink.focus();
214209
elLink.scrollIntoView({block: 'center'});
210+
break;
215211
}
216212
}
217213

@@ -223,16 +219,14 @@
223219
}
224220

225221
function getFocusableSibling(container, isBackward, startA) {
222+
if (!container.childElementCount) return;
226223
if (!startA) {
227224
startA = container.querySelector(':focus');
228225
}
229226
let startLI = startA && startA.closest('li');
230227
if (!startLI) {
231228
startLI = isBackward ? container.firstElementChild : container.lastElementChild;
232229
}
233-
if (!startLI) {
234-
return;
235-
}
236230

237231
let siblingLI = startLI;
238232
do {
@@ -970,36 +964,28 @@
970964

971965
const form = e.target;
972966

973-
function onLoad() {
974-
const status = this.status;
975-
if (status >= 200 && status < 300) {
976-
const elItem = form.closest('li');
977-
elItem.remove();
978-
} else {
979-
logError(`delete failed: ${status} ${this.statusText}`);
980-
}
981-
}
982-
983-
let params = '';
984-
const els = Array.from(form.elements);
985-
for (let i = 0; i < els.length; i++) {
986-
if (!els[i].name) {
987-
continue
988-
}
989-
if (params.length > 0) {
990-
params += '&'
991-
}
992-
params += els[i].name + '=' + encodeURIComponent(els[i].value)
993-
}
994-
const url = form.action;
967+
const params = Array.from(form.elements).map(el => {
968+
const {name, value} = el
969+
if (name) {
970+
return `${name}=${encodeURIComponent(value)}`;
971+
}
972+
}).filter(Boolean).join('&');
973+
974+
// follow redirect to update bf-cache
975+
fetch(form.action, {
976+
method: form.method,
977+
headers: {
978+
'Content-Type': form.enctype,
979+
},
980+
body: params
981+
}).then(resp => {
982+
const {status} = resp;
983+
if (status < 200 || status > 299) throw resp;
984+
const elItem = form.closest('li');
985+
elItem.remove();
986+
}).catch(logError);
995987

996-
const xhr = new XMLHttpRequest();
997-
xhr.open('POST', url); // will retrieve deleted result into bfcache
998-
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
999-
xhr.addEventListener('load', onLoad);
1000-
xhr.send(params);
1001988
e.preventDefault();
1002-
return false;
1003989
});
1004990
}
1005991

0 commit comments

Comments
 (0)