Merge thumbnail view feature contributed by scollett
[bookreader.git] / BookReader / BookReader.js
index 2ec1a0a..e581dda 100644 (file)
@@ -40,11 +40,13 @@ function BookReader() {
     this.padding = 10;
     this.mode    = 1; //1, 2, 3
     this.ui = 'full'; // UI mode
-    this.thumbScale = 10; // thumbnail default
-    this.thumbRowBuffer = 4; // number of rows to pre-cache out a view
 
-    this.displayedIndices = [];        
+    // thumbnail mode
+    this.thumbWidth = 100;
+    this.thumbRowBuffer = 3; // number of rows to pre-cache out a view
     this.displayedRows=[];
+    
+    this.displayedIndices = [];
     //this.indicesToDisplay = [];
     this.imgs = {};
     this.prefetchedImgs = {}; //an object with numeric keys cooresponding to page index
@@ -583,7 +585,7 @@ BookReader.prototype.drawLeafsThumbnail = function() {
     var leafMap = [];
 
     for (i=0; i<this.numLeafs; i++) {
-        leafWidth = parseInt(this.getPageWidth(i)/this.reduce, 10);
+        leafWidth = this.thumbWidth;
         if (rightPos + (leafWidth + this.padding) > viewWidth){
             currentRow++;
             rightPos = 0;
@@ -600,7 +602,7 @@ BookReader.prototype.drawLeafsThumbnail = function() {
         leafMap[currentRow].leafs[leafIndex].num = i;
         leafMap[currentRow].leafs[leafIndex].left = rightPos;
 
-        leafHeight = parseInt(this.getPageHeight(leafMap[currentRow].leafs[leafIndex].num)/this.reduce, 10);
+        leafHeight = parseInt((this.getPageHeight(leafMap[currentRow].leafs[leafIndex].num)*this.thumbWidth)/this.getPageWidth(leafMap[currentRow].leafs[leafIndex].num), 10);
         if (leafHeight > leafMap[currentRow].height) { leafMap[currentRow].height = leafHeight; }
         if (leafIndex===0) { bottomPos += this.padding + leafMap[currentRow].height; }
         rightPos += leafWidth + this.padding;
@@ -632,12 +634,15 @@ BookReader.prototype.drawLeafsThumbnail = function() {
         leafTop = leafBottom;
     }
 
+    // create a buffer of preloaded rows before and after the visible rows
     var firstRow = rowsToDisplay[0];
     var lastRow = rowsToDisplay[rowsToDisplay.length-1];
     for (i=1; i<this.thumbRowBuffer+1; i++) {
-        if (firstRow-i >= 0) { rowsToDisplay.unshift(firstRow-i); }
         if (lastRow+i < leafMap.length) { rowsToDisplay.push(lastRow+i); }
-}
+    }
+    for (i=1; i<this.thumbRowBuffer+1; i++) {
+        if (firstRow-i >= 0) { rowsToDisplay.push(firstRow-i); }
+    }
 
     // Update hash, but only if we're currently displaying a leaf
     // Hack that fixes #365790
@@ -661,8 +666,8 @@ BookReader.prototype.drawLeafsThumbnail = function() {
                 index = j;
                 leaf = leafMap[row].leafs[j].num;
 
-                leafWidth = parseInt(this.getPageWidth(leaf)/this.reduce, 10);
-                leafHeight = parseInt(this.getPageHeight(leaf)/this.reduce, 10);
+                leafWidth = this.thumbWidth;
+                leafHeight = parseInt((this.getPageHeight(leaf)*this.thumbWidth)/this.getPageWidth(leaf), 10);
                 leafTop = leafMap[row].top;
                 left = leafMap[row].leafs[index].left + pageViewBuffer;
                 if ('rl' == this.pageProgression){
@@ -1107,13 +1112,13 @@ BookReader.prototype.jumpToIndex = function(index, pageX, pageY) {
         var leafIndex = 0;
 
         for (i=0; i<(index+1); i++) {
-            leafWidth = parseInt(this.getPageWidth(i)/this.reduce, 10);
+            leafWidth = this.thumbWidth;
             if (rightPos + (leafWidth + this.padding) > viewWidth){
                 rightPos = 0;
                 rowHeight = 0;
                 leafIndex = 0;
             }
-            leafHeight = parseInt(this.getPageHeight(i)/this.reduce, 10);
+            leafHeight = parseInt((this.getPageHeight(leaf)*this.thumbWidth)/this.getPageWidth(leaf), 10);
             if(leafHeight > rowHeight) { rowHeight = leafHeight; }
             if (leafIndex==0) { leafTop = bottomPos; }
             if (leafIndex==0) { bottomPos += this.padding + rowHeight; }
@@ -1235,8 +1240,8 @@ BookReader.prototype.prepareThumbnailView = function() {
 
     // var startLeaf = this.displayedIndices[0];
     var startLeaf = this.currentIndex();
-    this.reduce = this.thumbScale;
-
+    this.reduce = this.getPageWidth(0)/this.thumbWidth;
+    
     $('#BRcontainer').empty();
     $('#BRcontainer').css({
         overflowY: 'scroll',
@@ -2376,13 +2381,13 @@ BookReader.prototype.search = function(term) {
     term = term.replace(/\//g, ' '); // strip slashes
     this.searchTerm = term;
     $('#BookReaderSearchScript').remove();
-       var script  = document.createElement("script");
-       script.setAttribute('id', 'BookReaderSearchScript');
-       script.setAttribute("type", "text/javascript");
-       script.setAttribute("src", 'http://'+this.server+'/BookReader/flipbook_search_br.php?url='+escape(this.bookPath + '_djvu.xml')+'&term='+term+'&format=XML&callback=br.BRSearchCallback');
-       document.getElementsByTagName('head')[0].appendChild(script);
-       $('#BookReaderSearchBox').val(term);
-       $('#BookReaderSearchResults').html('Searching...');
+    var script  = document.createElement("script");
+    script.setAttribute('id', 'BookReaderSearchScript');
+    script.setAttribute("type", "text/javascript");
+    script.setAttribute("src", 'http://'+this.server+'/BookReader/flipbook_search_br.php?url='+escape(this.bookPath + '_djvu.xml')+'&term='+term+'&format=XML&callback=br.BRSearchCallback');
+    document.getElementsByTagName('head')[0].appendChild(script);
+    $('#BookReaderSearchBox').val(term);
+    $('#BookReaderSearchResults').html('Searching...');
 }
 
 // BRSearchCallback()
@@ -2454,7 +2459,7 @@ BookReader.prototype.BRSearchCallback = function(txt) {
     $('#BookReaderSearchResults').append('</ul>');
 
     // $$$ update again for case of loading search URL in new browser window (search box may not have been ready yet)
-       $('#BookReaderSearchBox').val(this.searchTerm);
+    $('#BookReaderSearchBox').val(this.searchTerm);
 
     this.updateSearchHilites();
 }
@@ -3600,6 +3605,27 @@ BookReader.prototype._getPageURI = function(index, reduce, rotate) {
         return this.imagesBaseURL + "/transparent.png";
     }
     
+    if ('undefined' == typeof(reduce)) {
+        // reduce not passed in
+        var ratio = this.getPageHeight(index) / this.twoPage.height;
+        var scale;
+        // $$$ we make an assumption here that the scales are available pow2 (like kakadu)
+        if (ratio < 2) {
+            scale = 1;
+        } else if (ratio < 4) {
+            scale = 2;
+        } else if (ratio < 8) {
+            scale = 4;
+        } else if (ratio < 16) {
+            scale = 8;
+        } else  if (ratio < 32) {
+            scale = 16;
+        } else {
+            scale = 32;
+        }
+        reduce = scale;
+    }
+    
     return this.getPageURI(index, reduce, rotate);
 }