-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-completion.js
170 lines (152 loc) · 4.01 KB
/
auto-completion.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let container = document.querySelector(".form");
let list = container.querySelector(".auto-complete");
let input = container.querySelector("input")
const countries = [
"Nigeria",
"Kenya",
"Albania",
"China",
"Mexico",
"Zimbabwe",
"italy",
"Egypt",
"London",
"Arabia",
"Nepal",
" America", // Test for white space trimming
"Germany",
"Span",
"India",
"Tunisia",
"Egypt",
"Israel",
"Zambia",
"Czech Republic",
"Qatar",
"Morocco",
"Somalia",
"Sudan",
"Gambia",
"Gabon",
"South Africa",
"Cape Verde",
"France",
"Venezuela",
"Brazil",
"Pakistan",
"Bangledesh",
"Afghanistan",
"Iran",
"Iraq",
"South Korea",
];
// Items that matched
matches = [];
// Focus input onLoad
input.focus()
// Initial cursor position
var cursorPosition = 0;
input.addEventListener("keyup", browseMatches);
function browseMatches(e){
const {value} = e.target;
const {keyCode} = e;
let trimmedValue = value.trim(' ');
// Reset the list
list.innerHTML = "";
toggleContainer("hide");
// Start searching
if (trimmedValue.length > 0) {
// Call the match checker function
checkForMatches(trimmedValue);
// Load the matches into the DOM
showMatches(matches);
// Control the cursor position for hightlighting result
switch(keyCode) {
// Hide the results when Esc key is clicked
case 27: toggleContainer("hide")
break;
// Allow for infinite scrolling
case 38: if(cursorPosition !== 0){
(cursorPosition--)
}else{
// Enable infinite scrolling if enabled
if (container.getAttribute('data-infinite') == "true") {
(cursorPosition = ( matches.length-1) );
}
}
break;
case 40: if(cursorPosition !== (matches.length -1)){
(cursorPosition++)
}else{
// Enable infinite scrolling if enabled
if (container.getAttribute('data-infinite') == "true") {
(cursorPosition = 0);
}
}
break;
case 13:
break;
}
}
}
// Hide or show the autocomplete list
function toggleContainer(state){
// Recalculate max-height to effect animation
let totalHeight = 0;
let children = list.children;
// Delay the loop
setTimeout(()=>{
if (children.length) {
// Set the first result as selected
children[cursorPosition].classList.add("selected");
// Makes sure the selected result is in view
children[cursorPosition].scrollIntoViewIfNeeded()
}
// Gather individual heights of each list element
for (var child = 0; child < children.length; child++) {
totalHeight += children[child].scrollHeight;
// Enable click to select option
children[child].addEventListener("click", selectOption)
}
}, 0)
setTimeout(()=>{
if(state == "show"){
list.style.visibility = "visible";
list.style.height = totalHeight + "px";
}else if(state == "hide"){
list.style.visibility = "hidden";
list.style.height = "0px";
}
},8)
}
// Compare the search term with the countries in the array and return matches
function checkForMatches(searchTerm){
toggleContainer('show');
// Init the matches back to empty
matches = [];
// Start checking for matches
for (var i = 0; i < countries.length; i++) {
if(countries[i].toLowerCase().indexOf(searchTerm.toLowerCase()) != -1){
matches.push(countries[i])
}
}
return matches
}
// Show matches
function showMatches(matchList){
toggleContainer('show');
if (matchList.length > 0) {
for (let match of matchList) {
let firstLetter = match.trim(" ")[0];
list.innerHTML += `<li><span>${firstLetter}</span>${match}</li>`;
}
}else{
list.innerHTML = "<div>No match found!</div>"
}
}
// Select option
function selectOption(){
// Select the text in th option and hide the form
input.value = this.innerHTML.split("</span>")[1];
toggleContainer("hide")
}