Merge branch 'master' of git://github.com/openlibrary/bookreader
[bookreader.git] / GnuBook / GnuBook.js
index e1dfe34..434883b 100644 (file)
@@ -861,7 +861,7 @@ GnuBook.prototype.prepareTwoPagePopUp = function() {
     $(this.leafEdgeR).bind('mousemove', this, function(e) {
 
         var jumpIndex = e.data.jumpIndexForRightEdgePageX(e.pageX);
-        $(e.data.twoPagePopUp).text('View Page '+ e.data.getPageNum(jumpIndex));
+        $(e.data.twoPagePopUp).text('View ' + e.data.getPageName(jumpIndex));
         
         $(e.data.twoPagePopUp).css({
             left: e.pageX +5+ 'px',
@@ -872,7 +872,7 @@ GnuBook.prototype.prepareTwoPagePopUp = function() {
     $(this.leafEdgeL).bind('mousemove', this, function(e) {
     
         var jumpIndex = e.data.jumpIndexForLeftEdgePageX(e.pageX);
-        $(e.data.twoPagePopUp).text('View Page '+ e.data.getPageNum(jumpIndex));
+        $(e.data.twoPagePopUp).text('View '+ e.data.getPageName(jumpIndex));
         
         $(e.data.twoPagePopUp).css({
             left: e.pageX - $(e.data.twoPagePopUp).width() - 30 + 'px',
@@ -1571,6 +1571,7 @@ GnuBook.prototype.search = function(term) {
        script.setAttribute("type", "text/javascript");
        script.setAttribute("src", 'http://'+this.server+'/GnuBook/flipbook_search_gb.php?url='+escape(this.bookPath+'/'+this.bookId+'_djvu.xml')+'&term='+term+'&format=XML&callback=gb.GBSearchCallback');
        document.getElementsByTagName('head')[0].appendChild(script);
+       $('#GnuBookSearchResults').html('Searching...');
 }
 
 // GBSearchCallback()
@@ -1632,8 +1633,9 @@ GnuBook.prototype.GBSearchCallback = function(txt) {
                     }
                 }
             }
+            var pageName = this.getPageName(index);
             //TODO: remove hardcoded instance name
-            $('#GnuBookSearchResults').append('<li><b><a href="javascript:gb.jumpToIndex('+index+');">Leaf ' + index + '</a></b> - ' + context+'</li>');
+            $('#GnuBookSearchResults').append('<li><b><a href="javascript:gb.jumpToIndex('+index+');">' + pageName + '</a></b> - ' + context + '</li>');
         }
     }
     $('#GnuBookSearchResults').append('</ul>');
@@ -1868,14 +1870,14 @@ GnuBook.prototype.jumpIndexForLeftEdgePageX = function(pageX) {
     if ('rl' != this.pageProgression) {
         // LTR - flipping backward
         var jumpIndex = this.currentIndexL - ($(this.leafEdgeL).offset().left + $(this.leafEdgeL).width() - pageX) * 10;
-        // browser may have resized the div due to font size change -- see https://bugs.launchpad.net/gnubook/+bug/333570
-        jumpIndex = Math.min(jumpIndex, this.currentIndexL - 2);
-        jumpIndex = Math.max(jumpIndex, this.firstDisplayableIndex());
+
+        // browser may have resized the div due to font size change -- see https://bugs.launchpad.net/gnubook/+bug/333570        
+        jumpIndex = GnuBook.util.clamp(Math.round(jumpIndex), this.firstDisplayableIndex(), this.currentIndexL - 2);
         return jumpIndex;
+
     } else {
         var jumpIndex = this.currentIndexL + ($(this.leafEdgeL).offset().left + $(this.leafEdgeL).width() - pageX) * 10;
-        jumpIndex = Math.max(jumpIndex, this.currentIndexL + 2);
-        jumpIndex = Math.min(jumpIndex, this.lastDisplayableIndex());
+        jumpIndex = GnuBook.util.clamp(Math.round(jumpIndex), this.currentIndexL + 2, this.lastDisplayableIndex());
         return jumpIndex;
     }
 }
@@ -1887,13 +1889,11 @@ GnuBook.prototype.jumpIndexForRightEdgePageX = function(pageX) {
     if ('rl' != this.pageProgression) {
         // LTR
         var jumpIndex = this.currentIndexR + (pageX - $(this.leafEdgeR).offset().left) * 10;
-        jumpIndex = Math.max(jumpIndex, this.currentIndexR + 2);
-        jumpIndex = Math.min(jumpIndex, this.lastDisplayableIndex());
+        jumpIndex = GnuBook.util.clamp(Math.round(jumpIndex), this.currentIndexR + 2, this.lastDisplayableIndex());
         return jumpIndex;
     } else {
         var jumpIndex = this.currentIndexR - (pageX - $(this.leafEdgeR).offset().left) * 10;
-        jumpIndex = Math.min(jumpIndex, this.currentIndexR - 2);
-        jumpIndex = Math.max(jumpIndex, this.firstDisplayableIndex());
+        jumpIndex = GnuBook.util.clamp(Math.round(jumpIndex), this.firstDisplayableIndex(), this.currentIndexR - 2);
         return jumpIndex;
     }
 }
@@ -1903,7 +1903,7 @@ GnuBook.prototype.initToolbar = function(mode, ui) {
         + "<a class='GBicon logo rollover' href='" + this.logoURL + "'>&nbsp;</a>"
         + " <button class='GBicon rollover zoom_out' onclick='gb.zoom1up(-1); return false;'/>" 
         + "<button class='GBicon rollover zoom_in' onclick='gb.zoom1up(1); return false;'/>"
-        + " <span class='label'>Zoom: <span id='GBzoom'>25</span>%</span>"
+        + " <span class='label'>Zoom: <span id='GBzoom'>"+100/this.reduce+"</span>%</span>"
         + " <button class='GBicon rollover one_page_mode' onclick='gb.switchMode(1); return false;'/>"
         + " <button class='GBicon rollover two_page_mode' onclick='gb.switchMode(2); return false;'/>"
         + "&nbsp;&nbsp;<a class='GBblack title' href='"+this.bookUrl+"' target='_blank'>"+this.shortTitle(50)+"</a>"
@@ -2251,7 +2251,7 @@ GnuBook.prototype.getPageIndices = function(pageNum) {
     if (pageNum.slice(0,1) == 'n') {
         try {
             var pageIntStr = pageNum.slice(1, pageNum.length);
-            pageIndex = parseInt(pageIntStr);
+            var pageIndex = parseInt(pageIntStr);
             indices.append(pageIndex);
             return indices;
         } catch(err) {
@@ -2269,6 +2269,13 @@ GnuBook.prototype.getPageIndices = function(pageNum) {
     return indices;
 }
 
+// getPageName(index)
+//________
+// Returns the name of the page as it should be displayed in the user interface
+GnuBook.prototype.getPageName = function(index) {
+    return 'Page ' + this.getPageNum(index);
+}
+
 // updateLocationHash
 //________
 // Update the location hash from the current parameters.  Call this instead of manually
@@ -2328,4 +2335,12 @@ GnuBook.prototype.getEmbedURL = function() {
 // Returns the embed code HTML fragment suitable for copy and paste
 GnuBook.prototype.getEmbedCode = function() {
     return "<iframe src='" + this.getEmbedURL() + "' width='480px' height='430px'></iframe>";
+}
+
+
+// Library functions
+GnuBook.util = {
+    clamp: function(value, min, max) {
+        return Math.min(Math.max(value, min), max);
+    }
 }
\ No newline at end of file