function GnuBook() {
this.reduce = 4;
this.padding = 10;
- this.mode = 1; //1 or 2
+ this.mode = 1; //1, 2, 3
this.ui = 'full'; // UI mode
-
+ this.thumbScale = 10; // thumbnail default
+
this.displayedIndices = [];
+ this.displayedRows=[];
//this.indicesToDisplay = [];
this.imgs = {};
this.prefetchedImgs = {}; //an object with numeric keys cooresponding to page index
// Mode constants
this.constMode1up = 1;
this.constMode2up = 2;
+ this.constModeThumb = 3;
// Zoom levels
this.reductionFactors = [0.5, 1, 2, 4, 8, 16];
GnuBook.prototype.init = function() {
var startIndex = undefined;
+ this.pageScale = this.reduce; // preserve current reduce
// Find start index and mode if set in location hash
var params = this.paramsFromFragment(window.location.hash);
e.data.displayedIndices = [];
e.data.updateSearchHilites(); //deletes hilights but does not call remove()
e.data.loadLeafs();
+ } else if (3 == e.data.mode){
+ e.data.prepareThumbnailView();
} else {
//console.log('drawing 2 page view');
this.resizePageView();
this.firstIndex = startIndex;
this.jumpToIndex(startIndex);
- } else {
+ } else if (3 == this.mode) {
+ this.firstIndex = startIndex;
+ this.prepareThumbnailView();
+ this.jumpToIndex(startIndex);
+ } else {
//this.resizePageView();
this.displayedIndices=[0];
GnuBook.prototype.drawLeafs = function() {
if (1 == this.mode) {
this.drawLeafsOnePage();
+ } else if(3 == this.mode) {
+ this.drawLeafsThumbnail();
} else {
this.drawLeafsTwoPage();
}
}
+// drawLeafsThumbnail()
+//______________________________________________________________________________
+GnuBook.prototype.drawLeafsThumbnail = function() {
+ //alert('drawing leafs!');
+ this.timer = null;
+
+ var viewWidth = $('#GBcontainer').attr('scrollWidth') - 20; // width minus buffer
+
+ //console.log('top=' + scrollTop + ' bottom='+scrollBottom);
+
+ var i;
+ var leafWidth;
+ var leafHeight;
+ var rightPos = 0;
+ var bottomPos = 0;
+ var maxRight = 0;
+ var currentRow = 0;
+ var leafIndex = 0;
+ var leafMap = [];
+
+ for (i=0; i<this.numLeafs; i++) {
+ leafWidth = parseInt(this.getPageWidth(i)/this.reduce, 10);
+ if (rightPos + (leafWidth + this.padding) > viewWidth){
+ currentRow++;
+ rightPos = 0;
+ leafIndex = 0;
+ }
+
+ if (leafMap[currentRow]===undefined) { leafMap[currentRow] = {}; }
+ if (leafMap[currentRow].leafs===undefined) {
+ leafMap[currentRow].leafs = [];
+ leafMap[currentRow].height = 0;
+ leafMap[currentRow].top = 0;
+ }
+ leafMap[currentRow].leafs[leafIndex] = {};
+ leafMap[currentRow].leafs[leafIndex].num = i;
+ leafMap[currentRow].leafs[leafIndex].left = rightPos;
+
+ leafHeight = parseInt(this.getPageHeight(leafMap[currentRow].leafs[leafIndex].num)/this.reduce, 10);
+ if (leafHeight > leafMap[currentRow].height) { leafMap[currentRow].height = leafHeight; }
+ if (leafIndex===0) { bottomPos += this.padding + leafMap[currentRow].height; }
+ rightPos += leafWidth + this.padding;
+ if (rightPos > maxRight) { maxRight = rightPos; }
+ leafIndex++;
+ }
+
+ // reset the bottom position based on thumbnails
+ $('#GBpageview').height(bottomPos);
+
+ var pageViewBuffer = Math.floor(($('#GBcontainer').attr('scrollWidth') - maxRight) / 2) - 14;
+ var scrollTop = $('#GBcontainer').attr('scrollTop');
+ var scrollBottom = scrollTop + $('#GBcontainer').height();
+
+ var leafTop = 0;
+ var leafBottom = 0;
+ var rowsToDisplay = [];
+
+ for (i=0; i<leafMap.length; i++) {
+ leafBottom += this.padding + leafMap[i].height;
+ var topInView = (leafTop >= scrollTop) && (leafTop <= scrollBottom);
+ var bottomInView = (leafBottom >= scrollTop) && (leafBottom <= scrollBottom);
+ var middleInView = (leafTop <=scrollTop) && (leafBottom>=scrollBottom);
+ if (topInView | bottomInView | middleInView) {
+ //console.log('row to display: ' + j);
+ rowsToDisplay.push(i);
+ }
+ if(leafTop > leafMap[i].top) { leafMap[i].top = leafTop; }
+ leafTop = leafBottom;
+ }
+
+ var firstRow = rowsToDisplay[0];
+ var lastRow = rowsToDisplay[rowsToDisplay.length-1];
+ var rowBuffer = 4;
+ for (i=1; i<rowBuffer+1; i++) {
+ if (firstRow-i >= 0) { rowsToDisplay.unshift(firstRow-i); }
+ if (lastRow+i < leafMap.length) { rowsToDisplay.push(lastRow+i); }
+ }
+
+ // Update hash, but only if we're currently displaying a leaf
+ // Hack that fixes #365790
+ if (this.displayedRows.length > 0) {
+ this.updateLocationHash();
+ }
+
+ var j;
+ var row;
+ var left;
+ var index;
+ var div;
+ var link;
+ var img;
+ for (i=0; i<rowsToDisplay.length; i++) {
+ if (-1 == jQuery.inArray(rowsToDisplay[i], this.displayedRows)) {
+ row = rowsToDisplay[i];
+
+
+ for (j=0; j<leafMap[row].leafs.length; j++) {
+ index = leafMap[row].leafs[j].num;
+
+ leafWidth = parseInt(this.getPageWidth(index)/this.reduce, 10);
+ leafHeight = parseInt(this.getPageHeight(index)/this.reduce, 10);
+ leafTop = leafMap[row].top;
+ left = leafMap[row].leafs[j].left + pageViewBuffer;
+
+ div = document.createElement("div");
+ div.id = 'pagediv'+index;
+ div.style.position = "absolute";
+ div.className = "GBpagedivthumb";
+
+ left += this.padding;
+ $(div).css('top', leafTop + 'px');
+ $(div).css('left', left+'px');
+ $(div).css('width', leafWidth+'px');
+ $(div).css('height', leafHeight+'px');
+ //$(div).text('loading...');
+
+ // link to page in single page mode
+ link = document.createElement("a");
+ link.href = '#page/' + (this.getPageNum(index)) +'/mode/1up' ;
+ $(div).append(link);
+
+ $('#GBpageview').append(div);
+
+ img = document.createElement("img");
+ img.src = this.getPageURI(index);
+ $(img).css('width', leafWidth+'px');
+ $(img).css('height', leafHeight+'px');
+ img.style.border = "0";
+ $(link).append(img);
+ //console.log('displaying thumbnail: ' + leafMap[j]);
+ }
+ }
+ }
+
+
+ // remove previous highlights
+ if ($('.GBpagedivthumb_highlight').length>0) {
+ div = $('.GBpagedivthumb_highlight')
+ div.attr({className: 'GBpagedivthumb' });
+ }
+ // highlight current page
+ $('#pagediv'+this.currentIndex()).attr({className: 'GBpagedivthumb_highlight' });
+
+ var k;
+ for (i=0; i<this.displayedRows.length; i++) {
+ if (-1 == jQuery.inArray(this.displayedRows[i], rowsToDisplay)) {
+ row = this.displayedRows[i];
+ for (k=0; k<leafMap[row].leafs.length; k++) {
+ index = leafMap[row].leafs[k].num;
+ //console.log('Removing leaf ' + index);
+ $('#pagediv'+index).remove();
+ }
+ } else {
+
+ //console.log('NOT Removing leaf ' + this.displayedIndices[i]);
+ }
+ }
+
+ this.displayedRows = rowsToDisplay.slice();
+
+ if (null !== this.getPageNum(this.currentIndex())) {
+ $("#GBpagenum").val(this.getPageNum(this.currentIndex()));
+ } else {
+ $("#GBpagenum").val('');
+ }
+
+ this.updateToolbarZoom(this.reduce);
+}
+
// drawLeafsTwoPage()
//______________________________________________________________________________
GnuBook.prototype.drawLeafsTwoPage = function() {
if (this.reduce >= 8) return;
this.reduce*=2; //zoom out
}
-
+
+ this.pageScale = this.reduce; // preserve current reduce
this.resizePageView();
$('#GBpageview').empty()
}
this.twoPage.autofit = newZoom.autofit;
this.reduce = newZoom.reduce;
-
+ this.pageScale = this.reduce; // preserve current reduce
+
// Preserve view center position
var oldCenter = this.twoPageGetViewCenter();
this.flipFwdToIndex(index);
}
- } else {
+ } else if (3 == this.mode){
+ var viewWidth = $('#GBcontainer').attr('scrollWidth') - 20; // width minus buffer
+ var i;
+ var leafWidth = 0;
+ var leafHeight = 0;
+ var rightPos = 0;
+ var bottomPos = 0;
+ var rowHeight = 0;
+ var leafTop = 0;
+ var leafIndex = 0;
+
+ for (i=0; i<(index+1); i++) {
+ leafWidth = parseInt(this.getPageWidth(i)/this.reduce, 10);
+ if (rightPos + (leafWidth + this.padding) > viewWidth){
+ rightPos = 0;
+ rowHeight = 0;
+ leafIndex = 0;
+ }
+ leafHeight = parseInt(this.getPageHeight(i)/this.reduce, 10);
+ if(leafHeight > rowHeight) { rowHeight = leafHeight; }
+ if (leafIndex==0) { leafTop = bottomPos; }
+ if (leafIndex==0) { bottomPos += this.padding + rowHeight; }
+ rightPos += leafWidth + this.padding;
+ leafIndex++;
+ }
+ this.firstIndex=index;
+ if ($('#GBcontainer').attr('scrollTop') == leafTop) {
+ this.loadLeafs();
+ } else {
+ $('#GBcontainer').animate({scrollTop: leafTop },'fast');
+ }
+ } else {
var i;
var leafTop = 0;
var leafLeft = 0;
this.removeSearchHilites();
this.mode = mode;
-
this.switchToolbarMode(mode);
+
+ // reinstate scale if moving from thumbnail view
+ if (this.pageScale != this.reduce) this.reduce = this.pageScale;
// $$$ TODO preserve center of view when switching between mode
// See https://bugs.edge.launchpad.net/gnubook/+bug/416682
-
+
if (1 == mode) {
this.reduce = this.quantizeReduce(this.reduce);
this.prepareOnePageView();
+ } else if (3 == mode) {
+ this.reduce = this.quantizeReduce(this.reduce);
+ this.prepareThumbnailView();
+ this.jumpToIndex(this.currentIndex());
} else {
this.twoPage.autofit = false; // Take zoom level from other mode
this.reduce = this.quantizeReduce(this.reduce);
gbPageView[0].onselectstart = function(e) { return false; };
}
+//prepareThumbnailView()
+//______________________________________________________________________________
+GnuBook.prototype.prepareThumbnailView = function() {
+
+ // var startLeaf = this.displayedIndices[0];
+ var startLeaf = this.currentIndex();
+ this.reduce = this.thumbScale;
+
+ $('#GBcontainer').empty();
+ $('#GBcontainer').css({
+ overflowY: 'scroll',
+ overflowX: 'auto'
+ });
+
+ var gbPageView = $("#GBcontainer").append("<div id='GBpageview'></div>");
+
+ this.resizePageView();
+
+ this.displayedRows = [];
+ this.drawLeafsThumbnail();
+
+ // Bind mouse handlers
+ // Disable mouse click to avoid selected/highlighted page images - bug 354239
+ gbPageView.bind('mousedown', function(e) {
+ // $$$ check here for right-click and don't disable. Also use jQuery style
+ // for stopping propagation. See https://bugs.edge.launchpad.net/gnubook/+bug/362626
+ return false;
+ })
+ // Special hack for IE7
+ gbPageView[0].onselectstart = function(e) { return false; };
+}
+
// prepareTwoPageView()
//______________________________________________________________________________
// Some decisions about two page view:
// Returns the currently active index.
GnuBook.prototype.currentIndex = function() {
// $$$ we should be cleaner with our idea of which index is active in 1up/2up
- if (this.mode == this.constMode1up) {
+ if (this.mode == this.constMode1up || this.mode == this.constModeThumb) {
return this.firstIndex; // $$$ TODO page in center of view would be better
} else if (this.mode == this.constMode2up) {
// Only allow indices that are actually present in book
+ "<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: 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 thumbnail_mode' onclick='gb.switchMode(3); return false;'/>"
+ "<button class='GBicon rollover play' /><button class='GBicon rollover pause' style='display: none' />"
+ "</span>"
'.zoom_out': 'Zoom out',
'.one_page_mode': 'One-page view',
'.two_page_mode': 'Two-page view',
+ '.thumbnail_mode': 'Thumbnail view',
'.print': 'Print this page',
'.embed': 'Embed bookreader',
'.book_left': 'Flip left',
// E.g paramsFromFragment(window.location.hash)
GnuBook.prototype.paramsFromFragment = function(urlFragment) {
// URL fragment syntax specification: http://openlibrary.org/dev/docs/bookurls
-
+
var params = {};
// For convenience we allow an initial # character (as from window.location.hash)
params.mode = this.constMode1up;
} else if ('2up' == urlHash['mode']) {
params.mode = this.constMode2up;
+ } else if ('thumb' == urlHash['mode']) {
+ params.mode = this.constModeThumb;
}
// Index and page
// See http://openlibrary.org/dev/docs/bookurls for an explanation of the fragment syntax.
GnuBook.prototype.fragmentFromParams = function(params) {
var separator = '/';
-
+
var fragments = [];
if ('undefined' != typeof(params.page)) {
fragments.push('mode', '1up');
} else if (params.mode == this.constMode2up) {
fragments.push('mode', '2up');
+ } else if (params.mode == this.constModeThumb) {
+ fragments.push('mode', 'thumb');
} else {
throw 'fragmentFromParams called with unknown mode ' + params.mode;
}