aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/global_search.js
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /resources/assets/javascripts/bootstrap/global_search.js
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'resources/assets/javascripts/bootstrap/global_search.js')
-rw-r--r--resources/assets/javascripts/bootstrap/global_search.js42
1 files changed, 37 insertions, 5 deletions
diff --git a/resources/assets/javascripts/bootstrap/global_search.js b/resources/assets/javascripts/bootstrap/global_search.js
index 4d0738e..0e179b7 100644
--- a/resources/assets/javascripts/bootstrap/global_search.js
+++ b/resources/assets/javascripts/bootstrap/global_search.js
@@ -27,15 +27,47 @@ STUDIP.domReady(() => {
// Enlarge search input on focus and show hints.
$('#globalsearch-input').on('focus', function() {
STUDIP.GlobalSearch.toggleSearchBar(true, false);
- });
-
- // Start search on Enter
- $('#globalsearch-input').on('keypress', function(e) {
- if (e.which === 13) {
+ }).on('keypress', (e) => {
+ // Start search on Enter
+ if (e.key === 'Enter') {
STUDIP.GlobalSearch.doSearch();
return false;
}
});
+ $('#globalsearch-searchbar').on('keydown', function(e) {
+ if (!['ArrowDown', 'ArrowUp'].includes(e.key)) {
+ return;
+ }
+
+ e.preventDefault();
+
+ // Get all possible items
+ const items = $('#globalsearch-list [role=listitem]:visible');
+
+ // Find focussed element
+ const focussed = items.filter(':focus');
+
+ // Get index of focussed element in all items
+ let index = focussed.length > 0 ? items.index(focussed[0]) : null;
+
+ // Move focussed element up or down in items
+ if (e.key === 'ArrowDown') {
+ index = (index ?? -1) + 1;
+ } else {
+ index = (index ?? items.length) - 1;
+ }
+
+ // Clamp index to sane boundaries
+ if (index < 0) {
+ index = 0;
+ } else if (index > items.length - 1) {
+ index = items.length - 1;
+ }
+
+ // Focus new element by index
+ items.get(index).focus();
+ });
+
// Close search on click on page.
$('#navigation-level-1, #current-page-structure, #main-footer').on('click', function() {