diff options
Diffstat (limited to 'assets_src/js/lib/search.js')
-rw-r--r-- | assets_src/js/lib/search.js | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/assets_src/js/lib/search.js b/assets_src/js/lib/search.js index 0cb33df..85559ef 100644 --- a/assets_src/js/lib/search.js +++ b/assets_src/js/lib/search.js | |||
@@ -1,40 +1,51 @@ | |||
1 | var strokeTimeout = null; | 1 | import * as dom from './dom'; |
2 | import * as ajax from './ajax'; | ||
2 | 3 | ||
3 | function instantSearch() { | 4 | export class InstantSearch { |
4 | if (strokeTimeout) { | 5 | constructor(element, resultsElem) { |
5 | clearTimeout(strokeTimeout); | 6 | this.resultClicked = () => {}; |
6 | } | 7 | this.element = element; |
7 | strokeTimeout = setTimeout(doSearch, 150); | 8 | this.resultsElem = resultsElem; |
8 | } | 9 | this.strokeTimeout = null; |
9 | 10 | element.addEventListener('keydown', e => { | |
10 | function doSearch() { | 11 | if (this.strokeTimeout) { |
11 | value = document.getElementById("search").value; | 12 | clearTimeout(this.strokeTimeout); |
12 | |||
13 | if (value === "") { | ||
14 | strokeTimeout = null; | ||
15 | document.getElementById("search-results").innerHTML = ""; | ||
16 | return; | ||
17 | } | ||
18 | var xhr = new XMLHttpRequest(); | ||
19 | xhr.onreadystatechange = function() { | ||
20 | if (xhr.readyState == XMLHttpRequest.DONE) { | ||
21 | if (xhr.status === 404 || xhr.status === 400 || typeof xhr.response === 'undefined' || xhr.response === '404 not found') { | ||
22 | document.getElementById("search-results").innerHTML = ''; | ||
23 | return | ||
24 | } | 13 | } |
25 | if (value === "") { | 14 | this.strokeTimeout = setTimeout(() => this.doSearch(), 150); |
26 | document.getElementById("search-results").innerHTML = ""; | 15 | }, false); |
27 | return; | 16 | resultsElem.addEventListener('click', e => { |
17 | let li = dom.closest(e.target, el => el.tagName.match(/li/i)); | ||
18 | if (!li) { | ||
19 | return true; | ||
28 | } | 20 | } |
21 | this.resultsElem.innerHTML = ''; | ||
22 | this.resultClicked(li.getAttribute('data-id')); | ||
23 | }, true); | ||
24 | } | ||
29 | 25 | ||
30 | document.getElementById("search-results").innerHTML = xhr.response.replace(new RegExp('{ (.*?)(' + value + ')(.*?) }', 'gi'), "$1<b>$2</b>$3").replace(new RegExp('{ (.*?) }', 'gi'), '$1'); | 26 | async doSearch() { |
27 | let value = this.element.value; | ||
28 | if (value.length < 2) { | ||
29 | this.strokeTimeout = null; | ||
30 | this.resultsElem.innerHTML = ""; | ||
31 | return; | ||
32 | } | ||
33 | let response = null; | ||
34 | try { | ||
35 | response = await ajax.get(`/search/gril_instant/${value}`); | ||
36 | } catch (e) { | ||
37 | this.resultsElem.innerHTML = ''; | ||
38 | return; | ||
31 | } | 39 | } |
40 | this.resultsElem.innerHTML = response | ||
41 | .replace( | ||
42 | new RegExp('{ (.*?)(' + | ||
43 | value + | ||
44 | ')(.*?) }', 'gi'), | ||
45 | "$1<b>$2</b>$3") | ||
46 | .replace( | ||
47 | new RegExp('{ (.*?) }', 'gi'), | ||
48 | '$1'); | ||
49 | this.strokeTimeout = null; | ||
32 | } | 50 | } |
33 | xhr.open('GET', '/search/gril_instant/' + value, true); | ||
34 | xhr.send(null); | ||
35 | strokeTimeout = null; | ||
36 | } | 51 | } |
37 | |||
38 | function clickSearchResult(resId) { | ||
39 | |||
40 | } \ No newline at end of file | ||