-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
353 lines (332 loc) · 11.2 KB
/
index.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
const VIDEO_ID = "TtY9eRayseg";
// Const Songs
const songs = {
"Christmas Song": {
start: "0",
end: "211",
},
"Baby It's Cold Outside": {
start: "212",
end: "373",
},
"The Most Wonderful Time Of The Year": {
start: "374",
end: "524",
},
"Last Christmas": {
start: "525",
end: "807",
},
"Little Saint Nick": {
start: "808",
end: "939",
},
"All I Want For Christmas": {
start: "940",
end: "1179",
},
"All Of The Above": {
start: "0",
},
};
const songsDOM = document.querySelector(".songs");
const embed = document.getElementById("embed");
const newTabGithub = document.getElementById('grey');
const toggle = document.getElementById("round");
const slider = document.querySelector(".slider");
const audioControls = document.querySelector(".audio-controls");
const loader = document.querySelector("#loader");
const stopButton = document.querySelector("#stop-btn");
const playButton = document.querySelector("#play-btn");
const pauseButton = document.querySelector("#pause-btn");
const volumeBar = document.querySelector("#volume-bar");
const shuffleButton = document.querySelector("#shuffle-btn");
const loopButton = document.querySelector("#repeat-btn");
const canvasBody = document.getElementById("canvasBody");
const canvasHead = document.getElementById("canvasHead");
const navB = document.getElementById("navB");
const switchh = document.getElementById("switch");
let christmasCountdown = document.getElementById("christmasDayCountdown");
const body = document.getElementById("body")
let player;
let currSong;
let songList = [];
let playedNext = true;
let intervalId;
toggle.addEventListener("click", modeSwitch);
// toggle1.addEventListener("click", modeSwitch);
// Darkmode/Lightmode + Making songs play when clicked
let isLight = localStorage.getItem("darkmode") === "false";
let loopState = localStorage.getItem("loop") === "true";
let shuffleState = localStorage.getItem("shuffle") === "true";
// Open GitHub repo in a new window if user clicks GitHub icon on project website
newTabGithub.addEventListener("click", () => {
window.open(
"https://github.com/KendallDoesCoding/mogul-christmas",
"_blank",
"resizable=yes, scroll=yes, location=1, titlebar=yes, width=800, height=900, top=10, left=10"
);
});
document.getElementById('closed-menu-icon').addEventListener('click', displayMenu)
document.getElementById('open-menu-icon').addEventListener('click', closeMenu)
const updateMode = () => {
const rootElement = document.body;
if (!isLight) {
canvasBody.style.backgroundColor = "#F8F6EE";
canvasHead.style.backgroundColor = "#F8F6EE";
body.style.backgroundColor = "#F8F6EE";
// navB.style.backgroundColor = "#F8F6EE";
document.getElementById('line-1').style.stroke = "#272B2E";
document.getElementById('line-2').style.stroke = "#272B2E";
document.getElementById('line-3').style.stroke = "#272B2E";
document.getElementById('line-4').style.stroke = "#272B2E";
document.getElementById('line-5').style.stroke = "#272B2E";
rootElement.classList.remove("darkMode");
} else {
canvasBody.style.backgroundColor = "#272B2E";
canvasHead.style.backgroundColor = "#272B2E";
body.style.backgroundColor = "#272B2E";
// navB.style.backgroundColor = "#272B2E";
document.getElementById('line-1').style.stroke = "#F8F6EE";
document.getElementById('line-2').style.stroke = "#F8F6EE";
document.getElementById('line-3').style.stroke = "#F8F6EE";
document.getElementById('line-4').style.stroke = "#F8F6EE";
document.getElementById('line-5').style.stroke = "#F8F6EE";
rootElement.classList.add("darkMode");
}
};
window.addEventListener("load", updateMode)
function modeSwitch() {
isLight = !isLight;
updateMode();
localStorage.setItem("darkmode", !isLight);
}
// This function will execute on initial load of page
window.onload = function () {
updateMode();
updateLoop();
updateShuffle();
updateVolume();
audioControls.classList.remove("hidden");
loader.classList.add("hidden");
volumeBar.addEventListener("change", ({ target }) => {
player?.setVolume(target.value);
localStorage.setItem("volume", target.value);
});
};
function onYouTubeIframeAPIReady() {
player = new YT.Player("embed", {
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
},
});
}
function onPlayerReady(event) {
updateVolume();
}
function onPlayerStateChange(event) {
if (player.getPlayerState() === 0) {
if (loopState) player?.seekTo(songs[currSong]?.start || 0);
else {
if (shuffleState) {
if (!playedNext) {
playedNext = true;
player.loadVideoById({
videoId: VIDEO_ID,
startSeconds: songs[currSong].start,
endSeconds: songs[currSong].end,
});
} else {
let songNum = songList.findIndex((el) => el === currSong);
if (songList.length <= songNum + 1) {
stopVideo();
} else {
currSong = songList[songNum + 1];
playedNext = false;
player.loadVideoById({
videoId: VIDEO_ID,
startSeconds: songs[currSong].start,
endSeconds: songs[currSong].end,
});
}
}
} else stopVideo();
}
}
//if playing
if (player.getPlayerState() === 1) {
stopButton.classList.remove("hidden");
pauseButton.classList.remove("hidden");
playButton.classList.add("hidden");
intervalId = window.setInterval(() => {
if (player.getPlayerState() === 1) {
let secLeft =
(currSong ? songs[currSong].end : player.getDuration()) -
player.getCurrentTime();
secLeft = parseInt(secLeft);
const songBlock = document.getElementById(
`song_${(currSong ?? "All Of The Above").split(" ").join("_")}`
);
const timeEl = songBlock.childNodes[1];
timeEl.classList.add("active");
timeEl.innerHTML = `${parseInt(secLeft / 60)}:${
secLeft % 60 < 10 ? "0" + (secLeft % 60) : secLeft % 60
}`;
}
}, 500);
}
// paused
else if (player.getPlayerState() === 2) {
pauseButton.classList.add("hidden");
playButton.classList.remove("hidden");
} else {
stopButton.classList.add("hidden");
pauseButton.classList.add("hidden");
playButton.classList.remove("hidden");
}
// if buffering
if (player.getPlayerState() === 3) {
loader.classList.remove("hidden");
} else {
loader.classList.add("hidden");
}
}
embed.style = "display:none";
Object.keys(songs).map((song_title) => {
const startTime = songs[song_title].start;
const endTime = songs[song_title].end;
const outerElem = document.createElement("p");
outerElem.id = `song_${song_title.split(" ").join("_")}`;
outerElem.className = "song_item";
const link = document.createElement("a");
link.innerHTML = song_title;
link.style = "cursor: pointer";
link.onclick = () => {
clearTimeLeft();
player.loadVideoById({
videoId: VIDEO_ID,
startSeconds: startTime,
endSeconds: endTime,
});
currSong = song_title === "All Of The Above" ? undefined : song_title;
console.log(
"If you don't know this song, we suggest you go to the lyrics page. You can play the song from that page too :)"
);
};
outerElem.appendChild(link);
const timeLeft = document.createElement("span");
timeLeft.className = "time-left";
outerElem.appendChild(timeLeft);
songsDOM.appendChild(outerElem);
});
function playSongs() {
clearTimeLeft();
if (player.isMuted() || player.getPlayerState() === 2) {
player.playVideo();
} else {
if (songList.length) {
if (!currSong) currSong = songList[0];
player.loadVideoById({
videoId: VIDEO_ID,
startSeconds: songs[currSong].start,
endSeconds: songs[currSong].end,
});
} else {
player.loadVideoById({ videoId: VIDEO_ID });
}
}
}
function pauseSong() {
player.pauseVideo();
}
// randomly shuffle a song from main page's songs
function shuffleSongsList() {
let allSongs = { ...songs };
delete allSongs["All Of The Above"];
songList = Object.keys(allSongs).sort(() => Math.random() - 0.5);
console.log("songList", songList);
}
//stop button function
function stopVideo() {
songList = [];
currSong = undefined;
player.stopVideo();
clearTimeLeft();
}
//loop song code
//toggle loop button effect
function toggleLoop() {
loopState = !loopState;
localStorage.setItem("loop", loopState);
if (loopState === false) {
loopButton.classList.remove("active");
songList = [];
} else if (loopState === true) {
loopButton.classList.add("active");
}
}
function updateLoop() {
// Check the current state of the loop
if (loopState === false) {
// If it's off, remove the 'active' class from the loop button
loopButton.classList.remove("active");
} else if (loopState === true) {
// If it's on, add the 'active' class to the loop button
loopButton.classList.add("active");
}
}
// Function to toggle the shuffle mode
function toggleShuffle() {
shuffleState = !shuffleState; // Toggle the shuffle state
localStorage.setItem("shuffle", shuffleState); // Save the state to local storage
// If shuffle mode is turned off, remove the active class from the shuffle button
if (shuffleState === false) {
shuffleButton.classList.remove("active");
// If shuffle mode is turned on, add the active class to the shuffle button and shuffle the songs list
} else if (shuffleState === true) {
shuffleButton.classList.add("active");
shuffleSongsList();
}
}
// Function to update the shuffle mode
function updateShuffle() {
// If shuffle mode is turned off, remove the active class from the shuffle button
if (shuffleState === false) {
shuffleButton.classList.remove("active");
// If shuffle mode is turned on, add the active class to the shuffle button and shuffle the songs list
} else if (shuffleState === true) {
shuffleButton.classList.add("active");
shuffleSongsList();
}
}
// Function to update the volume
function updateVolume() {
let volumeState = parseInt(localStorage.getItem("volume") ?? 50);
// Set the volume of the player to the volume state
player?.setVolume?.(volumeState);
// Set the value of the volume bar to the volume state
volumeBar.value = volumeState;
}
// Function to clear the time left interval and remove the active class from the time left elements
function clearTimeLeft() {
clearInterval(intervalId); // Clear the interval
const a = document.getElementsByClassName("time-left");
for (let i = 0; i < a.length; i++) {
a[i].classList.remove("active"); // Remove the active class from each element
}
}
// Function to display the menu
function displayMenu(){
document.getElementById('closed-menu-icon').style.display = 'none'
document.getElementById('open-menu-icon').style.display = 'block'
document.getElementById('menu-display').style.opacity = '1'
document.getElementById('menu-display').style.left = '0'
}
// Function to close the menu
function closeMenu(){
document.getElementById('closed-menu-icon').style.display = 'block'
document.getElementById('open-menu-icon').style.display = 'none'
document.getElementById('menu-display').style.opacity = '0'
document.getElementById('menu-display').style.left = '-25vw'
}