@@ -8,6 +8,23 @@ let currentChapter = null;
88let bookData = null ;
99const bookCache = { } ;
1010
11+ // Initialize the app properly
12+ async function initApp ( ) {
13+ // Initialize book list
14+ const bookList = document . getElementById ( 'book-list' ) ;
15+ bookList . innerHTML = bookManifest
16+ . map ( book => `<button onclick="loadBook('${ book . file } ')">${ book . name } </button>` )
17+ . join ( '' ) ;
18+
19+ // Load Genesis by default
20+ try {
21+ await loadBook ( 'genesis.json' ) ;
22+ } catch ( error ) {
23+ console . error ( "Failed to load Genesis:" , error ) ;
24+ alert ( "Error loading initial content. Please refresh." ) ;
25+ }
26+ }
27+
1128// Navigation controls
1229document . getElementById ( 'prevChapter' ) . addEventListener ( 'click' , ( ) => {
1330 const prev = parseInt ( currentChapter ) - 1 ;
@@ -22,8 +39,8 @@ document.getElementById('nextChapter').addEventListener('click', () => {
2239async function loadBookData ( bookFile ) {
2340 if ( ! bookCache [ bookFile ] ) {
2441 const response = await fetch ( `data/books/${ bookFile } ` ) ;
25- const data = await response . json ( ) ;
26- bookCache [ bookFile ] = data ;
42+ if ( ! response . ok ) throw new Error ( `Failed to fetch ${ bookFile } ` ) ;
43+ bookCache [ bookFile ] = await response . json ( ) ;
2744 }
2845 return bookCache [ bookFile ] ;
2946}
@@ -32,28 +49,19 @@ async function loadBook(bookFile) {
3249 try {
3350 bookData = await loadBookData ( bookFile ) ;
3451 currentBook = Object . keys ( bookData ) [ 0 ] ;
35- currentChapter = Object . keys ( bookData [ currentBook ] ) [ 0 ] ;
52+ currentChapter = '1' ; // Force load chapter 1
3653
54+ // Update UI
3755 document . getElementById ( 'navigation' ) . style . display = 'flex' ;
38- updateChapterNavigation ( ) ;
3956 loadChapter ( currentChapter ) ;
57+ updateChapterNavigation ( ) ;
58+
4059 } catch ( error ) {
41- console . error ( "Failed to load book:" , error ) ;
60+ console . error ( "Book load failed:" , error ) ;
61+ alert ( "Error loading book. Please try again." ) ;
4262 }
4363}
4464
45- function updateChapterNavigation ( ) {
46- const chapters = Object . keys ( bookData [ currentBook ] ) ;
47- const chapterNumbers = document . getElementById ( 'chapter-numbers' ) ;
48-
49- chapterNumbers . innerHTML = chapters . map ( chapter => `
50- <button class="chapter-btn ${ chapter === currentChapter ? 'active' : '' } "
51- onclick="loadChapter('${ chapter } ')">
52- ${ chapter }
53- </button>
54- ` ) . join ( '' ) ;
55- }
56-
5765function loadChapter ( chapter ) {
5866 currentChapter = chapter ;
5967 const chapterContent = bookData [ currentBook ] [ chapter ] ;
@@ -67,6 +75,20 @@ function loadChapter(chapter) {
6775 updateChapterNavigation ( ) ;
6876}
6977
78+ function updateChapterNavigation ( ) {
79+ const chapters = Object . keys ( bookData [ currentBook ] ) ;
80+ const chapterNumbers = document . getElementById ( 'chapter-numbers' ) ;
81+
82+ chapterNumbers . innerHTML = chapters . map ( chapter => `
83+ <button class="chapter-btn ${ chapter === currentChapter ? 'active' : '' } "
84+ onclick="loadChapter('${ chapter } ')">
85+ ${ chapter }
86+ </button>
87+ ` ) . join ( '' ) ;
88+ }
89+
90+
91+
7092function initBooks ( ) {
7193 const bookList = document . getElementById ( 'book-list' ) ;
7294 bookList . innerHTML = bookManifest
@@ -121,16 +143,7 @@ window.addEventListener('DOMContentLoaded', async () => {
121143 }
122144} ) ;
123145
124- // Initialize books and load Genesis
125- function initApp ( ) {
126- initBooks ( ) ;
127-
128- // Load Genesis after slight delay to ensure DOM is ready
129- setTimeout ( async ( ) => {
130- const genesis = bookManifest . find ( b => b . name === "Genesis" ) ;
131- if ( genesis ) await loadBook ( genesis . file ) ;
132- } , 100 ) ;
133- }
146+
134147
135148// Start the app
136- initApp ( ) ;
149+ document . addEventListener ( 'DOMContentLoaded' , initApp ) ;
0 commit comments