|
1 | 1 | (function() {
|
| 2 | + function focusSearchInput() { |
| 3 | + // On the index page, we have a "#search" input. If we are on this page, we want to go back |
| 4 | + // to this one and not the one in the header navbar. |
| 5 | + var searchInput = document.getElementById("search"); |
| 6 | + if (searchInput) { |
| 7 | + searchInput.focus(); |
| 8 | + } else { |
| 9 | + document.getElementById("nav-search").focus() |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + function focusFirstSearchResult() { |
| 14 | + var elem = document.querySelector(".recent-releases-container a.release"); |
| 15 | + if (elem) { |
| 16 | + elem.focus(); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + function getWrappingLi(elem) { |
| 21 | + while (elem && elem.tagName !== "LI" && elem.tagName !== "BODY") { |
| 22 | + elem = elem.parentElement; |
| 23 | + } |
| 24 | + if (elem.tagName === "BODY") { |
| 25 | + return null; |
| 26 | + } |
| 27 | + return elem; |
| 28 | + } |
| 29 | + |
| 30 | + function focusOnLi(li) { |
| 31 | + var elem = li.querySelector(".release"); |
| 32 | + if (elem) { |
| 33 | + elem.focus(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
2 | 37 | function getKey(ev) {
|
3 | 38 | if ("key" in ev && typeof ev.key != "undefined") {
|
4 | 39 | return ev.key;
|
5 | 40 | }
|
6 | 41 | return String.fromCharCode(ev.charCode || ev.keyCode);
|
7 | 42 | }
|
8 | 43 |
|
9 |
| - var active = null; |
| 44 | + function checkIfHasParent(elem, className) { |
| 45 | + while (elem && elem.tagName !== "BODY") { |
| 46 | + elem = elem.parentElement; |
| 47 | + if (elem.classList.contains(className)) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
10 | 54 | function handleKey(ev) {
|
11 |
| - if (ev.ctrlKey || ev.altKey || ev.metaKey || document.activeElement.tagName === "INPUT") { |
| 55 | + if (ev.ctrlKey || ev.altKey || ev.metaKey) { |
| 56 | + return; |
| 57 | + } |
| 58 | + if (ev.which === 27) { // Escape |
| 59 | + document.activeElement.blur(); |
| 60 | + return; |
| 61 | + } |
| 62 | + var tagName = document.activeElement.tagName; |
| 63 | + // We want to check two things here: if an input or an element of the docs.rs topbar |
| 64 | + // has the focus. If so, then we do nothing and simply return. |
| 65 | + if (tagName === "INPUT" || |
| 66 | + (tagName === "A" && |
| 67 | + checkIfHasParent(document.activeElement, "nav-container"))) |
| 68 | + { |
12 | 69 | return;
|
13 | 70 | }
|
14 | 71 |
|
15 | 72 | if (ev.which === 40) { // Down arrow
|
16 | 73 | ev.preventDefault();
|
17 |
| - if (active === null) { |
18 |
| - active = document.getElementsByClassName("recent-releases-container")[0].getElementsByTagName("li")[0]; |
19 |
| - } else if (active.nextElementSibling) { |
20 |
| - active.classList.remove("selected"); |
21 |
| - active = active.nextElementSibling; |
| 74 | + if (tagName === "BODY") { |
| 75 | + focusFirstSearchResult(); |
| 76 | + } else { |
| 77 | + var wrappingLi = getWrappingLi(document.activeElement); |
| 78 | + if (!wrappingLi) { |
| 79 | + // Doesn't seem like we are in the crates list, let's focus the first element |
| 80 | + // of the list then! |
| 81 | + focusFirstSearchResult(); |
| 82 | + } else if (wrappingLi.nextElementSibling) { |
| 83 | + focusOnLi(wrappingLi.nextElementSibling); |
| 84 | + } |
22 | 85 | }
|
23 |
| - active.classList.add("selected"); |
24 | 86 | } else if (ev.which === 38) { // Up arrow
|
25 | 87 | ev.preventDefault();
|
26 |
| - if (active === null) { |
27 |
| - active = document.getElementsByClassName("recent-releases-container")[0].getElementsByTagName("li")[0]; |
28 |
| - } else if (active.previousElementSibling) { |
29 |
| - active.classList.remove("selected"); |
30 |
| - active = active.previousElementSibling; |
31 |
| - } |
32 |
| - active.classList.add("selected"); |
33 |
| - active.focus(); |
34 |
| - } else if (ev.which === 13) { // Return |
35 |
| - if (active !== null) { |
36 |
| - document.location.href = active.getElementsByTagName("a")[0].href; |
| 88 | + if (tagName === "A") { |
| 89 | + var wrappingLi = getWrappingLi(document.activeElement); |
| 90 | + if (!wrappingLi) { |
| 91 | + // Doesn't seem like we are in the crates list, let's focus the first element |
| 92 | + // of the list then! |
| 93 | + focusFirstSearchResult(); |
| 94 | + } else if (wrappingLi.previousElementSibling) { |
| 95 | + focusOnLi(wrappingLi.previousElementSibling); |
| 96 | + } else { |
| 97 | + focusSearchInput(); |
| 98 | + } |
| 99 | + } else if (tagName === "BODY") { |
| 100 | + focusFirstSearchResult(); |
37 | 101 | }
|
38 |
| - } else { |
| 102 | + } else if (tagName !== "INPUT") { |
39 | 103 | switch (getKey(ev)) {
|
40 | 104 | case "s":
|
41 | 105 | case "S":
|
42 | 106 | ev.preventDefault();
|
43 |
| - var searchInputNav = document.getElementsByClassName("search-input-nav"); |
44 |
| - if (searchInputNav.length > 0) { |
45 |
| - searchInputNav[0].focus(); |
46 |
| - } |
| 107 | + focusSearchInput(); |
47 | 108 | break;
|
48 | 109 | }
|
49 | 110 | }
|
50 | 111 | }
|
51 | 112 |
|
52 | 113 | document.onkeypress = handleKey;
|
53 | 114 | document.onkeydown = handleKey;
|
54 |
| - |
55 |
| - var crates = Array.prototype.slice.call(document.getElementsByClassName("recent-releases-container")[0].getElementsByTagName("li")); |
56 |
| - for (var i = 0; i < crates.length; ++i) { |
57 |
| - crates[i].addEventListener("mouseover", function (event) { |
58 |
| - this.classList.remove("selected"); |
59 |
| - active = null; |
60 |
| - }); |
61 |
| - crates[i].addEventListener("mouseout", function (event) { |
62 |
| - this.classList.remove("selected"); |
63 |
| - active = null; |
64 |
| - }); |
65 |
| - } |
66 | 115 | })();
|
0 commit comments