-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathblog-backup.html
More file actions
449 lines (388 loc) · 18.7 KB
/
Copy pathblog-backup.html
File metadata and controls
449 lines (388 loc) · 18.7 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AgriTech Blog</title>
<link rel="stylesheet" href="blog.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
<section class="agritech-blog">
<div class="blog-container">
<!-- Blog Header with Favorites -->
<div class="blog-header">
<div class="header-actions">
<button class="theme-toggle" id="themeToggle">
<span id="themeIcon">🌙</span>
<span id="themeText">Dark Mode</span>
</button>
<a href="favorites.html" class="favorites-link">
<i class="fas fa-heart"></i>
<span>My Favorites</span>
<span class="favorite-count">0</span>
</a>
</div>
<h1 class="blog-title">🌱 AgriTech Blog — Farming Tips & Insights</h1>
<p class="blog-subtitle">Stay updated with the latest in agriculture, technology, and sustainability.</p>
</div>
<!-- Search and Filter Section -->
<div class="search-filter-section">
<input type="text" class="search-bar" placeholder="Search blog posts..." id="searchInput">
<div class="category-filters">
<button class="filter-btn active" data-category="all">All Posts</button>
<button class="filter-btn" data-category="farming-tips">Farming Tips</button>
<button class="filter-btn" data-category="technology">Technology</button>
<button class="filter-btn" data-category="market-trends">Market Trends</button>
<button class="filter-btn" data-category="sustainability">Sustainability</button>
<button class="filter-btn" data-category="business">Business</button>
</div>
</div>
<!-- Blog Grid -->
<div class="blog-grid" id="blogGrid">
<!-- Blog cards will be populated by JavaScript -->
</div>
<!-- Load More Button -->
<div class="load-more-container">
<button class="load-more-btn" id="loadMoreBtn">Load More Posts</button>
</div>
</div>
</section>
<!-- Modal for Blog Post with Favorite Button -->
<div id="blogModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<button
class="modal-favorite-btn"
id="modalFavoriteBtn"
aria-label="Add blog to favorites"
aria-pressed="false"
>
<i class="far fa-heart" aria-hidden="true"></i>
</button>
<h2 class="modal-title" id="modalTitle"></h2>
<span class="modal-category" id="modalCategory"></span>
</div>
<div class="modal-body">
<img class="modal-image" id="modalImage" alt="Blog post image">
<div class="modal-text" id="modalContent"></div>
</div>
</div>
</div>
<!-- JavaScript Files -->
<script src="js/features/favorites.js"></script>
<script>
// Simple Favorites Manager - Include it directly to ensure it loads
if (!window.favoritesManager) {
class FavoritesManager {
constructor() {
this.storageKey = 'agritech_favorite_blogs';
this.favorites = JSON.parse(localStorage.getItem(this.storageKey)) || [];
window.favoritesManager = this;
// console.log('✅ FavoritesManager initialized');
}
isFavorite(blogId) {
return this.favorites.some(blog => blog.id === blogId);
}
addToFavorites(blogData) {
if (this.isFavorite(blogData.id)) return false;
blogData.addedAt = new Date().toISOString();
this.favorites.push(blogData);
localStorage.setItem(this.storageKey, JSON.stringify(this.favorites));
// Show notification
const notification = document.createElement('div');
notification.textContent = `Added to favorites: ${blogData.title}`;
notification.style.cssText = 'position:fixed;top:20px;right:20px;background:#2c5f2d;color:white;padding:12px 20px;border-radius:8px;z-index:10000;';
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 3000);
// Dispatch event
const event = new CustomEvent('favoriteToggle', {
detail: { blogId: blogData.id, isFavorite: true, blogData }
});
document.dispatchEvent(event);
return true;
}
removeFromFavorites(blogId) {
this.favorites = this.favorites.filter(blog => blog.id !== blogId);
localStorage.setItem(this.storageKey, JSON.stringify(this.favorites));
// Dispatch event
const event = new CustomEvent('favoriteToggle', {
detail: { blogId, isFavorite: false }
});
document.dispatchEvent(event);
return true;
}
getFavorites() {
return this.favorites;
}
}
// Initialize immediately
new FavoritesManager();
}
// Sample blog data
const blogPosts = [
{
id: 'sustainable-farming-2025',
title: "Sustainable Farming Practices for 2025",
category: "sustainability",
description: "Discover innovative sustainable farming techniques that are revolutionizing agriculture and helping farmers increase yield while protecting the environment.",
image: "https://images.unsplash.com/photo-1500382017468-9049fed747ef?w=400&h=250&fit=crop",
content: `<p>Sustainable farming practices for modern agriculture.</p>`,
author: "Dr. Sarah Green",
date: "2025-01-10"
},
{
id: 'ai-agriculture-revolution',
title: "AI in Agriculture: The Next Revolution",
category: "technology",
description: "How artificial intelligence is transforming farming operations, from predictive analytics to automated machinery.",
image: "https://images.unsplash.com/photo-1555255707-c07966088b7b?w=400&h=250&fit=crop",
content: `<p>AI applications in modern farming.</p>`,
author: "Tech Farm Review",
date: "2025-01-08"
},
{
id: 'organic-pest-control',
title: "Organic Pest Control Methods",
category: "sustainability",
description: "Natural and effective ways to protect your crops from pests without using harmful pesticides and chemicals.",
image: "https://images.unsplash.com/photo-1574323347407-f5e1ad6d020b?w=400&h=250&fit=crop",
content: `<p>Organic pest control methods.</p>`,
author: "Organic Farming Association",
date: "2025-01-05"
}
];
// Global variables
let currentPage = 0;
const postsPerPage = 6;
let filteredPosts = [...blogPosts];
let currentCategory = 'all';
let searchQuery = '';
let currentModalPostId = null;
// Initialize the blog
document.addEventListener('DOMContentLoaded', function() {
// console.log('📄 Blog initialized');
displayPosts();
setupEventListeners();
updateFavoriteCounter();
});
// Setup event listeners
function setupEventListeners() {
// Search functionality
document.getElementById('searchInput').addEventListener('input', function() {
searchQuery = this.value.toLowerCase();
filterPosts();
});
// Filter buttons
document.querySelectorAll('.filter-btn').forEach(button => {
button.addEventListener('click', function() {
document.querySelectorAll('.filter-btn').forEach(btn => btn.classList.remove('active'));
this.classList.add('active');
currentCategory = this.dataset.category;
filterPosts();
});
});
// Load more button
document.getElementById('loadMoreBtn').addEventListener('click', loadMorePosts);
// Modal functionality
document.querySelector('.close').addEventListener('click', closeModalHandler);
window.addEventListener('click', function(event) {
if (event.target === document.getElementById('blogModal')) {
closeModalHandler();
}
});
// Modal favorite button
document.getElementById('modalFavoriteBtn').addEventListener('click', toggleModalFavorite);
// Event delegation for favorite buttons
document.addEventListener('click', function(e) {
if (e.target.closest('.favorite-btn')) {
e.preventDefault();
e.stopPropagation();
const button = e.target.closest('.favorite-btn');
const blogId = button.getAttribute('data-blog-id');
// console.log('❤️ Favorite button clicked:', blogId);
toggleFavorite(blogId);
}
});
}
// Update favorite counter
function updateFavoriteCounter() {
if (window.favoritesManager) {
const favorites = window.favoritesManager.getFavorites();
document.querySelectorAll('.favorite-count').forEach(element => {
element.textContent = favorites.length;
});
}
}
// Filter posts
function filterPosts() {
filteredPosts = blogPosts.filter(post => {
const matchesSearch = post.title.toLowerCase().includes(searchQuery) ||
post.description.toLowerCase().includes(searchQuery);
const matchesCategory = currentCategory === 'all' || post.category === currentCategory;
return matchesSearch && matchesCategory;
});
currentPage = 0;
document.getElementById('blogGrid').innerHTML = '';
displayPosts();
}
// Display posts with favorite buttons
function displayPosts() {
const blogGrid = document.getElementById('blogGrid');
const startIndex = currentPage * postsPerPage;
const endIndex = startIndex + postsPerPage;
const postsToShow = filteredPosts.slice(startIndex, endIndex);
if (postsToShow.length === 0 && startIndex === 0) {
blogGrid.innerHTML = '<div style="grid-column: 1/-1; text-align: center; padding: 3rem; color: #666;">No posts found.</div>';
document.getElementById('loadMoreBtn').style.display = 'none';
return;
}
postsToShow.forEach(post => {
const isFavorite = window.favoritesManager ? window.favoritesManager.isFavorite(post.id) : false;
const favoriteIcon = isFavorite ? 'fas fa-heart' : 'far fa-heart';
const favoriteClass = isFavorite ? 'favorite-btn active' : 'favorite-btn';
const postElement = document.createElement('div');
postElement.className = 'blog-card';
postElement.innerHTML = `
<img src="${post.image}" alt="${post.title}">
<button
class="${favoriteClass}"
data-blog-id="${post.id}"
aria-label="${isFavorite ? 'Remove blog from favorites' : 'Add blog to favorites'}"
aria-pressed="${isFavorite}"
>
<i class="${favoriteIcon}" aria-hidden="true"></i>
</button>
<div class="card-content">
<span class="card-category">${post.category.replace('-', ' ')}</span>
<h3 class="card-title">${post.title}</h3>
<p class="card-description">${post.description}</p>
<div class="card-meta">
<span class="card-author">By ${post.author}</span>
<span class="card-date">${post.date}</span>
</div>
<button class="read-more-btn"style="margin-top:16px" onclick="openModal('${post.id}')">Read More</button>
</div>
`;
blogGrid.appendChild(postElement);
});
document.getElementById('loadMoreBtn').style.display =
endIndex >= filteredPosts.length ? 'none' : 'inline-block';
}
// Toggle favorite
function toggleFavorite(blogId) {
// console.log('🔄 Toggling favorite for:', blogId);
if (!window.favoritesManager) {
alert('❌ Favorites feature not loaded');
return;
}
const post = blogPosts.find(p => p.id === blogId);
if (!post) {
console.error('❌ Post not found:', blogId);
return;
}
const isFavorite = window.favoritesManager.isFavorite(blogId);
if (isFavorite) {
window.favoritesManager.removeFromFavorites(blogId);
} else {
window.favoritesManager.addToFavorites({
id: blogId,
title: post.title,
description: post.description,
category: post.category,
image: post.image,
author: post.author,
date: post.date,
content: post.content
});
}
updateFavoriteButtons(blogId);
updateFavoriteCounter();
}
// Update favorite buttons
function updateFavoriteButtons(blogId) {
const buttons = document.querySelectorAll(`.favorite-btn[data-blog-id="${blogId}"]`);
const isFavorite = window.favoritesManager.isFavorite(blogId);
buttons.forEach(button => {
const icon = button.querySelector('i');
button.classList.toggle('active', isFavorite);
icon.className = isFavorite ? 'fas fa-heart' : 'far fa-heart';
button.setAttribute(
'aria-label',
isFavorite ? 'Remove blog from favorites' : 'Add blog to favorites'
);
button.setAttribute('aria-pressed', isFavorite);
});
if (currentModalPostId === blogId) {
updateModalFavoriteButton();
}
}
// Load more posts
function loadMorePosts() {
currentPage++;
displayPosts();
}
// Open modal
function openModal(postId) {
const post = blogPosts.find(p => p.id === postId);
if (!post) return;
currentModalPostId = postId;
document.getElementById('modalTitle').textContent = post.title;
document.getElementById('modalCategory').textContent = post.category.replace('-', ' ');
document.getElementById('modalImage').src = post.image;
document.getElementById('modalContent').innerHTML = post.content;
updateModalFavoriteButton();
document.getElementById('blogModal').style.display = 'block';
document.body.style.overflow = 'hidden';
}
// Close modal
function closeModalHandler() {
document.getElementById('blogModal').style.display = 'none';
document.body.style.overflow = 'auto';
currentModalPostId = null;
}
// Update modal favorite button
function updateModalFavoriteButton() {
if (!window.favoritesManager || !currentModalPostId) return;
const isFavorite = window.favoritesManager.isFavorite(currentModalPostId);
const button = document.getElementById('modalFavoriteBtn');
const icon = button.querySelector('i');
button.classList.toggle('active', isFavorite);
icon.className = isFavorite ? 'fas fa-heart' : 'far fa-heart';
button.setAttribute(
'aria-label',
isFavorite ? 'Remove blog from favorites' : 'Add blog to favorites'
);
button.setAttribute('aria-pressed', isFavorite);
}
// Toggle favorite from modal
function toggleModalFavorite() {
if (currentModalPostId) {
toggleFavorite(currentModalPostId);
}
}
// Theme toggle
document.getElementById('themeToggle').addEventListener('click', function() {
const isDark = document.documentElement.hasAttribute('data-theme');
const icon = document.getElementById('themeIcon');
const text = document.getElementById('themeText');
if (isDark) {
document.documentElement.removeAttribute('data-theme');
icon.textContent = '🌙';
text.textContent = 'Dark Mode';
} else {
document.documentElement.setAttribute('data-theme', 'dark');
icon.textContent = '☀️';
text.textContent = 'Light Mode';
}
});
// Listen for favorite changes
document.addEventListener('favoriteToggle', function(event) {
// console.log('📢 Favorite event:', event.detail);
updateFavoriteButtons(event.detail.blogId);
updateFavoriteCounter();
});
</script>
</body>
</html>