-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
128 lines (111 loc) · 2.88 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
let namesUl = document.getElementById("names");
let searchInput = document.getElementById("search");
let randomButton = document.getElementById("random-button");
let h2 = document.querySelector("h2");
let pageInfo = document.getElementById("page-info");
let buttons = document.getElementsByClassName("buttons")[0];
let toggleButton = document.getElementById("toggle-button");
let searchButton = document.getElementById("search-button");
let form = document.getElementById("form");
let namesPerPage = 100;
let page = 0;
let searchByStart = true;
let currentNames = [];
let visibleNames = [];
let ALL_NAMES = [];
fetch("./names.json")
.then((res) => res.json())
.then((json) => {
ALL_NAMES = json;
drawNames(ALL_NAMES);
});
form.addEventListener("submit", (e) => {
e.preventDefault();
});
const clearTheSearch = () => {
searchInput.value = "";
searchInput.focus();
};
const toggleStart = () => {
searchByStart = !searchByStart;
toggleButton.innerHTML = searchByStart ? "START" : "ALL";
};
const nextPage = () => {
if (namesPerPage * page + namesPerPage >= currentNames.length) {
console.log("NO");
} else {
page++;
window.scrollTo(0, 0);
drawNames(currentNames);
}
};
const previousPage = () => {
if (page > 0) {
page--;
window.scrollTo(0, 0);
drawNames(currentNames);
}
};
const firstPage = () => {
if (page > 0) {
page = 0;
window.scrollTo(0, 0);
drawNames(currentNames);
}
};
const filterNames = (query) => {
let namesToShow = [];
if (searchByStart) {
ALL_NAMES.map((name) => {
if (name.toLowerCase().startsWith(query.toLowerCase())) {
namesToShow.push(name);
}
});
} else {
ALL_NAMES.map((name) => {
if (name.toLowerCase().includes(query.toLowerCase())) {
namesToShow.push(name);
}
});
}
return namesToShow;
};
// search.addEventListener("submit", () => {
// console.log("enter");
// });
const searchNames = () => {
page = 0;
searchButton.focus();
drawNames(filterNames(search.value));
};
const getRandomName = () => {
let name;
if (currentNames.length > 0) {
name = currentNames[Math.floor(Math.random() * currentNames.length)];
} else {
name = "No names!";
}
randomButton.innerHTML = name;
};
const drawNames = (names) => {
if (names.length == 0) {
h2.innerHTML = `no results for '${search.value}'`;
} else {
h2.innerHTML = `${names.length} ${
names.length == 1 ? "result" : "results"
}, page ${page + 1}`;
}
namesUl.innerHTML = "";
names
.slice(page * namesPerPage, page * namesPerPage + namesPerPage)
.map((name) => {
let li = document.createElement("li");
li.innerHTML = name;
namesUl.appendChild(li);
});
pageInfo.innerHTML = `page ${page + 1} of ${Math.ceil(
names.length / namesPerPage
)}`;
buttons.style.display = names.length != 0 ? "grid" : "none";
currentNames = names;
};