-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.js
414 lines (364 loc) · 11.3 KB
/
entry.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
const subjects = document.querySelector(".subjects");
const btnCloseModal = document.querySelector(".close-modal");
const btnShowSubjects = document.querySelector(".browse");
const browseArrow = document.querySelector(".browseArrow");
const main = document.querySelector("main");
const control = document.querySelectorAll(".control");
const btnRegister = document.querySelector(".btnRegister");
const btnLogin = document.querySelector(".btnLogin");
const linkAnchors = document.querySelectorAll(".info>ul>li>a");
const subjectsWrapper = document.querySelector(".subjectsWrapper");
const subjectLinks = document.querySelectorAll(".subjects-items>a");
const searchIcon = document.querySelector(".searchIcon");
const searchQuery = document.getElementById("q");
const newEntryArticle = document.getElementById("newEntry-article--container");
const subjectLists = [
"Africa history",
"America history",
"Anthropology",
"Asian history",
"Business and Management",
"Classical Studies",
"Climate Science",
"Communication",
"Criminology and Criminal Justice",
"Economics and Finance",
"Education",
"Environmental Science",
"Computer Science",
"Food Studies",
"Global Public Health",
"International Studies",
"Latin America History",
"Linguistics",
"Literature",
"Natural Hazard Science",
"Neuroscience",
"Physics",
"Planetry Science",
"Politics",
"Psychology",
"Religion",
"Social Work",
].sort((a, b) => b.localeCompare(a));
const displaySubjectList = function (lists) {
subjectsWrapper.innerHTML = "";
lists.forEach(function (list, i) {
const html = `<li class="subjects-items control" ">
<div class="sideStyle control"></div>
<a>${list}</a>
</li>`;
subjectsWrapper.insertAdjacentHTML("afterbegin", html);
});
const subjectItems = document.querySelectorAll(".subjectsWrapper>li");
const sideStyle = document.querySelectorAll(".subjectsWrapper>li>div");
const subjectAnchor = document.querySelectorAll(".subjectsWrapper>li>a");
for (let i = 0; i < subjectItems.length; i++) {
subjectItems[i].addEventListener(
"mouseover",
(e) => {
e.preventDefault();
sideStyle[i].style.display = "block";
},
true
);
subjectItems[i].addEventListener(
"mouseout",
(e) => {
e.preventDefault();
sideStyle[i].style.display = "none";
},
true
);
subjectItems[i].addEventListener("click", (e) => {
e.preventDefault();
const subjectTitle = subjectAnchor[i].textContent;
const subjectUrl = `https://www.google.com/search?q=${encodeURIComponent(
subjectTitle
)}`;
window.open(subjectUrl, "_blank");
});
}
};
const openModal = function () {
subjects.classList.remove("hidden");
browseArrow.classList.remove("hidden");
};
const closeModal = function () {
subjects.classList.add("hidden");
browseArrow.classList.add("hidden");
};
btnCloseModal.addEventListener("click", closeModal);
btnShowSubjects.addEventListener("click", (e) => {
e.preventDefault();
displaySubjectList(subjectLists);
let modalState = browseArrow.classList.contains("hidden");
if (modalState) {
openModal();
} else {
closeModal();
}
});
main.addEventListener(
"click",
(event) => {
event.preventDefault();
let modalState = false;
for (let i = 0; i < control.length; i++) {
if (
event.target === control[i] ||
event.target.offsetParent === control[i].offsetParent
) {
modalState = true;
break;
} else {
modalState = false;
}
}
if (modalState) {
return;
} else {
closeModal();
}
},
true
);
btnLogin.addEventListener("click", (e) => {
e.preventDefault();
window.location.href = "/login.html";
});
btnRegister.addEventListener("click", (e) => {
e.preventDefault();
window.location.href = "/register.html";
});
for (let i = 0; i < linkAnchors.length; i++) {
linkAnchors[i].addEventListener("click", (e) => {
e.preventDefault();
const aLink = e.target.innerText.toLowerCase();
switch (aLink) {
case "home":
window.location.href = "/index.html";
break;
case "about":
window.location.href = "/about.html";
break;
case "contact":
window.location.href = "/contact.html";
break;
case "new":
window.location.href = "/new.html";
break;
case "personal profile":
window.location.href = "/profile.html";
break;
case "edit":
window.location.href = "/edit.html";
break;
case "search":
searchQuery.focus();
break;
default:
window.location.href = "/";
}
});
}
searchIcon.addEventListener("click", (e) => {
e.preventDefault();
const searchParam = searchQuery.value;
if (!searchParam) return;
const apiUrl = `https://www.google.com/search?q=${searchParam}`;
window.open(apiUrl, "_blank");
});
searchQuery.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
event.preventDefault();
const searchTerm = searchQuery.value;
if (!searchTerm) return;
const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(
searchTerm
)}`;
window.open(searchUrl, "_blank");
}
});
const companies = [
"Apple",
"Microsoft",
"Amazon",
"Alphabet",
"Facebook",
"Alibaba",
"Tencent",
"Tesla",
"Johnson & Johnson",
"JPMorgan Chase",
"Berkshire Hathaway",
"Visa",
"Walmart",
"Procter & Gamble",
"Samsung",
"Toyota",
"Coca-Cola",
"Pfizer",
"Nestlé",
"Exxon Mobil",
"Walt Disney",
"AT&T",
"Netflix",
"Adobe",
"General Electric",
"Cisco",
"Mastercard",
"McDonald's",
"IBM",
"PepsiCo",
];
let randNewsList = [];
const randomCompany = function () {
const randomIndex = Math.floor(Math.random() * companies.length);
return companies[randomIndex];
};
function formatDate(dateString) {
const date = new Date(dateString);
const options = { year: "numeric", month: "long", day: "numeric" };
return date.toLocaleDateString("en-US", options).toUpperCase();
}
const fetchData = async function (company) {
try {
const today = new Date();
let yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
const monthBefore = yesterday.getMonth() + 1;
const yearBefore = yesterday.getFullYear();
const dayBefore = yesterday.getDate();
const dayOfWeekIndexBefore = yesterday.getDay();
const apiKey = "35ad6576bbf34ae79e79ebcd832c710a";
const url = `https://newsapi.org/v2/everything?q=${company}&from=${yearBefore}-${monthBefore}-${dayBefore}&to=${yearBefore}-${monthBefore}-${Math.abs(
dayBefore - dayOfWeekIndexBefore
)}&sortBy=popularity&apiKey=${apiKey}`;
console.log(
`${company}&from=${yearBefore}-${monthBefore}-${dayBefore}&to=${yearBefore}-${monthBefore}-${Math.abs(
dayBefore - dayOfWeekIndexBefore
)}`
);
const req = new Request(url);
const response = await fetch(req);
if (!response.ok) {
throw new Error("Unable to fetch the data");
}
const data = await response.json();
data.articles.company = company;
return data.articles;
} catch (error) {
console.error("Oops! problem occurs while fetching the data: ", error);
}
};
const displayNews = function (list) {
const newsArticleWrapper = document.getElementById("news-article--list");
newsArticleWrapper.innerHTML = "";
if (list.length === 0) {
const defaultHTML = `<pre>
<strong>This message only display with two major possibility:</strong>
<h2>The Problems:</h2>
<strong>•</strong> <code>No internet connection.</code>
<strong>•</strong> <code>The Api key duration had expired.</code>
<h2>The Solutions:</h2>
<strong>• </strong> <code>Connected to the internet</code>
<strong>•</strong> <code>The api key was registered for development purposes:
1) Wait for 24hours for it be renew if you have not reach the monthly lifespan.
2) Or visit this site <a class="newsapi" target="_blank" href="https://newsapi.org">newsapi.org</a> to register for new api key.
3) With the new api key obtain change the old api key in the url params found in the entry.js
file if you happen to have access to the source code.</code>
</pre>`;
newsArticleWrapper.insertAdjacentHTML("afterbegin", defaultHTML);
document.querySelector(".newsapi").addEventListener("click", (e) => {
e.preventDefault();
window.open("https://newsapi.org", "_blank");
});
}
if (!list) {
location.reload();
}
list.forEach((list, i) => {
const date = formatDate(list.publishedAt);
const html = `<li>
<p>${date}</p>
<h4>${list.title}</h4>
<p>${list.description}</p>
<a class="articleLink" target="_blank" href=${
list.url
}>click for detail news</a>
<p>Author:${list.author ? list.author : "Unknown"}</p>
</li>`;
newsArticleWrapper.insertAdjacentHTML("afterbegin", html);
document.querySelector(".articleLink").addEventListener("click", (e) => {
e.preventDefault();
window.open(`${list.url}`, "_blank");
});
});
};
for (let i = 0; i < 5; i++) {
fetchData(randomCompany()).then((data) => {
if (data) {
const randNews = data[Math.floor(Math.random() * data.length)];
randNewsList.push(randNews);
}
});
}
(function loadRandomNews() {
const newsArticleWrapper = document.getElementById("news-article--list");
newsArticleWrapper.innerHTML = "";
newsArticleWrapper.insertAdjacentHTML(
"afterbegin",
`<div class="loader"></div>`
);
setTimeout(() => {
displayNews(randNewsList);
}, 5000);
})();
document.addEventListener("DOMContentLoaded", (e) => {
e.preventDefault();
const localStorageData = localStorage.getItem("newEntry");
if (localStorageData) {
const jsonData = JSON.parse(localStorageData);
const jsonDataAuthor = jsonData.author;
const jsonDataTitle = jsonData.title;
const jsonHtmlDataBody = jsonData.htmlBody;
const newEntryHTML = `
<p class="entry-title"><strong>Title</strong>:<strong><em> ${jsonDataTitle}</em></strong></p>
${jsonHtmlDataBody}
<small><strong>Author: </strong>${jsonDataAuthor}</small>
<button class="edit-article"><code>Edit article</code></button>
`;
newEntryArticle.insertAdjacentHTML("afterbegin", newEntryHTML);
document.querySelector(".edit-article").addEventListener("click", (e) => {
e.preventDefault();
location.href = "/edit.html";
});
} else {
const newEntryHTMLDefault = `
<pre>
<p>You have no article or book added!</p>
<button class="btnEntry">Add New Entry</button>
</pre>
`;
newEntryArticle.insertAdjacentHTML("afterbegin", newEntryHTMLDefault);
document.querySelector(".btnEntry").addEventListener("click", (e) => {
e.preventDefault();
location.href = "/new.html";
});
}
});
const featureLinks = document.querySelectorAll("#featuredBody>h3>a");
featureLinks.forEach((link, i) => {
link.addEventListener("click", (e) => {
e.preventDefault();
window.open(e.target.href, "_blank");
});
});
const featureLinksImg = document.querySelectorAll(".featured-img--wrapper>a");
featureLinksImg.forEach((link, i) => {
link.addEventListener("click", (e) => {
e.preventDefault();
window.open(`${e.target.parentElement.href}`, "_blank");
});
});