11const bookManifest = [
2- { name : "Genesis" , file : "genesis.json" } ,
3- { name : "1 Enoch" , file : "enoch.json" }
2+ { name : "Genesis" , file : "genesis.json" } ,
3+ { name : "1 Enoch" , file : "enoch.json" }
44] ;
55
6- let currentBookData = null ; // To store the loaded book data
6+ let currentBook = null ;
7+ const bookCache = { } ;
78
89// Fetch book data
910async function loadBookData ( bookFile ) {
10- try {
11- const response = await fetch ( `data/books/${ bookFile } ` ) ;
12- const data = await response . json ( ) ;
13- return data ;
14- } catch ( error ) {
15- console . error ( "Error loading book:" , error ) ;
16- }
11+ if ( ! bookCache [ bookFile ] ) {
12+ const response = await fetch ( `data/books/${ bookFile } ` ) ;
13+ const data = await response . json ( ) ;
14+ bookCache [ bookFile ] = data ;
15+ }
16+ return bookCache [ bookFile ] ;
1717}
1818
19- // Display book list
20- function showBooks ( ) {
21- const bookList = document . getElementById ( 'book-list' ) ;
22- bookList . innerHTML = bookManifest . map ( book => `
23- <button onclick="handleBookClick('${ book . file } ')">
24- ${ book . name }
25- </button>
26- ` ) . join ( '' ) ;
19+ // Load chapter 1 of a book automatically
20+ async function loadBook ( bookFile ) {
21+ const bookData = await loadBookData ( bookFile ) ;
22+ currentBook = Object . keys ( bookData ) [ 0 ] ; // e.g., "1 Enoch"
23+ const firstChapter = Object . keys ( bookData [ currentBook ] ) [ 0 ] ; // Chapter 1
24+
25+ // Display verses
26+ const versesDiv = document . getElementById ( 'verses' ) ;
27+ versesDiv . innerHTML = `
28+ <h2>${ currentBook } </h2>
29+ ${ Object . entries ( bookData [ currentBook ] [ firstChapter ] )
30+ . map ( ( [ verse , text ] ) => `
31+ <p class="verse"><b>${ verse } :</b> ${ text } </p>
32+ ` ) . join ( '' ) }
33+ ` ;
2734}
2835
29- // Handle book click
30- async function handleBookClick ( bookFile ) {
31- currentBookData = await loadBookData ( bookFile ) ;
32- if ( ! currentBookData ) return ;
33-
34- const bookName = Object . keys ( currentBookData ) [ 0 ] ; // e.g., "1 Enoch"
35- const chapters = Object . keys ( currentBookData [ bookName ] ) ;
36-
37- // Show chapters
38- document . getElementById ( 'chapters-section' ) . style . display = 'block' ;
39- document . getElementById ( 'verses-section' ) . style . display = 'none' ;
40-
41- const chaptersDiv = document . getElementById ( 'chapters' ) ;
42- chaptersDiv . innerHTML = chapters . map ( chapter => `
43- <button class="chapter-btn" onclick="handleChapterClick('${ chapter } ')">
44- Chapter ${ chapter }
45- </button>
46- ` ) . join ( '' ) ;
47- }
48-
49- // Handle chapter click
50- function handleChapterClick ( chapter ) {
51- const bookName = Object . keys ( currentBookData ) [ 0 ] ;
52- const verses = currentBookData [ bookName ] [ chapter ] ;
53-
54- document . getElementById ( 'verses-section' ) . style . display = 'block' ;
55- const versesDiv = document . getElementById ( 'verses' ) ;
56- versesDiv . innerHTML = Object . entries ( verses ) . map ( ( [ verse , text ] ) => `
57- <p><b>${ verse } :</b> ${ text } </p>
58- ` ) . join ( '' ) ;
59- }
60-
61- // Back buttons
62- function showChapters ( ) {
63- document . getElementById ( 'verses-section' ) . style . display = 'none' ;
36+ // Initialize book list
37+ function initBooks ( ) {
38+ const bookList = document . getElementById ( 'book-list' ) ;
39+ bookList . innerHTML = bookManifest
40+ . map ( book => `
41+ <button onclick="loadBook('${ book . file } ')">
42+ ${ book . name }
43+ </button>
44+ ` )
45+ . join ( '' ) ;
6446}
6547
66- // Initialize
67- showBooks ( ) ;
48+ // Start
49+ initBooks ( ) ;
0 commit comments