-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
124 lines (106 loc) · 5.01 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
// script.js
// Scroll to the bottom of the page
function scrollToBottom() {
window.scrollTo({
top: document.body.scrollHeight,
behavior: 'smooth'
});
}
// Toggle Synopsis visibility
function toggleSynopsis() {
const synopsisSection = document.querySelector('.synopsis-section');
synopsisSection.classList.toggle('collapsed');
const button = synopsisSection.querySelector('button.view-synopsis');
button.textContent = synopsisSection.classList.contains('collapsed') ? 'v' : '^';
}
// Fetch chapters from the chapters.json file and populate the list
// Fetch chapters from the chapters.json file and populate the list
async function loadChapters() {
try {
const response = await fetch('chapters.json');
const chapters = await response.json();
const chaptersPerBook = [232, 500]; // Array of cutoff points for books
const chapterContainer = document.getElementById('chapterContainer');
chapterContainer.innerHTML = ''; // Clear any existing content
const books = [];
let currentChapterStart = 0;
chaptersPerBook.forEach((cutoff, index) => {
books[index] = chapters.slice(currentChapterStart, cutoff);
currentChapterStart = cutoff;
});
books.forEach((bookChapters, bookIndex) => {
const bookDiv = document.createElement('div');
bookDiv.className = 'book';
const bookButton = document.createElement('button');
bookButton.className = 'read-chapterLIST';
bookButton.textContent = `Book ${bookIndex + 1}`;
bookButton.onclick = function () {
const chapterList = this.nextElementSibling;
const isExpanded = chapterList.classList.toggle('expanded');
if (isExpanded) {
Array.from(chapterList.children).forEach((li, index) => {
setTimeout(() => {
li.classList.add('show');
}, index * 10); // Delay between each chapter appearing
});
// Save user choice in a data attribute
bookButton.dataset.expanded = "true";
} else {
Array.from(chapterList.children).forEach(li => {
li.classList.remove('show');
});
// Update user choice in a data attribute
bookButton.dataset.expanded = "false";
}
};
bookDiv.appendChild(bookButton);
const ul = document.createElement('ul');
bookChapters.forEach(chapter => {
const li = document.createElement('li');
li.classList.add('chapter-item'); // Adding class to control animation
const a = document.createElement('a');
a.href = `read/${chapter.filename.replace('.txt', '')}/`;
a.textContent = chapter.title; // Removed the 20-character slicing
li.appendChild(a);
ul.appendChild(li);
});
bookDiv.appendChild(ul);
chapterContainer.appendChild(bookDiv);
});
// Adjust visibility based on screen width, but keep user choice persistent
function adjustChapterVisibility() {
const screenWidth = window.innerWidth;
const thresholdWidth = 800; // Adjust this threshold as needed
const bookButtons = document.querySelectorAll('.read-chapterLIST');
bookButtons.forEach((button, index) => {
const chapterList = button.nextElementSibling;
const isLastBook = index === bookButtons.length - 1;
if (isLastBook) {
chapterList.classList.add('expanded');
Array.from(chapterList.children).forEach(li => li.classList.add('show'));
} else {
// Check if user has manually expanded/collapsed this book
const userExpanded = button.dataset.expanded === "true";
if (screenWidth < thresholdWidth && !userExpanded) {
chapterList.classList.remove('expanded');
Array.from(chapterList.children).forEach(li => li.classList.remove('show'));
} else if (userExpanded || screenWidth >= thresholdWidth) {
chapterList.classList.add('expanded');
Array.from(chapterList.children).forEach(li => li.classList.add('show'));
}
}
});
}
adjustChapterVisibility();
window.addEventListener('resize', adjustChapterVisibility);
} catch (error) {
console.error('Error loading chapters:', error);
}
}
document.addEventListener('DOMContentLoaded', () => {
const synopsisButton = document.querySelector('button.view-synopsis');
if (synopsisButton) {
synopsisButton.addEventListener('click', toggleSynopsis);
}
loadChapters();
});