From d76bc91ed0f1ad7a9dd0490905029297f4efbce3 Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Sat, 4 Sep 2021 15:44:22 +0530 Subject: [PATCH] Reduce Cognitive Complexity of fastsearch.js (#554) --- assets/js/fastsearch.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/assets/js/fastsearch.js b/assets/js/fastsearch.js index 8d4700b2..586941b2 100644 --- a/assets/js/fastsearch.js +++ b/assets/js/fastsearch.js @@ -103,31 +103,31 @@ document.onkeydown = function (e) { } } else if (current_elem) ae = current_elem; - if (key === "ArrowDown" && resultsAvailable && inbox) { + if (key === "Escape") { + reset() + } else if (!resultsAvailable || !inbox) { + return + } else if (key === "ArrowDown") { e.preventDefault(); if (ae == sInput) { // if the currently focused element is the search input, focus the of first
  • activeToggle(resList.firstChild.lastChild); - } else if (ae.parentElement == last) { + } else if (ae.parentElement != last) { // if the currently focused element's parent is last, do nothing - } else { // otherwise select the next search result activeToggle(ae.parentElement.nextSibling.lastChild); } - } else if (key === "ArrowUp" && resultsAvailable && inbox) { + } else if (key === "ArrowUp") { e.preventDefault(); - if (ae == sInput) { - // if the currently focused element is input box, do nothing - } else if (ae.parentElement == first) { + if (ae.parentElement == first) { // if the currently focused element is first item, go to input box activeToggle(sInput); - } else { + } else if (ae != sInput) { + // if the currently focused element is input box, do nothing // otherwise select the previous search result activeToggle(ae.parentElement.previousSibling.lastChild); } - } else if (key === "ArrowRight" && resultsAvailable && inbox) { + } else if (key === "ArrowRight") { ae.click(); // click on active link - } else if (key === "Escape") { - reset() } }