STUDIP.domReady(() => { // Clear search term $('#globalsearch-clear').on('click', function() { var before = $('#globalsearch-input').val(); STUDIP.GlobalSearch.resetSearch(); if ($('html').is('.responsive-display') && before.length === 0) { STUDIP.GlobalSearch.toggleSearchBar(false); } return false; }); // Bind icon click to performing search. $('#globalsearch-icon').on('click', function() { STUDIP.GlobalSearch.doSearch(); if ($('html').hasClass('responsified')) { var input = $('#globalsearch-input'); input.toggleClass('hidden-small-down', false); input.focus(); } return false; }); // Enlarge search input on focus and show hints. $('#globalsearch-input').on('focus', function() { STUDIP.GlobalSearch.toggleSearchBar(true, false); }).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() { if (!$('#globalsearch-input').hasClass('hidden-js')) { STUDIP.GlobalSearch.toggleSearchBar(false, false); } }); // Show/hide hints on click. $('#globalsearch-togglehints').on('click', function() { var toggle = $('#globalsearch-togglehints'), currentText = toggle.text(); toggle.text(toggle.data('toggle-text').trim()); toggle.data('toggle-text', currentText); toggle.toggleClass('open'); }); // Delegate events on result container so we don't have to bind them // one by one $('#globalsearch-results').on('click', '.globalsearch-category a', function() { var category = $(this) .closest('.globalsearch-category') .data('category'); STUDIP.GlobalSearch.expandCategory(category); return false; }); // Key bindings. $(document).keydown(function(e) { // Don't do anything if a dialog is open if (STUDIP.Dialog.stack.length > 0) { return; } // ctrl + space if (e.which === 32 && e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey) { e.preventDefault(); if ($('#globalsearch-searchbar').hasClass('is-visible')) { STUDIP.GlobalSearch.toggleSearchBar(false, false); $('#globalsearch-input').blur(); } else { $('#globalsearch-input').focus(); } // escape } else if (e.which === 27 && !e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey) { e.preventDefault(); STUDIP.GlobalSearch.toggleSearchBar(false, true); } }); // Start searching 750 ms after user stopped typing or content was added // by pasting text via right-click. $('#globalsearch-input').on('keyup paste', _.debounce(function() { STUDIP.GlobalSearch.doSearch(); }, 750) ); });