| 1 | /* list.js: the document list's search field. On a results page, the |
| 2 | field's own clear button (the × a search input draws) clears the |
| 3 | search, not just the field: it goes back to every document, and the |
| 4 | "Clear search" link under the results, the same way out for a reader |
| 5 | with no script, is hidden as redundant. |
| 6 | |
| 7 | Only the clear button, never typing: backspacing the query down to |
| 8 | nothing is someone about to type another one, so it stays put. The |
| 9 | clear button fires "search" with an empty field (WebKit, Blink); a |
| 10 | browser without that event reports the clear as an input event with |
| 11 | no inputType, which typing never is. |
| 12 | |
| 13 | Escape clears it too. Blink does that on its own; WebKit only |
| 14 | sometimes, Gecko never, so it is done here for all of them, before |
| 15 | the browser's own default, which then has nothing to clear. */ |
| 16 | (function () { |
| 17 | "use strict"; |
| 18 | var input = document.querySelector("[docs-search-input]"); |
| 19 | if (!input || !input.value) return; |
| 20 | var clear = document.querySelector("[docs-search-clear]"); |
| 21 | if (clear) clear.hidden = true; |
| 22 | // Once: the × fires both an input with no inputType and "search". |
| 23 | var left = false; |
| 24 | var leave = function () { |
| 25 | if (input.value !== "" || left) return; |
| 26 | left = true; |
| 27 | location.assign("/"); |
| 28 | }; |
| 29 | input.addEventListener("search", leave); |
| 30 | input.addEventListener("input", function (e) { |
| 31 | if (!e.inputType) leave(); |
| 32 | }); |
| 33 | input.addEventListener("keydown", function (e) { |
| 34 | // keyCode 229: WebKit ends a composition before the Escape that |
| 35 | // cancels it, so isComposing alone lets that Escape clear the query. |
| 36 | if (e.key !== "Escape" || input.value === "" || e.isComposing || e.keyCode === 229) return; |
| 37 | e.preventDefault(); |
| 38 | input.value = ""; |
| 39 | leave(); |
| 40 | }); |
| 41 | })(); |
| 42 | |