Merge branch 'master' of git://github.com/openlibrary/bookreader
[bookreader.git] / GnuBook / GnuBook.js
index 2e60e9e..434883b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright(c)2008 Internet Archive. Software license AGPL version 3.
+Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
 
 This file is part of GnuBook.
 
@@ -84,8 +84,10 @@ GnuBook.prototype.init = function() {
         startIndex = this.getPageIndex(params.page);
     }
     
-    if ('undefined' == typeof(startIndex)) {    
-        startIndex = this.leafNumToIndex(this.titleLeaf);
+    if ('undefined' == typeof(startIndex)) {
+        if ('undefined' != typeof(this.titleLeaf)) {
+            startIndex = this.leafNumToIndex(this.titleLeaf);
+        }
     }
     
     if ('undefined' == typeof(startIndex)) {
@@ -818,7 +820,7 @@ GnuBook.prototype.prepareTwoPageView = function() {
 
 // prepareTwoPagePopUp()
 //
-// This function prepares the "View leaf n" popup that shows while the mouse is
+// This function prepares the "View Page n" popup that shows while the mouse is
 // over the left/right "stack of sheets" edges.  It also binds the mouse
 // events for these divs.
 //______________________________________________________________________________
@@ -859,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 Leaf '+jumpIndex);
+        $(e.data.twoPagePopUp).text('View ' + e.data.getPageName(jumpIndex));
         
         $(e.data.twoPagePopUp).css({
             left: e.pageX +5+ 'px',
@@ -870,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 Leaf '+jumpIndex);
+        $(e.data.twoPagePopUp).text('View '+ e.data.getPageName(jumpIndex));
         
         $(e.data.twoPagePopUp).css({
             left: e.pageX - $(e.data.twoPagePopUp).width() - 30 + 'px',
@@ -1569,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()
@@ -1630,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>');
@@ -1866,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;
     }
 }
@@ -1885,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;
     }
 }
@@ -1901,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>"
@@ -1918,8 +1920,8 @@ GnuBook.prototype.initToolbar = function(mode, ui) {
     jToolbar.append("<span id='GBtoolbarbuttons' style='float: right'>"
         + "<button class='GBicon rollover embed' />"
         + "<form class='GBpageform' action='javascript:' onsubmit='gb.jumpToPage(this.elements[0].value)'> <span class='label'>Page:<input id='GBpagenum' type='text' size='3' onfocus='gb.autoStop();'></input></span></form>"
-        + "<div class='GBtoolbarmode2' style='display: inline'><button class='GBicon rollover book_leftmost' /><button class='GBicon rollover book_left' /><button class='GBicon rollover book_right' /><button class='GBicon rollover book_rightmost' /></div>"
-        + "<div class='GBtoolbarmode1' style='display: hidden'><button class='GBicon rollover book_top' /><button class='GBicon rollover book_up' /> <button class='GBicon rollover book_down' /><button class='GBicon rollover book_bottom' /></div>"
+        + "<div class='GBtoolbarmode2' style='display: none'><button class='GBicon rollover book_leftmost' /><button class='GBicon rollover book_left' /><button class='GBicon rollover book_right' /><button class='GBicon rollover book_rightmost' /></div>"
+        + "<div class='GBtoolbarmode1' style='display: none'><button class='GBicon rollover book_top' /><button class='GBicon rollover book_up' /> <button class='GBicon rollover book_down' /><button class='GBicon rollover book_bottom' /></div>"
         + "<button class='GBicon rollover play' /><button class='GBicon rollover pause' style='display: none' /></span>");
 
     this.bindToolbarNavHandlers(jToolbar);
@@ -1966,11 +1968,11 @@ GnuBook.prototype.switchToolbarMode = function(mode) {
     if (1 == mode) {
         // 1-up     
         $('#GBtoolbar .GBtoolbarmode2').hide();
-        $('#GBtoolbar .GBtoolbarmode1').css('display', 'inline').show();
+        $('#GBtoolbar .GBtoolbarmode1').show().css('display', 'inline');
     } else {
         // 2-up
         $('#GBtoolbar .GBtoolbarmode1').hide();
-        $('#GBtoolbar .GBtoolbarmode2').css('display', 'inline').show();
+        $('#GBtoolbar .GBtoolbarmode2').show().css('display', 'inline');
     }
 }
 
@@ -2108,6 +2110,7 @@ GnuBook.prototype.updateFromParams = function(params) {
             this.jumpToIndex(params.index);
         }
     } else if ('undefined' != typeof(params.page)) {
+        // $$$ this assumes page numbers are unique
         if (params.page != this.getPageNum(this.currentIndex())) {
             this.jumpToPage(params.page);
         }
@@ -2227,16 +2230,30 @@ GnuBook.prototype.fragmentFromParams = function(params) {
 
 // getPageIndex(pageNum)
 //________
-// Returns the index of the given page number, or undefined
+// Returns the *highest* index the given page number, or undefined
 GnuBook.prototype.getPageIndex = function(pageNum) {
-    var pageIndex = undefined;
+    var pageIndices = this.getPageIndices(pageNum);
     
+    if (pageIndices.length > 0) {
+        return pageIndices[pageIndices.length - 1];
+    }
+
+    return undefined;
+}
+
+// getPageIndices(pageNum)
+//________
+// Returns an array (possibly empty) of the indices with the given page number
+GnuBook.prototype.getPageIndices = function(pageNum) {
+    var indices = [];
+
     // Check for special "nXX" page number
     if (pageNum.slice(0,1) == 'n') {
         try {
             var pageIntStr = pageNum.slice(1, pageNum.length);
-            pageIndex = parseInt(pageIntStr);
-            return pageIndex;
+            var pageIndex = parseInt(pageIntStr);
+            indices.append(pageIndex);
+            return indices;
         } catch(err) {
             // Do nothing... will run through page names and see if one matches
         }
@@ -2245,12 +2262,18 @@ GnuBook.prototype.getPageIndex = function(pageNum) {
     var i;
     for (i=0; i<this.numLeafs; i++) {
         if (this.getPageNum(i) == pageNum) {
-            pageIndex = i;
-            return pageIndex;
+            indices.push(i);
         }
     }
+    
+    return indices;
+}
 
-    return pageIndex;
+// 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
@@ -2312,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