-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbum-history.js
66 lines (52 loc) · 2.04 KB
/
album-history.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
// Album history tracking from Last.fm
document.addEventListener('DOMContentLoaded', function() {
loadAlbumHistory();
});
// Function to load the album history
async function loadAlbumHistory() {
const albumGrid = document.getElementById('album-history-grid');
if (!albumGrid) return;
albumGrid.innerHTML = '<div class="loading-albums">Loading album history...</div>';
try {
// Add timestamp to prevent caching
const timestamp = new Date().getTime();
const response = await fetch(`/data/lastfm-albums-history.json?_=${timestamp}`);
if (!response.ok) {
throw new Error(`Failed to load album history: ${response.status}`);
}
const data = await response.json();
displayAlbumHistory(data.albums);
} catch (error) {
console.error('Error loading album history:', error);
albumGrid.innerHTML = '<div class="error-loading">Could not load album history</div>';
}
}
// Function to display the album history grid
function displayAlbumHistory(albums) {
const albumGrid = document.getElementById('album-history-grid');
if (!albums || albums.length === 0) {
albumGrid.innerHTML = '<div class="no-albums">No album history available</div>';
return;
}
// Clear the loading message
albumGrid.innerHTML = '';
// Create album elements
albums.forEach(album => {
const albumElement = document.createElement('div');
albumElement.className = 'album-item';
// Default image if none provided
const imageUrl = album.image || './assets/default-album.jpg';
// Add tooltip with album info
albumElement.setAttribute('title', `${album.name} by ${album.artist} - ${album.date}`);
albumElement.innerHTML = `
<div class="album-cover">
<img src="${imageUrl}" alt="${album.name}" onerror="this.src='./assets/default-album.jpg';">
</div>
<div class="album-info">
<div class="album-name">${album.name}</div>
<div class="album-artist">${album.artist}</div>
</div>
`;
albumGrid.appendChild(albumElement);
});
}