-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
475 lines (415 loc) · 12.6 KB
/
script.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
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
"use strict";
let data = [
// {
// id: 1,
// song: "Mówiłaś Mi",
// artist: "O.S.T.R.",
// cover:
// "https://images.genius.com/95158566e73ea4ee02dd2b7b24463d8d.500x500x1.jpg",
// audio: "/music/OSTR.mp3",
// fav: true,
// },
// {
// id: 2,
// song: "To jest ta miłość",
// artist: "O.S.T.R.",
// cover: "https://www.gloskultury.pl/wp-content/uploads/2017/12/ostry.jpg",
// audio: "/music/OSTR2.mp3",
// fav: true,
// },
// {
// id: 3,
// song: "Na Szczycie Blend",
// artist: "SzUsty Blend",
// cover: "https://i1.sndcdn.com/artworks-000554516715-g2a53s-t500x500.jpg",
// audio: "/music/Quebo.mp3",
// fav: true,
// },
// {
// id: 4,
// song: "Panamericana",
// artist: "Eldo",
// cover:
// "https://images.genius.com/40f956cb7b2fe1cabe03005755474b4d.803x800x1.jpg",
// audio: "/music/Eldo1.mp3",
// fav: false,
// },
];
const allSongs = [
{
id: 1,
song: "Mówiłaś Mi",
artist: "O.S.T.R.",
cover:
"https://images.genius.com/95158566e73ea4ee02dd2b7b24463d8d.500x500x1.jpg",
audio: "/music/OSTR.mp3",
fav: true,
},
{
id: 2,
song: "To jest ta miłość",
artist: "O.S.T.R.",
cover: "https://www.gloskultury.pl/wp-content/uploads/2017/12/ostry.jpg",
audio: "/music/OSTR2.mp3",
fav: true,
},
{
id: 3,
song: "Na Szczycie Blend",
artist: "SzUsty Blend",
cover: "https://i1.sndcdn.com/artworks-000554516715-g2a53s-t500x500.jpg",
audio: "/music/Quebo.mp3",
fav: true,
},
{
id: 4,
song: "Panamericana",
artist: "Eldo",
cover:
"https://images.genius.com/40f956cb7b2fe1cabe03005755474b4d.803x800x1.jpg",
audio: "/music/Eldo1.mp3",
fav: true,
},
];
let songID = 1;
let songIndex;
let songDataArray = [];
let favSongsData = [];
let arrayPosition = 0;
data = allSongs;
getSongDataArray();
function updateFavSongsList() {
let filter = allSongs.filter((x) => x.fav == true);
favSongsData = filter;
}
updateFavSongsList();
const createPlaylist = function () {
let songPlaylistIndex = 1;
data.forEach((song) => {
const newLi = document.createElement("li");
newLi.classList.add("playlist__list_element");
newLi.setAttribute("data-id", song.id);
playlist.appendChild(newLi);
newLi.innerHTML = `${songPlaylistIndex++}. ${song.artist} - ${song.song} ${
song.songDuration
? `<span class='songtime'>${song.songDuration}</span>`
: `<span class='songtime'></span>`
}`;
newLi.addEventListener("click", (e) => {
songID = e.target.getAttribute("data-id");
loadSong(Number(songID));
// "e", e.target.getAttribute("data-id");
const isPlaying = play.classList.contains("active");
if (isPlaying) {
playAudio();
}
});
});
};
// time converter seconds to MMSS
const currentTimeMMSS = function (seconds) {
let sec = 0;
let min = 0;
sec = Math.floor(seconds) % 60;
min = parseInt(Math.floor(seconds) / 60);
if (sec.toString().length == 1) sec = "0" + sec;
if (min <= 9) min = "0" + min;
return `${min}:${sec}`;
};
// OPTIONS
let repeatFlag = false;
let shuffleFlag = false;
const repeatBtn = document.querySelector("#repeat");
const shuffleBtn = document.querySelector("#shuffle");
const songTitle = document.getElementById("title");
const artistName = document.getElementById("artist");
const cover = document.getElementById("cover");
const play = document.getElementById("play");
const pause = document.getElementById("pause");
const next = document.getElementById("next");
const previous = document.getElementById("previous");
const currentTimeEl = document.getElementById("current");
const songDurationEl = document.getElementById("duration");
const progressBar = document.getElementById("progress");
const sliderDuration = document.getElementById("slider-duration");
const audio = document.getElementById("audio");
const player = document.getElementById("player");
const playlist = document.getElementById("playlist__list");
const volumeMinEl = document.getElementById("volume-min");
const volumeMaxEl = document.getElementById("volume-max");
//SONGS CHANGE
const albumCover = document.getElementById("cover");
const currentTimeCon = Math.floor(audio.currentTime);
const songDurationCon = Math.floor(audio.duration);
const volumeSlider = document.getElementById("volume-slider");
const currentVolumeSlider = document.getElementById("current-volume");
// FAV
const toggleFavPlaylist = document.getElementById("toggle-fav");
toggleFavPlaylist.innerText = "Fav Songs";
let showFav = false;
toggleFavPlaylist.addEventListener("click", () => {
showFav = !showFav;
toggleFavPlaylist.classList.toggle("toggle-active");
playlist.parentElement.classList.toggle("favstyle");
if (showFav == false) {
data = allSongs;
getSongDataArray();
toggleFavPlaylist.innerText = "Fav Songs";
} else {
data = favSongsData;
getSongDataArray();
toggleFavPlaylist.innerText = "All Songs";
}
// clear playlist
playlist.innerHTML = ``;
createPlaylist();
});
const favParent = document.querySelector(".options");
favParent.classList.add("options__like");
const favIcon = document.createElement("span");
favIcon.classList.add("fav");
favIcon.addEventListener("click", (e) => {
data[songIndex].fav = !data[songIndex].fav;
if (data[songIndex].fav == true) {
favIcon.classList.add("fav-active");
} else {
favIcon.classList.remove("fav-active");
}
updateFavSongsList();
playlistFilter();
playlist.innerHTML = ``;
createPlaylist();
});
// TIMERS
let timeMMSS = 0;
//LOAD SONG
const loadSong = function (songID) {
// find index of loaded song in html li element
songIndex = data
.map((e) => {
return e.id;
})
.indexOf(songID);
// if index is correct - set audio
if (songIndex >= 0) {
audio.src = data[songIndex].audio;
}
let currentPlaylistSong = document.querySelectorAll(
".playlist__list_element"
);
// check if fav and show icon
favSongs(data[songIndex]?.fav);
// highlight current song on playlist
const prevCurrentPlaylistSong = document.querySelector(".playlist-active");
prevCurrentPlaylistSong?.classList.remove("playlist-active");
currentPlaylistSong[songIndex]?.classList.add("playlist-active");
//LOAD DATA
albumCover.style.background = `url(\"${data[songIndex].cover}\")`;
albumCover.style.backgroundSize = "cover";
document.body.style.background = `url(\"${data[songIndex].cover}\")`;
document.body.style.backgroundSize = "cover";
document.body.style.background = `url(\"${data[songIndex].cover}\")`;
document.body.style.backgroundSize = "cover";
songTitle.innerHTML = data[songIndex].song;
artistName.innerHTML = data[songIndex].artist;
audio.addEventListener("canplay", function abc() {
songDurationEl.innerHTML = currentTimeMMSS(audio.duration);
data[songIndex].songDuration = currentTimeMMSS(audio.duration);
document.querySelectorAll(".songtime")[songIndex].innerHTML =
currentTimeMMSS(audio.duration);
audio.removeEventListener("canplay", abc);
});
//current time AND SONG DURATION
audio.addEventListener("timeupdate", () => {
// SHOW FUNCTION
progressBar.style.width = `${(audio.currentTime / audio.duration) * 100}%`;
currentTimeEl.innerHTML = currentTimeMMSS(audio.currentTime);
});
sliderDuration.addEventListener("click", (e) => {
let sliderPositionX = e.offsetX;
let sliderPositionXValue =
(sliderPositionX / sliderDuration.offsetWidth) * audio.duration;
audio.currentTime = sliderPositionXValue;
});
};
// PLAY
const playAudio = function () {
audio.play();
// VOLUME SLIDER
volumeSlider.addEventListener("click", (e) => {
let volumeSliderPositionX = e.offsetX;
let volumeSliderPositionXValue =
(volumeSliderPositionX / volumeSlider.offsetWidth) * 1;
audio.volume = volumeSliderPositionXValue;
currentVolumeSlider.style.width = `${volumeSliderPositionXValue * 100}%`;
volumeMaxEl.addEventListener("click", () => {
audio.volume = 1;
currentVolumeSlider.style.width = "100%";
volumeMaxEl.style.color = "white";
volumeMinEl.style.color = "black";
});
volumeMinEl.addEventListener("click", () => {
audio.volume = 0;
currentVolumeSlider.style.width = "0";
volumeMinEl.style.color = "white";
volumeMaxEl.style.color = "black";
});
audio.volume < 0.02
? (volumeMinEl.style.color = "white")
: (volumeMinEl.style.color = "black");
audio.volume > 0.98
? (volumeMaxEl.style.color = "white")
: (volumeMaxEl.style.color = "black");
});
};
// KEEP PLAYING AFTER SONGS END
audio.onended = function () {
if (repeatFlag == true) {
playAudio();
} else {
functionNext();
loadSong(songID);
const isPlaying = play.classList.contains("active");
if (isPlaying) {
setTimeout(() => {
playAudio();
}, 500);
}
}
};
const pauseAudio = function () {
audio.pause();
};
const stopAudio = function () {
audio.pause();
};
// PLAY CLICKED
play.addEventListener("click", () => {
play.classList.add("active");
pause.classList.remove("active");
cover.style.animationPlayState = "running";
playAudio();
});
// PAUSE CLICKED
pause.addEventListener("click", () => {
play.classList.remove("active");
pause.classList.add("active");
cover.style.animationPlayState = "paused";
pauseAudio();
});
// FORWARD CLICKED
next.addEventListener("click", () => {
functionNext();
loadSong(songID);
const isPlaying = play.classList.contains("active");
if (isPlaying) {
playAudio();
}
});
// PREVIOUS CLICKED
previous.addEventListener("click", () => {
functionPrvious();
loadSong(songID);
const isPlaying = play.classList.contains("active");
if (isPlaying) {
playAudio();
}
});
// INCREMENT SONG INDEX
function functionNext() {
arrayPosition++;
if (arrayPosition >= songDataArray.length) {
arrayPosition = 0;
}
songID = songDataArray[arrayPosition];
if (shuffleFlag == true) {
songID = randomNumber(songDataArray.length, songID);
}
}
// DECREMENT SONG INDEX
function functionPrvious() {
arrayPosition--;
if (arrayPosition < 0) {
arrayPosition = songDataArray.length - 1;
}
songID = songDataArray[arrayPosition];
if (shuffleFlag == true) {
songID = randomNumber(songDataArray.length, songID);
}
}
// FUNCTIONS ON START
loadSong(songID);
createPlaylist();
// REPEAT BUTTON
repeatBtn.addEventListener("click", () => {
if (repeatFlag == true) {
repeatFlag = false;
repeatBtn.classList.remove("option-active");
} else {
repeatBtn.classList.add("option-active");
repeatFlag = true;
}
// repeatFlag ? (repeatFlag = false) : (repeatFlag = true);
});
// SHUFFLE BUTTON
shuffleBtn.addEventListener("click", () => {
if (shuffleFlag == true) {
shuffleFlag = false;
shuffleBtn.classList.remove("option-active");
} else {
shuffleBtn.classList.add("option-active");
shuffleFlag = true;
}
});
// random number different than previous
function randomNumber(max, index) {
let prevNumber = index;
let number = Math.floor(Math.random() * max + 1);
if (prevNumber == number) {
return randomNumber(max, index);
} else return number;
}
// update data - fav songs/ all songs
function playlistFilter() {
if (showFav == false) {
data = allSongs;
getSongDataArray();
toggleFavPlaylist.innerText = "Fav Songs";
} else {
data = favSongsData;
getSongDataArray();
toggleFavPlaylist.innerText = "All Songs";
}
}
// get ID's of songs in data array
function getSongDataArray() {
songDataArray = [];
data.forEach((song) => {
songDataArray.push(song.id);
});
}
// check if songs is in fav playlist
function favSongs(isFav) {
const iconEmpty = `<svg width='2rem' height="2rem" viewBox="0 0 512 512"><path xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="64" d="M 352.92 80 C 288 80 256 144 256 144 s -32 -64 -96.92 -64 c -52.76 0 -94.54 44.14 -95.08 96.81 c -1.1 109.33 86.73 187.08 183 252.42 a 16 16 0 0 0 18 0 c 96.26 -65.34 184.09 -143.09 183 -252.42 c -0.54 -52.67 -42.32 -96.81 -95.08 -96.81 Z" /></svg>`;
if (isFav == true) {
favIcon.innerHTML = iconEmpty;
favIcon.classList.add("fav-active");
favParent.appendChild(favIcon);
} else {
favIcon.innerHTML = iconEmpty;
favIcon.classList.remove("fav-active");
favParent.appendChild(favIcon);
}
}
// update data - fav songs/ all songs
function playlistFilter() {
if (showFav == false) {
data = allSongs;
getSongDataArray();
toggleFavPlaylist.innerText = "Fav Songs";
} else {
data = favSongsData;
getSongDataArray();
toggleFavPlaylist.innerText = "All Songs";
}
}