From: Michael Ang Date: Tue, 5 Oct 2010 23:14:06 +0000 (+0000) Subject: Work in progress on retrieving table of contents from Open Library X-Git-Url: http://git.rot13.org/?p=bookreader.git;a=commitdiff_plain;h=a5e73bc7eb1271d1c23d417ed4f4653f0a7fffa7 Work in progress on retrieving table of contents from Open Library --- diff --git a/BookReader/BookReader.js b/BookReader/BookReader.js index a4bcf0d..e4fbf51 100644 --- a/BookReader/BookReader.js +++ b/BookReader/BookReader.js @@ -113,6 +113,17 @@ function BookReader() { autofit: 'auto' }; + // This object/dictionary controls which optional features are enabled + // XXXmang in progress + this.features = { + // search + // read aloud + // open library entry + // table of contents + // embed/share ui + // info ui + }; + return this; }; @@ -253,6 +264,46 @@ BookReader.prototype.init = function() { // Enact other parts of initial params this.updateFromParams(params); + // Start AJAX request for OL data + this.getOpenLibraryJSON(this.gotOpenLibraryRecord); + +} + +// XXXmang +BookReader.prototype.gotOpenLibraryRecord = function(olObject) { + // console.log(olObject); + if (olObject) { + if (olObject['table_of_contents']) { + console.log('xxx updating table of contents'); + br.updateTOC(olObject['table_of_contents']); // XXX + } + } +} + +BookReader.prototype.updateTOC = function(tocEntries) { + this.removeChapters(); + for (var i = 0; i < tocEntries.length; i++) { + this.addChapterFromEntry(tocEntries[i]); + } +} + +/* + * Example table of contents entry - this format is defined by Open Library + * { + * "pagenum": "17", + * "level": 1, + * "label": "CHAPTER I", + * "type": {"key": "/type/toc_item"}, + * "title": "THE COUNTRY AND THE MISSION" + * } + */ +BookReader.prototype.addChapterFromEntry = function(tocEntryObject) { + console.log(tocEntryObject); + var pageIndex = this.getPageIndex(tocEntryObject['pagenum']); + // Only add if we know where it is + if (pageIndex) { + this.addChapter(tocEntryObject['title'], tocEntryObject['pagenum'], pageIndex); + } } BookReader.prototype.setupKeyListeners = function() { @@ -3281,6 +3332,8 @@ BookReader.prototype.initNavbar = function() { }); */ + // XXXmang testing + this.addSearchResult('hi there', '25', 22); $("#pager").draggable({axis:'x',containment:'parent'}); } @@ -3349,6 +3402,7 @@ BookReader.prototype.addChapter = function(chapterTitle, pageNumber, pageIndex) $('
' + chapterTitle + '| ' + uiStringPage + ' ' + pageNumber + '
') .appendTo('#BRnavpos') + .data({'self': this, 'pageIndex': pageIndex }) .bt({ contentSelector: '$(this).find(".title")', trigger: 'hover', @@ -3390,7 +3444,10 @@ BookReader.prototype.addChapter = function(chapterTitle, pageNumber, pageIndex) }, function() { $(this).removeClass('front'); } - ); + ) + .bind('click', function() { + $(this).data('self').jumpToIndex($(this).data('pageIndex')); + }); } BookReader.prototype.removeChapters = function() { @@ -4108,6 +4165,15 @@ BookReader.prototype._getPageURI = function(index, reduce, rotate) { return this.getPageURI(index, reduce, rotate); } + +/////// Functions that can/should be overriden by third-parties + +// If your book has a record on Open Library you get some nice things for free +BookReader.prototype.getOpenLibraryJSON = function(callback) { + return null; +} + + // Library functions BookReader.util = { disableSelect: function(jObject) { diff --git a/BookReaderIA/datanode/BookReaderJSIA.php b/BookReaderIA/datanode/BookReaderJSIA.php index e20c44e..8b48238 100644 --- a/BookReaderIA/datanode/BookReaderJSIA.php +++ b/BookReaderIA/datanode/BookReaderJSIA.php @@ -328,6 +328,35 @@ br.getEmbedCode = function() { return ""; } +// getOpenLibraryJSON +br.getOpenLibraryJSON = function(callback) { + // Try looking up by ocaid first, then by source_record + + var jsonURL = 'http://openlibrary.org/query.json?type=/type/edition&*=&ocaid=' + br.bookId; + $.ajax({ + url: jsonURL, + success: function(data) { + if (data && data.length > 0) { + callback(data[0]); + } else { + // try sourceid + console.log('XXXmang couldnt find via ocaid'); + jsonURL = 'http://openlibrary.org/query.json?type=/type/edition&*=&source_records=ia:' + br.bookId; + $.ajax({ + url: jsonURL, + success: function(data) { + if (data && data.length > 0) { + callback(data[0]); + } + }, + dataType: 'jsonp' + }); + } + }, + dataType: 'jsonp' + }); +} + br.pageW = [