-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
275 lines (239 loc) · 9.27 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
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
// API configuration
const API_KEY = 'd0ae3a6558740d2b980a7db223676e16';
const API_URL = 'http://api.mediastack.com/v1/news';
// Global variables
let currentPage = 1;
let totalPages = 1;
let currentCategory = '';
let currentLanguage = 'en';
const newsContainer = document.getElementById('news-container');
const searchForm = document.getElementById('search-form');
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
const categorySelect = document.getElementById('category-select');
const languageSelect = document.getElementById('language-select');
const loading = document.getElementById('loading');
const prevButton = document.getElementById('prev-button');
const nextButton = document.getElementById('next-button');
const pageInfo = document.getElementById('page-info');
const modal = document.getElementById('modal');
const closeBtn = document.getElementsByClassName('close')[0];
const likeButton = document.getElementById('like-button');
const shareButton = document.getElementById('share-button');
const commentForm = document.getElementById('comment-form');
const commentInput = document.getElementById('comment-input');
const commentsList = document.getElementById('comments-list');
const contactForm = document.getElementById('contact-form');
// Event listeners
searchButton.addEventListener('click', (e) => {
e.preventDefault();
currentPage = 1;
fetchNews();
});
categorySelect.addEventListener('change', () => {
currentCategory = categorySelect.value;
currentPage = 1;
fetchNews();
});
languageSelect.addEventListener('change', () => {
currentLanguage = languageSelect.value;
currentPage = 1;
fetchNews();
});
prevButton.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
fetchNews();
}
});
nextButton.addEventListener('click', () => {
if (currentPage < totalPages) {
currentPage++;
fetchNews();
}
});
closeBtn.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
likeButton.addEventListener('click', () => {
if (currentArticle) {
const likedArticles = JSON.parse(localStorage.getItem('likedArticles') || '{}');
if (likedArticles[currentArticle.title]) {
delete likedArticles[currentArticle.title];
likeButton.classList.remove('liked');
} else {
likedArticles[currentArticle.title] = true;
likeButton.classList.add('liked');
}
localStorage.setItem('likedArticles', JSON.stringify(likedArticles));
}
});
shareButton.addEventListener('click', () => {
if (currentArticle) {
const shareText = `Check out this article: ${currentArticle.title} - ${currentArticle.url}`;
if (navigator.share) {
navigator.share({
title: currentArticle.title,
text: shareText,
url: currentArticle.url,
}).then(() => {
console.log('Successful share');
}).catch((error) => {
console.log('Error sharing:', error);
fallbackShare();
});
} else {
fallbackShare();
}
}
});
function fallbackShare() {
const shareText = `Check out this article: ${currentArticle.title} - ${currentArticle.url}`;
const tempInput = document.createElement('input');
document.body.appendChild(tempInput);
tempInput.value = shareText;
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
alert('Link copied to clipboard!');
}
commentForm.addEventListener('submit', (e) => {
e.preventDefault();
const commentText = commentInput.value.trim();
if (commentText && currentArticle) {
addComment(commentText);
commentInput.value = '';
}
});
contactForm.addEventListener('submit', (e) => {
e.preventDefault();
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const message = document.getElementById('message').value;
// Store the message in local storage
const messages = JSON.parse(localStorage.getItem('contactMessages') || '[]');
messages.push({ name, email, message, date: new Date().toISOString() });
localStorage.setItem('contactMessages', JSON.stringify(messages));
alert('Thank you for your message! We will get back to you soon.');
contactForm.reset();
});
async function fetchNews() {
loading.style.display = 'block';
newsContainer.innerHTML = '';
const searchTerm = searchInput.value;
try {
const url = new URL(API_URL);
url.searchParams.append('access_key', API_KEY);
url.searchParams.append('keywords', searchTerm);
url.searchParams.append('categories', currentCategory);
url.searchParams.append('languages', currentLanguage);
url.searchParams.append('offset', (currentPage - 1) * 20);
url.searchParams.append('limit', 20);
const response = await fetch(url);
const data = await response.json();
if (data.error) {
throw new Error(data.error.message);
}
totalPages = Math.ceil(data.pagination.total / data.pagination.limit);
displayNews(data.data);
updatePaginationInfo();
} catch (error) {
newsContainer.innerHTML = `<p>Error: ${error.message}</p>`;
} finally {
loading.style.display = 'none';
}
}
function displayNews(articles) {
articles.forEach((article, index) => {
const articleElement = createArticleElement(article, index);
newsContainer.appendChild(articleElement);
});
}
function createArticleElement(article, index) {
const articleElement = document.createElement('div');
articleElement.classList.add('article-card', 'fade-in');
articleElement.style.animationDelay = `${index * 0.1}s`;
articleElement.innerHTML = `
<img src="${article.image || '/api/placeholder/400/200'}" alt="${article.title}" class="article-image">
<div class="article-content">
<h2 class="article-title">${article.title}</h2>
<p class="article-description">${article.description || 'No description available'}</p>
<div class="article-meta">
<span>${article.source}</span>
<span>${new Date(article.published_at).toLocaleDateString()}</span>
</div>
</div>
`;
articleElement.addEventListener('click', () => openModal(article));
return articleElement;
}
function updatePaginationInfo() {
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === totalPages;
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
}
function openModal(article) {
currentArticle = article;
document.getElementById('modal-title').textContent = article.title;
document.getElementById('modal-date').textContent = new Date(article.published_at).toLocaleString();
document.getElementById('modal-description').textContent = article.description || 'No description available';
document.getElementById('modal-content').innerHTML = article.content || 'No content available';
document.getElementById('modal-source').textContent = `Source: ${article.source}`;
// Check if the article is liked
const likedArticles = JSON.parse(localStorage.getItem('likedArticles') || '{}');
if (likedArticles[article.title]) {
likeButton.classList.add('liked');
} else {
likeButton.classList.remove('liked');
}
loadComments();
modal.style.display = "block";
}
function addComment(text) {
if (currentArticle) {
const comments = JSON.parse(localStorage.getItem(`comments_${currentArticle.title}`) || '[]');
comments.push({ text, date: new Date().toISOString() });
localStorage.setItem(`comments_${currentArticle.title}`, JSON.stringify(comments));
loadComments();
}
}
function loadComments() {
commentsList.innerHTML = '';
if (currentArticle) {
const comments = JSON.parse(localStorage.getItem(`comments_${currentArticle.title}`) || '[]');
comments.forEach(comment => {
const li = document.createElement('li');
li.textContent = `${comment.text} - ${new Date(comment.date).toLocaleString()}`;
commentsList.appendChild(li);
});
}
}
// Add smooth scrolling for navigation links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
// Animate elements on scroll
function animateOnScroll() {
const elements = document.querySelectorAll('.fade-in, .slide-in-left');
elements.forEach(element => {
const elementTop = element.getBoundingClientRect().top;
const elementBottom = element.getBoundingClientRect().bottom;
if (elementTop < window.innerHeight && elementBottom > 0) {
element.classList.add('show');
}
});
}
window.addEventListener('scroll', animateOnScroll);
animateOnScroll(); // Initial check on page load
// Initial news fetch
fetchNews();