the-glorious-startpage/js/search-box-key-events.js
Gerome Matilla 7b45ba5c2c
Quick search (#29)
* readme

* add quick search

* fix errors

* fix errors part 2
2020-06-19 10:39:25 +08:00

47 lines
1021 B
JavaScript

class SearchboxKeyEvents {
constructor() {
this._searchBox = document.querySelector('#searchBox');
this._searchQuerySend = searchQuerySend;
this.sendQuery = this._searchQuerySend.sendQuery;
this._registerSearchBoxOnKeyUpEvent();
}
_searchBoxOnKeyUpEvent = event => {
// Cancel the default action, if needed
event.preventDefault();
if (event.key === 'Tab') return;
// Autosuggestion
if (event.key.length === 1 || event.key === 'Backspace') {
if (this._searchBox.value < 1) {
// Hide suggestions
autoSuggestion.hideSuggestions();
return;
}
// Fetch suggestion/phrases
autoSuggestion.fetchSuggestions();
return;
}
// Search query
// Number 13 is the "Enter" key on the keyboard
if (event.key === 'Enter') {
// Don't accept empty strings
if (searchBox.value < 1) {
return;
}
// Search the web
this.sendQuery();
}
}
_registerSearchBoxOnKeyUpEvent = () => {
this._searchBox.addEventListener('keyup', this._searchBoxOnKeyUpEvent);
}
}