forked from Ctoic/Lisbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
178 lines (155 loc) · 5.9 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
document.addEventListener("DOMContentLoaded", function () {
const playlistItems = document.querySelectorAll("#playlist li");
const audioPlayer = document.getElementById("audio-player");
const themeToggle = document.getElementById("theme-toggle");
const menuToggle = document.getElementById("menu-toggle");
const menuClose = document.getElementById("menu-close");
const menu = document.getElementById("menu");
const bookList = document.getElementById("audio-books-list");
// Function to display error popup
function showErrorPopup(message) {
const overlay = document.createElement("div");
overlay.id = "overlay";
overlay.style.position = "fixed";
overlay.style.top = "0";
overlay.style.left = "0";
overlay.style.width = "100%";
overlay.style.height = "100%";
overlay.style.backgroundColor = "rgba(0, 0, 0, 0.7)";
overlay.style.zIndex = "9998";
overlay.style.backdropFilter = "blur(5px)";
document.body.appendChild(overlay);
// Create the error popup
const errorPopup = document.createElement("div");
errorPopup.id = "errorPopup";
errorPopup.style.position = "fixed";
errorPopup.style.top = "50%";
errorPopup.style.left = "50%";
errorPopup.style.transform = "translate(-50%, -50%)";
errorPopup.style.backgroundColor = "#fff";
errorPopup.style.padding = "20px";
errorPopup.style.border = "1px solid #ccc";
errorPopup.style.zIndex = "9999";
errorPopup.innerHTML = `
<h2 style="color: red; font-weight: 600; margin-bottom: 5px;">Error</h2>
<p>${message}</p>
<button style="margin-top: 10px; padding: 5px 10px; float: right;">Close</button>
`;
document.body.appendChild(errorPopup);
// Attach event listener to the close button directly via DOM element
const closeButton = errorPopup.querySelector("button");
closeButton.addEventListener("click", () => {
errorPopup.remove();
overlay.remove();
});
}
// Playlist item click event
playlistItems.forEach((item) => {
item.addEventListener("click", function () {
const audioSource = item.getAttribute("data-src");
// Check if the file exists before playing
if (audioSource) {
audioPlayer.src = audioSource;
// Handle playback errors
audioPlayer.play().catch((error) => {
console.error("Audio playback error:", error);
if (error.name === "NotSupportedError") {
showErrorPopup("Failed to play audio. Unsupported format or file missing.");
} else {
showErrorPopup("Failed to play audio. There was an issue with playback.");
}
});
} else {
showErrorPopup("Audio source not available.");
}
});
});
//Keyboard Shortcuts buttons
document.addEventListener("keydown", function (e) {
switch (e.code) {
case "Space": // Play / Pause
e.preventDefault(); // Prevents page scrolling when Space is pressed
if (audioPlayer.paused) {
audioPlayer.play();
} else {
audioPlayer.pause();
}
break;
case "ArrowRight": // Jump forward 30 seconds
audioPlayer.currentTime += 30;
break;
case "ArrowLeft": // Go back 30 seconds
audioPlayer.currentTime -= 30;
break;
case "Equal": // Increase volume
if (audioPlayer.volume < 1) {
audioPlayer.volume = Math.min(audioPlayer.volume + 0.1, 1);
}
break;
case "NumpadAdd": // Augmenter le volume avec le pavé numérique
if (audioPlayer.volume < 1) {
audioPlayer.volume = Math.min(audioPlayer.volume + 0.1, 1);
}
break;
case "Minus": // Reduce volume
if (audioPlayer.volume > 0) {
audioPlayer.volume = Math.max(audioPlayer.volume - 0.1, 0);
}
break;
case "NumpadSubtract": // Diminuer le volume avec le pavé numérique
if (audioPlayer.volume > 0) {
audioPlayer.volume = Math.max(audioPlayer.volume - 0.1, 0);
}
break;
default:
break;
}
});
// Comment Submission
document.getElementById("comment-form").addEventListener("submit", function (e) {
e.preventDefault();
const username = document.getElementById("username").value;
const comment = document.getElementById("comment").value;
const commentHTML = `<div class="bg-gray-700 text-white p-4 rounded-lg">
<strong>${username}:</strong>
<p>${comment}</p>
</div>`;
document.getElementById("comments-list").insertAdjacentHTML("beforeend", commentHTML);
// Reset form
document.getElementById("comment-form").reset();
});
// Theme Toggle
themeToggle.addEventListener("click", function () {
if (document.body.classList.contains("dark-theme")) {
document.body.classList.remove("dark-theme");
document.body.classList.add("light-theme");
console.log("light");
} else {
document.body.classList.remove("light-theme");
document.body.classList.add("dark-theme");
console.log("dark");
}
});
//Mobile menu toggle
menuToggle.addEventListener("click", () => {
menu.classList.remove("scale-0");
menu.classList.add("scale-100");
});
menuClose.addEventListener("click", () => {
menu.classList.add("scale-0");
menu.classList.remove("scale-100");
});
fetch('/data/books.json')
.then(response=>response.json())
.then(response=>{
const template = document.getElementById("book-card-template");
response.forEach(book=>{
const element = document.importNode(template.content, true);
element.getElementById("book-title").textContent = book.title;
element.getElementById("book-author").textContent = `By ${book.author}`;
element.getElementById("img").src = `https://picsum.photos/200`;
element.getElementById("img").alt = `Cover of ${book.title}`;
bookList.appendChild(element);
})
})
});