-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
58 lines (53 loc) · 1.76 KB
/
main.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
var app = (function(data){
function show(suggestions){
document.getElementById("results").innerHTML = "";
var fragment = document.createDocumentFragment();
suggestions.forEach(function(suggestion){
var result = document.createElement("div");
result.className = "result";
result.innerHTML = suggestion;
result.addEventListener('click', function(e){
addToken(suggestion);
}, false);
fragment.appendChild(result);
});
document.getElementById("results").appendChild(fragment);
}
function addToken(content){
var token = document.createElement("span");
token.className = "token";
token.innerHTML = content;
token.addEventListener('click', function(e){
this.parentNode.removeChild(this);
});
document.getElementById("tokens").appendChild(token);
document.getElementById("query").value = "";
show(data);
}
// The first query is ""
show(data);
document.getElementById("query").addEventListener("input",function(e){
var query = e.target.value;
var regex = new RegExp(query,"i");
var suggestions = [];
data.forEach(function(datum){
if(datum.match(regex)) {
suggestions.push(datum);
};
});
show(suggestions);
}, false);
// User should be blocked from submit(enter) the input unless the input matches one of the entries
document.getElementById("query").addEventListener("keyup",function(e){
if(e.keyCode === 13 ) { // Enter
var query = e.target.value;
var regex = new RegExp(query,"i");
for(var i=data.length-1 ; i>0 ;i--){
if(data[i].toLowerCase() === query.toLowerCase()) {
return addToken(data[i]);
}
}
};
}, false);
return {show:show,addToken:addToken};
})(TZ);