aboutsummaryrefslogtreecommitdiff
path: root/assets_src/js/lib/search.js
blob: 0cb33df2cadd80d593125bf353e47968e9ac4836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var strokeTimeout = null;

function instantSearch() {
	if (strokeTimeout) {
		clearTimeout(strokeTimeout);
	}
	strokeTimeout = setTimeout(doSearch, 150);
}

function doSearch() {
	value = document.getElementById("search").value;

	if (value === "") {
		strokeTimeout = null;
		document.getElementById("search-results").innerHTML = "";
		return;
	}
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
	if (xhr.readyState == XMLHttpRequest.DONE) {
			if (xhr.status === 404 || xhr.status === 400 || typeof xhr.response === 'undefined' || xhr.response === '404 not found') {
				document.getElementById("search-results").innerHTML = '';
				return
			}
			if (value === "") {
				document.getElementById("search-results").innerHTML = "";
				return;
			}

			document.getElementById("search-results").innerHTML = xhr.response.replace(new RegExp('{ (.*?)(' + value + ')(.*?) }', 'gi'), "$1<b>$2</b>$3").replace(new RegExp('{ (.*?) }', 'gi'), '$1');
		}
	}
	xhr.open('GET', '/search/gril_instant/' + value, true);
	xhr.send(null);
	strokeTimeout = null;
}

function clickSearchResult(resId) {

}