@@ -14,6 +14,8 @@ import '@internetarchive/icon-toc/icon-toc';
1414jQuery . extend ( BookReader . defaultOptions , {
1515 olHost : 'https://openlibrary.org' ,
1616 enableChaptersPlugin : true ,
17+ /** @type {TocEntry[] } */
18+ table_of_contents : null ,
1719 bookId : '' ,
1820} ) ;
1921
@@ -28,11 +30,28 @@ BookReader.prototype.init = (function(super_) {
2830} ) ( BookReader . prototype . init ) ;
2931
3032BookReader . 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- ) ) ;
33+ let rawTableOfContents = null ;
34+ // Prefer IA TOC for now, until we update the second half to check for
35+ // `openlibrary_edition` on the IA metadata instead of making a bunch of
36+ // requests to OL.
37+ if ( this . options . table_of_contents ?. length ) {
38+ rawTableOfContents = this . options . table_of_contents ;
39+ } else {
40+ const olEdition = await this . getOpenLibraryRecord ( this . options . olHost , this . options . bookId ) ;
41+ if ( olEdition ?. table_of_contents ?. length ) {
42+ rawTableOfContents = olEdition . table_of_contents ;
43+ }
44+ }
45+
46+ if ( rawTableOfContents ) {
47+ this . _tocEntries = rawTableOfContents
48+ . map ( rawTOCEntry => ( Object . assign ( { } , rawTOCEntry , {
49+ pageIndex : (
50+ rawTOCEntry . pagenum ? this . book . getPageIndex ( rawTOCEntry . pagenum ) :
51+ rawTOCEntry . leaf ? this . book . leafNumToIndex ( rawTOCEntry . leaf ) :
52+ undefined
53+ ) ,
54+ } ) ) ) ;
3655 this . _chaptersRender ( this . _tocEntries ) ;
3756 this . bind ( BookReader . eventNames . pageChanged , ( ) => this . _chaptersUpdateCurrent ( ) ) ;
3857 }
@@ -60,19 +79,18 @@ BookReader.prototype._chaptersRender = function() {
6079 / > ` ,
6180 } ;
6281 shell . updateMenuContents ( ) ;
63- for ( const tocEntry of this . _tocEntries ) {
64- this . _chaptersRenderMarker ( tocEntry ) ;
65- }
82+ this . _tocEntries . forEach ( ( tocEntry , i ) => this . _chaptersRenderMarker ( tocEntry , i ) ) ;
6683} ;
6784
6885/**
6986 * @typedef {Object } TocEntry
7087 * 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
88+ * @property {number } [level]
89+ * @property {string } [label]
90+ * @property {string } [title]
91+ * @property {PageString } [pagenum]
92+ * @property {LeafNum } [leaf]
93+ * @property {number } [pageIndex] - Added
7694 *
7795 * @example {
7896 * "pagenum": "17",
@@ -84,21 +102,25 @@ BookReader.prototype._chaptersRender = function() {
84102
85103/**
86104 * @param {TocEntry } tocEntry
105+ * @param {number } entryIndex
87106 */
88- BookReader . prototype . _chaptersRenderMarker = function ( tocEntry ) {
107+ BookReader . prototype . _chaptersRenderMarker = function ( tocEntry , entryIndex ) {
89108 if ( tocEntry . pageIndex == undefined ) return ;
90109
91110 //creates a string with non-void tocEntry.label and tocEntry.title
92111 const chapterStr = [ tocEntry . label , tocEntry . title ]
93112 . filter ( x => x )
94- . join ( ' ' ) ;
113+ . join ( ' ' ) || `Chapter ${ entryIndex + 1 } ` ;
95114
96115 const percentThrough = BookReader . util . cssPercentage ( tocEntry . pageIndex , this . book . getNumLeafs ( ) - 1 ) ;
97116 $ ( `<div></div>` )
98117 . append (
99118 $ ( '<div />' )
100119 . text ( chapterStr )
101- . append ( $ ( '<div class="BRchapterPage" />' ) . text ( `Page ${ tocEntry . pagenum } ` ) )
120+ . append (
121+ $ ( '<div class="BRchapterPage" />' )
122+ . text ( this . book . getPageName ( tocEntry . pageIndex ) )
123+ )
102124 )
103125 . addClass ( 'BRchapter' )
104126 . css ( { left : percentThrough } )
0 commit comments