-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
472 lines (374 loc) · 17.8 KB
/
script.js
File metadata and controls
472 lines (374 loc) · 17.8 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
console.log("Let's write some Java Script");
/*
fetch() is a built-in JavaScript function used to request data from a URL (API, file, image, etc.).
It works over HTTP/HTTPS and returns a Promise that resolves to a Response object.
We have to use either async/await or .thrn() or any other promise methods in order to use fetch()
*/
/*
async and await are features in JavaScript used to work with asynchronous code in a simpler, cleaner way than using .then() chains.
✅ async
async is a keyword you put before a function to tell JavaScript that the function will use asynchronous operations and return a Promise.
✅ await
await is used inside an async function.
It pauses the function execution until a Promise is resolved, then gives you the actual value.
*/
/*
()=> is the arrow function syntax in JavaScript. It’s a shorter way to write a function.
arrow function can be stored in a variable. An function can be called using <variable>()
*/
/*
setTimeout() is a built-in JavaScript function that runs a piece of code after a delay (in milliseconds).
It does not stop your program; it schedules the function to run later (asynchronously).
*/
/*
using '>' while targetting elements or classes means, direct child
*/
let currentSong = new Audio(); //global variable
let songs;
async function getSongs(name){
let a = await fetch(`${name}`);
let response = await a.text();
//console.log(response);
let div = document.createElement("div");
div.innerHTML = response;
let listOfas = div.querySelectorAll("li a"); //note that its elements not element
let songs = [];
for (let index = 0; index < listOfas.length; index++) {
const element = listOfas[index];
if(element.href.endsWith(".mp3")){
songs.push(element.href.split(`/${name}/`)[1]); //removing unwanted things from the song link
}
}
//console.log(songs);
return songs;
}
async function getAlbums(){
let a = await fetch("albums");
let response = await a.text();
//console.log(response);
let div = document.createElement("div");
div.innerHTML = response;
let albums = div.getElementsByTagName("a");
songs = [];
for (let index = 0; index < albums.length; index++) {
const element = albums[index];
if(element.href.includes("albums/")){
//console.log(element);
//console.log(element.href.split("albums/")[1].replaceAll("%20"," "));
let albumName = element.href.split("albums/")[1].replaceAll("%20"," ").replaceAll("%26","&");
const div = document.createElement("div");
div.classList.add("card");
div.innerHTML = `
<img src="albums/${albumName}/cover.jpg" class="poster">
<img src="assets/greenPlay.svg" class="greenPlayBtn">
<h1>${albumName}</h1>
`;
document.querySelector(".my-music .card-container").append(div);
}
}
}
function formatTime(sec){
const m = Math.floor(sec/60);
const s = Math.floor(sec%60).toString().padStart(2,"0");
return `${m}:${s}`;
}
const playMusic = (track) =>{
//console.log(track);
/*
let audio = new Audio("songs/" + track + ".mp3"); //play as many songs simultaneoulsy as we click
audio.play();
*/
currentSong.src = "songs/" + track + ".mp3"; //one song at a time
currentSong.play();
play.src = "assets/pause.svg";
document.querySelector(".song-cover-details .song-details b").innerHTML = track;
currentSong.addEventListener("loadedmetadata", () => {
document.querySelector(".play-pause .lower .total-duration").textContent = formatTime(currentSong.duration);
});
}
/*
✅ Why P2-Spotify Clone was dispperaring:
Earlier I was doing:
new Audio("/songs/" + track + ".mp3");
That leading slash / means: “Start from server root”
So the browser looks for:
http://127.0.0.1:5500/songs/...
and skips your folder P2-Spotify Clone.
That’s why it omits it.
✅ Why it becomes double when you add it manually
If you do: "/P2-Spotify Clone/songs/..."
and your page is already inside P2-Spotify Clone, the real resolved path becomes: http://127.0.0.1:5500/P2-Spotify Clone/P2-Spotify Clone/songs/...
So it duplicates.
*/
async function pushAllSongsIntoList(folder){
//getting list of all songs
songs = await getSongs(`${folder}`);
//console.log(songs);
//Getting all songs into our library
let songUL = document.querySelector(".songs-list").getElementsByTagName("ul")[0];
/*
for(const song of songs){
songUL.innerHTML = songUL.innerHTML + `<li class="flex justify-center items-center hover">
<span>
<img src="assets/music.svg" class="invert" alt="cover">
</span>
<span class="about-song">
<sName href=""><b>${song.replaceAll("%20"," ").split(".mp3")[0]}</b></sName>
<br>
<aName href="">Song Artists</aName>
</span>
<span class="flex">
<img src="assets/threeDots.svg" class="invert" alt="threeDots">
</span>
</li>`;
}
This is VERY expensive. Each time the loop runs, the browser:
->Reads the entire existing HTML
->Concatenates string
->Deletes the whole DOM tree
->Rebuilds entire <ul> again
->Re-parses everything
->Repaints
*/
let html = "";
for (const song of songs) {
html += `<li class="flex justify-center items-center hover">
<span>
<img src="assets/music.svg" class="invert" alt="cover">
</span>
<span class="about-song">
<sName><b>${song.replaceAll("%20"," ").split(".mp3")[0]}</b></sName>
<br>
<aName>Song Artists</aName>
</span>
<span class="flex">
<img src="assets/threeDots.svg" class="invert" alt="threeDots">
</span>
</li>`;
}
songUL.innerHTML = html;
return [songs,songUL];
}
async function main(){
let [songs,songUL] = await pushAllSongsIntoList("songs");
/*
//playing the first song
var audio = new Audio(songs[0]);
//audio.play();
audio.addEventListener("loadeddata",() => {
let duration = audio.duration;
console.log(audio.duration,audio.currentSrc,audio.currentTime); //in seconds
})
*/
/*
The browser calls the arrow funciton automatically.
1️⃣ You attach a listener
2️⃣ When the audio finishes loading enough data to know its duration,
the browser fires the "loadeddata" event.
3️⃣ At that moment, the browser runs the callback (your arrow function).
#THERE ARE SPECIAL ATTRIBUTES OF Audio IN JS USED TO GET VARIOUS META DATA ABOUT THE AUDIOS LIKE duration, currentTime, src, paused, muted, volume, etc many more
✅ What "loadeddata" means
It fires when the browser has loaded enough media data to start playing the audio/video, at least the first frame.
*/
/*
✅ What addEventListener() does:
It listens for an event and then calls your function when that event occurs. Here that event is "loadeddata"
❓ Why do we need it instead of running code directly?
Because the duration is NOT available immediately when you create the audio.
It takes time to load. So if you run code too early, duration is NaN.
*/
/*
querySelector() → Select the first element using CSS selector syntax (class,id, tag, nested selectors).
querySelectorAll() -> All matching CSS selectors
.target is most commonly seen as event.target in JavaScript event handling.
It refers to the exact element that triggered the event (the element that was actually clicked, typed in, hovered, etc.).
*/
//Attach an event listner to each song
function attachSongsClickListeners(){
Array.from(document.querySelector(".songs-list").getElementsByTagName("li")).forEach(li => {
li.addEventListener("click", element => {
const name = li.querySelector(".about-song > sName > b").innerHTML;
console.log(name);
playMusic(name);
})
});
}
attachSongsClickListeners();
//Attach an event listner to play, next and previous
let playButton = document.querySelector(".upper > .play-btn");
playButton.addEventListener("click", () => {
if(currentSong.paused){
currentSong.play();
play.src = "assets/pause.svg";
}
else{
currentSong.pause();
play.src = "assets/play.svg";
}
});
next.addEventListener("click", () => {
//console.log(songs);
//console.log(currentSong.src);
let index = songs.indexOf(currentSong.src.split("songs/")[1]);
//console.log(index);
let nextIndex = (index+1) % songs.length;
playMusic(songs[nextIndex].split(".mp3")[0].replaceAll("%20"," "));
});
previous.addEventListener("click", () => {
let index = songs.indexOf(currentSong.src.split("songs/")[1]);
//console.log(index);
let prevIndex = (index - 1 + songs.length) % songs.length;
playMusic(songs[prevIndex].split(".mp3")[0].replaceAll("%20"," "));
});
//Listen for time-update
currentSong.addEventListener("timeupdate", () => {
document.querySelector(".play-pause .lower .current-duration").textContent = formatTime(currentSong.currentTime);
//document.querySelector(".current-instance-circle").style.left = (currentSong.currentTime/currentSong.duration) * 100 + "%"; //use of left is laggy
const percent = currentSong.currentTime / currentSong.duration;
const barWidth = document.querySelector(".seek-bar").clientWidth;
document.querySelector(".current-instance-circle").style.transform = `translate(-50%, -50%) translateX(${barWidth * percent}px)`;
document.querySelector(".seek-bar .fill").style.width = `${percent*100}%`;
});
//Add event listener to seekbar
document.querySelector(".seek-bar").addEventListener("click", e => {
//console.log(e.offsetX , e.target.getBoundingClientRect().width);
const percent = e.offsetX / e.target.getBoundingClientRect().width; //this will work correct if we disable clicks on .fill
const barWidth = document.querySelector(".seek-bar").clientWidth;
document.querySelector(".current-instance-circle").style.transform = `translate(-50%, -50%) translateX(${barWidth * percent}px)`;
document.querySelector(".seek-bar .fill").style.width = `${percent * 100}%`;
currentSong.currentTime = currentSong.duration * percent;
});
//Add an event listener for hamburger
document.querySelector(".hamburger").addEventListener("click", () => {
let libraryBar = document.querySelector("main > .library");
if(libraryBar.style.left == `0%`){
libraryBar.style.left = `-100%`;
document.querySelector(".hamburger > img").src = "assets/hamburger.svg";
//libraryBar.style.display = "none"; //only for phone like iphone se
}
else{
libraryBar.style.left = `0%`;
document.querySelector(".hamburger > img").src = "assets/close.svg";
//libraryBar.style.display = "block"; //only for phone like iphone se
}
});
//Add an event listener for volume button
document.querySelector(".volume-button-and-bar > button").addEventListener("click", ()=> {
let seekBar = document.querySelector(".volume");
if(window.matchMedia("(max-width: 800px)").matches){
if(seekBar.style.display == "none"){
seekBar.style.display = "inline"
seekBar.style.transform = "rotate(-90deg)";
}
else
seekBar.style.display = "none";
}
});
document.querySelector(".volume").addEventListener("change", (e) => {
console.log(e.target.value);
currentSong.volume = e.target.value / 100;
});
//Add event listeners for ALL, SONGS, PODCASTS
document.querySelector(".songs > header .all").addEventListener("click", () =>{
pushAllSongsIntoList("songs");
Swal.fire({ //suggested by chat gpt
title: "All Songs added to Library",
text: "Enjoy listening 🎧",
icon: "success",
timer: 1000,
showConfirmButton: false
});
//console.log("Music Tab Switched");
document.querySelector(".songs > header .all").style.backgroundColor = "white";
document.querySelector(".songs > header .all").style.color = "black";
document.querySelector(".songs > header .music").style.backgroundColor = "#383838";
document.querySelector(".songs > header .music").style.color = "white";
document.querySelector(".songs > header .podcasts").style.backgroundColor = "#383838";
document.querySelector(".songs > header .podcasts").style.color = "white";
document.querySelector(".songs .ALL").style.display = "block";
document.querySelector(".songs .PODCASTS").style.display = "none";
document.querySelector(".songs .MUSIC").style.display = "none";
});
document.querySelector(".songs > header .music").addEventListener("click", () =>{
//console.log("Music Tab Switched");
document.querySelector(".MUSIC .card-container").innerHTML = "";
getAlbums("albums");
document.querySelector(".songs > header .music").style.backgroundColor = "white";
document.querySelector(".songs > header .music").style.color = "black";
document.querySelector(".songs > header .all").style.backgroundColor = "#383838";
document.querySelector(".songs > header .all").style.color = "white";
document.querySelector(".songs > header .podcasts").style.backgroundColor = "#383838";
document.querySelector(".songs > header .podcasts").style.color = "white";
document.querySelector(".songs .ALL").style.display = "none";
document.querySelector(".songs .PODCASTS").style.display = "none";
document.querySelector(".songs .MUSIC").style.display = "block";
});
document.querySelector(".songs > header .podcasts").addEventListener("click", () =>{
//console.log("Music Tab Switched");
document.querySelector(".songs > header .podcasts").style.backgroundColor = "white";
document.querySelector(".songs > header .podcasts").style.color = "black";
document.querySelector(".songs > header .music").style.backgroundColor = "#383838";
document.querySelector(".songs > header .music").style.color = "white";
document.querySelector(".songs > header .all").style.backgroundColor = "#383838";
document.querySelector(".songs > header .all").style.color = "white";
document.querySelector(".songs .ALL").style.display = "none";
document.querySelector(".songs .PODCASTS").style.display = "block";
document.querySelector(".songs .MUSIC").style.display = "none";
});
//Openning specific album
document.querySelector(".MUSIC").addEventListener("click", async (e) => {
const card = e.target.closest(".card");
if(!card) return;
let albumName = card.querySelector("h1").innerHTML;
console.log(albumName);
let songs = await getSongs(`albums/${albumName.replaceAll(" ","%20")}`);
console.log(songs);
let html = "";
for (const song of songs) {
html += `<li class="flex justify-center items-center hover">
<span>
<img src="assets/music.svg" class="invert" alt="cover">
</span>
<span class="about-song">
<sName><b>${song.replaceAll("%20"," ").split(".mp3")[0]}</b></sName>
<br>
<aName>Song Artists</aName>
</span>
<span class="flex">
<img src="assets/threeDots.svg" class="invert" alt="threeDots">
</span>
</li>`;
}
songUL.innerHTML = html;
attachSongsClickListeners(); //When you replace innerHTML, all previous click event listeners are destroyed. Hence we need to re-attack them after inserting all required html.
document.querySelector(".hamburger").click();
//alert("Songs added to library");
Swal.fire({ //suggested by chat gpt
title: "Songs added to Library",
text: "Enjoy listening 🎧",
icon: "success",
timer: 1000,
showConfirmButton: false
});
});
//Add event listener to under-dev
document.querySelectorAll(".devAtWork").forEach(el => { //querySelectorAll return a node-list
el.addEventListener("click", () => {
console.log("Under development...");
Swal.fire({
title: "Development At Work 🧑🏻💻🛠️",
text: "Happy Learning......",
icon: "info",
timer: 1000,
showConfirmButton: false,
showClass: {
popup: 'animate__animated animate__zoomIn animate__faster'
},
hideClass: {
popup: 'animate__animated animate__fadeOut'
}
})
})
});
}
main();