Skip to content

Commit d76bc91

Browse files
Reduce Cognitive Complexity of fastsearch.js (#554)
1 parent 42a23c9 commit d76bc91

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

assets/js/fastsearch.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,31 @@ document.onkeydown = function (e) {
103103
}
104104
} else if (current_elem) ae = current_elem;
105105

106-
if (key === "ArrowDown" && resultsAvailable && inbox) {
106+
if (key === "Escape") {
107+
reset()
108+
} else if (!resultsAvailable || !inbox) {
109+
return
110+
} else if (key === "ArrowDown") {
107111
e.preventDefault();
108112
if (ae == sInput) {
109113
// if the currently focused element is the search input, focus the <a> of first <li>
110114
activeToggle(resList.firstChild.lastChild);
111-
} else if (ae.parentElement == last) {
115+
} else if (ae.parentElement != last) {
112116
// if the currently focused element's parent is last, do nothing
113-
} else {
114117
// otherwise select the next search result
115118
activeToggle(ae.parentElement.nextSibling.lastChild);
116119
}
117-
} else if (key === "ArrowUp" && resultsAvailable && inbox) {
120+
} else if (key === "ArrowUp") {
118121
e.preventDefault();
119-
if (ae == sInput) {
120-
// if the currently focused element is input box, do nothing
121-
} else if (ae.parentElement == first) {
122+
if (ae.parentElement == first) {
122123
// if the currently focused element is first item, go to input box
123124
activeToggle(sInput);
124-
} else {
125+
} else if (ae != sInput) {
126+
// if the currently focused element is input box, do nothing
125127
// otherwise select the previous search result
126128
activeToggle(ae.parentElement.previousSibling.lastChild);
127129
}
128-
} else if (key === "ArrowRight" && resultsAvailable && inbox) {
130+
} else if (key === "ArrowRight") {
129131
ae.click(); // click on active link
130-
} else if (key === "Escape") {
131-
reset()
132132
}
133133
}

0 commit comments

Comments
 (0)