Skip to content

Commit

Permalink
fixed bug in src/unarchiver.js where contained epub failed to load if…
Browse files Browse the repository at this point in the history
… a book asset could not be found
  • Loading branch information
AJRenold committed Feb 4, 2014
1 parent 80051ad commit 5d52ff4
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 55 deletions.
69 changes: 51 additions & 18 deletions build/epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -1794,8 +1794,9 @@ EPUBJS.Book = function(options){
restore: false,
reload : false,
goto : false,
styles : {},
headTags : {},
withCredentials: false,
styles : {}
});

this.settings.EPUBJSVERSION = EPUBJS.VERSION;
Expand Down Expand Up @@ -2472,6 +2473,11 @@ EPUBJS.Book.prototype.removeStyle = function(style) {
delete this.settings.styles[style];
};

EPUBJS.Book.prototype.addHeadTag = function(tag, attrs) {
if(!this.isRendered) return this._enqueue("addHeadTag", arguments);
this.settings.headTags[tag] = attrs;
};

EPUBJS.Book.prototype.useSpreads = function(use) {
if(!this.isRendered) return this._enqueue("useSpreads", arguments);
this.settings.spreads = use;
Expand Down Expand Up @@ -2823,7 +2829,7 @@ EPUBJS.core.folder = function(url){

var lastSlash = url.lastIndexOf('/');

if(lastSlash == -1) folder = '';
if(lastSlash == -1) var folder = '';

folder = url.slice(0, lastSlash + 1);

Expand Down Expand Up @@ -2953,6 +2959,7 @@ EPUBJS.core.resolveUrl = function(base, path) {

return url.join("/");
};

EPUBJS.EpubCFI = function(cfiStr){
if(cfiStr) return this.parse(cfiStr);
};
Expand Down Expand Up @@ -3811,10 +3818,12 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
this.iframe.onload = function() {
renderer.doc = renderer.iframe.contentDocument;
renderer.docEl = renderer.doc.documentElement;
renderer.headEl = renderer.doc.head;
renderer.bodyEl = renderer.doc.body;
renderer.contentWindow = renderer.iframe.contentWindow;

renderer.applyStyles();
renderer.applyHeadTags();

if(renderer.book.settings.fixedLayout) {
renderer.fixedLayout();
Expand Down Expand Up @@ -3884,20 +3893,22 @@ EPUBJS.Renderer.prototype.formatSpread = function(){
// this.bodyEl.style.fontSize = localStorage.getItem("fontSize") || "medium";

//-- Clear Margins
if(this.bodyEl) this.bodyEl.style.margin = "0";
if(this.bodyEl) {
this.bodyEl.style.margin = "0";

this.docEl.style.overflow = "hidden";
this.docEl.style.overflow = "hidden";

this.docEl.style.width = this.elWidth + "px";
this.docEl.style.width = this.elWidth + "px";

//-- Adjust height
this.docEl.style.height = this.iframe.clientHeight + "px";
//-- Adjust height
this.docEl.style.height = this.iframe.clientHeight + "px";

//-- Add columns
this.docEl.style[EPUBJS.Renderer.columnAxis] = "horizontal";
this.docEl.style[EPUBJS.Renderer.columnGap] = this.gap+"px";
this.docEl.style[EPUBJS.Renderer.columnWidth] = this.colWidth+"px";
}

//-- Add columns
this.docEl.style[EPUBJS.Renderer.columnAxis] = "horizontal";
this.docEl.style[EPUBJS.Renderer.columnGap] = this.gap+"px";
this.docEl.style[EPUBJS.Renderer.columnWidth] = this.colWidth+"px";

};

EPUBJS.Renderer.prototype.fixedLayout = function(){
Expand Down Expand Up @@ -3945,6 +3956,24 @@ EPUBJS.Renderer.prototype.applyStyles = function() {
}
};

EPUBJS.Renderer.prototype.addHeadTag = function(tag, attrs) {
var s = document.createElement(tag);

for(attr in attrs) {
s[attr] = attrs[attr];
}
this.headEl.appendChild(s);
}

EPUBJS.Renderer.prototype.applyHeadTags = function() {

var headTags = this.book.settings.headTags;

for ( var headTag in headTags ) {
this.addHeadTag(headTag, headTags[headTag])
}
};

EPUBJS.Renderer.prototype.gotoChapterEnd = function(){
this.chapterEnd();
};
Expand All @@ -3963,7 +3992,7 @@ EPUBJS.Renderer.prototype.visible = function(bool){

EPUBJS.Renderer.prototype.calcPages = function() {

this.totalWidth = this.docEl.scrollWidth;
if(this.docEl) this.totalWidth = this.docEl.scrollWidth;

this.displayedPages = Math.ceil(this.totalWidth / this.spreadWidth);

Expand Down Expand Up @@ -4338,6 +4367,7 @@ EPUBJS.Renderer.prototype.remove = function() {

//-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);

var EPUBJS = EPUBJS || {};
EPUBJS.replace = {};

Expand Down Expand Up @@ -4453,6 +4483,7 @@ EPUBJS.Unarchiver = function(url){

};

//-- Load the zip lib and set the workerScriptsPath
EPUBJS.Unarchiver.prototype.loadLib = function(callback){
if(typeof(zip) == "undefined") console.error("Zip lib not loaded");

Expand Down Expand Up @@ -4496,8 +4527,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var _URL = window.URL || window.webkitURL || window.mozURL;

if(!entry) {
console.error("File not found in the epub:", url);
return;
console.warn("File not found in the epub:", url);
return deferred.promise;
}

if(url in this.urlCache) {
Expand All @@ -4521,8 +4552,10 @@ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var entry = this.zipFs.find(decodededUrl);
var _URL = window.URL || window.webkitURL || window.mozURL;

if(!entry) console.error(url);

if(!entry) {
console.warn("File not found in the contained epub:", url);
return deferred.promise;
}

entry.getText(function(text){
deferred.resolve(text);
Expand Down Expand Up @@ -4575,4 +4608,4 @@ EPUBJS.Unarchiver.prototype.saveEntryFileToStorage = function(entry, callback){
entry.getData(new zip.BlobWriter(), function(blob) {
EPUBJS.storage.save(entry.filename, blob, callback);
});
};
};
4 changes: 2 additions & 2 deletions build/epub.min.js

Large diffs are not rendered by default.

69 changes: 51 additions & 18 deletions build/epub_no_underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1793,8 +1793,9 @@ EPUBJS.Book = function(options){
restore: false,
reload : false,
goto : false,
styles : {},
headTags : {},
withCredentials: false,
styles : {}
});

this.settings.EPUBJSVERSION = EPUBJS.VERSION;
Expand Down Expand Up @@ -2471,6 +2472,11 @@ EPUBJS.Book.prototype.removeStyle = function(style) {
delete this.settings.styles[style];
};

EPUBJS.Book.prototype.addHeadTag = function(tag, attrs) {
if(!this.isRendered) return this._enqueue("addHeadTag", arguments);
this.settings.headTags[tag] = attrs;
};

EPUBJS.Book.prototype.useSpreads = function(use) {
if(!this.isRendered) return this._enqueue("useSpreads", arguments);
this.settings.spreads = use;
Expand Down Expand Up @@ -2822,7 +2828,7 @@ EPUBJS.core.folder = function(url){

var lastSlash = url.lastIndexOf('/');

if(lastSlash == -1) folder = '';
if(lastSlash == -1) var folder = '';

folder = url.slice(0, lastSlash + 1);

Expand Down Expand Up @@ -2952,6 +2958,7 @@ EPUBJS.core.resolveUrl = function(base, path) {

return url.join("/");
};

EPUBJS.EpubCFI = function(cfiStr){
if(cfiStr) return this.parse(cfiStr);
};
Expand Down Expand Up @@ -3810,10 +3817,12 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
this.iframe.onload = function() {
renderer.doc = renderer.iframe.contentDocument;
renderer.docEl = renderer.doc.documentElement;
renderer.headEl = renderer.doc.head;
renderer.bodyEl = renderer.doc.body;
renderer.contentWindow = renderer.iframe.contentWindow;

renderer.applyStyles();
renderer.applyHeadTags();

if(renderer.book.settings.fixedLayout) {
renderer.fixedLayout();
Expand Down Expand Up @@ -3883,20 +3892,22 @@ EPUBJS.Renderer.prototype.formatSpread = function(){
// this.bodyEl.style.fontSize = localStorage.getItem("fontSize") || "medium";

//-- Clear Margins
if(this.bodyEl) this.bodyEl.style.margin = "0";
if(this.bodyEl) {
this.bodyEl.style.margin = "0";

this.docEl.style.overflow = "hidden";
this.docEl.style.overflow = "hidden";

this.docEl.style.width = this.elWidth + "px";
this.docEl.style.width = this.elWidth + "px";

//-- Adjust height
this.docEl.style.height = this.iframe.clientHeight + "px";
//-- Adjust height
this.docEl.style.height = this.iframe.clientHeight + "px";

//-- Add columns
this.docEl.style[EPUBJS.Renderer.columnAxis] = "horizontal";
this.docEl.style[EPUBJS.Renderer.columnGap] = this.gap+"px";
this.docEl.style[EPUBJS.Renderer.columnWidth] = this.colWidth+"px";
}

//-- Add columns
this.docEl.style[EPUBJS.Renderer.columnAxis] = "horizontal";
this.docEl.style[EPUBJS.Renderer.columnGap] = this.gap+"px";
this.docEl.style[EPUBJS.Renderer.columnWidth] = this.colWidth+"px";

};

EPUBJS.Renderer.prototype.fixedLayout = function(){
Expand Down Expand Up @@ -3944,6 +3955,24 @@ EPUBJS.Renderer.prototype.applyStyles = function() {
}
};

EPUBJS.Renderer.prototype.addHeadTag = function(tag, attrs) {
var s = document.createElement(tag);

for(attr in attrs) {
s[attr] = attrs[attr];
}
this.headEl.appendChild(s);
}

EPUBJS.Renderer.prototype.applyHeadTags = function() {

var headTags = this.book.settings.headTags;

for ( var headTag in headTags ) {
this.addHeadTag(headTag, headTags[headTag])
}
};

EPUBJS.Renderer.prototype.gotoChapterEnd = function(){
this.chapterEnd();
};
Expand All @@ -3962,7 +3991,7 @@ EPUBJS.Renderer.prototype.visible = function(bool){

EPUBJS.Renderer.prototype.calcPages = function() {

this.totalWidth = this.docEl.scrollWidth;
if(this.docEl) this.totalWidth = this.docEl.scrollWidth;

this.displayedPages = Math.ceil(this.totalWidth / this.spreadWidth);

Expand Down Expand Up @@ -4337,6 +4366,7 @@ EPUBJS.Renderer.prototype.remove = function() {

//-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);

var EPUBJS = EPUBJS || {};
EPUBJS.replace = {};

Expand Down Expand Up @@ -4452,6 +4482,7 @@ EPUBJS.Unarchiver = function(url){

};

//-- Load the zip lib and set the workerScriptsPath
EPUBJS.Unarchiver.prototype.loadLib = function(callback){
if(typeof(zip) == "undefined") console.error("Zip lib not loaded");

Expand Down Expand Up @@ -4495,8 +4526,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var _URL = window.URL || window.webkitURL || window.mozURL;

if(!entry) {
console.error("File not found in the epub:", url);
return;
console.warn("File not found in the epub:", url);
return deferred.promise;
}

if(url in this.urlCache) {
Expand All @@ -4520,8 +4551,10 @@ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var entry = this.zipFs.find(decodededUrl);
var _URL = window.URL || window.webkitURL || window.mozURL;

if(!entry) console.error(url);

if(!entry) {
console.warn("File not found in the contained epub:", url);
return deferred.promise;
}

entry.getText(function(text){
deferred.resolve(text);
Expand Down Expand Up @@ -4574,4 +4607,4 @@ EPUBJS.Unarchiver.prototype.saveEntryFileToStorage = function(entry, callback){
entry.getData(new zip.BlobWriter(), function(blob) {
EPUBJS.storage.save(entry.filename, blob, callback);
});
};
};
2 changes: 1 addition & 1 deletion build/libs/inflate.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/libs/zip.min.js

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions build/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ EPUBJS.Reader = function(path, _options) {
bookmarks : null,
contained : null,
bookKey : null,
styles : null
styles : null,
sidebarReflow: false
});

this.setBookKey(path); //-- This could be username + path or any unique string
Expand Down Expand Up @@ -421,12 +422,20 @@ EPUBJS.reader.ReaderController = function(book) {
$prev = $("#prev");

var slideIn = function() {
$main.removeClass("closed");
};

var slideOut = function() {
$main.addClass("closed");
};
if (Reader.settings.sidebarReflow){
$('#main').removeClass('single');
} else {
$main.removeClass("closed");
}
};

var slideOut = function() {
if (Reader.settings.sidebarReflow){
$('#main').addClass('single');
} else {
$main.addClass("closed");
}
};

var showLoader = function() {
$loader.show();
Expand Down Expand Up @@ -523,6 +532,12 @@ EPUBJS.reader.SettingsController = function() {
$settings.removeClass("md-show");
};

var $sidebarReflowSetting = $('#sidebarReflow');

$sidebarReflowSetting.on('click', function() {
Reader.settings.sidebarReflow = !Reader.settings.sidebarReflow;
});

$settings.find(".closer").on("click", function() {
hide();
});
Expand Down
4 changes: 2 additions & 2 deletions demo/js/epub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/js/libs/inflate.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/js/libs/zip.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5d52ff4

Please sign in to comment.