-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (60 loc) · 2.55 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
const API_KEY = "47b5bc1a3de04f40b657feac3f6a043d";
const url = "https://newsapi.org/v2/everything?q=";
window.addEventListener("load", () => fetchNews("Trending"));
// window load hote hi fetcheNews() call ho jaye with value India as hume pehle India ki search karni hai
async function fetchNews(query) {
const res = await fetch(`${url}${query}&apikey=${API_KEY}`);
const data = await res.json();
// console.log(data);
bindData(data.articles);
}
// function reload{
// window.location.reload(); // js function to reload the whole page
// }
function bindData(articles) {
const cardsContainer = document.getElementById("cards-container");
const newsCardTemplate = document.getElementById("template-news-card");
cardsContainer.innerHTML = "";
articles.forEach((article) => {
if (!article.urlToImage) return;
const cardClone = newsCardTemplate.content.cloneNode(true); // iska matlab hum deep cloning karna chahte hai // div ke andar div bhi clone ho jayenge
fillDataInCard(cardClone, article);
cardsContainer.appendChild(cardClone);
});
}
function fillDataInCard(cardClone, article) {
const newsImg = cardClone.querySelector("#news-img");
const newsTitle = cardClone.querySelector("#news-title");
const newsSource = cardClone.querySelector("#news-source");
const newsDesc = cardClone.querySelector("#news-desc");
newsImg.src = article.urlToImage;
newsTitle.innerHTML = article.title;
newsDesc.innerHTML = article.description;
const date = new Date(article.publishedAt).toLocaleString("en-US", {
timeZone: "Asia/Jakarta",
});
newsSource.innerHTML = `${article.source.name}.${date}`;
cardClone.firstElementChild.addEventListener("click", () => {
window.open(article.url, "_blank");
});
}
let currentSelectedNav = null;
function onNavItemClick(id) {
fetchNews(id);
// now jispe click kara hai usko blue banana hai to use ek active class deni padegi
const navItem = document.getElementById(id);
currentSelectedNav?.classlist.remove("active");
currentSelectedNav = navItem;
currentSelectedNav.classList.add("active");
}
const searchButton = document.getElementById("search-button");
const searchText = document.getElementById("search-text");
searchButton.addEventListener("click", () => {
const query = searchText.value;
if (!query)
// agar user ne kuch nahi likha ie query nahi aaayi to us case me bas return kar dega
return;
fetchNews(query);
currentSelectedNav?.classList.remove("active");
currentSelectedNav = null;
});