1
|
|
|
class SearchForm { |
2
|
|
|
|
3
|
|
|
constructor(container) { |
4
|
|
|
this.container = container; |
5
|
|
|
this.input = this.container.querySelector('input[name="search"]'); |
6
|
|
|
this.timeout = null; // init timeout variable to be used below in bindEvents |
7
|
|
|
this.time = 500; // in ms |
8
|
|
|
|
9
|
|
|
this.bindEvents(); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
bindEvents() { |
13
|
|
|
// listen for keystroke events |
14
|
|
|
this.input.addEventListener('keyup', (e) => { |
|
|
|
|
15
|
|
|
// clear timeout if it has already been set. |
16
|
|
|
// this will prevent the previous task from executing |
17
|
|
|
// if it has been less than <MILLISECONDS> |
18
|
|
|
clearTimeout(this.timeout); |
19
|
|
|
|
20
|
|
|
// make a new timeout set to go off in {this.time} |
21
|
|
|
this.timeout = setTimeout(() => this.search(this.input.value), this.time); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
search(string) { |
26
|
|
|
let link = this.setUrlParameter( |
27
|
|
|
this.container.getAttribute('action'), |
28
|
|
|
'search', |
29
|
|
|
string |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
let moduleId = this.container.querySelector('input[name="moduleId"]'); |
33
|
|
|
|
34
|
|
|
if (moduleId) { |
35
|
|
|
link = this.setUrlParameter(link, 'moduleId', moduleId.value); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
window.location = link; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
setUrlParameter(url, key, value) { |
42
|
|
|
let parts = url.split("#", 2), anchor = parts.length > 1 ? "#" + parts[1] : ''; |
43
|
|
|
let query = (url = parts[0]).split("?", 2); |
44
|
|
|
if (query.length === 1) |
45
|
|
|
return url + "?" + key + "=" + value + anchor; |
|
|
|
|
46
|
|
|
|
47
|
|
|
for (let params = query[query.length - 1].split("&"), i = 0; i < params.length; i++) |
48
|
|
|
if (params[i].toLowerCase().startsWith(key.toLowerCase() + "=")) |
|
|
|
|
49
|
|
|
return params[i] = key + "=" + value, query[query.length - 1] = params.join("&"), query.join("?") + anchor; |
|
|
|
|
50
|
|
|
|
51
|
|
|
return url + "&" + key + "=" + value + anchor |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
export default SearchForm; |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.