-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
154 lines (136 loc) · 3.83 KB
/
utils.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
function $(name) { return document.getElementById(name); }
function Tag(name, attrs, body, children) {
let elem = document.createElement(name);
if (body) {
elem.innerHTML = body;
}
for (let k in attrs) {
elem.setAttribute(k, attrs[k]);
}
for (let i in children) {
elem.appendChild(children[i]);
}
return elem;
}
function Text(text) {
return document.createTextNode(text);
}
function debounce(func, cancelFunc, timeout = 200) {
let timer;
return (...args) => {
cancelFunc();
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
}
}
function sendRequest(method, path, query, body, funcDone, funcError) {
let req = new XMLHttpRequest();
if (query) {
let params = new URLSearchParams(query);
path = path + "?" + params.toString();
}
req.open(method, path, true);
req.onreadystatechange = function () {
if (req.readyState == XMLHttpRequest.DONE) {
let status = req.status;
if (status === 0 || (status >= 200 && status < 400)) {
if (funcDone)
funcDone(req.responseText, status);
} else {
if (funcError)
funcError(req.responseText, status);
}
}
}
if (body) {
req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify(body));
} else {
req.send();
}
return req;
}
const KEY_DOWN = 40;
const KEY_UP = 38;
const KEY_ENTER = 13;
function Autocomplete(args) {
this.input_field = args.input;
this.parent = this.input_field.parentNode;
this.suggestionContainer = Tag('div', {class: 'autocomplete-items'});
this.parent.appendChild(this.suggestionContainer);
this.focus = -1;
// funcFindVariants(value, funcSuccess(suggestion[] {.render}, funcFail)
this.funcFindVariants = args.funcFindVariants;
this.funcSelect = args.funcSelect; // (suggestion)
this.current_search = null;
this.input_field.addEventListener('input', (e) => {
this.clearSuggestionList();
let value = this.input_field.value;
if (value) {
this.requestSuggestionList(value);
}
});
this.input_field.addEventListener('keydown', (e) => {
if (!this.suggestions)
return;
if (e.keyCode == KEY_DOWN) {
e.preventDefault();
this.changeFocus(1);
} else if (e.keyCode == KEY_UP) {
e.preventDefault();
this.changeFocus(-1);
} else if (e.keyCode == KEY_ENTER) {
e.preventDefault();
if (this.focus >= 0 && this.focus < this.suggestions.length) {
this.select(this.focus);
}
}
});
}
Autocomplete.prototype.select = function(index) {
let selection = this.suggestions[index];
this.clearSuggestionList();
this.input_field.blur();
this.funcSelect(selection);
}
Autocomplete.prototype.changeFocus = function(delta) {
let prevFocus = this.focus;
this.focus += delta;
if (this.focus < 0) this.focus = this.suggestions.length - 1;
if (this.focus >= this.suggestions.length) this.focus = 0;
if (prevFocus >= 0 && prevFocus < this.suggestions.length)
this.suggestions[prevFocus].deactivate();
if (this.focus >= 0 && this.focus < this.suggestions.length)
this.suggestions[this.focus].activate();
}
Autocomplete.prototype.clearSuggestionList = function() {
if (this.current_search) {
this.current_search.abort();
this.current_search = null;
}
this.suggestionContainer.innerHTML = "";
this.focus = -1;
this.suggestions = [];
}
Autocomplete.prototype.requestSuggestionList = function(value) {
this.clearSuggestionList();
debounce(() => {
this.clearSuggestionList();
this.current_search = this.funcFindVariants(value, (suggestions) => {
this.suggestions = suggestions;
for (let i in this.suggestions) {
let item = this.suggestions[i].element;
// TODO add click
let elem = Tag('div', {class:'autocomplete-item'}, null, [item]);
elem.addEventListener("click", (e) => {
this.select(i);
});
this.suggestionContainer.appendChild(elem);
}
}, (error) => {
abort("LOL ERROR", error);
});
}, () => {}
)();
}