-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent.js
101 lines (92 loc) · 3.93 KB
/
advent.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
const calendarButton = document.querySelector(".btn-start");
const calendarContainer = document.querySelector(".container");
const calendarDays = 25;
const doorsOpened = JSON.parse(localStorage.getItem("doorsOpened")) || []
const setVideo = (i, youtubeURL, isAfterToday) => {
const calendarDay = document.createElement("div");
const calendarDayText = document.createElement("div");
calendarDay.classList.add("image");
calendarDay.style.gridArea = "day" + (i + 1);
calendarContainer.appendChild(calendarDay);
calendarDayText.classList.add("text");
calendarDayText.innerHTML = i + 1;
calendarDay.appendChild(calendarDayText);
const iframeElement = document.createElement('iframe');
iframeElement.width = "238";
iframeElement.height = "218";
iframeElement.src = youtubeURL;
iframeElement.title = "YouTube video player";
iframeElement.frameborder = "0";
iframeElement.allow = "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture";
iframeElement.allowfullscreen = true;
iframeElement.classList.add('video');
if (doorsOpened.includes(i)) {
calendarDayText.parentNode.replaceChildren(iframeElement);
}
if (!isAfterToday) {
calendarDayText.addEventListener("click", (event) => {
doorsOpened.push(i)
localStorage.setItem('doorsOpened', JSON.stringify(doorsOpened))
event.target.parentNode.replaceChildren(iframeElement);
});
}
}
const createCalendar = () => {
const todayDate = document.getElementById('start').value;
for (let i = 0; i < calendarDays; i++) {
const date = `2022-12-${String(i + 1).padStart(2, '0')}`
const isAfterToday = todayDate < date
if (i === 15) {
setVideo(i, 'https://www.youtube.com/embed/G9d_L6JnkSQ', isAfterToday)
}
else if (i === 2) {
setVideo(i, 'https://www.youtube.com/embed/-y6t1N8qTIU', isAfterToday)
}
else if (i === 6) {
setVideo(i, "https://www.youtube.com/embed/jWRbbHe1ryU", isAfterToday)
}
else if (i === 8) {
setVideo(i, "https://www.youtube.com/embed/f2UnLAU1x_I", isAfterToday)
}
else if (i === 11) {
setVideo(i, "https://www.youtube.com/embed/H2ifOP25wLs", isAfterToday)
}
else if (i === 13) {
setVideo(i, "https://www.youtube.com/embed/ZtjKa72uq9k", isAfterToday)
}
else if (i === 21) {
setVideo(i, "https://www.youtube.com/embed/Oq3gi9IUkws", isAfterToday)
}
else if (i === 24) {
setVideo(i, "https://www.youtube.com/embed/feYR2StoV3s", isAfterToday)
} else {
const calendarDay = document.createElement("div");
const calendarDayText = document.createElement("div");
calendarDay.classList.add("image");
calendarDay.style.gridArea = "day" + (i + 1);
calendarContainer.appendChild(calendarDay);
calendarDayText.classList.add("text");
calendarDayText.innerHTML = i + 1;
calendarDay.appendChild(calendarDayText);
const imageNumber = i + 1;
const imagePath = `./images/image${imageNumber}.jpg`;
const openDoor = (element) => {
element.parentNode.style.backgroundImage = `url(${imagePath})`;
element.style.opacity = "0";
element.style.backgroundColor = "#521751";
}
if (doorsOpened.includes(i)) {
openDoor(calendarDayText)
}
if (!isAfterToday) {
calendarDayText.addEventListener("click", (event) => {
doorsOpened.push(i)
localStorage.setItem('doorsOpened', JSON.stringify(doorsOpened))
openDoor(event.target)
}
);
}
}
}
}
calendarButton.addEventListener("click", createCalendar);