|
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.tagName !== "LI") { |
| 22 | + elem = elem.parentElement; |
| 23 | + } |
| 24 | + return elem; |
| 25 | + } |
| 26 | + |
| 27 | + function focusOnLi(li) { |
| 28 | + var elem = li.querySelector(".release"); |
| 29 | + if (elem) { |
| 30 | + elem.focus(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
2 | 34 | function getKey(ev) {
|
3 | 35 | if ("key" in ev && typeof ev.key != "undefined") {
|
4 | 36 | return ev.key;
|
5 | 37 | }
|
6 | 38 | return String.fromCharCode(ev.charCode || ev.keyCode);
|
7 | 39 | }
|
8 | 40 |
|
9 |
| - var active = null; |
| 41 | + function checkIfHasParent(elem, className) { |
| 42 | + while (elem && elem.tagName !== "BODY") { |
| 43 | + elem = elem.parentElement; |
| 44 | + if (elem.classList.constains(className)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
10 | 51 | function handleKey(ev) {
|
11 |
| - if (ev.ctrlKey || ev.altKey || ev.metaKey || document.activeElement.tagName === "INPUT") { |
| 52 | + if (ev.ctrlKey || ev.altKey || ev.metaKey) { |
| 53 | + return; |
| 54 | + } |
| 55 | + var tagName = document.activeElement.tagName; |
| 56 | + if (["BODY", "INPUT"].indexOf(tagName) === -1 && |
| 57 | + tagName !== "A" && |
| 58 | + !checkIfHasParent(document.activeElement, "recent-releases-container")) |
| 59 | + { |
12 | 60 | return;
|
13 | 61 | }
|
14 | 62 |
|
15 | 63 | if (ev.which === 40) { // Down arrow
|
16 | 64 | 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; |
| 65 | + if (["BODY", "INPUT"].indexOf(tagName) !== -1) { |
| 66 | + focusFirstSearchResult(); |
| 67 | + } else { |
| 68 | + var wrappingLi = getWrappingLi(document.activeElement); |
| 69 | + if (wrappingLi.nextElementSibling) { |
| 70 | + focusOnLi(wrappingLi.nextElementSibling); |
| 71 | + } |
22 | 72 | }
|
23 |
| - active.classList.add("selected"); |
24 | 73 | } else if (ev.which === 38) { // Up arrow
|
25 | 74 | 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; |
| 75 | + if (tagName === "A") { |
| 76 | + var wrappingLi = getWrappingLi(document.activeElement); |
| 77 | + if (wrappingLi.previousElementSibling) |
| 78 | + { |
| 79 | + focusOnLi(wrappingLi.previousElementSibling); |
| 80 | + } else { |
| 81 | + focusSearchInput(); |
| 82 | + } |
| 83 | + } else if (tagName === "BODY") { |
| 84 | + focusFirstSearchResult(); |
37 | 85 | }
|
38 |
| - } else { |
| 86 | + } else if (ev.which === 27) { // Escape |
| 87 | + document.activeElement.blur(); |
| 88 | + } else if (tagName !== "INPUT") { |
39 | 89 | switch (getKey(ev)) {
|
40 | 90 | case "s":
|
41 | 91 | case "S":
|
42 | 92 | ev.preventDefault();
|
43 |
| - var searchInputNav = document.getElementsByClassName("search-input-nav"); |
44 |
| - if (searchInputNav.length > 0) { |
45 |
| - searchInputNav[0].focus(); |
46 |
| - } |
| 93 | + focusSearchInput(); |
47 | 94 | break;
|
48 | 95 | }
|
49 | 96 | }
|
50 | 97 | }
|
51 | 98 |
|
52 | 99 | document.onkeypress = handleKey;
|
53 | 100 | 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 | 101 | })();
|
0 commit comments