Skip to content

Commit f860f1d

Browse files
committed
refactor(player): move repeated actions to functions
1 parent 11bee87 commit f860f1d

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

static/js/player.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const playPauseToggleEl = document.getElementById("play"),
3030
songCurrentTimeEl = document.getElementById("song-current-time"),
3131
songImageEl = document.getElementById("song-image"),
3232
loadingEl = document.getElementById("loading"),
33-
audioPlayerEl = document.getElementById("audio-player");
33+
audioPlayerEl = document.getElementById("audio-player"),
34+
muzikkContainerEl = document.getElementById("muzikk");
3435

3536
let currentLoopIdx = 0;
3637

@@ -71,9 +72,7 @@ function setMediaSession(videoData) {
7172
});
7273

7374
navigator.mediaSession.setActionHandler("stop", () => {
74-
audioPlayerEl.pause();
75-
audioPlayerEl.currentTime = 0;
76-
document.getElementById("play").innerHTML = playerButtonsIcons.play;
75+
stopMuzikk();
7776
});
7877

7978
navigator.mediaSession.setActionHandler("seekbackward", () => {
@@ -106,18 +105,31 @@ function setMediaSession(videoData) {
106105
});
107106
}
108107

108+
function playMuzikk() {
109+
audioPlayerEl.play();
110+
playPauseToggleEl.innerHTML = playerButtonsIcons.pause;
111+
}
112+
113+
function pauseMuzikk() {
114+
audioPlayerEl.pause();
115+
playPauseToggleEl.innerHTML = playerButtonsIcons.play;
116+
}
117+
118+
function stopMuzikk() {
119+
pauseMuzikk();
120+
audioPlayerEl.currentTime = 0;
121+
}
122+
109123
function playPauseToggle() {
110124
if (audioPlayerEl.paused) {
111-
audioPlayerEl.play();
112-
document.getElementById("play").innerHTML = playerButtonsIcons.pause;
125+
playMuzikk();
113126
} else {
114-
audioPlayerEl.pause();
115-
document.getElementById("play").innerHTML = playerButtonsIcons.play;
127+
pauseMuzikk();
116128
}
117129
}
118130

119131
async function fetchMusic(youtubeId) {
120-
document.getElementById("play").innerHTML = playerButtonsIcons.loading;
132+
playPauseToggleEl.innerHTML = playerButtonsIcons.loading;
121133
document.body.style.cursor = "progress";
122134
Utils.toggleLoading();
123135

@@ -126,10 +138,8 @@ async function fetchMusic(youtubeId) {
126138
.catch((err) => console.error(err));
127139

128140
if (audioPlayerEl) {
129-
audioPlayerEl.pause();
130-
audioPlayerEl.currentTime = 0;
141+
stopMuzikk();
131142
}
132-
document.getElementById("muzikk").style.display = "block";
133143
audioPlayerEl.src = `/music/${youtubeId}.mp3`;
134144
audioPlayerEl.load();
135145
}
@@ -138,6 +148,7 @@ async function playYTSongById(id, thumbnailUrl, title, artist) {
138148
const videoData = { id, thumbnailUrl, title, artist };
139149
await fetchMusic(videoData.id);
140150
setMediaSession(videoData);
151+
showPlayer();
141152

142153
if (videoData.title) {
143154
songNameEl.innerHTML = videoData.title;
@@ -158,10 +169,19 @@ async function playYTSongById(id, thumbnailUrl, title, artist) {
158169
}
159170
}
160171

161-
audioPlayerEl.play();
172+
playMuzikk();
162173
songImageEl.style.backgroundImage = `url("${videoData.thumbnailUrl}")`;
163174
}
164175

176+
function showPlayer() {
177+
muzikkContainerEl.style.display = "block";
178+
}
179+
180+
function hidePlayer() {
181+
muzikkContainerEl.style.display = "none";
182+
audioPlayerEl.stopMuzikk();
183+
}
184+
165185
loopEl.addEventListener("click", (event) => {
166186
currentLoopIdx = (currentLoopIdx + 1) % loopModes.length;
167187
event.target.src = "/static/images/" + loopModes[currentLoopIdx].icon;
@@ -196,10 +216,9 @@ audioPlayerEl.addEventListener("loadeddata", (event) => {
196216
songDurationEl.innerHTML = Utils.formatTime(duration);
197217
}
198218

199-
document.getElementById("play").innerHTML = playerButtonsIcons.pause;
219+
playPauseToggleEl.innerHTML = playerButtonsIcons.pause;
200220
document.body.style.cursor = "auto";
201221
Utils.toggleLoading();
202-
// duration = a.duration;
203222
});
204223

205224
audioPlayerEl.addEventListener("timeupdate", (event) => {
@@ -215,13 +234,11 @@ audioPlayerEl.addEventListener("timeupdate", (event) => {
215234
audioPlayerEl.addEventListener("ended", () => {
216235
switch (loopModes[currentLoopIdx].mode) {
217236
case "OFF":
218-
document.getElementById("play").innerHTML = playerButtonsIcons.play;
219-
audioPlayerEl.currentTime = 0;
237+
stopMuzikk();
220238
break;
221239
case "ONCE":
222-
document.getElementById("play").innerHTML = playerButtonsIcons.pause;
223-
audioPlayerEl.currentTime = 0;
224-
audioPlayerEl.play();
240+
stopMuzikk();
241+
playMuzikk();
225242
break;
226243
case "ALL":
227244
break;
@@ -234,4 +251,6 @@ audioPlayerEl.addEventListener("progress", () => {
234251

235252
window.Player = {
236253
playYTSongById,
254+
showPlayer,
255+
hidePlayer,
237256
};

0 commit comments

Comments
 (0)