X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/e7e83a4109ab95e58f3772a2d9cd759b43ddbd3f..refs/pull/5280/head:/resources/js/components/global-search.js diff --git a/resources/js/components/global-search.js b/resources/js/components/global-search.js index c940ad983..798bd7aac 100644 --- a/resources/js/components/global-search.js +++ b/resources/js/components/global-search.js @@ -1,10 +1,13 @@ -import {htmlToDom} from "../services/dom"; -import {debounce} from "../services/util"; +import {htmlToDom} from '../services/dom'; +import {debounce} from '../services/util'; +import {KeyboardNavigationHandler} from '../services/keyboard-navigation'; +import {Component} from './component'; /** - * @extends {Component} + * Global (header) search box handling. + * Mainly to show live results preview. */ -class GlobalSearch { +export class GlobalSearch extends Component { setup() { this.container = this.$el; @@ -22,12 +25,12 @@ class GlobalSearch { // Handle search input changes this.input.addEventListener('input', () => { - const value = this.input.value; + const {value} = this.input; if (value.length > 0) { this.loadingWrap.style.display = 'block'; this.suggestionResultsWrap.style.opacity = '0.5'; updateSuggestionsDebounced(value); - } else { + } else { this.hideSuggestions(); } }); @@ -37,7 +40,11 @@ class GlobalSearch { this.input.setAttribute('autocomplete', 'on'); this.button.focus(); this.input.focus(); - }) + }); + + new KeyboardNavigationHandler(this.container, () => { + this.hideSuggestions(); + }); } /** @@ -48,7 +55,7 @@ class GlobalSearch { if (!this.input.value) { return; } - + const resultDom = htmlToDom(results); this.suggestionResultsWrap.innerHTML = ''; @@ -64,7 +71,7 @@ class GlobalSearch { this.container.classList.add('search-active'); window.requestAnimationFrame(() => { this.suggestions.classList.add('search-suggestions-animation'); - }) + }); } hideSuggestions() { @@ -72,6 +79,5 @@ class GlobalSearch { this.suggestions.classList.remove('search-suggestions-animation'); this.suggestionResultsWrap.innerHTML = ''; } -} -export default GlobalSearch; \ No newline at end of file +}