Skip to content
Snippets Groups Projects
Commit 9b515f7d authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms Committed by Jan-Hendrik Willms
Browse files

remove duplicated code and make moving the focus less complex, re #4072

Merge request studip/studip!3054
parent e766694b
No related branches found
No related tags found
No related merge requests found
...@@ -27,58 +27,45 @@ STUDIP.domReady(() => { ...@@ -27,58 +27,45 @@ STUDIP.domReady(() => {
// Enlarge search input on focus and show hints. // Enlarge search input on focus and show hints.
$('#globalsearch-input').on('focus', function() { $('#globalsearch-input').on('focus', function() {
STUDIP.GlobalSearch.toggleSearchBar(true, false); STUDIP.GlobalSearch.toggleSearchBar(true, false);
}); }).on('keypress', (e) => {
// Start search on Enter
// Start search on Enter if (e.key === 'Enter') {
$('#globalsearch-input').on('keypress', function(e) {
if (e.which === 13) {
STUDIP.GlobalSearch.doSearch();
return false;
}
});
$('#globalsearch-input').on('keypress', function(e) {
if (e.which === 13) {
STUDIP.GlobalSearch.doSearch(); STUDIP.GlobalSearch.doSearch();
return false; return false;
} }
}); });
$('#globalsearch-searchbar').on('keydown', function(e) { $('#globalsearch-searchbar').on('keydown', function(e) {
if (e.originalEvent.code === 'ArrowDown') { if (!['ArrowDown', 'ArrowUp'].includes(e.key)) {
if ($('#globalsearch-list [role=listitem]:focus').length === 0) { return;
$('#globalsearch-list [role=listitem]:visible').first().focus();
} else {
let n = $('#globalsearch-list [role=listitem]:focus').next();
if (n.length > 0 && n.is('[role=listitem]:visible')) {
n.focus();
} else {
n = $('#globalsearch-list [role=listitem]:focus').parent().next().find('[role=listitem]:visible').first();
if (n.length > 0) {
n.focus();
} else {
$('#globalsearch-list [role=listitem]:visible').first().focus();
}
}
}
return false;
} }
if (e.originalEvent.code === 'ArrowUp') {
if ($('#globalsearch-list [role=listitem]:focus').length === 0) { e.preventDefault();
$('#globalsearch-list [role=listitem]:visible').last().focus();
} else { // Get all possible items
let n = $('#globalsearch-list [role=listitem]:focus').prev(); const items = $('#globalsearch-list [role=listitem]:visible');
if (n.length > 0 && n.is('[role=listitem]:visible')) {
n.focus(); // Find focussed element
} else { const focussed = items.filter(':focus');
n = $('#globalsearch-list [role=listitem]:focus').parent().prev().find('[role=listitem]:visible').last();
if (n.length > 0) { // Get index of focussed element in all items
n.focus(); let index = focussed.length > 0 ? items.index(focussed[0]) : null;
} else {
$('#globalsearch-list [role=listitem]:visible').last().focus(); // Move focussed element up or down in items
} if (e.key === 'ArrowDown') {
} index = (index ?? -1) + 1;
} } else {
return false; 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();
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment