-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
102 lines (87 loc) · 3.16 KB
/
Copy pathutils.js
File metadata and controls
102 lines (87 loc) · 3.16 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
// MUSIC PLAYER UTILITY FUNCTIONS
const MusicPlayerUtils = {
/**
* Finds album art from a list of albums by matching artist name
* @param {Array} albumList - Array of albums from LastFM API
* @param {string} trackName - Name of the track
* @param {string} artistName - Name of the artist to match
* @returns {string|null} - URL of the extralarge album art image
*/
findAlbumArtFromSearch(albumList, trackName, artistName) {
const matchingAlbum = albumList.find(album =>
album.artist.toLowerCase() === artistName.toLowerCase()
);
if (matchingAlbum && matchingAlbum.image) {
const albumArtImage = matchingAlbum.image.find(img => img.size === 'extralarge');
return albumArtImage ? albumArtImage['#text'] : null;
}
return null;
},
/**
* Saves playback queue and current track index
* @param {Array} queue - Array of track objects
* @param {number} currentIndex - Current track index in the queue
*/
saveQueueToStorage(queue, currentIndex) {
try {
localStorage.setItem('musicQueue', JSON.stringify(queue));
localStorage.setItem('currentTrackIndex', currentIndex.toString());
} catch (error) {
console.error('Error saving queue to storage:', error);
}
},
/**
* Loads playback queue and current track index
* @returns {Object} - Object with queue and currentIndex properties
*/
loadQueueFromStorage() {
try {
const savedQueue = localStorage.getItem('musicQueue');
const savedIndex = localStorage.getItem('currentTrackIndex');
const queue = savedQueue ? JSON.parse(savedQueue) : [];
const currentIndex = savedIndex ? parseInt(savedIndex, 10) : -1;
return { queue, currentIndex };
} catch (error) {
console.error('Error loading queue from storage:', error);
return { queue: [], currentIndex: -1 };
}
},
/**
* Handles the queue management logic
* @param {Array} currentQueue - Current playback queue
* @param {number} currentIndex - Current track index
* @param {Object} track - Track object to add/switch to
* @returns {Object} - Object with queue and currentIndex properties
*/
handleSongClick(currentQueue, currentIndex, track) {
const queue = [...currentQueue];
const indexInQueue = queue.findIndex(
t => t.name === track.name && t.artist === track.artist
);
if (indexInQueue === -1) {
queue.push(track);
return { queue, currentIndex: queue.length - 1 };
} else {
return { queue, currentIndex: indexInQueue };
}
},
/**
* Formats time in seconds to MM:SS format
* @param {number} seconds - Time in seconds
* @returns {string} - Formatted time string (MM:SS)
*/
formatTime(seconds) {
if (!seconds || isNaN(seconds)) return '0:00';
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${minutes}:${secs.toString().padStart(2, '0')}`;
}
};
// Export for use in browser (globally available)
if (typeof window !== 'undefined') {
window.MusicPlayerUtils = MusicPlayerUtils;
}
// Export for CommonJS
if (typeof module !== 'undefined' && module.exports) {
module.exports = MusicPlayerUtils;
}