-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (146 loc) · 5.2 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const searchBar = document.getElementById('searchBar');
const form = document.getElementById('search-form');
const searchBtn = document.getElementById('search-btn');
// FUNCTIONS:
function submitSearch(e) {
e.preventDefault();
deleteSearchResults();
processSearch();
searchBar.value = '';
// document.body.remove();
}
async function processSearch() {
clearStatsLine();
const searchTerm = getSearchTerm();
if(searchTerm === '') return;
const resultArray = await getSearchResults(searchTerm);
if(resultArray.length) {
buildSearchResults(resultArray);
} // build search results
setStatsLine(resultArray.length);
}
function deleteSearchResults() {
const parent = document.getElementById('searchResults');
let child = parent.lastElementChild;
while(child) {
parent.removeChild(child);
child = parent.lastElementChild;
}
}
function buildSearchResults(resultArray) {
resultArray.forEach(result => {
const resultItem = createResultItem(result);
const resultContents = document.createElement("div");
resultContents.classList.add("resultContents", "py-2", "px-3");
if (result.image) {
const resultImage = createResultImage(result);
resultContents.append(resultImage);
}
const resultText = createResultText(result);
resultContents.append(resultText);
resultItem.append(resultContents);
const searchResults = document.getElementById("searchResults");
searchResults.append(resultItem);
});
}
const createResultItem = (result) => {
const resultItem = document.createElement("div");
resultItem.classList.add("resultItem");
const resultTitle = document.createElement("div");
resultTitle.classList.add("resultTitle", "my-3", "px-3");
const link = document.createElement("a");
link.href = `https://en.wikipedia.org/?curid=${result.id}`;
link.textContent = result.title;
link.target = "_blank";
resultTitle.append(link);
resultItem.append(resultTitle);
return resultItem;
};
const createResultImage = (result) => {
const resultFlex = document.createElement("div");
resultFlex.classList.add("resultFlex", "d-flex");
const resultImage = document.createElement("div", "mb-3");
resultImage.classList.add("resultImage", "img-fluid", "mb-3");
const img = document.createElement("img");
img.src = result.image;
img.alt = result.title;
resultImage.append(img);
resultFlex.append(resultImage);
return resultFlex;
};
const createResultText = (result) => {
const resultText = document.createElement("div");
resultText.classList.add("resultText");
const resultDescription = document.createElement("p");
resultDescription.classList.add("resultDescription", "mx-3");
resultDescription.textContent = result.text;
resultText.append(resultDescription);
return resultText;
};
const setStatsLine = (numberOfResults) => {
const statLine = document.getElementById("search-info");
if (numberOfResults) {
statLine.textContent = `Displaying ${numberOfResults} results.`;
} else {
statLine.textContent = "Sorry, no results.";
}
};
function clearStatsLine() {
document.getElementById("search-info").textContent = "";
}
function getSearchTerm () {
const rawSearchTerm = document.getElementById('searchBar').value.trim();
const regex = /[ ]{2,}/gi;
const searchTerm = rawSearchTerm.replaceAll(regex, ' ');
return searchTerm;
}
async function getSearchResults(searchTerm) {
const wikiSearchString = getWikiSearchString(searchTerm);
const wikiSearchResults = await requestData(wikiSearchString);
let resultArray = [];
if(wikiSearchResults.hasOwnProperty("query")) {
resultArray = processWikiResults(wikiSearchResults.query.pages);
}
return resultArray;
}
function processWikiResults(results) {
const resultArray = [];
Object.keys(results).forEach(key => {
const id = key;
const title = results[key].title;
const text = results[key].extract;
const image = results[key].hasOwnProperty('thumbnail') ? results[key].thumbnail.source : null;
const item = {
id: id,
title: title,
image: image,
text: text
};
resultArray.push(item);
});
console.log(resultArray);
return resultArray;
}
async function requestData(searchString) {
try {
const response = await fetch(searchString);
const data = await response.json();
return data;
} catch (err) {
console.error(err);
}
}
function getWikiSearchString(searchTerm) {
const maxChars = getMaxChars();
const rawSearchString = `https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=${searchTerm}&gsrlimit=20&prop=pageimages|extracts&exchars=${maxChars}&exintro&explaintext&exlimit=max&format=json&origin=*`;
const searchString = encodeURI(rawSearchString);
return searchString;
}
function getMaxChars() {
const width = window.innerWidth;
let maxChars = 200;
return maxChars;
}
// EVENT LISTENERS
form.addEventListener('submit', submitSearch);
searchBtn.addEventListener('click', submitSearch);