forked from Ctoic/Lisbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1078 lines (932 loc) · 38.7 KB
/
script.js
File metadata and controls
1078 lines (932 loc) · 38.7 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Utility function to display a custom error popup (replacing native alert)
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%)";
// Responsive styling using CSS variables defined in about.html or assumed global
errorPopup.style.backgroundColor = document.documentElement.getAttribute("data-theme") === "dark" ? "#1e1e1e" : "#fff";
errorPopup.style.color = document.documentElement.getAttribute("data-theme") === "dark" ? "#e0e0e0" : "#2c3e50";
errorPopup.style.padding = "30px";
errorPopup.style.border = "1px solid #ccc";
errorPopup.style.zIndex = "9999";
errorPopup.style.borderRadius = "12px"; // Enhanced rounded corners
errorPopup.style.textAlign = "center";
errorPopup.style.boxShadow = "0 10px 25px rgba(0, 0, 0, 0.5)";
errorPopup.style.maxWidth = "90%";
errorPopup.style.minWidth = "250px";
errorPopup.innerHTML = `
<h2 style="color: var(--color-primary); font-weight: 700; margin-bottom: 15px; font-size: 1.5rem;">Heads Up!</h2>
<p style="margin-bottom: 20px;">${message}</p>
<button class="close-btn" style="padding: 10px 20px; background-color: var(--color-primary); color: #121212; border: none; border-radius: 6px; cursor: pointer; font-weight: 600; transition: background-color 0.2s;">Got It</button>
`;
document.body.appendChild(errorPopup);
// Attach event listener to the close button
const closeButton = errorPopup.querySelector("button");
closeButton.addEventListener("click", () => {
errorPopup.remove();
overlay.remove();
});
}
// Format time from seconds to MM:SS
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secondsLeft = Math.floor(seconds % 60);
return `${minutes}:${secondsLeft < 10 ? "0" : ""}${secondsLeft}`;
}
// Function to load an HTML file into an element
function loadHTML(file, elementId) {
fetch(file)
.then((response) => {
if (!response.ok)
throw new Error("Erreur lors du chargement du fichier " + file);
return response.text();
})
.then((data) => {
const element = document.getElementById(elementId);
if (element) element.innerHTML = data;
})
.catch((error) => console.error(error));
}
// Function to start the typing animation (used for features section)
function startTyping(target, text) {
new Typed(target, {
strings: [text],
typeSpeed: 50,
backSpeed: 0,
loop: false,
showCursor: false,
});
}
// Update sun/moon icons based on active theme
function updateThemeIcons(theme) {
const sunIcon = document.getElementById("sun-icon");
const moonIcon = document.getElementById("moon-icon");
if (sunIcon && moonIcon) {
if (theme === "dark") {
sunIcon.classList.remove("hidden");
sunIcon.style.display = "inline";
moonIcon.classList.add("hidden");
moonIcon.style.display = "none";
} else {
moonIcon.classList.remove("hidden");
moonIcon.style.display = "inline";
sunIcon.classList.add("hidden");
sunIcon.style.display = "none";
}
}
}
function setTheme(newTheme) {
document.documentElement.setAttribute("data-theme", newTheme);
localStorage.setItem("theme", newTheme);
if (document.body) {
if (newTheme === "light") {
document.body.classList.add("light-theme");
document.body.classList.remove("dark-theme");
} else {
document.body.classList.add("dark-theme");
document.body.classList.remove("light-theme");
}
}
// Update the images based on the new theme
const img1 = document.getElementById("image1");
const img2 = document.getElementById("image2");
if (newTheme === "light") {
if (img1) img1.src = "https://placehold.co/800x600/f5f7fa/2c3e50?text=Lisbook+Story+Light";
if (img2) img2.src = "https://placehold.co/800x600/f5f7fa/2c3e50?text=Lisbook+Open+Source+Light";
} else {
if (img1) img1.src = "https://placehold.co/800x600/121212/a7e078?text=Lisbook+Story+Dark";
if (img2) img2.src = "https://placehold.co/800x600/121212/a7e078?text=Lisbook+Open+Source+Dark";
}
updateThemeIcons(newTheme);
// Notify any embedded iframe (gd-iframe) about theme change so it can
// apply the same theme (useful for same-origin iframes)
try {
const iframe = document.getElementById('gd-iframe');
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage({ type: 'theme', theme: newTheme }, '*');
}
} catch (e) {
// ignore cross-origin or other errors
console.warn('Could not post theme to iframe', e);
}
}
// Toggle between light and dark themes
const toggleTheme = () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
const newTheme = currentTheme === "light" ? "dark" : "light";
setTheme(newTheme);
};
// --- Contributor Auto Update Logic ---
async function fetchContributors(repoOwner, repoName) {
try {
const response = await fetch(
`https://api.github.com/repos/${repoOwner}/${repoName}/contributors`
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const contributors = await response.json();
return contributors;
} catch (error) {
console.error("Error fetching contributors:", error);
return null;
}
}
function generateContributorsHTML(contributors) {
// Check if the response is an array before mapping
if (!Array.isArray(contributors)) {
return `<p class="text-center text-danger col-span-full">Contributor data format is invalid.</p>`;
}
return contributors
.map(
(contributor) => `
<div class="col">
<div class="features-card rounded-xl p-4 shadow-md hover:shadow-xl transition duration-300">
<div class="card-body text-center">
<img
src="${contributor.avatar_url}"
class="rounded-full mx-auto mb-3 object-cover"
style="width: 100px; height: 100px"
alt="${contributor.login}"
/>
<div class="card-body">
<h5 class="font-bold text-lg">${contributor.login}</h5>
<p class="text-sm opacity-70 mb-3">${contributor.contributions} Contributions</p>
<a href="${contributor.html_url}" target="_blank"
class="inline-block px-4 py-2 bg-primary-color text-black font-semibold rounded-lg text-sm transition duration-200 hover:bg-opacity-80"
style="background-color: var(--color-primary); color: var(--color-background);"
>
GitHub Profile
</a>
</div>
</div>
</div>
</div>
`
)
.join("");
}
async function loadContributors() {
const repoOwner = "Ctoic"; // GitHub username or organization
const repoName = "Lisbook"; // Repository name
const contributors = await fetchContributors(repoOwner, repoName);
if (contributors && contributors.length > 0) {
// Target the container in about.html using the class selector
const container = document.querySelector(
".row.row-cols-1.row-cols-md-3.row-cols-lg-5.g-4.mt-4.align-items-stretch"
);
if (container) {
const contributorsHTML = generateContributorsHTML(contributors);
container.innerHTML = contributorsHTML;
} else {
console.warn("Contributor container not found in the DOM.");
}
} else if (contributors === null) {
// Display an error message in the container
const container = document.querySelector(".row.row-cols-1.row-cols-md-3.row-cols-lg-5.g-4.mt-4.align-items-stretch");
if (container) {
container.innerHTML = `<div class="col-span-full text-center p-6 bg-red-900/20 rounded-lg text-red-400">
<p>Failed to load contributors from GitHub. Please check the repository name and connection.</p>
</div>`;
}
}
}
// --- Main DOM Content Loaded Listener ---
document.addEventListener("DOMContentLoaded", function () {
console.log("✓ DOM fully loaded, starting application setup...");
try {
const currentTheme = document.documentElement.getAttribute('data-theme') || localStorage.getItem('theme') || 'dark';
const iframe = document.getElementById('gd-iframe');
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage({ type: 'theme', theme: currentTheme }, '*');
}
} catch (e) {
console.warn('Could not post initial theme to iframe', e);
}
const playlistItems = document.querySelectorAll("#playlist li");
const progressBars = document.querySelectorAll(".progress-bar");
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 favBooksList = document.getElementById("fav-audio-books-list");
const favouriteButton = document.getElementById("favourite-btn");
const commentForm = document.getElementById("comment-form");
const FAV_BOOKS_KEY = "fav_books";
const currentBookId = "7";
let allBooksList = []; // Assuming this will be populated elsewhere
let currentBook; // Assuming this will be populated elsewhere
// Audio Player Controls Buttons
const ctrlPlay = document.getElementById("ctrl-play");
const ctrlFastBackward = document.getElementById("fast-backward");
const ctrlFastForward = document.getElementById("fast-forward");
const volumeUp = document.getElementById("volume-up");
const volumeDown = document.getElementById("volume-down");
const seekSlider = document.getElementById("seekSlider");
const currentTimeLabel = document.getElementById("currentTime");
const durationLabel = document.getElementById("duration");
// --- Scroll to Top Button Setup ---
const scrollBtn = document.createElement("button");
scrollBtn.id = "scrollToTopBtn";
scrollBtn.className = "scroll-to-top-btn";
scrollBtn.setAttribute("aria-label", "Scroll to top");
scrollBtn.setAttribute("title", "Scroll to top");
scrollBtn.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" class="scroll-arrow" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18" />
</svg>
`;
document.body.appendChild(scrollBtn);
window.addEventListener("scroll", () => {
const chatbotButton = document.getElementById("chatbot-button");
if (window.scrollY > 100) {
scrollBtn.classList.add("show");
if (chatbotButton) {
chatbotButton.classList.add("shifted-up");
}
} else {
scrollBtn.classList.remove("show");
if (chatbotButton) {
chatbotButton.classList.remove("shifted-up");
}
}
});
scrollBtn.addEventListener("click", (e) => {
e.preventDefault();
window.scrollTo({ top: 0, behavior: "smooth" });
});
console.log("✓ Scroll button attached");
// --- End Scroll to Top Button Setup ---
// --- URL Parameters for Audio Playback ---
const urlParams = new URLSearchParams(window.location.search);
const audioFile = urlParams.get("file");
const startTime = urlParams.get("t");
if (audioPlayer && audioFile && startTime) {
audioPlayer.src = `audio/${audioFile}`;
audioPlayer.currentTime = startTime;
audioPlayer.play().catch((error) => {
console.error("Auto-play failed:", error);
});
}
// --- Share Button ---
const shareBtn = document.getElementById("share-link");
if (shareBtn && audioPlayer) {
shareBtn.addEventListener("click", () => {
const currentAudioFile = audioPlayer.src.split("/").pop();
const currentTime = audioPlayer.currentTime;
const currentUrl = `${
window.location.origin + window.location.pathname
}?file=${currentAudioFile}&t=${Math.floor(currentTime)}`; // Fixed URL construction
// Use custom popup instead of alert
navigator.clipboard
.writeText(currentUrl)
.then(() => {
showErrorPopup("Share link copied to clipboard!");
})
.catch((err) => {
console.error("Failed to copy: ", err);
showErrorPopup("Failed to copy link to clipboard.");
});
});
}
// --- Speed Controls ---
const speedButton = document.getElementById("speedButton");
const speedDropdown = document.getElementById("speedDropdown");
if (speedButton && speedDropdown && audioPlayer) {
speedButton.addEventListener("click", () => {
speedDropdown.classList.toggle("hidden");
});
const speedItems = speedDropdown.querySelectorAll("li");
speedItems.forEach((item) => {
item.addEventListener("click", () => {
const selectedSpeed = parseFloat(item.getAttribute("data-speed"));
audioPlayer.playbackRate = selectedSpeed;
speedButton.textContent = selectedSpeed + "x";
speedDropdown.classList.add("hidden");
});
});
// Ensure initial playback rate is set if not already
audioPlayer.addEventListener("canplay", () => {
speedButton.textContent = audioPlayer.playbackRate + "x";
});
}
// --- Favourites Logic ---
function toggleHeart(activate) {
if (favouriteButton) {
if (activate == null)
activate = favouriteButton.classList.contains("bi-heart");
if (activate) {
favouriteButton.classList.remove("bi-heart");
favouriteButton.classList.add("bi-heart-fill");
} else {
favouriteButton.classList.add("bi-heart");
favouriteButton.classList.remove("bi-heart-fill");
}
return activate;
}
return false;
}
function markFavourite(id, addItem) {
var favsList = [];
const favs = localStorage.getItem(FAV_BOOKS_KEY);
if (favs != null) favsList = JSON.parse(favs);
if (addItem == null) addItem = !favsList.includes(id);
if (addItem) favsList.push(id);
else {
const idx = favsList.indexOf(id);
if (idx != -1) favsList.splice(idx, 1);
}
localStorage.setItem(FAV_BOOKS_KEY, JSON.stringify(favsList));
}
// Placeholder for renderBookItem as the full book list/template is not provided
function renderBookItem(book, listElement) {
// This function is dependent on HTML for book cards, which is missing.
// Assuming for now it's not critical for script execution.
// console.log("Rendering book item placeholder for:", book.id);
}
function loadFavourites() {
const favs = localStorage.getItem(FAV_BOOKS_KEY);
var favsList = JSON.parse(favs) || [];
if (favBooksList) favBooksList.innerHTML = "";
const favEmptyContainer = document.getElementById("fav-empty-container");
if (favsList.length === 0) {
if (favEmptyContainer) favEmptyContainer.classList.remove("invisible");
return;
} else {
if (favEmptyContainer) favEmptyContainer.classList.add("invisible");
}
if (favsList.includes(currentBookId)) {
toggleHeart(true);
}
// Iterating over allBooksList (which is currently empty)
// allBooksList.forEach((book) => {
// if (favsList.includes(book.id)) {
// renderBookItem(book, favBooksList);
// }
// });
}
loadFavourites(); // Initial load of favourites
if (favouriteButton) {
favouriteButton.addEventListener("click", () => {
const activated = toggleHeart();
markFavourite(currentBookId);
if (activated) {
document
.getElementById("fav-empty-container")
.classList.add("invisible");
// renderBookItem(currentBook, favBooksList); // Re-enable once currentBook and renderBookItem are defined
} else {
loadFavourites();
}
});
}
// --- End Favourites Logic ---
// --- Playlist Item Click Event ---
playlistItems.forEach((item) => {
item.addEventListener("click", function () {
const audioSource = item.getAttribute("data-src");
if (audioPlayer && audioSource) {
audioPlayer.src = audioSource;
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 if (!audioSource) {
showErrorPopup("Audio source not available.");
}
});
});
// --- Cursor Smooth Animation ---
const circles = document.querySelectorAll(".circle");
const logo = document.querySelector(".logo");
const coords = { x: 0, y: 0 };
if (logo) {
const logoRect = logo.getBoundingClientRect();
coords.x = logoRect.left + logoRect.width / 2;
coords.y = logoRect.top + logoRect.height / 2;
}
const colors = [
"#a7e078", "#9dd36c", "#94c760", "#8abc55", "#80b14b", "#76a640",
"#6cbf58", "#62a24d", "#579643", "#4e8a3b", "#458132", "#3b752a",
"#336824", "#2c9137", "#23802c", "#1f7628", "#1b6c25", "#121212",
"#0f0f0f", "#2b2b2b", "#1e1e1e", "#1a1a1a",
];
circles.forEach((circle, index) => {
circle.style.backgroundColor = colors[index % colors.length];
circle.x = 0;
circle.y = 0;
});
document.addEventListener("mousemove", function (e) {
coords.x = e.clientX;
coords.y = e.clientY;
});
function animateCircles() {
let x = coords.x;
let y = coords.y;
circles.forEach((circle, index) => {
circle.style.left = `${x - 12}px`;
circle.style.top = `${y - 12}px`;
circle.style.transform = `scale(${
(circles.length - index) / circles.length
})`;
const nextCircle = circles[index + 1] || circles[0];
x += (nextCircle.x - x) * 0.2;
y += (nextCircle.y - y) * 0.2;
circle.x = x;
circle.y = y;
});
requestAnimationFrame(animateCircles);
}
if (circles.length > 0) {
animateCircles();
}
// --- End Cursor Smooth Animation ---
// --- Keyboard Shortcuts ---
if (audioPlayer) {
document.addEventListener("keydown", function (e) {
// Don't block space/shortcuts if typing in an input, textarea, or contenteditable
const tag = e.target.tagName.toLowerCase();
const isEditable = e.target.isContentEditable;
if (tag === "input" || tag === "textarea" || isEditable) return;
switch (e.code) {
case "Space": // Play / Pause
e.preventDefault();
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":
case "NumpadAdd": // Increase volume
if (audioPlayer.volume < 1) {
audioPlayer.volume = Math.min(audioPlayer.volume + 0.1, 1);
}
break;
case "Minus":
case "NumpadSubtract": // Reduce volume
if (audioPlayer.volume > 0) {
audioPlayer.volume = Math.max(audioPlayer.volume - 0.1, 0);
}
break;
default:
break;
}
});
}
// --- Audio Player Controls ---
if (ctrlPlay && audioPlayer) {
ctrlPlay.addEventListener("click", function () {
if (audioPlayer.paused) {
audioPlayer.play();
} else {
audioPlayer.pause();
}
});
if (ctrlFastForward) {
ctrlFastForward.addEventListener("click", function () {
audioPlayer.currentTime += 30;
});
}
if (ctrlFastBackward) {
ctrlFastBackward.addEventListener("click", function () {
audioPlayer.currentTime -= 30;
});
}
if (volumeUp) {
volumeUp.addEventListener("click", function () {
if (audioPlayer.volume < 1) {
audioPlayer.volume = Math.min(audioPlayer.volume + 0.1, 1);
}
});
}
if (volumeDown) {
volumeDown.addEventListener("click", function () {
if (audioPlayer.volume > 0) {
audioPlayer.volume = Math.max(audioPlayer.volume - 0.1, 0);
}
});
}
// Seek Player
audioPlayer.addEventListener("timeupdate", () => {
if (seekSlider) {
seekSlider.value = audioPlayer.currentTime;
}
if (currentTimeLabel) {
currentTimeLabel.textContent = formatTime(audioPlayer.currentTime);
}
});
// Seek functionality
if (seekSlider) {
seekSlider.addEventListener("input", (event) => {
audioPlayer.currentTime = event.target.value;
});
}
audioPlayer.addEventListener("ended", function () {
if (ctrlPlay) {
ctrlPlay.innerHTML = '<i class="bi bi-play-fill"></i>';
}
});
audioPlayer.addEventListener("play", function () {
if (ctrlPlay) {
ctrlPlay.innerHTML = '<i class="bi bi-pause-fill"></i>';
}
});
audioPlayer.addEventListener("pause", function () {
if (ctrlPlay) {
ctrlPlay.innerHTML = '<i class="bi bi-play-fill"></i>';
}
});
audioPlayer.addEventListener("loadedmetadata", () => {
if (durationLabel) {
durationLabel.textContent = formatTime(audioPlayer.duration);
}
if (seekSlider) {
seekSlider.max = audioPlayer.duration;
}
});
audioPlayer.addEventListener("loadeddata", function () {
playlistItems?.forEach((item) => {
const dataSrc = item.getAttribute("data-src");
if (audioPlayer.src.includes(dataSrc)) {
let time = parseInt(localStorage.getItem(dataSrc)) || 0;
audioPlayer.currentTime = (time * audioPlayer.duration) / 100;
}
});
});
}
// --- Comment Submission ---
if (commentForm) {
commentForm.addEventListener("submit", function (e) {
e.preventDefault();
const username = document.getElementById("username")?.value;
const comment = document.getElementById("comment")?.value;
const commentsList = document.getElementById("comments-list");
if (username && comment && commentsList) {
const commentHTML = `<div class="bg-gray-700 text-white p-4 rounded-lg">
<strong>${username}:</strong>
<p>${comment}</p>
</div>`;
commentsList.insertAdjacentHTML("beforeend", commentHTML);
document.getElementById("comment-form")?.reset();
}
});
}
// --- Feedback Submission (EmailJS Dependency - unchanged) ---
const feedbackForm = document.getElementById("feedback-form");
if (feedbackForm) {
feedbackForm.addEventListener("submit", function (event) {
event.preventDefault();
// NOTE: This requires EmailJS setup ('your_service_id' and 'feedback_form' template ID)
// The EmailJS library script is expected to be loaded in the HTML.
if (typeof emailjs !== 'undefined' && emailjs.sendForm) {
emailjs.sendForm("your_service_id", "feedback_form", this).then(
() => {
console.log("SUCCESS! Feedback Sent.");
feedbackForm.reset();
},
(error) => {
console.log("FAILED to send feedback...", error);
}
);
} else {
console.warn("EmailJS library not loaded. Feedback submission skipped.");
feedbackForm.reset();
}
});
}
// --- Theme Toggle ---
function initializeTheme() {
const storedTheme = localStorage.getItem("theme") || "dark";
setTheme(storedTheme);
}
initializeTheme();
if (themeToggle) {
themeToggle.addEventListener("click", () => {
toggleTheme();
});
}
// --- Mobile Menu Toggle ---
if (menuToggle && menu && menuClose) {
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");
});
}
// --- Progress Bars (Local Storage) ---
function initializeProgressBars() {
progressBars?.forEach((progressBar, index) => {
const dataSrc = playlistItems[index]?.getAttribute("data-src");
if (dataSrc) {
let percentage = parseInt(localStorage.getItem(dataSrc)) || 0;
progressBar.style.width = percentage + "%";
progressBar.textContent = percentage ? percentage + "%" : "";
}
});
}
initializeProgressBars();
audioPlayer?.addEventListener("timeupdate", function () {
if (audioPlayer.readyState)
progressBars?.forEach((progressBar, index) => {
const item = playlistItems[index];
if (!item) return;
const dataSrc = item.getAttribute("data-src");
// Check if the current audio source matches the playlist item
if (audioPlayer.src.includes(dataSrc) && audioPlayer.duration > 0) {
let percentage = Math.min(100,
parseInt((audioPlayer.currentTime / audioPlayer.duration) * 100)
);
progressBar.style.width = percentage + "%";
progressBar.textContent = percentage ? percentage + "%" : "";
localStorage.setItem(dataSrc, percentage);
}
});
});
// --- FAQ Auto Type Answer ---
const faqBoxes = document.querySelectorAll(".faq-box");
faqBoxes.forEach((box) => {
box.addEventListener("mouseenter", function () {
const question = box.querySelector(".faq-question");
const answerId = question?.getAttribute("data-answer");
const answerElement = document.getElementById(answerId);
if (!answerElement || !answerElement.classList.contains("hidden")) return;
typeAnswer(
answerElement,
answerElement.dataset.fulltext || answerElement.innerHTML
);
});
});
function typeAnswer(element, answer) {
element.dataset.fulltext = answer;
element.innerHTML = "";
element.classList.remove("hidden");
let i = 0;
function type() {
if (i < answer.length) {
element.innerHTML += answer.charAt(i);
i++;
setTimeout(type, 50);
}
}
type();
}
const editNameBtn = document.getElementById("edit-name");
if (editNameBtn) {
editNameBtn.addEventListener("click", () => {
// Using prompt for simplicity, but a custom modal is recommended
const newName = prompt("Enter your new name:");
const profileNameElement = document.querySelector(".profile-section p");
if (newName && profileNameElement) {
profileNameElement.innerText = `Name: ${newName}`;
}
});
}
document.querySelectorAll(".friend-card").forEach((card) => {
card.addEventListener("click", function () {
const targetId = this.getAttribute("data-target");
const content = document.querySelector(targetId);
if (content) {
const isCollapsed = content.classList.contains("expanded");
if (isCollapsed) {
content.classList.remove("expanded");
card.classList.add("collapsed");
} else {
content.classList.add("expanded");
card.classList.remove("collapsed");
}
}
});
});
// --- General Auto-Type for Hero Section ---
const autoTypeElement = document.querySelector(".auto-type");
if (autoTypeElement) {
var typed = new Typed(".auto-type", {
strings: [
"Play/Pause",
"Stop",
"Skip Chapters",
"Change Speed",
"Change Volume",
"Change Theme",
],
typeSpeed: 150,
backSpeed: 150,
loop: true,
});
}
// --- Feature Section Type Animation on Hover ---
const featureSection = document.querySelector("section.rounded-5.p-5.my-5");
let typedOnce = false;
if (featureSection) {
featureSection.addEventListener("mouseenter", function () {
if (!typedOnce) {
document.querySelectorAll(".type-target").forEach((targetSpan) => {
const text = targetSpan.getAttribute("data-text");
if (text) {
targetSpan.innerHTML = "";
startTyping(targetSpan, text);
}
});
typedOnce = true;
}
});
}
// --- Contributor Title Auto-Type ---
const titles = [
"Collaborators",
"Authors",
"Developers",
"Co-authors",
"Team Members",
"Participants",
"Co-contributors",
"Supporters",
"Associates",
"Engagers",
"Project Allies",
"engineers",
"Contributing Members",
];
let titleIndex = 0;
let charIndex = 0;
const typingSpeed = 100;
const erasingSpeed = 50;
const pauseDuration = 1500;
const autoTypeTitleElement = document.getElementById("auto-type-title");
const autoTypeTitle = () => {
const currentTitle = titles[titleIndex];
if (autoTypeTitleElement && charIndex < currentTitle.length) {
autoTypeTitleElement.textContent += currentTitle.charAt(charIndex);
charIndex++;
setTimeout(autoTypeTitle, typingSpeed);
} else if (autoTypeTitleElement) {
setTimeout(eraseTitle, pauseDuration);
}
};
const eraseTitle = () => {
const currentTitle = titles[titleIndex];
if (autoTypeTitleElement && charIndex > 0) {
autoTypeTitleElement.textContent = currentTitle.slice(
0,
charIndex - 1
);
charIndex--;
setTimeout(eraseTitle, erasingSpeed);
} else {
titleIndex = (titleIndex + 1) % titles.length;
setTimeout(autoTypeTitle, typingSpeed);
}
};
if (autoTypeTitleElement) {
autoTypeTitle();
}
// --- End Contributor Title Auto-Type ---
// --- Contact Page Submission ---
const contactForm = document.getElementById("contact-form");
if (contactForm) {
contactForm.addEventListener("submit", function (e) {
e.preventDefault();
const name = document.getElementById("name")?.value;
const email = document.getElementById("email")?.value;
const phone = document.getElementById("phone")?.value;
const message = document.getElementById("message")?.value;
const successMsg = document.getElementById("success-msg");
if (name && email && phone && message && successMsg) {
successMsg.classList.remove("hidden");
setTimeout(
() => successMsg.classList.add("hidden"),
5000
);
this.reset();
}
});
}
// --- File Loading ---
// Load header and footer (dependent on external files)
loadHTML("./pages/header.html", "header-placeholder");
loadHTML("./pages/footer.html", "footer-placeholder");
// --- Load Contributors on About Page ---
// Check if the contributor container exists (only on about.html)
if (document.querySelector(".row.row-cols-1.row-cols-md-3.row-cols-lg-5.g-4.mt-4.align-items-stretch")) {
loadContributors();
}
let booksData = null;
const chatbotButton = document.getElementById("chatbot-button");
const chatbotWindow = document.getElementById("chatbot-window");
const chatbotClose = document.getElementById("chatbot-close");
const chatbotForm = document.getElementById("chatbot-form");
const chatbotInput = document.getElementById("chatbot-input");
const chatbotMessages = document.getElementById("chatbot-messages");
// Show/hide chatbot window
chatbotButton.onclick = () => {
chatbotWindow.style.display = "flex";
chatbotButton.style.display = "none";
};
chatbotClose.onclick = () => {
chatbotWindow.style.display = "none";
chatbotButton.style.display = "flex";