-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
151 lines (125 loc) · 4.08 KB
/
script.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
window.onload = init;
function init() {
autocomplete(document.getElementsByName("country")[0], countries);
}
/**
* Sends AJAX req to server to get dos and donts file of country as specified by the user
*/
function getData() {
let country = verifyCountry();
//if there is atleat one country in the system that matches the search from user, get its files via AJAX req
if(country != "-1") {
let xhr = new XMLHttpRequest();
xhr.open('GET', "http://localhost/dos-donts/server.php?country=" + country);
xhr.send();
xhr.onload = () => {
let obj = JSON.parse(xhr.response);
document.getElementById("doContent").innerHTML = obj['dos'];
document.getElementById("dontContent").innerHTML = obj['donts'];
document.getElementsByName("country")[0].blur();
}
}
else {
alert("Unfortunately, this country is not available in our system yet.");
}
}
/**
* Verifies if a country exists in the system.
*
* @return If a country exists, return the formatted country name else return -1
*/
function verifyCountry() {
//Format input -> remove leading, trailing spaces, and convert to PascalCase
let formattedSearch = document.getElementsByName("country")[0].value.trim()
.split(' ').map(name =>
name.charAt(0).toUpperCase() + name.slice(1).toLowerCase()
).join('');
if(countries.indexOf(formattedSearch) > -1)
return formattedSearch;
//In case of error alert the user and dont send request
return -1;
}
//https://www.w3schools.com/howto/howto_js_autocomplete.asp
/**
* Auto-completes user input, using an input element and an araay
* of values to auto complete from.
*
* @param {input element} inp
* @param {array to acutocomplete from} arr
*/
function autocomplete(inp, arr) {
let currentFocus;
inp.addEventListener("input", function(e) {
let a, b, i, val = this.value;
closeAllLists();
if (!val)
return false;
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
getData();
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
let x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.key === "ArrowDown") {
currentFocus++;
addActive(x);
} else if (e.key === "ArrowUp") {
currentFocus--;
addActive(x);
} else if(e.key === "Tab") {
e.preventDefault();
} else if(e.key === "Escape") {
closeAllLists();
} else if (e.key === "Enter") {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
} else {
getData();
closeAllLists();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length)
currentFocus = 0;
if (currentFocus < 0)
currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}