From 5230daaf17d01d342fb33fb33b02101c382654fd Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 13:51:11 +0200 Subject: [PATCH] #12 Registered Shift + F for global search --- enhancements/quickSearch.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/enhancements/quickSearch.js b/enhancements/quickSearch.js index cf9db68..68cf247 100644 --- a/enhancements/quickSearch.js +++ b/enhancements/quickSearch.js @@ -1,3 +1,5 @@ +const quickSearchID = 'ea-quickSearch'; + runAfterLoad(() => { initSearch(); }, ".*"); @@ -13,7 +15,9 @@ function initSearch() { quickSearchElement.type = 'text'; quickSearchElement.classList.add('ng-pristine', 'ng-valid', 'ng-empty', 'ng-touched'); quickSearchElement.placeholder = 'Quick Search (Shift + F)'; - quickSearchElement.addEventListener('keydown', event => handleQuickSearch(event, quickSearchElement)); + quickSearchElement.id = quickSearchID; + // register Enter keybinding + quickSearchElement.addEventListener('keypress', event => handleQuickSearch(event)); entry.appendChild(quickSearchElement); @@ -24,14 +28,26 @@ function initSearch() { let menu = document.getElementById('materialize-menu-dropdown'); menu.insertAdjacentElement('beforeend', entry); + + // register focus hotkey + document.addEventListener('keypress', event => handleSearchForShiftF(event)); } -function handleQuickSearch(event, quickSearchElement) { +function handleQuickSearch(event) { if (event.key === 'Enter') { let url = new URL(window.location.origin) url.pathname = '/search'; - url.searchParams.append('q', quickSearchElement.value); + url.searchParams.append('q', document.getElementById(quickSearchID).value); window.location.href = url.href; } +} + +function handleSearchForShiftF(event) { + if (isShiftPressed) { + if (event.key === 'F') { + event.preventDefault(); + document.getElementById(quickSearchID).focus(); + } + } } \ No newline at end of file