1+ // Book manifest - Add all books here
12const bookManifest = [
23 { name : "Genesis" , file : "genesis.json" } ,
34 { name : "1 Enoch" , file : "enoch.json" }
@@ -8,74 +9,79 @@ let currentChapter = null;
89let bookData = null ;
910const bookCache = { } ;
1011
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- }
12+ // Mobile menu functionality
13+ const mobileMenu = document . getElementById ( 'mobile-menu' ) ;
14+ const sidebar = document . getElementById ( 'sidebar' ) ;
15+
16+ function toggleSidebar ( ) {
17+ sidebar . style . left = sidebar . style . left === '0px' ? '-100%' : '0' ;
2618}
2719
20+ mobileMenu . addEventListener ( 'click' , ( e ) => {
21+ e . stopPropagation ( ) ;
22+ toggleSidebar ( ) ;
23+ } ) ;
24+
25+ document . addEventListener ( 'click' , ( e ) => {
26+ if ( window . innerWidth < 768 && ! sidebar . contains ( e . target ) ) {
27+ sidebar . style . left = '-100%' ;
28+ }
29+ } ) ;
30+
2831// Navigation controls
2932document . getElementById ( 'prevChapter' ) . addEventListener ( 'click' , ( ) => {
30- const prev = parseInt ( currentChapter ) - 1 ;
31- if ( prev >= 1 ) loadChapter ( prev . toString ( ) ) ;
33+ const prev = String ( parseInt ( currentChapter ) - 1 ) ;
34+ if ( bookData [ currentBook ] [ prev ] ) loadChapter ( prev ) ;
3235} ) ;
3336
3437document . getElementById ( 'nextChapter' ) . addEventListener ( 'click' , ( ) => {
35- const next = parseInt ( currentChapter ) + 1 ;
36- if ( bookData [ currentBook ] [ next ] ) loadChapter ( next . toString ( ) ) ;
38+ const next = String ( parseInt ( currentChapter ) + 1 ) ;
39+ if ( bookData [ currentBook ] [ next ] ) loadChapter ( next ) ;
3740} ) ;
3841
42+ // Fetch book data with error handling
3943async function loadBookData ( bookFile ) {
40- if ( ! bookCache [ bookFile ] ) {
41- const response = await fetch ( `data/books/${ bookFile } ` ) ;
42- if ( ! response . ok ) throw new Error ( `Failed to fetch ${ bookFile } ` ) ;
43- bookCache [ bookFile ] = await response . json ( ) ;
44+ try {
45+ if ( ! bookCache [ bookFile ] ) {
46+ const response = await fetch ( `data/books/${ bookFile } ` ) ;
47+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
48+ bookCache [ bookFile ] = await response . json ( ) ;
49+ }
50+ return bookCache [ bookFile ] ;
51+ } catch ( error ) {
52+ console . error ( `Failed to load ${ bookFile } :` , error ) ;
53+ return null ;
4454 }
45- return bookCache [ bookFile ] ;
4655}
4756
57+ // Load book and initialize first chapter
4858async function loadBook ( bookFile ) {
4959 try {
5060 bookData = await loadBookData ( bookFile ) ;
61+ if ( ! bookData ) throw new Error ( 'No book data received' ) ;
62+
5163 currentBook = Object . keys ( bookData ) [ 0 ] ;
52- currentChapter = '1' ; // Force load chapter 1
64+ currentChapter = '1' ; // Force first chapter
5365
54- // Update UI
55- document . getElementById ( 'navigation' ) . style . display = 'flex' ;
56- loadChapter ( currentChapter ) ;
66+ if ( ! bookData [ currentBook ] [ currentChapter ] ) {
67+ currentChapter = Object . keys ( bookData [ currentBook ] ) [ 0 ] ;
68+ }
69+
5770 updateChapterNavigation ( ) ;
58-
71+ loadChapter ( currentChapter ) ;
72+ document . getElementById ( 'navigation' ) . style . display = 'flex' ;
73+
5974 } catch ( error ) {
6075 console . error ( "Book load failed:" , error ) ;
61- alert ( "Error loading book. Please try again." ) ;
76+ document . getElementById ( 'verses' ) . innerHTML =
77+ `<p class="error">Error loading book. Please try again.</p>` ;
6278 }
6379}
6480
65- function loadChapter ( chapter ) {
66- currentChapter = chapter ;
67- const chapterContent = bookData [ currentBook ] [ chapter ] ;
68-
69- document . getElementById ( 'verses' ) . innerHTML = `
70- <h2>${ currentBook } ${ currentChapter } </h2>
71- ${ Object . entries ( chapterContent ) . map ( ( [ verse , text ] ) => `
72- <p><b>${ verse } :</b> ${ text } </p>
73- ` ) . join ( '' ) }
74- ` ;
75- updateChapterNavigation ( ) ;
76- }
77-
81+ // Update chapter navigation buttons
7882function updateChapterNavigation ( ) {
83+ if ( ! bookData || ! currentBook ) return ;
84+
7985 const chapters = Object . keys ( bookData [ currentBook ] ) ;
8086 const chapterNumbers = document . getElementById ( 'chapter-numbers' ) ;
8187
@@ -87,35 +93,46 @@ function updateChapterNavigation() {
8793 ` ) . join ( '' ) ;
8894}
8995
96+ // Load specific chapter
97+ function loadChapter ( chapter ) {
98+ if ( ! bookData || ! currentBook || ! bookData [ currentBook ] [ chapter ] ) return ;
99+
100+ currentChapter = chapter ;
101+ const chapterContent = bookData [ currentBook ] [ chapter ] ;
102+
103+ const versesHtml = Object . entries ( chapterContent )
104+ . map ( ( [ verse , text ] ) => `<p><b>${ verse } :</b> ${ text } </p>` )
105+ . join ( '' ) ;
106+
107+ document . getElementById ( 'verses' ) . innerHTML = `
108+ <h2>${ currentBook } ${ currentChapter } </h2>
109+ ${ versesHtml }
110+ ` ;
90111
112+ updateChapterNavigation ( ) ;
113+ }
91114
115+ // Initialize book list
92116function initBooks ( ) {
93117 const bookList = document . getElementById ( 'book-list' ) ;
94118 bookList . innerHTML = bookManifest
95- . map ( book => `
96- <button onclick="loadBook('${ book . file } ')">
97- ${ book . name }
98- </button>
99- ` )
119+ . map ( book => `<button onclick="loadBook('${ book . file } ')">${ book . name } </button>` )
100120 . join ( '' ) ;
101121}
102122
103- // Mobile menu functionality
104- const mobileMenu = document . getElementById ( 'mobile-menu' ) ;
105- const sidebar = document . getElementById ( 'sidebar' ) ;
106-
107- function toggleSidebar ( ) {
108- sidebar . style . left = sidebar . style . left === '0px' ? '-100%' : '0' ;
109- }
110-
111- mobileMenu . addEventListener ( 'click' , ( e ) => {
112- e . stopPropagation ( ) ;
113- toggleSidebar ( ) ;
114- } ) ;
115-
116- document . addEventListener ( 'click' , ( e ) => {
117- if ( window . innerWidth < 768 && ! sidebar . contains ( e . target ) ) {
118- sidebar . style . left = '-100%' ;
123+ // Start the application
124+ document . addEventListener ( 'DOMContentLoaded' , async ( ) => {
125+ initBooks ( ) ;
126+
127+ // Load Genesis by default
128+ const genesisBook = bookManifest . find ( b => b . name === "Genesis" ) ;
129+ if ( genesisBook ) {
130+ await loadBook ( genesisBook . file ) ;
131+
132+ // Close sidebar on mobile after initial load
133+ if ( window . innerWidth < 768 ) {
134+ sidebar . style . left = '-100%' ;
135+ }
119136 }
120137} ) ;
121138
@@ -126,24 +143,4 @@ window.addEventListener('resize', () => {
126143 } else {
127144 sidebar . style . left = '-100%' ;
128145 }
129- } ) ;
130-
131- // Load Genesis by default
132- window . addEventListener ( 'DOMContentLoaded' , async ( ) => {
133- // Find Genesis in book manifest
134- const genesisBook = bookManifest . find ( b => b . name === "Genesis" ) ;
135-
136- if ( genesisBook ) {
137- await loadBook ( genesisBook . file ) ;
138-
139- // Close mobile sidebar after loading
140- if ( window . innerWidth < 768 ) {
141- document . getElementById ( 'sidebar' ) . style . left = '-100%' ;
142- }
143- }
144- } ) ;
145-
146-
147-
148- // Start the app
149- document . addEventListener ( 'DOMContentLoaded' , initApp ) ;
146+ } ) ;
0 commit comments