@@ -121,120 +121,120 @@ export function initBookChapterControls() {
121121 } ) ;
122122 populateChapterDropdown ( BOOK_ORDER [ 0 ] ) ;
123123}
124- export function manualPrevChapter ( ) {
125- let bookIdx = BOOK_ORDER . indexOf ( state . settings . manualBook ) ;
126- let chap = state . settings . manualChapter ;
127- let nextBook = state . settings . manualBook ;
128- let nextChapter = chap ;
129- if ( chap > 1 ) {
130- nextChapter = chap - 1 ;
131- } else {
132- if ( bookIdx > 0 ) {
133- const prevBook = BOOK_ORDER [ bookIdx - 1 ] ;
134- const maxCh = CHAPTER_COUNTS [ prevBook ] ;
135- nextBook = prevBook ;
136- nextChapter = maxCh ;
137- } else {
138- return ;
124+ export async function randomPassage ( ) {
125+ try {
126+ state . settings . readingMode = 'manual' ;
127+ const randomLoc = await getRandomBibleLocation ( ) ;
128+ state . settings . manualBook = randomLoc . book ;
129+ state . settings . manualChapter = randomLoc . chapter ;
130+ const translation = getCurrentTranslation ( ) ;
131+ updateURL ( translation , randomLoc . book , randomLoc . chapter ) ;
132+ saveToStorage ( ) ;
133+ await loadPassageFromAPI ( randomLoc ) ;
134+ document . getElementById ( 'passageReference' ) . textContent = randomLoc . displayRef ;
135+ state . currentPassageReference = randomLoc . displayRef ;
136+ syncBookChapterSelectors ( ) ;
137+ if ( state . settings . referencePanelOpen ) {
138+ updateReferencePanel ( ) ;
139139 }
140- }
141- state . settings . manualBook = nextBook ;
142- state . settings . manualChapter = nextChapter ;
143- state . settings . readingMode = 'manual' ;
144- const translation = getCurrentTranslation ( ) ;
145- updateURL ( translation , nextBook , nextChapter ) ;
146- const displayRef = `${ nextBook } ${ nextChapter } ` ;
147- document . getElementById ( 'passageReference' ) . textContent = displayRef ;
148- state . currentPassageReference = displayRef ;
149- loadSelectedChapter ( nextBook , nextChapter ) ;
150- syncBookChapterSelectors ( ) ;
151- saveToStorage ( ) ;
152- if ( state . settings . referencePanelOpen ) {
153- updateReferencePanel ( ) ;
140+ } catch ( err ) {
141+ handleError ( err , 'randomPassage' ) ;
142+ showError ( 'Could not load a random passage – see console for details.' ) ;
154143 }
155144}
156- export function manualNextChapter ( ) {
157- let bookIdx = BOOK_ORDER . indexOf ( state . settings . manualBook ) ;
158- let chap = state . settings . manualChapter ;
159- const maxCh = CHAPTER_COUNTS [ state . settings . manualBook ] ;
160- let nextBook = state . settings . manualBook ;
161- let nextChapter = chap ;
162- if ( chap < maxCh ) {
163- nextChapter = chap + 1 ;
145+ export function nextPassage ( ) {
146+ if ( state . settings . readingMode === 'readingPlan' ) {
147+ const plan = getActivePlan ( ) ;
148+ const len = plan . length ;
149+ let newIndex = ( state . settings . currentPassageIndex + 1 ) % len ;
150+ if ( newIndex < 0 ) newIndex = len - 1 ;
151+ state . settings . currentPassageIndex = newIndex ;
152+ const passage = plan [ newIndex ] ;
153+ const translation = getCurrentTranslation ( ) ;
154+ updateURL ( translation , passage . book , passage . chapter ) ;
155+ updateUIMode ( 'readingPlan' , translation , passage . displayRef ) ;
156+ loadPassage ( passage . book , passage . chapter , translation ) ;
157+ syncSelectorsToReadingPlan ( ) ;
164158 } else {
165- if ( bookIdx < BOOK_ORDER . length - 1 ) {
166- const newBook = BOOK_ORDER [ bookIdx + 1 ] ;
167- nextBook = newBook ;
159+ let bookIdx = BOOK_ORDER . indexOf ( state . settings . manualBook ) ;
160+ let chap = state . settings . manualChapter ;
161+ const maxCh = CHAPTER_COUNTS [ state . settings . manualBook ] ;
162+ let nextBook = state . settings . manualBook ;
163+ let nextChapter = chap ;
164+ if ( chap < maxCh ) {
165+ nextChapter = chap + 1 ;
166+ } else if ( bookIdx < BOOK_ORDER . length - 1 ) {
167+ nextBook = BOOK_ORDER [ bookIdx + 1 ] ;
168168 nextChapter = 1 ;
169169 } else {
170170 return ;
171171 }
172+ updateManualNavigation ( nextBook , nextChapter ) ;
172173 }
173- state . settings . manualBook = nextBook ;
174- state . settings . manualChapter = nextChapter ;
175- state . settings . readingMode = 'manual' ;
176- const translation = getCurrentTranslation ( ) ;
177- updateURL ( translation , nextBook , nextChapter ) ;
178- const displayRef = `${ nextBook } ${ nextChapter } ` ;
179- document . getElementById ( 'passageReference' ) . textContent = displayRef ;
180- state . currentPassageReference = displayRef ;
181- loadSelectedChapter ( nextBook , nextChapter ) ;
182- syncBookChapterSelectors ( ) ;
183- saveToStorage ( ) ;
184- if ( state . settings . referencePanelOpen ) {
185- updateReferencePanel ( ) ;
186- }
174+ document . getElementById ( 'scriptureSection' ) . scrollTop = 0 ;
187175}
188176export function prevPassage ( ) {
189177 if ( state . settings . readingMode === 'readingPlan' ) {
190- const len = getActivePlan ( ) . length ;
178+ const plan = getActivePlan ( ) ;
179+ const len = plan . length ;
191180 let newIndex = ( state . settings . currentPassageIndex - 1 + len ) % len ;
192181 state . settings . currentPassageIndex = newIndex ;
193- const plan = getActivePlan ( ) ;
194182 const passage = plan [ newIndex ] ;
195183 const translation = getCurrentTranslation ( ) ;
196184 updateURL ( translation , passage . book , passage . chapter ) ;
185+ updateUIMode ( 'readingPlan' , translation , passage . displayRef ) ;
197186 loadPassage ( passage . book , passage . chapter , translation ) ;
187+ syncSelectorsToReadingPlan ( ) ;
198188 } else {
199- manualPrevChapter ( ) ;
189+ let bookIdx = BOOK_ORDER . indexOf ( state . settings . manualBook ) ;
190+ let chap = state . settings . manualChapter ;
191+ let nextBook = state . settings . manualBook ;
192+ let nextChapter = chap ;
193+ if ( chap > 1 ) {
194+ nextChapter = chap - 1 ;
195+ } else if ( bookIdx > 0 ) {
196+ const prevBook = BOOK_ORDER [ bookIdx - 1 ] ;
197+ nextBook = prevBook ;
198+ nextChapter = CHAPTER_COUNTS [ prevBook ] ;
199+ } else {
200+ return ;
201+ }
202+ updateManualNavigation ( nextBook , nextChapter ) ;
200203 }
201204 document . getElementById ( 'scriptureSection' ) . scrollTop = 0 ;
202205}
203- export function nextPassage ( ) {
204- if ( state . settings . readingMode === 'readingPlan' ) {
205- const len = getActivePlan ( ) . length ;
206- let newIndex = ( state . settings . currentPassageIndex + 1 ) % len ;
207- if ( newIndex < 0 ) newIndex = len - 1 ;
208- state . settings . currentPassageIndex = newIndex ;
209- const plan = getActivePlan ( ) ;
210- const passage = plan [ newIndex ] ;
211- const translation = getCurrentTranslation ( ) ;
212- updateURL ( translation , passage . book , passage . chapter ) ;
213- loadPassage ( passage . book , passage . chapter , translation ) ;
214- } else {
215- manualNextChapter ( ) ;
206+ function updateManualNavigation ( book , chapter ) {
207+ state . settings . readingMode = 'manual' ;
208+ state . settings . manualBook = book ;
209+ state . settings . manualChapter = chapter ;
210+ const translation = getCurrentTranslation ( ) ;
211+ updateURL ( translation , book , chapter ) ;
212+ const displayRef = ` ${ book } ${ chapter } ` ;
213+ updateUIMode ( 'manual' , translation , displayRef ) ;
214+ loadSelectedChapter ( book , chapter ) ;
215+ syncBookChapterSelectors ( ) ;
216+ saveToStorage ( ) ;
217+ if ( state . settings . referencePanelOpen ) {
218+ updateReferencePanel ( ) ;
216219 }
217- document . getElementById ( 'scriptureSection' ) . scrollTop = 0 ;
218220}
219- export async function randomPassage ( ) {
220- try {
221- state . settings . readingMode = 'manual' ;
222- const randomLoc = await getRandomBibleLocation ( ) ;
223- state . settings . manualBook = randomLoc . book ;
224- state . settings . manualChapter = randomLoc . chapter ;
225- const translation = getCurrentTranslation ( ) ;
226- updateURL ( translation , randomLoc . book , randomLoc . chapter ) ;
227- saveToStorage ( ) ;
228- await loadPassageFromAPI ( randomLoc ) ;
229- document . getElementById ( 'passageReference' ) . textContent = randomLoc . displayRef ;
230- state . currentPassageReference = randomLoc . displayRef ;
231- syncBookChapterSelectors ( ) ;
232- if ( state . settings . referencePanelOpen ) {
233- updateReferencePanel ( ) ;
221+ function updateUIMode ( mode , translation , displayRef ) {
222+ const headerTitleEl = document . getElementById ( 'passageHeaderTitle' ) ;
223+ const planLabelEl = document . getElementById ( 'planLabel' ) ;
224+ const passageRefElement = document . getElementById ( 'passageReference' ) ;
225+ if ( headerTitleEl ) {
226+ headerTitleEl . textContent = `Holy Bible: ${ translation } ` ;
227+ }
228+ if ( passageRefElement ) {
229+ passageRefElement . textContent = displayRef ;
230+ state . currentPassageReference = displayRef ;
231+ }
232+ if ( planLabelEl ) {
233+ if ( mode === 'readingPlan' ) {
234+ planLabelEl . textContent = `Reading plan: ${ getCurrentPlanLabel ( ) } ` ;
235+ } else {
236+ planLabelEl . textContent = '' ;
234237 }
235- } catch ( err ) {
236- handleError ( err , 'randomPassage' ) ;
237- showError ( 'Could not load a random passage – see console for details.' ) ;
238238 }
239239}
240240export function syncBookChapterSelectors ( ) {
@@ -339,7 +339,7 @@ export function navigateFromURL() {
339339 return false ;
340340}
341341function loadDefaultPassage ( params ) {
342- state . settings . readingMode = 'manual ' ;
342+ state . settings . readingMode = 'readingPlan ' ;
343343 state . settings . manualBook = params . book ;
344344 state . settings . manualChapter = params . chapter ;
345345 state . settings . bibleTranslation = params . translation ;
@@ -362,32 +362,22 @@ function loadDefaultPassage(params) {
362362 loadSelectedChapter ( params . book , params . chapter ) ;
363363 return true ;
364364}
365- function updateNavigation ( book , chapter , translation ) {
366- updateURL ( translation , book , chapter ) ;
367- }
368365export function setupPopStateListener ( ) {
369- window . addEventListener ( 'popstate' , ( event ) => {
370- if ( event . state ) {
371- const { translation, book, chapter } = event . state ;
372- loadPassage ( book , chapter , translation ) ;
373- } else {
374- navigateFromURL ( ) ;
375- }
376- } ) ;
366+ window . addEventListener ( 'popstate' , navigateFromURL ) ;
377367}
378368export function setupNavigationWithURL ( ) {
379369 document . getElementById ( 'bookSelect' ) . addEventListener ( 'change' , ( e ) => {
380370 const book = e . target . value ;
381- const chapter = 1 ;
371+ const chapter = 1 ;
382372 const translation = getCurrentTranslation ( ) ;
383- updateNavigation ( book , chapter , translation ) ;
384- loadPassage ( book , chapter , translation ) ;
373+ updateURL ( translation , book , chapter ) ;
374+ loadSelectedChapter ( book , chapter ) ;
385375 } ) ;
386376 document . getElementById ( 'chapterSelect' ) . addEventListener ( 'change' , ( e ) => {
387377 const book = document . getElementById ( 'bookSelect' ) . value ;
388378 const chapter = parseInt ( e . target . value ) ;
389379 const translation = getCurrentTranslation ( ) ;
390- updateNavigation ( book , chapter , translation ) ;
391- loadPassage ( book , chapter , translation ) ;
380+ updateURL ( translation , book , chapter ) ;
381+ loadSelectedChapter ( book , chapter ) ;
392382 } ) ;
393383}
0 commit comments