-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
606 lines (504 loc) · 21 KB
/
Copy pathindex.js
File metadata and controls
606 lines (504 loc) · 21 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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* SOME ELEMENTS */
const searchBarMain = document.getElementById("searchBarMain");
const searchBarPanel = document.getElementById("searchBarPanel");
const searchInputPanel = document.getElementById("searchInputPanel");
const searchInputMain = document.getElementById("searchInputMain");
const searchPanel = document.getElementById("searchPanel");
const fullView = document.getElementById("fullView");
const form1 = document.getElementById("searchBarMain");
const form2 = document.getElementById("searchBarPanel");
/* OTHER STUFF */
let ytPlayer;
let globalAlbumList = [];
let isPlaying = false;
let progressInterval;
let playbackQueue = []; // array of track objects
let currentTrackIndex = -1;
window.onload = () => {
document.getElementById("albumArtContainer").classList.add("hidden");
};
/* FUNCTIONALITY TO CLOSE SIDE PANEL */
//To close side panel when music player section is clicked
const musicPlayer = document.getElementById("musicPlayer");
musicPlayer.addEventListener("click", function () {
resetToMainSearch();
});
const PanelClose = document.getElementById("backToMPButton");
if (PanelClose) {
PanelClose.addEventListener('click', () => {
resetToMainSearch();
});
}
/* TAKING SEARCH INPUT */
//Form 1 submission (music player search bar)
form1.addEventListener('submit', async function(e) {
e.preventDefault();
const query = searchInputMain.value.trim();
if (query !== "") {
await performSearch(query);
showSearchPanelOnly();
searchInputPanel.focus();
}
form1.reset();
});
// Form 2 submission (panel search bar)
form2.addEventListener('submit', async function(e) {
e.preventDefault();
const query = searchInputPanel.value.trim();
if (query !== "") {
await performSearch(query);
showSearchPanelOnly();
searchInputPanel.focus();
}
form2.reset();
});
// Search function
async function performSearch(query) {
const trackUrl = `/api/lastfm?method=track.search&track=${encodeURIComponent(query)}`;
const artistUrl = `/api/lastfm?method=artist.search&artist=${encodeURIComponent(query)}`;
const albumUrl = `/api/lastfm?method=album.search&album=${encodeURIComponent(query)}`;
try {
const [trackResult, artistResult, albumResult] = await Promise.all([
fetch(trackUrl),
fetch(artistUrl),
fetch(albumUrl)
]);
const [trackData, artistData, albumData] = await Promise.all([
trackResult.json(),
artistResult.json(),
albumResult.json()
]);
const trackList = trackData.results?.trackmatches?.track || [];
const artistList = artistData.results?.artistmatches?.artist || [];
const albumList = albumData.results?.albummatches?.album || [];
globalAlbumList = albumList;
renderTracks(trackList);
renderArtists(artistList);
renderAlbums(albumList);
if (trackList.length === 0 && artistList.length === 0 && albumList.length === 0) {
console.log("😞 No results found for:", query);
}
} catch (err) {
console.error("API error:", err);
}
}
/* HELPER FUNCTIONS */
function showSearchPanelOnly() {
searchPanel.classList.remove("hidden");
fullView.classList.add("hidden");
form1.classList.add("hidden");
searchBarMain.classList.add("hidden");
}
function showFullViewOnly() {
searchPanel.classList.add("hidden");
fullView.classList.remove("hidden");
}
function resetToMainSearch() {
searchPanel.classList.add("hidden");
fullView.classList.add("hidden");
searchBarMain.classList.remove("hidden");
form1.classList.remove("hidden");
}
function findAlbumArtFromSearch(trackName, artistName) {
return MusicPlayerUtils.findAlbumArtFromSearch(globalAlbumList, trackName, artistName);
}
async function fetchYTArtistImage(artistName) {
const url = `/api/youtube?part=snippet&type=channel&q=${encodeURIComponent(artistName)}`;
try {
const res = await fetch(url);
const data = await res.json();
const channel = data.items?.[0];
const imageUrl = channel?.snippet?.thumbnails?.high?.url || "assests/fallback.png";
return imageUrl;
} catch (err) {
console.error("YouTube channel fetch failed:", err);
return "assests/fallback.png";
}
}
let ytPlayerReady = false;
/* YT PLAYER FUNCTION */
function onYouTubeIframeAPIReady() {
ytPlayer = new YT.Player("youtubePlayer", {
height: "0px",
width: "0px",
videoId: "",
events: {
onReady: (event) => {
console.log("YouTube player ready");
ytPlayerReady = true;
},
onStateChange: onPlayerStateChange
}
});
}
/* MAIN FUNCTIONS */
//fetch songs related to the search query
function renderTracks(tracks) {
const section = document.getElementById("songsSection");
const fullView = document.getElementById("fullView");
const fullList = document.getElementById("fullList");
const backBtn = document.getElementById("backToSummaryBtn");
fullView.classList.add("hidden");
section.innerHTML = "";
if (tracks.length > 0) {
section.innerHTML = "<h3 class='songsList'>Songs</h3>";
tracks.slice(0, 5).forEach(track => {
const div = document.createElement("div");
div.classList.add("track-card");
div.textContent = `${track.name} - ${track.artist}`;
div.addEventListener("click", () => handleSongClick(track, tracks));
section.appendChild(div);
});
const showMoreBtn = document.createElement("button");
showMoreBtn.textContent = "Show more";
showMoreBtn.classList.add("show-more-btn");
section.appendChild(showMoreBtn);
showMoreBtn.addEventListener("click", () => {
document.getElementById("searchPanel").classList.add("hidden");
fullView.classList.remove("hidden");
fullList.innerHTML = "<h3 class='songsList'>Songs</h3>";
tracks.forEach(track => {
const div = document.createElement("div");
div.classList.add("track-card");
div.textContent = `${track.name} - ${track.artist}`;
div.addEventListener("click", () => handleSongClick(track, tracks));
fullList.appendChild(div);
});
if (!fullView.classList.contains("hidden") && searchPanel.classList.contains("hidden")) {
backBtn.addEventListener("click", () => {
showSearchPanelOnly();
});
}
});
}
}
//fetch artists related to the search query
async function renderArtists(artists) {
const section = document.getElementById("artistsSection");
const fullView = document.getElementById("fullView");
const fullList = document.getElementById("fullList");
const backBtn = document.getElementById("backToSummaryBtn");
fullView.classList.add("hidden");
section.innerHTML = "";
if (artists.length === 0) {
section.innerHTML = "<p>No artists found</p>";
return;
}
section.innerHTML = "<h3 class='artistsList'>Artists</h3>";
// Top artist match
const topArtist = artists[0];
const imageUrl = await fetchYTArtistImage(topArtist.name);
if (imageUrl != "") {
section.innerHTML += `
<div class="top-artist-card">
<div class="top-artist-name">
<p class="top-artist">${topArtist.name}</p>
</div>
<div
class="artist-image-with-fade"
style="background-image: linear-gradient(to right, rgb(12, 20, 19) 0%, rgba(0,0,0,0) 100%), url('${imageUrl}');"
role="img"
aria-label="${topArtist.name}"
></div>
</div>
`;
} else {
section.innerHTML += `
<div class="top-artist-card">
<div class="top-artist-name">
<p class="top-artist">${topArtist.name}</p>
</div>
</div>
`;
}
const showMoreBtn = document.createElement("button");
showMoreBtn.textContent = "Show more";
showMoreBtn.classList.add("show-more-btn");
section.appendChild(showMoreBtn);
showMoreBtn.addEventListener("click", () => {
document.getElementById("searchPanel").classList.add("hidden");
fullView.classList.remove("hidden");
fullList.innerHTML = "<h3 class='artistsList'>Artists</h3>";
artists.forEach(artist => {
fullList.innerHTML += `<div class="artist-card">${artist.name}</div>`;
});
if (!fullView.classList.contains("hidden") && searchPanel.classList.contains("hidden")) {
backBtn.addEventListener("click", () => {
showSearchPanelOnly();
});
}
});
}
//fetch albums related to the search query
function renderAlbums(albums) {
const section = document.getElementById("albumsSection");
const fullView = document.getElementById("fullView");
const fullList = document.getElementById("fullList");
const backBtn = document.getElementById("backToSummaryBtn");
fullView.classList.add("hidden");
section.innerHTML = "";
if (albums.length === 0) {
section.innerHTML = "<p>No albums found</p>";
return;
}
section.innerHTML = "<h3 class='albumsList'>Albums</h3>";
albums.slice(0, 3).forEach(album => {
const albumCard = document.createElement('div');
albumCard.classList.add('album-card');
albumCard.innerHTML = `<h4>${album.name}</h4><p style="font-size:15px; margin-top:2px;">${album.artist}</p>`;
section.appendChild(albumCard);
});
const showMoreBtn = document.createElement("button");
showMoreBtn.textContent = "Show more";
showMoreBtn.classList.add("show-more-btn");
section.appendChild(showMoreBtn);
showMoreBtn.addEventListener("click", () => {
document.getElementById("searchPanel").classList.add("hidden");
fullView.classList.remove("hidden");
renderAlbumsInFullView();
if (!fullView.classList.contains("hidden") && searchPanel.classList.contains("hidden")) {
backBtn.addEventListener("click", () => {
showSearchPanelOnly();
});
}
});
}
/* ALBUM FUNCTIONS */
function renderAlbumsInFullView() {
const fullList = document.getElementById("fullList");
fullList.innerHTML = "<h3 class='albumsList'>Albums</h3>";
globalAlbumList.forEach(album => {
const albumDiv = document.createElement('div');
albumDiv.classList.add('album-card', 'clickable-album');
albumDiv.innerHTML = `<h4>${album.name}</h4><p style="font-size:15px; margin-top:2px;">${album.artist}</p>`;
albumDiv.addEventListener('click', () => {
showAlbumTracks(album.name, album.artist);
});
fullList.appendChild(albumDiv);
});
}
// Function to get album tracks using album.getInfo
async function getAlbumTracks(artistName, albumName) {
const albumInfoUrl = `/api/lastfm?method=album.getInfo&artist=${encodeURIComponent(artistName)}&album=${encodeURIComponent(albumName)}`;
try {
const response = await fetch(albumInfoUrl);
const albumData = await response.json();
if (albumData.album && albumData.album.tracks && albumData.album.tracks.track) {
const tracks = Array.isArray(albumData.album.tracks.track)
? albumData.album.tracks.track
: [albumData.album.tracks.track];
return tracks.map(track => ({
name: track.name,
artist: artistName,
duration: track.duration || '0'
}));
}
} catch (error) {
console.error('Error fetching album tracks:', error);
}
return [];
}
// Function to show album tracks view
async function showAlbumTracks(albumName, artistName) {
const fullView = document.getElementById("fullView");
const fullList = document.getElementById("fullList");
const backBtn = document.getElementById("backToSummaryBtn");
fullList.innerHTML = `
<div class="album-header">
<h3>${albumName}</h3>
<p>${artistName}</p>
<p>Loading tracks...</p>
</div>
`;
try {
const tracks = await getAlbumTracks(artistName, albumName);
const mainBackButton = document.getElementById('backToSummaryBtn');
mainBackButton.classList.add('hidden');
fullList.innerHTML = `
<div class="album-header">
<button id="backToAlbumsBtn" class="back-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="rgb(12, 20, 19)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-left-icon lucide-arrow-left"><path d="m12 19-7-7 7-7"/><path d="M19 12H5"/></svg>
Back to Albums</button>
<h3>${albumName}</h3>
<p>${artistName}</p>
<p>${tracks.length} tracks</p>
</div>
<div class="tracks-container">
${tracks.length > 0
? tracks.map((track, index) => `
<div class="track-item" data-track='${JSON.stringify(track)}'>
<span class="track-number">${index + 1}</span>
<div class="track-info">
<div class="track-name">${track.name}</div>
<div class="track-artist">${track.artist}</div>
</div>
<div class="play-btn">
<svg class="play-symbol" xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="rgb(232, 240, 239)" viewBox="0 0 256 256">
<path d="M240,128a15.74,15.74,0,0,1-7.6,13.51L88.32,229.65a16,16,0,0,1-16.2.3A15.86,15.86,0,0,1,64,216.13V39.87a15.86,15.86,0,0,1,8.12-13.82,16,16,0,0,1,16.2.3L232.4,114.49A15.74,15.74,0,0,1,240,128Z"/>
</svg>
</div>
</div>
`).join('')
: '<p>No tracks found for this album</p>'
}
</div>
`;
if (tracks.length > 0) {
const trackItems = fullList.querySelectorAll('.track-item');
trackItems.forEach(item => {
item.addEventListener('click', () => {
const trackData = JSON.parse(item.dataset.track);
playTrack(trackData);
});
});
}
const backToAlbumsBtn = document.getElementById("backToAlbumsBtn");
backToAlbumsBtn.addEventListener('click', () => {
renderAlbumsInFullView();
mainBackButton.classList.remove('hidden');
});
} catch (error) {
console.error('Error loading album tracks:', error);
fullList.innerHTML = `
<div class="album-header">
<button id="backToAlbumsBtn" class="back-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="rgb(12, 20, 19)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-left-icon lucide-arrow-left"><path d="m12 19-7-7 7-7"/><path d="M19 12H5"/></svg>
Back to Albums</button>
<h3>${albumName}</h3>
<p>${artistName}</p>
<p>Error loading tracks</p>
</div>
`;
}
}
/* PLAYBACK & SONG CONTROL */
async function playTrack(track) {
const songQuery = `${track.name} ${track.artist}`;
const songName = document.getElementById("songName");
const artistName = document.getElementById("artistName");
const albumArt = document.getElementById("albumArt");
const progressBar = document.getElementById("progressBar");
const playButton = document.getElementById("playButton");
stopProgressUpdater();
progressBar.style.width = "0%";
try {
const ytRes = await fetch(`/api/youtube?part=snippet&q=${encodeURIComponent(songQuery)}&maxResults=1&type=video`);
const ytData = await ytRes.json();
const videoId = ytData.items?.[0]?.id?.videoId;
console.log("YouTube video ID:", videoId);
// Fetch album art with a single, optimized API call
try {
const artResponse = await fetch(`/api/getAlbumArt?artist=${encodeURIComponent(track.artist)}&track=${encodeURIComponent(track.name)}`);
const artData = await artResponse.json();
if (artData.imageUrl) {
albumArt.src = artData.imageUrl;
document.getElementById("albumArtContainer").classList.remove("hidden");
albumArt.classList.remove("hidden");
} else {
throw new Error("No image URL returned");
}
} catch (artErr) {
console.warn("Album art fallback used:", artErr);
document.getElementById("albumArtContainer").classList.remove("hidden");
albumArt.src = "assests/fallback.png";
}
songName.textContent = track.name;
artistName.textContent = track.artist;
if (videoId && ytPlayer && ytPlayerReady && ytPlayer.loadVideoById) {
ytPlayer.loadVideoById(videoId);
ytPlayer.playVideo();
isPlaying = true;
startProgressUpdater();
playButton.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" fill="rgb(232, 240, 239)" viewBox="0 0 256 256">
<path d="M216,48V208a16,16,0,0,1-16,16H160a16,16,0,0,1-16-16V48a16,16,0,0,1,16-16h40A16,16,0,0,1,216,48ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Z"/>
</svg>`;
} else {
console.warn("No video ID found or player not ready.");
}
} catch (err) {
console.error("Error playing track:", err);
songName.textContent = "Error playing track";
}
}
//to save songs in playback queue
function saveQueueToStorage() {
MusicPlayerUtils.saveQueueToStorage(playbackQueue, currentTrackIndex);
}
//to load songs in playback queue
function LoadQueueFromStorage() {
const { queue, currentIndex } = MusicPlayerUtils.loadQueueFromStorage();
playbackQueue = queue;
currentTrackIndex = currentIndex;
}
//to add songs in playback queue
function handleSongClick(track) {
const { queue, currentIndex } = MusicPlayerUtils.handleSongClick(playbackQueue, currentTrackIndex, track);
playbackQueue = queue;
currentTrackIndex = currentIndex;
playTrack(track);
saveQueueToStorage();
}
// progress bar functions
function startProgressUpdater() {
clearInterval(progressInterval);
progressInterval = setInterval(() => {
if (ytPlayer && ytPlayer.getCurrentTime && ytPlayer.getDuration) {
const currentTime = ytPlayer.getCurrentTime();
const duration = ytPlayer.getDuration();
if (duration > 0) {
const progressPercent = (currentTime / duration) * 100;
document.getElementById("progressBar").style.width = `${progressPercent}%`;
}
}
}, 500);
}
function stopProgressUpdater() {
clearInterval(progressInterval);
}
//song control buttons
document.getElementById("nextButton").addEventListener("click", () => {
if (playbackQueue.length > 0 && currentTrackIndex < playbackQueue.length - 1) {
currentTrackIndex++;
playTrack(playbackQueue[currentTrackIndex]);
saveQueueToStorage();
}
});
document.getElementById("prevButton").addEventListener("click", () => {
if (playbackQueue.length > 0 && currentTrackIndex > 0) {
currentTrackIndex--;
playTrack(playbackQueue[currentTrackIndex]);
saveQueueToStorage();
}
});
function onPlayerStateChange(event) {
if (event.data === YT.PlayerState.ENDED) {
document.getElementById("nextButton").click();
}
}
document.getElementById("playButton").addEventListener("click", () => {
if (!ytPlayer) return;
if (!isPlaying) {
ytPlayer.playVideo();
document.getElementById("playButton").innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" fill="rgb(232, 240, 239)" viewBox="0 0 256 256">
<path d="M216,48V208a16,16,0,0,1-16,16H160a16,16,0,0,1-16-16V48a16,16,0,0,1,16-16h40A16,16,0,0,1,216,48ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Z"/>
</svg>
`;
startProgressUpdater();
isPlaying = true;
} else {
ytPlayer.pauseVideo();
document.getElementById("playButton").innerHTML = `
<svg class="play-symbol" xmlns="http://www.w3.org/2000/svg" width="40" height="40" fill="rgb(232, 240, 239)" viewBox="0 0 256 256">
<path d="M240,128a15.74,15.74,0,0,1-7.6,13.51L88.32,229.65a16,16,0,0,1-16.2.3A15.86,15.86,0,0,1,64,216.13V39.87a15.86,15.86,0,0,1,8.12-13.82,16,16,0,0,1,16.2.3L232.4,114.49A15.74,15.74,0,0,1,240,128Z"/>
</svg>
`;
stopProgressUpdater();
isPlaying = false;
}
});
// on screen load
document.addEventListener('DOMContentLoaded', () => {
LoadQueueFromStorage();
});