@@ -28,11 +28,28 @@ BookReader.prototype.init = (function(super_) {
2828} ) ( BookReader . prototype . init ) ;
2929
3030BookReader . prototype . _chapterInit = async function ( ) {
31- const olEdition = await this . getOpenLibraryRecord ( this . options . olHost , this . options . bookId ) ;
32- if ( olEdition ?. table_of_contents ?. length ) {
33- this . _tocEntries = olEdition . table_of_contents . map ( rawTOCEntry => (
34- Object . assign ( { } , rawTOCEntry , { pageIndex : this . book . getPageIndex ( rawTOCEntry . pagenum ) } )
35- ) ) ;
31+ let rawTableOfContents = null ;
32+ // Prefer IA TOC for now, until we update the second half to check for
33+ // `openlibrary_edition` on the IA metadata instead of making a bunch of
34+ // requests to OL.
35+ if ( this . options . table_of_contents ?. length ) {
36+ rawTableOfContents = this . options . table_of_contents ;
37+ } else {
38+ const olEdition = await this . getOpenLibraryRecord ( this . options . olHost , this . options . bookId ) ;
39+ if ( olEdition ?. table_of_contents ?. length ) {
40+ rawTableOfContents = olEdition . table_of_contents ;
41+ }
42+ }
43+
44+ if ( rawTableOfContents ) {
45+ this . _tocEntries = rawTableOfContents
46+ . map ( rawTOCEntry => ( Object . assign ( { } , rawTOCEntry , {
47+ pageIndex : (
48+ typeof ( rawTOCEntry . leaf ) == 'number' ? this . book . leafNumToIndex ( rawTOCEntry . leaf ) :
49+ rawTOCEntry . pagenum ? this . book . getPageIndex ( rawTOCEntry . pagenum ) :
50+ undefined
51+ ) ,
52+ } ) ) ) ;
3653 this . _chaptersRender ( this . _tocEntries ) ;
3754 this . bind ( BookReader . eventNames . pageChanged , ( ) => this . _chaptersUpdateCurrent ( ) ) ;
3855 }
@@ -60,19 +77,18 @@ BookReader.prototype._chaptersRender = function() {
6077 / > ` ,
6178 } ;
6279 shell . updateMenuContents ( ) ;
63- for ( const tocEntry of this . _tocEntries ) {
64- this . _chaptersRenderMarker ( tocEntry ) ;
65- }
80+ this . _tocEntries . forEach ( ( tocEntry , i ) => this . _chaptersRenderMarker ( tocEntry , i ) ) ;
6681} ;
6782
6883/**
6984 * @typedef {Object } TocEntry
7085 * Table of contents entry as defined by Open Library, with some extra values for internal use
71- * @property {string } pagenum
72- * @property {number } level
73- * @property {string } label
74- * @property {string } title
75- * @property {number } pageIndex - Added
86+ * @property {number } [level]
87+ * @property {string } [label]
88+ * @property {string } [title]
89+ * @property {PageString } [pagenum]
90+ * @property {LeafNum } [leaf]
91+ * @property {number } [pageIndex] - Added
7692 *
7793 * @example {
7894 * "pagenum": "17",
@@ -84,21 +100,25 @@ BookReader.prototype._chaptersRender = function() {
84100
85101/**
86102 * @param {TocEntry } tocEntry
103+ * @param {number } entryIndex
87104 */
88- BookReader . prototype . _chaptersRenderMarker = function ( tocEntry ) {
105+ BookReader . prototype . _chaptersRenderMarker = function ( tocEntry , entryIndex ) {
89106 if ( tocEntry . pageIndex == undefined ) return ;
90107
91108 //creates a string with non-void tocEntry.label and tocEntry.title
92109 const chapterStr = [ tocEntry . label , tocEntry . title ]
93110 . filter ( x => x )
94- . join ( ' ' ) ;
111+ . join ( ' ' ) || `Chapter ${ entryIndex + 1 } ` ;
95112
96113 const percentThrough = BookReader . util . cssPercentage ( tocEntry . pageIndex , this . book . getNumLeafs ( ) - 1 ) ;
97114 $ ( `<div></div>` )
98115 . append (
99116 $ ( '<div />' )
100117 . text ( chapterStr )
101- . append ( $ ( '<div class="BRchapterPage" />' ) . text ( `Page ${ tocEntry . pagenum } ` ) )
118+ . append (
119+ $ ( '<div class="BRchapterPage" />' )
120+ . text ( this . book . getPageName ( tocEntry . pageIndex ) )
121+ )
102122 )
103123 . addClass ( 'BRchapter' )
104124 . css ( { left : percentThrough } )
0 commit comments