|
19 | 19 |
|
20 | 20 | const Enter = 'Enter'; |
21 | 21 | const Escape = 'Escape'; |
22 | | - const Esc = 'Esc'; |
23 | 22 | const Space = ' '; |
24 | 23 |
|
25 | 24 | let hasStorage = false; |
|
109 | 108 | }; |
110 | 109 |
|
111 | 110 | 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(); |
122 | 117 | } |
123 | 118 | }); |
124 | 119 | clear.addEventListener('click', function () { |
|
137 | 132 | } |
138 | 133 |
|
139 | 134 | 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); |
142 | 138 | } |
143 | 139 | }); |
144 | 140 | } |
|
150 | 146 | function keepFocusOnBackwardForward() { |
151 | 147 | function onFocus(e) { |
152 | 148 | const link = e.target.closest('a'); |
153 | | - if (!link || link === lastFocused) return; |
154 | | - lastFocused = link; |
| 149 | + if (link && link !== lastFocused) lastFocused = link; |
155 | 150 | } |
156 | 151 |
|
157 | 152 | const itemList = document.body.querySelector(selectorItemList); |
|
212 | 207 | lastFocused = elLink; |
213 | 208 | elLink.focus(); |
214 | 209 | elLink.scrollIntoView({block: 'center'}); |
| 210 | + break; |
215 | 211 | } |
216 | 212 | } |
217 | 213 |
|
|
223 | 219 | } |
224 | 220 |
|
225 | 221 | function getFocusableSibling(container, isBackward, startA) { |
| 222 | + if (!container.childElementCount) return; |
226 | 223 | if (!startA) { |
227 | 224 | startA = container.querySelector(':focus'); |
228 | 225 | } |
229 | 226 | let startLI = startA && startA.closest('li'); |
230 | 227 | if (!startLI) { |
231 | 228 | startLI = isBackward ? container.firstElementChild : container.lastElementChild; |
232 | 229 | } |
233 | | - if (!startLI) { |
234 | | - return; |
235 | | - } |
236 | 230 |
|
237 | 231 | let siblingLI = startLI; |
238 | 232 | do { |
|
970 | 964 |
|
971 | 965 | const form = e.target; |
972 | 966 |
|
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); |
995 | 987 |
|
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); |
1001 | 988 | e.preventDefault(); |
1002 | | - return false; |
1003 | 989 | }); |
1004 | 990 | } |
1005 | 991 |
|
|
0 commit comments