-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
156 lines (129 loc) · 4.38 KB
/
main.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
import data from "./src/data";
// DOM elements
const replayBtn = document.getElementById("btn-replay");
const prevBtn = document.getElementById("btn-prev");
const playBtn = document.getElementById("btn-play");
const nextBtn = document.getElementById("btn-next");
const stopBtn = document.getElementById("btn-stop");
const audioElement = document.getElementById("player-audio");
const creditsButton = document.getElementById("credits-btn");
// Array of audio files' directories
const songsArray = data.map((item) => {
return item.trackPath;
});
const songsCount = songsArray.length;
// set initial song when document loads
let currentSongIndex = 0;
setCurrentSong(currentSongIndex);
let isPlaying = false;
// Event listeners for player control btns
prevBtn.addEventListener("click", setPrevSong);
playBtn.addEventListener("click", switchSongOnOff);
nextBtn.addEventListener("click", setNextSong);
stopBtn.addEventListener("click", stopSong);
replayBtn.addEventListener("click", replaySong);
creditsButton.addEventListener("click", toggleCredits);
// Functions
function switchSongOnOff() {
isPlaying = !isPlaying;
if (isPlaying) {
audioElement.play();
playBtn.src = "src/assets/icons/btn-pause.svg";
} else {
audioElement.pause();
playBtn.src = "src/assets/icons/btn-play.svg";
}
}
function setNextSong() {
currentSongIndex++;
// when last song reached, go to first song
if (currentSongIndex > songsCount - 1) {
currentSongIndex = 0;
}
setCurrentSong(currentSongIndex);
isPlaying = false;
switchSongOnOff();
}
function setPrevSong() {
currentSongIndex--;
// when first song reached, go to last song
if (currentSongIndex < 0) {
currentSongIndex = songsCount - 1;
}
setCurrentSong(currentSongIndex);
isPlaying = false;
switchSongOnOff();
}
function setCurrentSong(index) {
audioElement.src = "src/assets/" + songsArray[index];
setSongInfo(index);
setImage(index);
getCredits(index);
}
function stopSong() {
audioElement.load();
isPlaying = false;
playBtn.src = "src/assets/icons/btn-play.svg";
}
function replaySong() {
audioElement.load();
isPlaying = false;
switchSongOnOff();
}
function setSongInfo(index) {
const songTitleElement = document.getElementById("song-title");
const songAuthorElement = document.getElementById("song-author");
songTitleElement.textContent = data[index].title;
songAuthorElement.textContent = data[index].author;
}
function setCredits(...elements) {
const creditElements = elements;
const container = document.getElementById("song-credits");
container.innerHTML = "";
creditElements.forEach((element) => {
const p = document.createElement("p");
p.innerHTML = element;
container.appendChild(p);
});
}
function getCredits(index) {
// destructure source data
const {
title: songTitle,
author: songAuthor,
source: songSource,
license: songLicense,
img: {
author: { name: imgAuthor, link: imgAuthorLink },
source: imgSource,
link: imgLink,
},
} = data[index];
// prepare DOM element
const musicInfo = `<p>"${songTitle}" ${songAuthor} (${songSource})</p><p>${songLicense}</p>`;
const imgInfo = `Photo by <a href="${imgAuthorLink}">${imgAuthor}</a> on <a href="${imgLink}">${imgSource}</a>`;
setCredits(musicInfo, imgInfo);
}
function toggleCredits() {
const container = document.getElementById("song-credits");
container.classList.toggle("player__credits-container--active");
}
function setImage(index) {
const prevImg = data[index === 0 ? data.length - 1 : index - 1].img.imgPath;
const currentImg = data[index].img.imgPath;
const nextImg = data[index === data.length - 1 ? 0 : index + 1].img.imgPath;
const container = document.getElementById("player-images");
const prevImgElement = document.createElement("img");
prevImgElement.src = "src/assets/" + prevImg;
prevImgElement.classList.add("player__image-prev");
const currentImgElement = document.createElement("img");
currentImgElement.src = "src/assets/" + currentImg;
currentImgElement.classList.add("player__image-current");
const nextImgElement = document.createElement("img");
nextImgElement.src = "src/assets/" + nextImg;
nextImgElement.classList.add("player__image-next");
container.innerHTML = "";
container.appendChild(prevImgElement);
container.appendChild(currentImgElement);
container.appendChild(nextImgElement);
}