-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
235 lines (190 loc) · 8.39 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Utility function to calculate the current theme setting.
* Look for a local storage value.
* Fall back to system setting.
* Fall back to light mode.
*/
function calculateSettingAsThemeString({ localStorageTheme, systemSettingDark }) {
if (localStorageTheme !== null) {
return localStorageTheme;
}
if (systemSettingDark.matches) {
return "dark";
}
return "light";
}
/**
* Utility function to update the button text and aria-label.
*/
function updateButton({ buttonEl, isDark }) {
const newCta = isDark ? "header__themeToggle" : "header__themeToggle header__themeToggle--light";
// use an aria-label if you are omitting text on the button
// and using a sun/moon icon, for example
buttonEl.setAttribute("aria-label", "a");
buttonEl.setAttribute("class", newCta);
//buttonEl.innerText = newCta;
}
/**
* Utility function to update the theme setting on the html tag
*/
function updateThemeOnHtmlEl({ theme }) {
document.querySelector("html").setAttribute("data-theme", theme);
}
/**
* On page load:
*/
/**
* 1. Grab what we need from the DOM and system settings on page load
*/
const button = document.querySelector("[data-theme-toggle]");
const localStorageTheme = localStorage.getItem("theme");
const systemSettingDark = window.matchMedia("(prefers-color-scheme: dark)");
/**
* 2. Work out the current site settings
*/
let currentThemeSetting = calculateSettingAsThemeString({ localStorageTheme, systemSettingDark });
/**
* 3. Update the theme setting and button text accoridng to current settings
*/
updateButton({ buttonEl: button, isDark: currentThemeSetting === "dark" });
updateThemeOnHtmlEl({ theme: currentThemeSetting });
/**
* 4. Add an event listener to toggle the theme
*/
button.addEventListener("click", (event) => {
const newTheme = currentThemeSetting === "dark" ? "light" : "dark";
localStorage.setItem("theme", newTheme);
updateButton({ buttonEl: button, isDark: newTheme === "dark" });
updateThemeOnHtmlEl({ theme: newTheme });
currentThemeSetting = newTheme;
});
function searchWord(word = null) {
const inputElement = document.getElementById("wordInput");
const definitionDiv = document.getElementById("definition");
const previousSearchesList = document.getElementById("previousSearches");
definitionDiv.innerHTML = ""; // Clear previous result
if (word === null) {
word = inputElement.value.toLowerCase();
}
if(word === ""){
console.log("Fadlan wax raadi!");
const notFoundCardDiv = document.createElement("div");
notFoundCardDiv.classList.add("not-found-card");
notFoundCardDiv.textContent = `Ciyaarta naga daayee, fadlan wax raadi, Saaxiib!`;
definitionDiv.appendChild(notFoundCardDiv);
return;
}
else {
word = word.toLowerCase();
}
inputElement.value = ''; // Clear the input field
fetch(`https://api.erey.ga:3001/api/search/${word}`)
.then(response => response.json())
.then(data => {
let wordFound = Array.isArray(data) && data.length > 0;
updatePreviousSearches(word, wordFound);
if (wordFound) {
data.forEach(result => {
const cardDiv = document.createElement("div");
cardDiv.classList.add("card");
const dictionaryNameDiv = document.createElement("div");
dictionaryNameDiv.classList.add("dictionary-name");
dictionaryNameDiv.textContent = result.dictionary;
cardDiv.appendChild(dictionaryNameDiv);
const wordTitleDiv = document.createElement("div");
wordTitleDiv.classList.add("word-title");
wordTitleDiv.textContent = `Erayga: ${result.word}`;
cardDiv.appendChild(wordTitleDiv);
result.definitions.forEach(definition => {
const definitionTextDiv = document.createElement("div");
definitionTextDiv.classList.add("word-definition");
definitionTextDiv.textContent = definition;
cardDiv.appendChild(definitionTextDiv);
});
definitionDiv.appendChild(cardDiv);
}
);
} else {
const notFoundCardDiv = document.createElement("div");
notFoundCardDiv.classList.add("not-found-card");
notFoundCardDiv.textContent = `Erayga "${word}" lagama helin abwaannada aan hayno.`;
definitionDiv.appendChild(notFoundCardDiv);
}
})
.catch(error => {
console.error('Error searching word:', error);
updatePreviousSearches(word, false);
const errorCardDiv = document.createElement("div");
errorCardDiv.classList.add("error-card");
errorCardDiv.textContent = "Cilad ayaa dhacday marka la raadinayay erayga. Fadlan mar kale isku day.";
definitionDiv.appendChild(errorCardDiv);
});
}
function updatePreviousSearches(word, found) {
let previousSearches = JSON.parse(localStorage.getItem("previousSearches")) || [];
let searchEntry = { word: word, found: found };
// Remove the word if it already exists
previousSearches = previousSearches.filter(entry => entry.word !== word);
// Add the new entry at the beginning of the array
previousSearches.unshift(searchEntry);
// Limit the list to the last 10 searches
previousSearches = previousSearches.slice(0, 10);
localStorage.setItem("previousSearches", JSON.stringify(previousSearches));
displayPreviousSearches();
}
function listAllEntries() {
const definitionDiv = document.getElementById("definition");
definitionDiv.innerHTML = ""; // Clear previous result
fetch('https://api.erey.ga:3001/api/entries')
.then(response => response.json())
.then(entries => {
entries.forEach(entry => {
const cardDiv = document.createElement("div");
cardDiv.classList.add("card");
const dictionaryNameDiv = document.createElement("div");
dictionaryNameDiv.classList.add("dictionary-name");
dictionaryNameDiv.textContent = entry.dictionary;
cardDiv.appendChild(dictionaryNameDiv);
const wordTitleDiv = document.createElement("div");
wordTitleDiv.classList.add("word-title");
wordTitleDiv.textContent = `Word: ${entry.word}`;
cardDiv.appendChild(wordTitleDiv);
entry.definitions.forEach(definition => {
const definitionTextDiv = document.createElement("div");
definitionTextDiv.classList.add("word-definition");
definitionTextDiv.textContent = definition;
cardDiv.appendChild(definitionTextDiv);
});
definitionDiv.appendChild(cardDiv);
});
})
.catch(error => {
console.error('Error listing all entries:', error);
const errorCardDiv = document.createElement("div");
errorCardDiv.classList.add("error-card");
errorCardDiv.textContent = "Cilad ayaa dhacday marka la soo bandhigayay dhammaan gelitaannada. Fadlan mar kale isku day.";
definitionDiv.appendChild(errorCardDiv);
});
}
function displayPreviousSearches() {
const previousSearchesList = document.getElementById("previousSearches");
previousSearchesList.innerHTML = "";
let previousSearches = JSON.parse(localStorage.getItem("previousSearches")) || [];
previousSearches.forEach(search => {
const listItem = document.createElement("li");
listItem.textContent = search.word;
listItem.onclick = () => searchWord(search.word);
if (!search.found) {
listItem.style.color = 'red';
}
previousSearchesList.appendChild(listItem);
});
}
function clearPreviousSearches() {
localStorage.removeItem("previousSearches");
displayPreviousSearches();
}
// Call this function when the page loads
document.addEventListener('DOMContentLoaded', (event) => {
displayPreviousSearches();
});