Skip to content

Commit

Permalink
v3.0.3
Browse files Browse the repository at this point in the history
- Add options: showToolbar, showLogo
- Add this.options field, which stores the options last used in setup call
- Improve `_getDataFlattened` to have simple cache breaker
- Fix: don't show search UI if this.enableSearch is false
- Fix: add missing 'defaults' option to search plugin
- Move constants out of instance, and into class (eg BookReader.constMode1up)
  • Loading branch information
rchrd2 committed Mar 3, 2018
1 parent 1d3fec5 commit 163c67b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
68 changes: 43 additions & 25 deletions BookReader/BookReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ function BookReader(options) {
this.setup(options);
}

BookReader.version = "3.0.2";
BookReader.version = "3.0.3";

// Mode constants
BookReader.constMode1up = 1;
BookReader.constMode2up = 2;
BookReader.constModeThumb = 3;

BookReader.defaultOptions = {
// Padding in 1up
Expand All @@ -59,6 +64,8 @@ BookReader.defaultOptions = {
// speed for flip animation
flipSpeed: 'fast',

showToolbar: true,
showLogo: true,
// Where the logo links to
logoURL: 'https://archive.org',

Expand Down Expand Up @@ -168,13 +175,15 @@ BookReader.optionOverrides = {};
* Setup
* It is separate from the constructor, so plugins can extend.
* @param {Object} options
* TODO document all options properties
*/
BookReader.prototype.setup = function(options) {
// Mode constants
this.constMode1up = 1;
this.constMode2up = 2;
this.constModeThumb = 3;
// Store the options used to setup bookreader
this.options = options;

// @deprecated: Instance constants. Use Class constants instead
this.constMode1up = BookReader.constMode1up;
this.constMode2up = BookReader.constMode2up;
this.constModeThumb = BookReader.constModeThumb;

// Private properties below. Configuration should be done with options.
this.reduce = 4;
Expand Down Expand Up @@ -203,6 +212,8 @@ BookReader.prototype.setup = function(options) {
this.firstIndex = null;
this.lastDisplayableIndex2up = null;

this.showToolbar = options.showToolbar;
this.showLogo = options.showLogo;
this.logoURL = options.logoURL;
this.imagesBaseURL = options.imagesBaseURL;

Expand Down Expand Up @@ -358,7 +369,7 @@ BookReader.prototype.init = function() {
if (window.location.hash) {
// params explicitly set in URL
params = this.paramsFromFragment(window.location.hash);
} else if ('defaults' in this) {
} else if (this.defaults) {
// params not explicitly set, use defaults if we have them
params = this.paramsFromFragment(this.defaults);
}
Expand Down Expand Up @@ -391,7 +402,8 @@ BookReader.prototype.init = function() {
var nextMode;
if ('undefined' != typeof(params.mode)) {
nextMode = params.mode;
} else if (this.ui == 'full' && windowWidth <= this.onePageMinBreakpoint) {
} else if (this.ui == 'full'
&& (this.enableMobileNav && windowWidth <= this.onePageMinBreakpoint)) {
// In full mode, we set the default based on width
nextMode = this.constMode1up;
} else {
Expand Down Expand Up @@ -1447,7 +1459,7 @@ BookReader.prototype.quantizeReduce = function(reduce, reductionFactors) {

// reductionFactors should be array of sorted reduction factors
// e.g. [ {reduce: 0.25, autofit: null}, {reduce: 0.3, autofit: 'width'}, {reduce: 1, autofit: null} ]
BookReader.prototype.nextReduce = function( currentReduce, direction, reductionFactors ) {
BookReader.prototype.nextReduce = function(currentReduce, direction, reductionFactors) {
// XXX add 'closest', to replace quantize function

if (direction === 'in') {
Expand Down Expand Up @@ -2230,6 +2242,7 @@ BookReader.prototype.next = function() {
if (this.constMode2up == this.mode) {
this.autoStop();
this.flipFwdToIndex(null);

} else {
if (this.firstIndex < this.lastDisplayableIndex()) {
this.jumpToIndex(this.firstIndex+1);
Expand Down Expand Up @@ -2330,7 +2343,6 @@ BookReader.prototype.flipBackToIndex = function(index) {
if (null == index) {
index = leftIndex-2;
}
//if (index<0) return;

this.updateNavIndex(index);

Expand Down Expand Up @@ -3223,6 +3235,10 @@ BookReader.prototype.initNavbar = function() {
// Initialize the navigation bar when embedded
BookReader.prototype.initEmbedNavbar = function() {
var thisLink = (window.location + '').replace('?ui=embed',''); // IA-specific
var logoHtml = '';
if (this.showLogo) {
logoHtml = "<a class='logo' href='" + this.logoURL + "' 'target='_blank' ></a>";
}

this.refs.$br.append(
'<div id="BRnav" class="BRnavEmbed">'
Expand All @@ -3231,7 +3247,7 @@ BookReader.prototype.initEmbedNavbar = function() {
+ '<button class="BRicon book_left"></button>'
+ '<button class="BRicon book_right"></button>'
+ "</span>"
+ "<a class='logo' href='" + this.logoURL + "' 'target='_blank' ></a>"
+ logoHtml
+ "<span class='BRembedreturn'><a href='" + thisLink + "' target='_blank' ></a></span>"
+ '</div>'
);
Expand Down Expand Up @@ -3277,16 +3293,19 @@ BookReader.prototype.updateNavIndexThrottled = BookReader.util.throttle(BookRead
* @return {jqueryElement}
*/
BookReader.prototype.buildToolbarElement = function() {
var logoHtml = '';
if (this.showLogo) {
logoHtml = "<span class='BRtoolbarSection BRtoolbarSectionLogo tc'>"
+ "<a class='logo' href='" + this.logoURL + "'></a>"
+ "</span>";
}

// Add large screen navigation
this.refs.$BRtoolbar = $(
"<div class='BRtoolbar header'>"
+ "<span class='BRtoolbarbuttons'>"
+ "<span class='BRtoolbarLeft'>"
+ "<span class='BRtoolbarSection BRtoolbarSectionLogo tc'>"
+ "<a class='logo' href='" + this.logoURL + "'></a>"
+ "</span>"

+ logoHtml
+ "<span class='BRtoolbarSection BRtoolbarSectionTitle title tl ph10 last'>"
+ "<span id='BRreturn'><a></a></span>"
+ "<div id='BRnavCntlTop' class='BRnabrbuvCntl'></div>"
Expand Down Expand Up @@ -3316,7 +3335,7 @@ BookReader.prototype.buildToolbarElement = function() {


BookReader.prototype.initToolbar = function(mode, ui) {
if (ui == 'embed') {
if (ui == 'embed' || !this.showToolbar) {
return; // No toolbar at top in embed mode
}
var self = this;
Expand All @@ -3337,10 +3356,6 @@ BookReader.prototype.initToolbar = function(mode, ui) {

this.updateToolbarZoom(this.reduce); // Pretty format

if (ui == "embed") {
this.refs.$br.find("a.logo").attr("target","_blank");
}

// We build in mode 2
this.refs.$BRtoolbar.append();

Expand Down Expand Up @@ -3564,14 +3579,16 @@ BookReader.prototype.bindNavigationHandlers = function() {
var promises = [];
// TODO don't use magic constants
if ($('#BRnavCntlBtm').hasClass('BRdn')) {
promises.push(self.refs.$BRtoolbar.animate({top: self.refs.$BRtoolbar.height() * -1}).promise());
if (self.refs.$BRtoolbar)
promises.push(self.refs.$BRtoolbar.animate({top: self.refs.$BRtoolbar.height() * -1}).promise());
promises.push($('#BRnav').animate({bottom:-55}).promise());
$('#BRnavCntlBtm').addClass('BRup').removeClass('BRdn');
$('#BRnavCntlTop').addClass('BRdn').removeClass('BRup');
$('#BRnavCntlBtm.BRnavCntl').animate({height:'45px'});
$('.BRnavCntl').delay(1000).animate({opacity:.25}, 1000);
} else {
promises.push(self.refs.$BRtoolbar.animate({top:0}).promise());
if (self.refs.$BRtoolbar)
promises.push(self.refs.$BRtoolbar.animate({top:0}).promise());
promises.push($('#BRnav').animate({bottom:0}).promise());
$('#BRnavCntlBtm').addClass('BRdn').removeClass('BRup');
$('#BRnavCntlTop').addClass('BRup').removeClass('BRdn');
Expand Down Expand Up @@ -4482,8 +4499,8 @@ BookReader.prototype.leafNumToIndex = function(index) {
* @return {Array}
*/
BookReader.prototype._getDataFlattened = function() {
if (this._getDataFlattened.cached)
return this._getDataFlattened.cached;
if (this._getDataFlattened.cached && this._getDataFlattened.cached[1] === this.data.length)
return this._getDataFlattened.cached[0];

var flattend = [];
var prevPageSide = null;
Expand All @@ -4498,7 +4515,8 @@ BookReader.prototype._getDataFlattened = function() {
flattend.push(this.data[i][j]);
}
}
this._getDataFlattened.cached = flattend;
// length is used as a cache breaker
this._getDataFlattened.cached = [flattend, this.data.length];
return flattend;
};

Expand Down
21 changes: 11 additions & 10 deletions BookReader/plugins/plugin.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ BookReader.prototype.buildMobileDrawerElement = (function (super_) {
BookReader.prototype.buildToolbarElement = (function (super_) {
return function () {
var $el = super_.call(this);
var readIcon = '';
$el.find('.BRtoolbarRight').append($("<span class='BRtoolbarSection BRtoolbarSectionSearch tc ph20 last'>"
+ "<form class='booksearch desktop'>"
+ "<input type='search' class='textSrch form-control' name='textSrch' val='' placeholder='Search inside this book'/>"
+ "<button type='submit' id='btnSrch' name='btnSrch'>"
+ "<img src=\""+this.imagesBaseURL+"icon_search_button.svg\" />"
+ "</button>"
+ "</form>"
+ "</span>"));
if (this.enableSearch) {
var readIcon = '';
$el.find('.BRtoolbarRight').append($("<span class='BRtoolbarSection BRtoolbarSectionSearch tc ph20 last'>"
+ "<form class='booksearch desktop'>"
+ "<input type='search' class='textSrch form-control' name='textSrch' val='' placeholder='Search inside this book'/>"
+ "<button type='submit' id='btnSrch' name='btnSrch'>"
+ "<img src=\""+this.imagesBaseURL+"icon_search_button.svg\" />"
+ "</button>"
+ "</form>"
+ "</span>"));
}
return $el;
};
})(BookReader.prototype.buildToolbarElement);
Expand Down Expand Up @@ -483,7 +485,6 @@ BookReader.prototype.searchHighlightVisible = function() {

var i, j;
for (i=0; i<results.matches.length; i++) {
//console.log(results.matches[i].par[0]);
for (j=0; j<results.matches[i].par[0].boxes.length; j++) {
var box = results.matches[i].par[0].boxes[j];
var pageIndex = this.leafNumToIndex(box.page);
Expand Down
3 changes: 3 additions & 0 deletions BookReader/plugins/plugin.url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
jQuery.extend(true, BookReader.defaultOptions, {
enableUrlPlugin: true,
bookId: '',
// Defaults can be a urlFragment string
defaults: null,
});

BookReader.prototype.setup = (function(super_) {
Expand All @@ -13,6 +15,7 @@ BookReader.prototype.setup = (function(super_) {

this.enableUrlPlugin = options.enableUrlPlugin;
this.bookId = options.bookId;
this.defaults = options.defaults;

this.bind('PostInit', function(e, br) {
// Set document title -- may have already been set in enclosing html for
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 3.0.3
- Add more options: showToolbar, showLogo
- Add this.options field, which stores the options last used in setup call
- Improve `_getDataFlattened` to have simple cache breaker
- Fix: don't show search UI if this.enableSearch is false
- Fix: add missing 'defaults' option to search plugin
- Move constants out of instance, and into class (eg BookReader.constMode1up)

# 3.0.2
In the process of upgrading IA, to use the new BookReader API, more changes/fixes were made.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bookreader",
"version": "3.0.2",
"version": "3.0.3",
"description": "The Internet Archive BookReader.",
"repository": {
"type": "git",
Expand Down

0 comments on commit 163c67b

Please sign in to comment.