-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (80 loc) · 2.84 KB
/
app.js
File metadata and controls
95 lines (80 loc) · 2.84 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
/**
* WEB222 – Assignment 06
*
* I declare that this assignment is my own work in accordance with
* Seneca Academic Policy. No part of this assignment has been
* copied manually or electronically from any other source
* (including web sites) or distributed to other students.
*
* Please update the following with your information:
*
* Name: MD ARAFAT KOYES
* Student ID: 13368229
* Date: 06-08-2024
*/
// All of our data is available on the global `window` object.
// Create local variables to work with it in this file.
const { artists, songs } = window;
document.addEventListener("DOMContentLoaded", function () {
const menu = document.getElementById("menu");
const artistName = document.getElementById("selected-artist");
const artistLinks = document.getElementById("artists");
const cardContainer = document.getElementById("cards");
function createSongCard(song) {
const card = document.createElement("div");
card.classList.add("card");
const songImg = document.createElement("img");
songImg.src = song.imageUrl;
songImg.classList.add("card-image");
card.appendChild(songImg);
const songTitle = document.createElement("h2");
songTitle.innerText = song.title;
card.appendChild(songTitle);
const songYear = document.createElement("time");
songYear.innerText = `Year: ${song.year}`;
card.appendChild(songYear);
const songDuration = document.createElement("span");
songDuration.innerText = `Duration: ${formatTime(song.duration)}`;
card.appendChild(songDuration);
songImg.addEventListener("click", () => {
window.open(song.url, "_blank");
});
return card;
}
function formatTime(secs) {
const m = Math.floor(secs / 60);
const s = secs % 60;
return `${m}:${s < 10 ? "0" : ""}${s}`;
}
function showSongs(artistId) {
const artist = artists.find((a) => a.artistId === artistId);
if (artist) {
artistName.innerText = artist.name;
artistLinks.innerHTML = "";
for (let i = 0; i < artist.urls.length; i++) {
const link = artist.urls[i];
artistLinks.innerHTML += `<a href="${link.url}" target="_blank">${link.title}</a>`;
if (i < artist.urls.length - 1) artistLinks.innerHTML += ", ";
}
cardContainer.innerHTML = "";
for (let i = 0; i < songs.length; i++) {
if (songs[i].artistId === artistId && !songs[i].explicit) {
cardContainer.appendChild(createSongCard(songs[i]));
}
}
}
}
for (let i = 0; i < artists.length; i++) {
const btn = document.createElement("button");
btn.innerText = artists[i].name;
btn.onclick = (
(id) => () =>
showSongs(id)
)(artists[i].artistId);
menu.appendChild(btn);
}
// Show songs for the first artist by default
if (artists.length > 0) {
showSongs(artists[0].artistId);
}
});