-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
580 lines (512 loc) · 19.9 KB
/
Copy pathscript.js
File metadata and controls
580 lines (512 loc) · 19.9 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
const GEMINI_API_KEY = 'AIzaSyBBFw76JNYRSIqCeWm7Ho9RSLGxu3MFB1M';
const UNSPLASH_API_KEY = 'j9MJBf7t2eWfCg647RDX7ln3DMPODRrCPkxEg21qLu8';
const OPENWEATHER_API_KEY = '230942273341be46649f863c933efcb6';
/*******************************
DEFAULT HOBBIES
*******************************/
const defaultHobbies = [
{
name: 'Photography',
category: 'creative',
description: 'Capture moments through your lens.',
difficulty: 'Medium',
wiki: 'https://en.wikipedia.org/wiki/Photography',
equipment: 'Camera, lens, tripod',
tutorials: 'Online photography courses',
classes: 'Local photography workshops'
},
{
name: 'Hiking',
category: 'physical',
description: 'Explore scenic trails and nature paths.',
difficulty: 'Easy',
wiki: 'https://en.wikipedia.org/wiki/Hiking',
equipment: 'Hiking boots, water, map',
tutorials: 'Hiking tips online',
classes: 'Local hiking clubs'
},
{
name: 'Chess',
category: 'intellectual',
description: 'Challenge your mind with strategic gameplay.',
difficulty: 'Medium',
wiki: 'https://en.wikipedia.org/wiki/Chess',
equipment: 'Chess board, pieces',
tutorials: 'Online chess lessons',
classes: 'Local chess clubs'
},
{
name: 'Cooking',
category: 'creative',
description: 'Experiment with recipes and flavors.',
difficulty: 'Easy',
wiki: 'https://en.wikipedia.org/wiki/Cooking',
equipment: 'Basic kitchen utensils',
tutorials: 'Cooking tutorials online',
classes: 'Local cooking classes'
},
{
name: 'Dancing',
category: 'social',
description: 'Express yourself through movement and rhythm.',
difficulty: 'Medium',
wiki: 'https://en.wikipedia.org/wiki/Dance',
equipment: 'Comfortable clothes, shoes',
tutorials: 'Online dance lessons',
classes: 'Local dance studios'
},
{
name: 'Yoga',
category: 'physical',
description: 'Enhance your flexibility and mindfulness.',
difficulty: 'Easy',
wiki: 'https://en.wikipedia.org/wiki/Yoga',
equipment: 'Yoga mat, comfortable clothes',
tutorials: 'Online yoga classes',
classes: 'Local yoga centers'
},
{
name: 'Painting',
category: 'creative',
description: 'Bring colors to life on canvas.',
difficulty: 'Medium',
wiki: 'https://en.wikipedia.org/wiki/Painting',
equipment: 'Paints, brushes, canvas',
tutorials: 'Online painting tutorials',
classes: 'Local art classes'
}
];
// Local storage functions storage functions
function saveHobbiesToStorage(hobbies) {
localStorage.setItem('savedHobbies', JSON.stringify(hobbies));
}
function getHobbiesFromStorage() {
const saved = localStorage.getItem('savedHobbies');
return saved ? JSON.parse(saved) : defaultHobbies;
}
/*******************************
DOM ELEMENTS
*******************************/
const elements = {
themeButton: document.getElementById('themeButton'),
findHobbiesBtn: document.getElementById('findHobbies'),
hobbyGrid: document.getElementById('hobbyGrid'),
loadingOverlay: document.getElementById('loadingOverlay'),
modal: document.getElementById('hobbyModal'),
currentLocation: document.getElementById('currentLocation'),
weatherWidget: document.getElementById('weatherWidget'),
weatherSuggestions: document.getElementById('weatherSuggestions'),
refreshWeather: document.getElementById('refreshWeather'),
refreshLocation: document.getElementById('refreshLocation'),
};
/*******************************
THEME MANAGEMENT
*******************************/
function initTheme() {
const savedTheme = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', savedTheme);
updateThemeIcon(savedTheme);
}
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
updateThemeIcon(newTheme);
}
function updateThemeIcon(theme) {
const icon = elements.themeButton.querySelector('i');
icon.className = theme === 'dark' ? 'fas fa-sun' : 'fas fa-moon';
}
/*******************************
WEATHER & LOCATION SERVICES
*******************************/
async function getUserLocation() {
try {
const position = await new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
return {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
} catch (error) {
console.error('Location error:', error);
return null;
}
}
async function updateLocation() {
elements.currentLocation.textContent = 'Updating location...';
const location = await getUserLocation();
if (location) {
const locationName = await reverseGeocode(location.latitude, location.longitude);
elements.currentLocation.textContent = locationName;
updateWeather(location);
updateMap(location);
} else {
elements.currentLocation.textContent = 'Location unavailable';
}
}
async function reverseGeocode(lat, lon) {
try {
const response = await fetch(
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`
);
const data = await response.json();
return data.address.city || data.address.town || 'Unknown location';
} catch (error) {
console.error('Geocoding error:', error);
return 'Unknown location';
}
}
async function updateWeather(location) {
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longitude}&appid=${OPENWEATHER_API_KEY}&units=metric`
);
const data = await response.json();
displayWeather(data);
updateWeatherSuggestions(data);
} catch (error) {
console.error('Weather error:', error);
elements.weatherWidget.innerHTML = '<p>Weather data unavailable</p>';
}
}
function displayWeather(data) {
const { temp } = data.main;
const { description, icon } = data.weather[0];
elements.weatherWidget.innerHTML = `
<div class="weather-display">
<img src="https://openweathermap.org/img/wn/${icon}@2x.png" alt="${description}">
<div class="weather-info">
<h3>${Math.round(temp)}°C</h3>
<p>${description.charAt(0).toUpperCase() + description.slice(1)}</p>
</div>
</div>
`;
}
function updateWeatherSuggestions(weatherData) {
const condition = weatherData.weather[0].main.toLowerCase();
const temp = weatherData.main.temp;
let suggestions = [];
// Weather-based hobby suggestions
// this are simply suggestions based on temperature range
// thsi does not take in api data
if (temp > 20 && ['clear', 'clouds'].includes(condition)) {
suggestions.push(
{ name: 'Outdoor Photography', icon: 'camera' },
{ name: 'Hiking', icon: 'hiking' },
{ name: 'Garden', icon: 'leaf' }
);
} else if (condition === 'rain' || condition === 'snow') {
suggestions.push(
{ name: 'Indoor Painting', icon: 'paint-brush' },
{ name: 'Reading', icon: 'book' },
{ name: 'Cooking', icon: 'utensils' }
);
} else {
suggestions.push(
{ name: 'Mixed Activities', icon: 'random' },
{ name: 'Flexible Hobbies', icon: 'puzzle-piece' }
);
}
elements.weatherSuggestions.innerHTML = suggestions.map(suggestion => `
<div class="suggestion-item">
<i class="fas fa-${suggestion.icon}"></i>
<span>${suggestion.name}</span>
</div>
`).join('');
}
/*******************************
POPULAR HOBBIES
******************************/
elements.popularHobbiesToggle = document.getElementById('popularHobbiesToggle');
elements.popularHobbiesContent = document.getElementById('popularHobbiesContent');
// Add this function to display popular hobbies
function displayPopularHobbies() {
const popularHobbiesGrid = document.querySelector('.popular-hobbies-grid');
popularHobbiesGrid.innerHTML = defaultHobbies.map(hobby => `
<div class="popular-hobby-card" data-category="${hobby.category}">
<h3>${hobby.name}</h3>
<div class="category">
<i class="fas fa-tag"></i> ${hobby.category.charAt(0).toUpperCase() + hobby.category.slice(1)}
</div>
<p class="description">${hobby.description}</p>
</div>
`).join('');
// Add click event to each card
document.querySelectorAll('.popular-hobby-card').forEach((card, index) => {
card.addEventListener('click', () => showHobbyDetails(defaultHobbies[index]));
});
}
// Add this to handle the toggle functionality
function initializePopularHobbies() {
elements.popularHobbiesToggle.addEventListener('click', () => {
elements.popularHobbiesToggle.classList.toggle('active');
elements.popularHobbiesContent.classList.toggle('active');
});
displayPopularHobbies();
}
/*******************************
HOBBY MANAGEMENT & UI
*******************************/
async function getHobbyImage(hobbyName) {
try {
const response = await fetch(
`https://api.unsplash.com/search/photos?query=${encodeURIComponent(hobbyName)}&per_page=1`,
{
headers: {
'Authorization': `Client-ID ${UNSPLASH_API_KEY}`
}
}
);
const data = await response.json();
return data.results?.[0]?.urls?.regular || 'default-image.jpg';
} catch (error) {
console.error('Image fetch error:', error);
return 'default-image.jpg';
}
}
// async function getHobbySuggestions(preferences)
async function getHobbySuggestions(preferences) {
const prompt = `Suggest 8 hobbies based on these preferences:
Time available: ${preferences.duration}
Energy level: ${preferences.energy}
Social preference: ${preferences.social}
For each hobby, provide:
Name | Category | Description | Difficulty | Equipment Needed
Format as pipe-separated values. No extra text.`;
try {
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${GEMINI_API_KEY}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{ parts: [{ text: prompt }] }]
})
}
);
const data = await response.json();
const parsedHobbies = parseGeminiResponse(data);
saveHobbiesToStorage(parsedHobbies); // Save to local storage
return parsedHobbies;
} catch (error) {
console.error('Gemini API error:', error);
return getHobbiesFromStorage(); // Return saved hobbies if API fails
}
}
function parseGeminiResponse(data) {
if (!data?.candidates?.[0]?.content?.parts?.[0]?.text) {
return defaultHobbies;
}
const text = data.candidates[0].content.parts[0].text;
return text.split('\n')
.filter(line => line.trim())
.map(line => {
const [name, category, description, difficulty, equipment] =
line.split('|').map(s => s.trim());
return { name, category: category.toLowerCase(), description, difficulty, equipment };
});
}
async function displayHobbies(hobbies) {
elements.hobbyGrid.innerHTML = '';
for (const hobby of hobbies) {
const image = await getHobbyImage(hobby.name);
const card = createHobbyCard(hobby, image);
elements.hobbyGrid.appendChild(card);
// Add fade-in animation
setTimeout(() => card.classList.add('visible'), 100);
}
}
function createHobbyCard(hobby, image) {
const card = document.createElement('div');
card.className = 'hobby-card glass-effect';
card.setAttribute('data-category', hobby.category);
card.innerHTML = `
<img src="${image}" alt="${hobby.name}" class="hobby-image">
<div class="hobby-content">
<h3>${hobby.name}</h3>
<p>${hobby.description}</p>
<div class="hobby-tags">
<span class="difficulty-tag">${hobby.difficulty}</span>
<span class="category-tag">${hobby.category}</span>
</div>
</div>
`;
card.addEventListener('click', () => showHobbyDetails(hobby, image));
return card;
}
function showHobbyDetails(hobby, image) {
const modalContent = elements.modal.querySelector('.modal-body');
modalContent.innerHTML = `
<div class="hobby-detail">
<img src="${image}" alt="${hobby.name}" class="detail-image">
<div class="detail-content">
<div>
<h2>${hobby.name}</h2>
<div class="tags">
<span class="tag difficulty">
<i class="fas fa-signal"></i> ${hobby.difficulty}
</span>
<span class="tag category">
<i class="fas fa-tag"></i> ${hobby.category}
</span>
</div>
</div>
<div class="description">
<p>${hobby.description}</p>
</div>
<div class="equipment">
<h3><i class="fas fa-tools"></i> Equipment Needed</h3>
<p>${hobby.equipment}</p>
</div>
<div class="getting-started">
<h3><i class="fas fa-flag"></i> Getting Started</h3>
<ul>
<li>Research basic techniques and tutorials online</li>
<li>Join local groups or classes in your area</li>
<li>Start with basic equipment and gradually upgrade</li>
</ul>
</div>
<div class="links">
<h3><i class="fas fa-link"></i> Useful Links</h3>
<a href="https://en.wikipedia.org/wiki/${hobby.name.replace(' ', '_')}"
target="_blank"
class="wiki-link">
<i class="fab fa-wikipedia-w"></i> Learn more on Wikipedia
</a>
</div>
</div>
</div>
`;
elements.modal.style.display = 'block';
}
function filterHobbies(category) {
const currentHobbies = getHobbiesFromStorage();
const filteredHobbies = category === 'all'
? currentHobbies
: currentHobbies.filter(hobby => hobby.category.toLowerCase() === category.toLowerCase());
displayHobbies(filteredHobbies);
}
function initializeCategoryFilters() {
document.querySelectorAll('.pill').forEach(pill => {
pill.addEventListener('click', () => {
document.querySelectorAll('.pill').forEach(p => p.classList.remove('active'));
pill.classList.add('active');
filterHobbies(pill.dataset.category);
});
});
}
function showError(message) {
const errorDiv = document.createElement('div');
errorDiv.className = 'error-message';
errorDiv.innerHTML = `
<i class="fas fa-exclamation-circle"></i>
<p>${message}</p>
`;
document.querySelector('.container').prepend(errorDiv);
setTimeout(() => errorDiv.remove(), 5000);
}
// /*******************************
// EVENT LISTENERS
// *******************************/
function initializeEventListeners() {
// Theme toggle
elements.themeButton.addEventListener('click', toggleTheme);
// Find hobbies button
elements.findHobbiesBtn.addEventListener('click', async () => {
const preferences = {
duration: document.getElementById('duration').value,
energy: document.getElementById('energy').value,
social: document.getElementById('social').value
};
if (!preferences.duration || !preferences.energy || !preferences.social) {
showError('Please select all preferences.');
return;
}
elements.loadingOverlay.style.display = 'flex';
const suggestions = await getHobbySuggestions(preferences);
await displayHobbies(suggestions);
elements.loadingOverlay.style.display = 'none';
});
// Category filtering
document.querySelectorAll('.pill').forEach(pill => {
pill.addEventListener('click', () => {
document.querySelector('.pill.active')?.classList.remove('active');
pill.classList.add('active');
filterHobbies(pill.dataset.category);
});
});
// Location refresh
elements.refreshLocation.addEventListener('click', updateLocation);
// Modal close
window.onclick = (event) => {
if (event.target === elements.modal) {
elements.modal.style.display = 'none';
}
};
document.querySelector('.close-button').onclick = () => {
elements.modal.style.display = 'none';
};
}
// /*******************************
// INITIALIZATION
// *******************************/
document.addEventListener('DOMContentLoaded', async () => {
initTheme();
// Initialize all event listeners
elements.themeButton.addEventListener('click', toggleTheme);
elements.findHobbiesBtn.addEventListener('click', async () => {
const preferences = {
duration: document.getElementById('duration').value,
energy: document.getElementById('energy').value,
social: document.getElementById('social').value
};
if (!preferences.duration || !preferences.energy || !preferences.social) {
showError('Please select all preferences.');
return;
}
elements.loadingOverlay.style.display = 'flex';
const suggestions = await getHobbySuggestions(preferences);
await displayHobbies(suggestions);
// Reset category pills to 'all'
document.querySelectorAll('.pill').forEach(p => p.classList.remove('active'));
document.querySelector('.pill[data-category="all"]').classList.add('active');
elements.loadingOverlay.style.display = 'none';
});
// Initialize popular hobbies
initializePopularHobbies();
// Initialize category filters
initializeCategoryFilters();
// Initialize modal close handlers
window.onclick = (event) => {
if (event.target === elements.modal) {
elements.modal.style.display = 'none';
}
};
document.querySelector('.close-button').onclick = () => {
elements.modal.style.display = 'none';
};
// Initialize location and display hobbies
await updateLocation();
const savedHobbies = getHobbiesFromStorage();
await displayHobbies(savedHobbies);
});
// Update the weather refresh event listener
elements.refreshWeather.addEventListener('click', async () => {
const refreshIcon = elements.refreshWeather.querySelector('i');
refreshIcon.classList.add('refreshing');
elements.weatherWidget.innerHTML += `
<div class="weather-updating">
<div class="loader"></div>
</div>
`;
const location = await getUserLocation();
if (location) {
await updateWeather(location);
}
refreshIcon.classList.remove('refreshing');
const updatingDiv = document.querySelector('.weather-updating');
if (updatingDiv) updatingDiv.remove();
});