1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

Pass iframe events as render event, Added selected event, useSpreads(true | false), update after css changes, fix compressed links onload not fireing, fix encoding for compressed epubs

This commit is contained in:
Fred Chasen 2014-01-08 16:15:04 -08:00
parent 8ad2e980cb
commit 0585c873e7
23 changed files with 634 additions and 242 deletions

View file

@ -1945,6 +1945,7 @@ EPUBJS.Book.prototype.loadPackage = function(_containerPath){
then(function(paths){ then(function(paths){
book.settings.contentsPath = book.bookUrl + paths.basePath; book.settings.contentsPath = book.bookUrl + paths.basePath;
book.settings.packageUrl = book.bookUrl + paths.packagePath; book.settings.packageUrl = book.bookUrl + paths.packagePath;
book.settings.encoding = paths.encoding;
return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata
}); });
} else { } else {
@ -2045,9 +2046,9 @@ EPUBJS.Book.prototype.networkListeners = function(){
//-- Choose between a request from store or a request from network //-- Choose between a request from store or a request from network
EPUBJS.Book.prototype.loadXml = function(url){ EPUBJS.Book.prototype.loadXml = function(url){
if(this.settings.fromStorage) { if(this.settings.fromStorage) {
return this.storage.getXml(url); return this.storage.getXml(url, this.settings.encoding);
} else if(this.settings.contained) { } else if(this.settings.contained) {
return this.zip.getXml(url); return this.zip.getXml(url, this.settings.encoding);
}else{ }else{
return EPUBJS.core.request(url, 'xml'); return EPUBJS.core.request(url, 'xml');
} }
@ -2220,14 +2221,16 @@ EPUBJS.Book.prototype.restore = function(identifier){
fetch = ['manifest', 'spine', 'metadata', 'cover', 'toc', 'spineNodeIndex', 'spineIndexByURL'], fetch = ['manifest', 'spine', 'metadata', 'cover', 'toc', 'spineNodeIndex', 'spineIndexByURL'],
reject = false, reject = false,
bookKey = this.setBookKey(identifier), bookKey = this.setBookKey(identifier),
fromStore = localStorage.getItem(bookKey); fromStore = localStorage.getItem(bookKey),
len = fetch.length,
i;
if(this.settings.clearSaved) reject = true; if(this.settings.clearSaved) reject = true;
if(!reject && fromStore != 'undefined' && fromStore != null){ if(!reject && fromStore != 'undefined' && fromStore != null){
book.contents = JSON.parse(fromStore); book.contents = JSON.parse(fromStore);
for(var i = 0, len = fetch.length; i < len; i++) { for(i = 0; i < len; i++) {
var item = fetch[i]; var item = fetch[i];
if(!book.contents[item]) { if(!book.contents[item]) {
@ -2412,13 +2415,16 @@ EPUBJS.Book.prototype.goto = function(url){
EPUBJS.Book.prototype.preloadNextChapter = function() { EPUBJS.Book.prototype.preloadNextChapter = function() {
var next; var next;
var chap = this.spinePos + 1;
if(this.spinePos >= this.spine.length){ if(chap >= this.spine.length){
return false; return false;
} }
next = new EPUBJS.Chapter(this.spine[this.spinePos + 1]); next = new EPUBJS.Chapter(this.spine[chap]);
if(next) {
EPUBJS.core.request(next.absolute); EPUBJS.core.request(next.absolute);
}
}; };
@ -2464,17 +2470,28 @@ EPUBJS.Book.prototype.fromStorage = function(stored) {
*/ */
EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) { EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) {
if(!this.isRendered) return this._enqueue("setStyle", arguments);
this.settings.styles[style] = val; this.settings.styles[style] = val;
if(this.render) this.render.setStyle(style, val, prefixed); this.render.setStyle(style, val, prefixed);
this.render.reformat();
}; };
EPUBJS.Book.prototype.removeStyle = function(style) { EPUBJS.Book.prototype.removeStyle = function(style) {
if(this.render) this.render.removeStyle(style); if(!this.isRendered) return this._enqueue("removeStyle", arguments);
this.render.removeStyle(style);
this.render.reformat();
delete this.settings.styles[style]; delete this.settings.styles[style];
}; };
EPUBJS.Book.prototype.useSpreads = function(use) {
if(!this.isRendered) return this._enqueue("useSpreads", arguments);
this.settings.spreads = use;
this.render.reformat();
this.trigger("book:spreads", use);
};
EPUBJS.Book.prototype.unload = function(){ EPUBJS.Book.prototype.unload = function(){
if(this.settings.restore) { if(this.settings.restore) {
@ -3201,12 +3218,14 @@ EPUBJS.Parser.prototype.container = function(containerXml){
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/> //-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
var rootfile = containerXml.querySelector("rootfile"), var rootfile = containerXml.querySelector("rootfile"),
fullpath = rootfile.getAttribute('full-path'), fullpath = rootfile.getAttribute('full-path'),
folder = EPUBJS.core.folder(fullpath); folder = EPUBJS.core.folder(fullpath),
encoding = containerXml.xmlEncoding;
//-- Now that we have the path we can parse the contents //-- Now that we have the path we can parse the contents
return { return {
'packagePath' : fullpath, 'packagePath' : fullpath,
'basePath' : folder 'basePath' : folder,
'encoding' : encoding
}; };
}; };
@ -3519,6 +3538,8 @@ EPUBJS.Renderer = function(book) {
this.epubcfi = new EPUBJS.EpubCFI(); this.epubcfi = new EPUBJS.EpubCFI();
this.initialize(); this.initialize();
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click"];
this.listeners(); this.listeners();
//-- Renderer events for listening //-- Renderer events for listening
@ -3616,6 +3637,50 @@ EPUBJS.Renderer.prototype.hideHashChanges = function(){
*/ */
//-- Listeners for events in the frame
EPUBJS.Renderer.prototype.addIframeListeners = function(){
this.listenedEvents.forEach(function(eventName){
this.doc.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this);
};
EPUBJS.Renderer.prototype.removeIframeListeners = function(){
this.listenedEvents.forEach(function(eventName){
this.doc.removeEventListener(eventName, this.triggerEvent, false);
}, this);
};
EPUBJS.Renderer.prototype.triggerEvent = function(e){
this.book.trigger("renderer:"+e.type, e);
};
EPUBJS.Renderer.prototype.addSelectionListeners = function(){
this.doc.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
this.contentWindow.addEventListener("mouseup", this.onMouseUp.bind(this), false);
};
EPUBJS.Renderer.prototype.removeSelectionListeners = function(){
this.doc.removeEventListener("selectionchange", this.onSelectionChange, false);
this.contentWindow.removeEventListener("mouseup", this.onMouseUp, false);
};
EPUBJS.Renderer.prototype.onSelectionChange = function(e){
this.highlighted = true;
};
EPUBJS.Renderer.prototype.onMouseUp = function(e){
var selection;
if(this.highlighted) {
selection = this.contentWindow.getSelection();
this.book.trigger("renderer:selected", selection);
this.highlighted = false;
}
};
EPUBJS.Renderer.prototype.onResized = function(e){ EPUBJS.Renderer.prototype.onResized = function(e){
var msg = { var msg = {
@ -3684,7 +3749,6 @@ EPUBJS.Renderer.prototype.crossBrowserColumnCss = function(){
}; };
EPUBJS.Renderer.prototype.setIframeSrc = function(url){ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
var renderer = this, var renderer = this,
deferred = new RSVP.defer(); deferred = new RSVP.defer();
@ -3697,6 +3761,7 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
renderer.doc = renderer.iframe.contentDocument; renderer.doc = renderer.iframe.contentDocument;
renderer.docEl = renderer.doc.documentElement; renderer.docEl = renderer.doc.documentElement;
renderer.bodyEl = renderer.doc.body; renderer.bodyEl = renderer.doc.body;
renderer.contentWindow = renderer.iframe.contentWindow;
renderer.applyStyles(); renderer.applyStyles();
@ -3724,7 +3789,9 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
}); });
renderer.iframe.contentWindow.addEventListener("resize", renderer.resized, false); renderer.contentWindow.addEventListener("resize", this.resized, false);
renderer.addIframeListeners();
renderer.addSelectionListeners();
}; };
return deferred.promise; return deferred.promise;
@ -3809,6 +3876,7 @@ EPUBJS.Renderer.prototype.setStyle = function(style, val, prefixed){
} }
if(this.bodyEl) this.bodyEl.style[style] = val; if(this.bodyEl) this.bodyEl.style[style] = val;
}; };
EPUBJS.Renderer.prototype.removeStyle = function(style){ EPUBJS.Renderer.prototype.removeStyle = function(style){
@ -3924,9 +3992,9 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
var items = this.doc.querySelectorAll(query), var items = this.doc.querySelectorAll(query),
resources = Array.prototype.slice.call(items), resources = Array.prototype.slice.call(items),
count = resources.length, count = resources.length,
after = function(result){ after = function(result, full){
count--; count--;
if(progress) progress(result, count); if(progress) progress(result, full, count);
if(count <= 0 && finished) finished(true); if(count <= 0 && finished) finished(true);
}; };
@ -3951,11 +4019,11 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_uri = EPUBJS.core.uri(this.book.chapter.absolute), _uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base, _chapterBase = _uri.base,
_attr = attr, _attr = attr,
_wait = 2000,
progress = function(url, full, count) { progress = function(url, full, count) {
_newUrls[full] = url; _newUrls[full] = url;
}, },
finished = function(notempty) { finished = function(notempty) {
if(callback) callback(); if(callback) callback();
_.each(_oldUrls, function(url){ _.each(_oldUrls, function(url){
@ -3975,10 +4043,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
full = EPUBJS.core.resolveUrl(_chapterBase, src); full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) { var replaceUrl = function(url) {
var timeout;
link.onload = function(){ link.onload = function(){
clearTimeout(timeout);
done(url, full); done(url, full);
}; };
link.onerror = function(e){ link.onerror = function(e){
clearTimeout(timeout);
done(url, full);
console.error(e); console.error(e);
}; };
@ -3987,7 +4061,18 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
link.setAttribute("externalResourcesRequired", "true"); link.setAttribute("externalResourcesRequired", "true");
} }
if(query == "link[href]") {
//-- Only Stylesheet links seem to have a load events, just continue others
done(url, full);
}
link.setAttribute(_attr, url); link.setAttribute(_attr, url);
//-- If elements never fire Load Event, should continue anyways
timeout = setTimeout(function(){
done(url, full);
}, _wait);
}; };
if(full in _oldUrls){ if(full in _oldUrls){
@ -4193,13 +4278,15 @@ EPUBJS.Renderer.prototype.height = function(el){
}; };
EPUBJS.Renderer.prototype.remove = function() { EPUBJS.Renderer.prototype.remove = function() {
this.iframe.contentWindow.removeEventListener("resize", this.resized); this.contentWindow.removeEventListener("resize", this.resized);
this.removeIframeListeners();
this.removeSelectionListeners();
this.el.removeChild(this.iframe); this.el.removeChild(this.iframe);
}; };
//-- Enable binding events to parser //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype); RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
var EPUBJS = EPUBJS || {}; var EPUBJS = EPUBJS || {};
EPUBJS.replace = {}; EPUBJS.replace = {};
@ -4241,7 +4328,6 @@ EPUBJS.replace.links = function(_store, full, done, link){
}else{ }else{
_store.getUrl(full).then(done); _store.getUrl(full).then(done);
} }
}; };
EPUBJS.replace.stylesheets = function(_store, full) { EPUBJS.replace.stylesheets = function(_store, full) {
@ -4337,9 +4423,9 @@ EPUBJS.Unarchiver.prototype.openZip = function(zipUrl, callback){
return deferred.promise; return deferred.promise;
}; };
EPUBJS.Unarchiver.prototype.getXml = function(url){ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
return this.getText(url). return this.getText(url, encoding).
then(function(text){ then(function(text){
var parser = new DOMParser(); var parser = new DOMParser();
return parser.parseFromString(text, "application/xml"); return parser.parseFromString(text, "application/xml");
@ -4369,7 +4455,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
return deferred.promise; return deferred.promise;
}; };
EPUBJS.Unarchiver.prototype.getText = function(url){ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); var deferred = new RSVP.defer();
var entry = this.zipFs.find(url); var entry = this.zipFs.find(url);
@ -4380,7 +4466,7 @@ EPUBJS.Unarchiver.prototype.getText = function(url){
entry.getText(function(text){ entry.getText(function(text){
deferred.resolve(text); deferred.resolve(text);
}, null, null, 'ISO-8859-1'); }, null, null, encoding || 'UTF-8');
return deferred.promise; return deferred.promise;
}; };
@ -4388,7 +4474,6 @@ EPUBJS.Unarchiver.prototype.getText = function(url){
EPUBJS.Unarchiver.prototype.revokeUrl = function(url){ EPUBJS.Unarchiver.prototype.revokeUrl = function(url){
var _URL = window.URL || window.webkitURL || window.mozURL; var _URL = window.URL || window.webkitURL || window.mozURL;
var fromCache = unarchiver.urlCache[url]; var fromCache = unarchiver.urlCache[url];
console.log("revoke", fromCache);
if(fromCache) _URL.revokeObjectURL(fromCache); if(fromCache) _URL.revokeObjectURL(fromCache);
}; };

4
build/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1944,6 +1944,7 @@ EPUBJS.Book.prototype.loadPackage = function(_containerPath){
then(function(paths){ then(function(paths){
book.settings.contentsPath = book.bookUrl + paths.basePath; book.settings.contentsPath = book.bookUrl + paths.basePath;
book.settings.packageUrl = book.bookUrl + paths.packagePath; book.settings.packageUrl = book.bookUrl + paths.packagePath;
book.settings.encoding = paths.encoding;
return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata
}); });
} else { } else {
@ -2044,9 +2045,9 @@ EPUBJS.Book.prototype.networkListeners = function(){
//-- Choose between a request from store or a request from network //-- Choose between a request from store or a request from network
EPUBJS.Book.prototype.loadXml = function(url){ EPUBJS.Book.prototype.loadXml = function(url){
if(this.settings.fromStorage) { if(this.settings.fromStorage) {
return this.storage.getXml(url); return this.storage.getXml(url, this.settings.encoding);
} else if(this.settings.contained) { } else if(this.settings.contained) {
return this.zip.getXml(url); return this.zip.getXml(url, this.settings.encoding);
}else{ }else{
return EPUBJS.core.request(url, 'xml'); return EPUBJS.core.request(url, 'xml');
} }
@ -2219,14 +2220,16 @@ EPUBJS.Book.prototype.restore = function(identifier){
fetch = ['manifest', 'spine', 'metadata', 'cover', 'toc', 'spineNodeIndex', 'spineIndexByURL'], fetch = ['manifest', 'spine', 'metadata', 'cover', 'toc', 'spineNodeIndex', 'spineIndexByURL'],
reject = false, reject = false,
bookKey = this.setBookKey(identifier), bookKey = this.setBookKey(identifier),
fromStore = localStorage.getItem(bookKey); fromStore = localStorage.getItem(bookKey),
len = fetch.length,
i;
if(this.settings.clearSaved) reject = true; if(this.settings.clearSaved) reject = true;
if(!reject && fromStore != 'undefined' && fromStore != null){ if(!reject && fromStore != 'undefined' && fromStore != null){
book.contents = JSON.parse(fromStore); book.contents = JSON.parse(fromStore);
for(var i = 0, len = fetch.length; i < len; i++) { for(i = 0; i < len; i++) {
var item = fetch[i]; var item = fetch[i];
if(!book.contents[item]) { if(!book.contents[item]) {
@ -2411,13 +2414,16 @@ EPUBJS.Book.prototype.goto = function(url){
EPUBJS.Book.prototype.preloadNextChapter = function() { EPUBJS.Book.prototype.preloadNextChapter = function() {
var next; var next;
var chap = this.spinePos + 1;
if(this.spinePos >= this.spine.length){ if(chap >= this.spine.length){
return false; return false;
} }
next = new EPUBJS.Chapter(this.spine[this.spinePos + 1]); next = new EPUBJS.Chapter(this.spine[chap]);
if(next) {
EPUBJS.core.request(next.absolute); EPUBJS.core.request(next.absolute);
}
}; };
@ -2463,17 +2469,28 @@ EPUBJS.Book.prototype.fromStorage = function(stored) {
*/ */
EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) { EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) {
if(!this.isRendered) return this._enqueue("setStyle", arguments);
this.settings.styles[style] = val; this.settings.styles[style] = val;
if(this.render) this.render.setStyle(style, val, prefixed); this.render.setStyle(style, val, prefixed);
this.render.reformat();
}; };
EPUBJS.Book.prototype.removeStyle = function(style) { EPUBJS.Book.prototype.removeStyle = function(style) {
if(this.render) this.render.removeStyle(style); if(!this.isRendered) return this._enqueue("removeStyle", arguments);
this.render.removeStyle(style);
this.render.reformat();
delete this.settings.styles[style]; delete this.settings.styles[style];
}; };
EPUBJS.Book.prototype.useSpreads = function(use) {
if(!this.isRendered) return this._enqueue("useSpreads", arguments);
this.settings.spreads = use;
this.render.reformat();
this.trigger("book:spreads", use);
};
EPUBJS.Book.prototype.unload = function(){ EPUBJS.Book.prototype.unload = function(){
if(this.settings.restore) { if(this.settings.restore) {
@ -3200,12 +3217,14 @@ EPUBJS.Parser.prototype.container = function(containerXml){
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/> //-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
var rootfile = containerXml.querySelector("rootfile"), var rootfile = containerXml.querySelector("rootfile"),
fullpath = rootfile.getAttribute('full-path'), fullpath = rootfile.getAttribute('full-path'),
folder = EPUBJS.core.folder(fullpath); folder = EPUBJS.core.folder(fullpath),
encoding = containerXml.xmlEncoding;
//-- Now that we have the path we can parse the contents //-- Now that we have the path we can parse the contents
return { return {
'packagePath' : fullpath, 'packagePath' : fullpath,
'basePath' : folder 'basePath' : folder,
'encoding' : encoding
}; };
}; };
@ -3518,6 +3537,8 @@ EPUBJS.Renderer = function(book) {
this.epubcfi = new EPUBJS.EpubCFI(); this.epubcfi = new EPUBJS.EpubCFI();
this.initialize(); this.initialize();
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click"];
this.listeners(); this.listeners();
//-- Renderer events for listening //-- Renderer events for listening
@ -3615,6 +3636,50 @@ EPUBJS.Renderer.prototype.hideHashChanges = function(){
*/ */
//-- Listeners for events in the frame
EPUBJS.Renderer.prototype.addIframeListeners = function(){
this.listenedEvents.forEach(function(eventName){
this.doc.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this);
};
EPUBJS.Renderer.prototype.removeIframeListeners = function(){
this.listenedEvents.forEach(function(eventName){
this.doc.removeEventListener(eventName, this.triggerEvent, false);
}, this);
};
EPUBJS.Renderer.prototype.triggerEvent = function(e){
this.book.trigger("renderer:"+e.type, e);
};
EPUBJS.Renderer.prototype.addSelectionListeners = function(){
this.doc.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
this.contentWindow.addEventListener("mouseup", this.onMouseUp.bind(this), false);
};
EPUBJS.Renderer.prototype.removeSelectionListeners = function(){
this.doc.removeEventListener("selectionchange", this.onSelectionChange, false);
this.contentWindow.removeEventListener("mouseup", this.onMouseUp, false);
};
EPUBJS.Renderer.prototype.onSelectionChange = function(e){
this.highlighted = true;
};
EPUBJS.Renderer.prototype.onMouseUp = function(e){
var selection;
if(this.highlighted) {
selection = this.contentWindow.getSelection();
this.book.trigger("renderer:selected", selection);
this.highlighted = false;
}
};
EPUBJS.Renderer.prototype.onResized = function(e){ EPUBJS.Renderer.prototype.onResized = function(e){
var msg = { var msg = {
@ -3683,7 +3748,6 @@ EPUBJS.Renderer.prototype.crossBrowserColumnCss = function(){
}; };
EPUBJS.Renderer.prototype.setIframeSrc = function(url){ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
var renderer = this, var renderer = this,
deferred = new RSVP.defer(); deferred = new RSVP.defer();
@ -3696,6 +3760,7 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
renderer.doc = renderer.iframe.contentDocument; renderer.doc = renderer.iframe.contentDocument;
renderer.docEl = renderer.doc.documentElement; renderer.docEl = renderer.doc.documentElement;
renderer.bodyEl = renderer.doc.body; renderer.bodyEl = renderer.doc.body;
renderer.contentWindow = renderer.iframe.contentWindow;
renderer.applyStyles(); renderer.applyStyles();
@ -3723,7 +3788,9 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
}); });
renderer.iframe.contentWindow.addEventListener("resize", renderer.resized, false); renderer.contentWindow.addEventListener("resize", this.resized, false);
renderer.addIframeListeners();
renderer.addSelectionListeners();
}; };
return deferred.promise; return deferred.promise;
@ -3808,6 +3875,7 @@ EPUBJS.Renderer.prototype.setStyle = function(style, val, prefixed){
} }
if(this.bodyEl) this.bodyEl.style[style] = val; if(this.bodyEl) this.bodyEl.style[style] = val;
}; };
EPUBJS.Renderer.prototype.removeStyle = function(style){ EPUBJS.Renderer.prototype.removeStyle = function(style){
@ -3923,9 +3991,9 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
var items = this.doc.querySelectorAll(query), var items = this.doc.querySelectorAll(query),
resources = Array.prototype.slice.call(items), resources = Array.prototype.slice.call(items),
count = resources.length, count = resources.length,
after = function(result){ after = function(result, full){
count--; count--;
if(progress) progress(result, count); if(progress) progress(result, full, count);
if(count <= 0 && finished) finished(true); if(count <= 0 && finished) finished(true);
}; };
@ -3950,11 +4018,11 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_uri = EPUBJS.core.uri(this.book.chapter.absolute), _uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base, _chapterBase = _uri.base,
_attr = attr, _attr = attr,
_wait = 2000,
progress = function(url, full, count) { progress = function(url, full, count) {
_newUrls[full] = url; _newUrls[full] = url;
}, },
finished = function(notempty) { finished = function(notempty) {
if(callback) callback(); if(callback) callback();
_.each(_oldUrls, function(url){ _.each(_oldUrls, function(url){
@ -3974,10 +4042,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
full = EPUBJS.core.resolveUrl(_chapterBase, src); full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) { var replaceUrl = function(url) {
var timeout;
link.onload = function(){ link.onload = function(){
clearTimeout(timeout);
done(url, full); done(url, full);
}; };
link.onerror = function(e){ link.onerror = function(e){
clearTimeout(timeout);
done(url, full);
console.error(e); console.error(e);
}; };
@ -3986,7 +4060,18 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
link.setAttribute("externalResourcesRequired", "true"); link.setAttribute("externalResourcesRequired", "true");
} }
if(query == "link[href]") {
//-- Only Stylesheet links seem to have a load events, just continue others
done(url, full);
}
link.setAttribute(_attr, url); link.setAttribute(_attr, url);
//-- If elements never fire Load Event, should continue anyways
timeout = setTimeout(function(){
done(url, full);
}, _wait);
}; };
if(full in _oldUrls){ if(full in _oldUrls){
@ -4192,13 +4277,15 @@ EPUBJS.Renderer.prototype.height = function(el){
}; };
EPUBJS.Renderer.prototype.remove = function() { EPUBJS.Renderer.prototype.remove = function() {
this.iframe.contentWindow.removeEventListener("resize", this.resized); this.contentWindow.removeEventListener("resize", this.resized);
this.removeIframeListeners();
this.removeSelectionListeners();
this.el.removeChild(this.iframe); this.el.removeChild(this.iframe);
}; };
//-- Enable binding events to parser //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype); RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
var EPUBJS = EPUBJS || {}; var EPUBJS = EPUBJS || {};
EPUBJS.replace = {}; EPUBJS.replace = {};
@ -4240,7 +4327,6 @@ EPUBJS.replace.links = function(_store, full, done, link){
}else{ }else{
_store.getUrl(full).then(done); _store.getUrl(full).then(done);
} }
}; };
EPUBJS.replace.stylesheets = function(_store, full) { EPUBJS.replace.stylesheets = function(_store, full) {
@ -4336,9 +4422,9 @@ EPUBJS.Unarchiver.prototype.openZip = function(zipUrl, callback){
return deferred.promise; return deferred.promise;
}; };
EPUBJS.Unarchiver.prototype.getXml = function(url){ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
return this.getText(url). return this.getText(url, encoding).
then(function(text){ then(function(text){
var parser = new DOMParser(); var parser = new DOMParser();
return parser.parseFromString(text, "application/xml"); return parser.parseFromString(text, "application/xml");
@ -4368,7 +4454,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
return deferred.promise; return deferred.promise;
}; };
EPUBJS.Unarchiver.prototype.getText = function(url){ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); var deferred = new RSVP.defer();
var entry = this.zipFs.find(url); var entry = this.zipFs.find(url);
@ -4379,7 +4465,7 @@ EPUBJS.Unarchiver.prototype.getText = function(url){
entry.getText(function(text){ entry.getText(function(text){
deferred.resolve(text); deferred.resolve(text);
}, null, null, 'ISO-8859-1'); }, null, null, encoding || 'UTF-8');
return deferred.promise; return deferred.promise;
}; };
@ -4387,7 +4473,6 @@ EPUBJS.Unarchiver.prototype.getText = function(url){
EPUBJS.Unarchiver.prototype.revokeUrl = function(url){ EPUBJS.Unarchiver.prototype.revokeUrl = function(url){
var _URL = window.URL || window.webkitURL || window.mozURL; var _URL = window.URL || window.webkitURL || window.mozURL;
var fromCache = unarchiver.urlCache[url]; var fromCache = unarchiver.urlCache[url];
console.log("revoke", fromCache);
if(fromCache) _URL.revokeObjectURL(fromCache); if(fromCache) _URL.revokeObjectURL(fromCache);
}; };

View file

@ -183,35 +183,6 @@ EPUBJS.Hooks.register("beforeChapterDisplay").mathml = function(callback, render
} }
} }
EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns = function(callback, renderer){
var lock = false;
$(renderer.docEl).keydown(function(e){
if(lock) return;
if (e.keyCode == 37) {
renderer.book.prevPage();
lock = true;
setTimeout(function(){
lock = false;
}, 100);
return false;
}
if (e.keyCode == 39) {
renderer.book.nextPage();
lock = true;
setTimeout(function(){
lock = false;
}, 100);
return false;
}
});
if(callback) callback();
}
EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, chapter){ EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, chapter){
var images = chapter.doc.querySelectorAll('img'), var images = chapter.doc.querySelectorAll('img'),
items = Array.prototype.slice.call(images), items = Array.prototype.slice.call(images),

2
build/hooks.min.js vendored
View file

@ -1 +1 @@
EPUBJS.Hooks.register("beforeChapterDisplay").endnotes=function(a,b){var c=b.doc.querySelectorAll("a[href]"),d=Array.prototype.slice.call(c),e="epub:type",f="noteref",g=EPUBJS.core.folder(location.pathname),h=g+EPUBJS.cssPath||g,i={};EPUBJS.core.addCss(h+"popup.css",!1,b.doc.head),d.forEach(function(a){function c(){var c,e=b.iframe.height,f=b.iframe.width,j=225;o||(c=l.cloneNode(!0),o=c.querySelector("p")),b.replaceLinks.bind(this),i[k]||(i[k]=document.createElement("div"),i[k].setAttribute("class","popup"),pop_content=document.createElement("div"),i[k].appendChild(pop_content),pop_content.appendChild(o),pop_content.setAttribute("class","pop_content"),b.bodyEl.appendChild(i[k]),i[k].addEventListener("mouseover",d,!1),i[k].addEventListener("mouseout",g,!1),b.book.on("renderer:pageChanged",h,this),b.book.on("renderer:pageChanged",g,this)),c=i[k],itemRect=a.getBoundingClientRect(),m=itemRect.left,n=itemRect.top,c.classList.add("show"),popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width/2+"px",c.style.top=n+"px",j>e/2.5&&(j=e/2.5,pop_content.style.maxHeight=j+"px"),popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),m-popRect.width<=0?(c.style.left=m+"px",c.classList.add("left")):c.classList.remove("left"),m+popRect.width/2>=f?(c.style.left=m-300+"px",popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width+"px",popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),c.classList.add("right")):c.classList.remove("right")}function d(){i[k].classList.add("on")}function g(){i[k].classList.remove("on")}function h(){setTimeout(function(){i[k].classList.remove("show")},100)}var j,k,l,m,n,o,p=a.getAttribute(e);p==f&&(j=a.getAttribute("href"),k=j.replace("#",""),l=b.doc.getElementById(k),a.addEventListener("mouseover",c,!1),a.addEventListener("mouseout",h,!1))}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").mathml=function(a,b){if(-1!==b.currentChapter.properties.indexOf("mathml")){b.iframe.contentWindow.mathmlCallback=a;var c=document.createElement("script");c.type="text/x-mathjax-config",c.innerHTML=' MathJax.Hub.Register.StartupHook("End",function () { window.mathmlCallback(); }); MathJax.Hub.Config({jax: ["input/TeX","input/MathML","output/SVG"],extensions: ["tex2jax.js","mml2jax.js","MathEvents.js"],TeX: {extensions: ["noErrors.js","noUndefined.js","autoload-all.js"]},MathMenu: {showRenderer: false},menuSettings: {zoom: "Click"},messageStyle: "none"}); ',b.doc.body.appendChild(c),EPUBJS.core.addScript("http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML",null,b.doc.head)}else a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns=function(a,b){var c=!1;$(b.docEl).keydown(function(a){return c?void 0:37==a.keyCode?(b.book.prevPage(),c=!0,setTimeout(function(){c=!1},100),!1):39==a.keyCode?(b.book.nextPage(),c=!0,setTimeout(function(){c=!1},100),!1):void 0}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").smartimages=function(a,b){var c=b.doc.querySelectorAll("img"),d=Array.prototype.slice.call(c),e=b.bodyEl.clientHeight;d.forEach(function(a){function c(){var c,d=a.getBoundingClientRect(),f=d.height,g=d.top,h=a.getAttribute("data-height"),i=h||f;e=b.docEl.clientHeight,0>g&&(g=0),i+g>=e?(e/2>g?(c=e-g,a.style.maxHeight=c+"px",a.style.width="auto"):(c=e>i?i:e,a.style.maxHeight=c+"px",a.style.marginTop=e-g+"px",a.style.width="auto",console.log(c)),a.setAttribute("data-height",c)):(a.style.removeProperty("max-height"),a.style.removeProperty("margin-top"))}a.addEventListener("load",c,!1),b.on("renderer:resized",c),b.on("renderer:chapterUnloaded",function(){a.removeEventListener("load",c),b.off("renderer:resized",c)}),c()}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").transculsions=function(a,b){var c=b.doc.querySelectorAll("[transclusion]"),d=Array.prototype.slice.call(c);d.forEach(function(a){function c(){j=g,k=h,j>b.colWidth&&(d=b.colWidth/j,j=b.colWidth,k*=d),f.width=j,f.height=k}var d,e=a.getAttribute("ref"),f=document.createElement("iframe"),g=a.getAttribute("width"),h=a.getAttribute("height"),i=a.parentNode,j=g,k=h;c(),b.book.listenUntil("book:resized","book:chapterDestroy",c),f.src=e,i.replaceChild(f,a)}),a&&a()}; EPUBJS.Hooks.register("beforeChapterDisplay").endnotes=function(a,b){var c=b.doc.querySelectorAll("a[href]"),d=Array.prototype.slice.call(c),e="epub:type",f="noteref",g=EPUBJS.core.folder(location.pathname),h=g+EPUBJS.cssPath||g,i={};EPUBJS.core.addCss(h+"popup.css",!1,b.doc.head),d.forEach(function(a){function c(){var c,e=b.iframe.height,f=b.iframe.width,j=225;o||(c=l.cloneNode(!0),o=c.querySelector("p")),b.replaceLinks.bind(this),i[k]||(i[k]=document.createElement("div"),i[k].setAttribute("class","popup"),pop_content=document.createElement("div"),i[k].appendChild(pop_content),pop_content.appendChild(o),pop_content.setAttribute("class","pop_content"),b.bodyEl.appendChild(i[k]),i[k].addEventListener("mouseover",d,!1),i[k].addEventListener("mouseout",g,!1),b.book.on("renderer:pageChanged",h,this),b.book.on("renderer:pageChanged",g,this)),c=i[k],itemRect=a.getBoundingClientRect(),m=itemRect.left,n=itemRect.top,c.classList.add("show"),popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width/2+"px",c.style.top=n+"px",j>e/2.5&&(j=e/2.5,pop_content.style.maxHeight=j+"px"),popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),m-popRect.width<=0?(c.style.left=m+"px",c.classList.add("left")):c.classList.remove("left"),m+popRect.width/2>=f?(c.style.left=m-300+"px",popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width+"px",popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),c.classList.add("right")):c.classList.remove("right")}function d(){i[k].classList.add("on")}function g(){i[k].classList.remove("on")}function h(){setTimeout(function(){i[k].classList.remove("show")},100)}var j,k,l,m,n,o,p=a.getAttribute(e);p==f&&(j=a.getAttribute("href"),k=j.replace("#",""),l=b.doc.getElementById(k),a.addEventListener("mouseover",c,!1),a.addEventListener("mouseout",h,!1))}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").mathml=function(a,b){if(-1!==b.currentChapter.properties.indexOf("mathml")){b.iframe.contentWindow.mathmlCallback=a;var c=document.createElement("script");c.type="text/x-mathjax-config",c.innerHTML=' MathJax.Hub.Register.StartupHook("End",function () { window.mathmlCallback(); }); MathJax.Hub.Config({jax: ["input/TeX","input/MathML","output/SVG"],extensions: ["tex2jax.js","mml2jax.js","MathEvents.js"],TeX: {extensions: ["noErrors.js","noUndefined.js","autoload-all.js"]},MathMenu: {showRenderer: false},menuSettings: {zoom: "Click"},messageStyle: "none"}); ',b.doc.body.appendChild(c),EPUBJS.core.addScript("http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML",null,b.doc.head)}else a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").smartimages=function(a,b){var c=b.doc.querySelectorAll("img"),d=Array.prototype.slice.call(c),e=b.bodyEl.clientHeight;d.forEach(function(a){function c(){var c,d=a.getBoundingClientRect(),f=d.height,g=d.top,h=a.getAttribute("data-height"),i=h||f;e=b.docEl.clientHeight,0>g&&(g=0),i+g>=e?(e/2>g?(c=e-g,a.style.maxHeight=c+"px",a.style.width="auto"):(c=e>i?i:e,a.style.maxHeight=c+"px",a.style.marginTop=e-g+"px",a.style.width="auto",console.log(c)),a.setAttribute("data-height",c)):(a.style.removeProperty("max-height"),a.style.removeProperty("margin-top"))}a.addEventListener("load",c,!1),b.on("renderer:resized",c),b.on("renderer:chapterUnloaded",function(){a.removeEventListener("load",c),b.off("renderer:resized",c)}),c()}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").transculsions=function(a,b){var c=b.doc.querySelectorAll("[transclusion]"),d=Array.prototype.slice.call(c);d.forEach(function(a){function c(){j=g,k=h,j>b.colWidth&&(d=b.colWidth/j,j=b.colWidth,k*=d),f.width=j,f.height=k}var d,e=a.getAttribute("ref"),f=document.createElement("iframe"),g=a.getAttribute("width"),h=a.getAttribute("height"),i=a.parentNode,j=g,k=h;c(),b.book.listenUntil("book:resized","book:chapterDestroy",c),f.src=e,i.replaceChild(f,a)}),a&&a()};

View file

@ -1,7 +1,7 @@
EPUBJS.reader = {}; EPUBJS.reader = {};
EPUBJS.reader.plugins = {}; //-- Attach extra Controllers as plugins (like search?) EPUBJS.reader.plugins = {}; //-- Attach extra Controllers as plugins (like search?)
(function(root) { (function(root, $) {
var previousReader = root.ePubReader || {}; var previousReader = root.ePubReader || {};
@ -24,16 +24,20 @@ EPUBJS.reader.plugins = {}; //-- Attach extra Controllers as plugins (like searc
//Node //Node
module.exports = ePubReader; module.exports = ePubReader;
})(window); })(window, jQuery);
EPUBJS.Reader = function(path, _options) { EPUBJS.Reader = function(path, _options) {
var reader = this; var reader = this;
var book; var book;
var plugin;
this.settings = _.defaults(_options || {}, { this.settings = _.defaults(_options || {}, {
restore: true, restore : true,
reload: false, reload : false,
bookmarks: null bookmarks : null,
contained : null,
bookKey : null,
styles : null
}); });
this.setBookKey(path); //-- This could be username + path or any unique string this.setBookKey(path); //-- This could be username + path or any unique string
@ -42,10 +46,17 @@ EPUBJS.Reader = function(path, _options) {
this.applySavedSettings(); this.applySavedSettings();
} }
this.settings.styles = this.settings.styles || {
fontSize : "100%"
};
this.book = book = new EPUBJS.Book({ this.book = book = new EPUBJS.Book({
bookPath: path, bookPath: path,
restore: this.settings.restore, restore: this.settings.restore,
reload: this.settings.reload reload: this.settings.reload,
contained: this.settings.contained,
bookKey: this.settings.bookKey,
styles: this.settings.styles
}); });
if(this.settings.previousLocationCfi) { if(this.settings.previousLocationCfi) {
@ -67,7 +78,7 @@ EPUBJS.Reader = function(path, _options) {
reader.BookmarksController = EPUBJS.reader.BookmarksController.call(reader, book); reader.BookmarksController = EPUBJS.reader.BookmarksController.call(reader, book);
// Call Plugins // Call Plugins
for(var plugin in EPUBJS.reader.plugins) { for(plugin in EPUBJS.reader.plugins) {
if(EPUBJS.reader.plugins.hasOwnProperty(plugin)) { if(EPUBJS.reader.plugins.hasOwnProperty(plugin)) {
reader[plugin] = EPUBJS.reader.plugins[plugin].call(reader, book); reader[plugin] = EPUBJS.reader.plugins[plugin].call(reader, book);
} }
@ -87,9 +98,48 @@ EPUBJS.Reader = function(path, _options) {
window.addEventListener("beforeunload", this.unload.bind(this), false); window.addEventListener("beforeunload", this.unload.bind(this), false);
document.addEventListener('keydown', this.adjustFontSize.bind(this), false);
book.on("renderer:keydown", this.adjustFontSize.bind(this));
book.on("renderer:keydown", reader.ReaderController.arrowKeys.bind(this));
return this; return this;
}; };
EPUBJS.Reader.prototype.adjustFontSize = function(e) {
var fontSize;
var interval = 2;
var PLUS = 187;
var MINUS = 189;
var ZERO = 48;
var MOD = (e.ctrlKey || e.metaKey );
if(!this.settings.styles) return;
if(!this.settings.styles.fontSize) {
this.settings.styles.fontSize = "100%";
}
fontSize = parseInt(this.settings.styles.fontSize.slice(0, -1));
if(MOD && e.keyCode == PLUS) {
e.preventDefault();
this.book.setStyle("fontSize", (fontSize + interval) + "%");
}
if(MOD && e.keyCode == MINUS){
e.preventDefault();
this.book.setStyle("fontSize", (fontSize - interval) + "%");
}
if(MOD && e.keyCode == ZERO){
e.preventDefault();
this.book.setStyle("fontSize", "100%");
}
};
EPUBJS.Reader.prototype.addBookmark = function(cfi) { EPUBJS.Reader.prototype.addBookmark = function(cfi) {
var present = this.isBookmarked(cfi); var present = this.isBookmarked(cfi);
if(present > -1 ) return; if(present > -1 ) return;
@ -117,9 +167,10 @@ EPUBJS.Reader.prototype.isBookmarked = function(cfi) {
/* /*
EPUBJS.Reader.prototype.searchBookmarked = function(cfi) { EPUBJS.Reader.prototype.searchBookmarked = function(cfi) {
var bookmarks = this.settings.bookmarks, var bookmarks = this.settings.bookmarks,
len = bookmarks.length; len = bookmarks.length,
i;
for(var i = 0; i < len; i++) { for(i = 0; i < len; i++) {
if (bookmarks[i]['cfi'] === cfi) return i; if (bookmarks[i]['cfi'] === cfi) return i;
} }
return -1; return -1;
@ -271,6 +322,8 @@ EPUBJS.reader.ControlsController = function(book) {
// $store.attr("src", $icon.data("saved")); // $store.attr("src", $icon.data("saved"));
}; };
var fullscreen = false;
book.on("book:online", goOnline); book.on("book:online", goOnline);
book.on("book:offline", goOffline); book.on("book:offline", goOffline);
@ -288,10 +341,22 @@ EPUBJS.reader.ControlsController = function(book) {
$fullscreen.on("click", function() { $fullscreen.on("click", function() {
screenfull.toggle($('#container')[0]); screenfull.toggle($('#container')[0]);
$fullscreenicon.toggle();
$cancelfullscreenicon.toggle();
}); });
document.addEventListener(screenfull.raw.fullscreenchange, function() {
fullscreen = screenfull.isFullscreen;
if(fullscreen) {
$fullscreen
.addClass("icon-resize-small")
.removeClass("icon-resize-full");
} else {
$fullscreen
.addClass("icon-resize-full")
.removeClass("icon-resize-small");
}
});
$settings.on("click", function() { $settings.on("click", function() {
reader.SettingsController.show(); reader.SettingsController.show();
}); });
@ -372,7 +437,7 @@ EPUBJS.reader.ReaderController = function(book) {
$loader.hide(); $loader.hide();
//-- If the book is using spreads, show the divider //-- If the book is using spreads, show the divider
if(!book.single) { if(book.settings.spreads) {
showDivider(); showDivider();
} }
}; };
@ -426,13 +491,22 @@ EPUBJS.reader.ReaderController = function(book) {
e.preventDefault(); e.preventDefault();
}); });
book.on("book:spreads", function(){
if(book.settings.spreads) {
showDivider();
} else {
hideDivider();
}
});
return { return {
"slideOut" : slideOut, "slideOut" : slideOut,
"slideIn" : slideIn, "slideIn" : slideIn,
"showLoader" : showLoader, "showLoader" : showLoader,
"hideLoader" : hideLoader, "hideLoader" : hideLoader,
"showDivider" : showDivider, "showDivider" : showDivider,
"hideDivider" : hideDivider "hideDivider" : hideDivider,
"arrowKeys" : arrowKeys
}; };
}; };
EPUBJS.reader.SettingsController = function() { EPUBJS.reader.SettingsController = function() {

View file

@ -81,7 +81,9 @@ body {
opacity: 1; opacity: 1;
color: rgba(0,0,0,.6); color: rgba(0,0,0,.6);
/* margin: 1px -1px -1px 1px; */ /* margin: 1px -1px -1px 1px; */
-moz-box-shadow: inset 0 0 6px rgba(155,155,155,.8);
-webkit-box-shadow: inset 0 0 6px rgba(155,155,155,.8); -webkit-box-shadow: inset 0 0 6px rgba(155,155,155,.8);
box-shadow: inset 0 0 6px rgba(155,155,155,.8);
} }
#book-title { #book-title {
@ -129,7 +131,8 @@ body {
color: #777; color: #777;
} }
.arrow:active { .arrow:active,
.arrow.active {
color: #000; color: #000;
} }
@ -166,8 +169,11 @@ body {
left: 0; left: 0;
top: 0; top: 0;
width: 100%; width: 100%;
padding: 10px 0; padding: 13px 0;
height: 21px; height: 14px;
-moz-box-shadow: 0px 1px 3px rgba(0,0,0,.6);
-webkit-box-shadow: 0px 1px 3px rgba(0,0,0,.6);
box-shadow: 0px 1px 3px rgba(0,0,0,.6);
} }
#opener { #opener {

View file

@ -63,7 +63,7 @@
<script async src="../hooks/default/transculsions.js"></script> <script async src="../hooks/default/transculsions.js"></script>
<script async src="../hooks/default/endnotes.js"></script> <script async src="../hooks/default/endnotes.js"></script>
<script async src="../hooks/default/smartimages.js"></script> <script async src="../hooks/default/smartimages.js"></script>
<script async src="../hooks/default/pageturns.js"></script>
<!-- Reader --> <!-- Reader -->
<script src="../reader/reader.js"></script> <script src="../reader/reader.js"></script>

4
demo/js/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1 +1 @@
EPUBJS.Hooks.register("beforeChapterDisplay").endnotes=function(a,b){var c=b.doc.querySelectorAll("a[href]"),d=Array.prototype.slice.call(c),e="epub:type",f="noteref",g=EPUBJS.core.folder(location.pathname),h=g+EPUBJS.cssPath||g,i={};EPUBJS.core.addCss(h+"popup.css",!1,b.doc.head),d.forEach(function(a){function c(){var c,e=b.iframe.height,f=b.iframe.width,j=225;o||(c=l.cloneNode(!0),o=c.querySelector("p")),b.replaceLinks.bind(this),i[k]||(i[k]=document.createElement("div"),i[k].setAttribute("class","popup"),pop_content=document.createElement("div"),i[k].appendChild(pop_content),pop_content.appendChild(o),pop_content.setAttribute("class","pop_content"),b.bodyEl.appendChild(i[k]),i[k].addEventListener("mouseover",d,!1),i[k].addEventListener("mouseout",g,!1),b.book.on("renderer:pageChanged",h,this),b.book.on("renderer:pageChanged",g,this)),c=i[k],itemRect=a.getBoundingClientRect(),m=itemRect.left,n=itemRect.top,c.classList.add("show"),popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width/2+"px",c.style.top=n+"px",j>e/2.5&&(j=e/2.5,pop_content.style.maxHeight=j+"px"),popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),m-popRect.width<=0?(c.style.left=m+"px",c.classList.add("left")):c.classList.remove("left"),m+popRect.width/2>=f?(c.style.left=m-300+"px",popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width+"px",popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),c.classList.add("right")):c.classList.remove("right")}function d(){i[k].classList.add("on")}function g(){i[k].classList.remove("on")}function h(){setTimeout(function(){i[k].classList.remove("show")},100)}var j,k,l,m,n,o,p=a.getAttribute(e);p==f&&(j=a.getAttribute("href"),k=j.replace("#",""),l=b.doc.getElementById(k),a.addEventListener("mouseover",c,!1),a.addEventListener("mouseout",h,!1))}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").mathml=function(a,b){if(-1!==b.currentChapter.properties.indexOf("mathml")){b.iframe.contentWindow.mathmlCallback=a;var c=document.createElement("script");c.type="text/x-mathjax-config",c.innerHTML=' MathJax.Hub.Register.StartupHook("End",function () { window.mathmlCallback(); }); MathJax.Hub.Config({jax: ["input/TeX","input/MathML","output/SVG"],extensions: ["tex2jax.js","mml2jax.js","MathEvents.js"],TeX: {extensions: ["noErrors.js","noUndefined.js","autoload-all.js"]},MathMenu: {showRenderer: false},menuSettings: {zoom: "Click"},messageStyle: "none"}); ',b.doc.body.appendChild(c),EPUBJS.core.addScript("http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML",null,b.doc.head)}else a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns=function(a,b){var c=!1;$(b.docEl).keydown(function(a){return c?void 0:37==a.keyCode?(b.book.prevPage(),c=!0,setTimeout(function(){c=!1},100),!1):39==a.keyCode?(b.book.nextPage(),c=!0,setTimeout(function(){c=!1},100),!1):void 0}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").smartimages=function(a,b){var c=b.doc.querySelectorAll("img"),d=Array.prototype.slice.call(c),e=b.bodyEl.clientHeight;d.forEach(function(a){function c(){var c,d=a.getBoundingClientRect(),f=d.height,g=d.top,h=a.getAttribute("data-height"),i=h||f;e=b.docEl.clientHeight,0>g&&(g=0),i+g>=e?(e/2>g?(c=e-g,a.style.maxHeight=c+"px",a.style.width="auto"):(c=e>i?i:e,a.style.maxHeight=c+"px",a.style.marginTop=e-g+"px",a.style.width="auto",console.log(c)),a.setAttribute("data-height",c)):(a.style.removeProperty("max-height"),a.style.removeProperty("margin-top"))}a.addEventListener("load",c,!1),b.on("renderer:resized",c),b.on("renderer:chapterUnloaded",function(){a.removeEventListener("load",c),b.off("renderer:resized",c)}),c()}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").transculsions=function(a,b){var c=b.doc.querySelectorAll("[transclusion]"),d=Array.prototype.slice.call(c);d.forEach(function(a){function c(){j=g,k=h,j>b.colWidth&&(d=b.colWidth/j,j=b.colWidth,k*=d),f.width=j,f.height=k}var d,e=a.getAttribute("ref"),f=document.createElement("iframe"),g=a.getAttribute("width"),h=a.getAttribute("height"),i=a.parentNode,j=g,k=h;c(),b.book.listenUntil("book:resized","book:chapterDestroy",c),f.src=e,i.replaceChild(f,a)}),a&&a()}; EPUBJS.Hooks.register("beforeChapterDisplay").endnotes=function(a,b){var c=b.doc.querySelectorAll("a[href]"),d=Array.prototype.slice.call(c),e="epub:type",f="noteref",g=EPUBJS.core.folder(location.pathname),h=g+EPUBJS.cssPath||g,i={};EPUBJS.core.addCss(h+"popup.css",!1,b.doc.head),d.forEach(function(a){function c(){var c,e=b.iframe.height,f=b.iframe.width,j=225;o||(c=l.cloneNode(!0),o=c.querySelector("p")),b.replaceLinks.bind(this),i[k]||(i[k]=document.createElement("div"),i[k].setAttribute("class","popup"),pop_content=document.createElement("div"),i[k].appendChild(pop_content),pop_content.appendChild(o),pop_content.setAttribute("class","pop_content"),b.bodyEl.appendChild(i[k]),i[k].addEventListener("mouseover",d,!1),i[k].addEventListener("mouseout",g,!1),b.book.on("renderer:pageChanged",h,this),b.book.on("renderer:pageChanged",g,this)),c=i[k],itemRect=a.getBoundingClientRect(),m=itemRect.left,n=itemRect.top,c.classList.add("show"),popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width/2+"px",c.style.top=n+"px",j>e/2.5&&(j=e/2.5,pop_content.style.maxHeight=j+"px"),popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),m-popRect.width<=0?(c.style.left=m+"px",c.classList.add("left")):c.classList.remove("left"),m+popRect.width/2>=f?(c.style.left=m-300+"px",popRect=c.getBoundingClientRect(),c.style.left=m-popRect.width+"px",popRect.height+n>=e-25?(c.style.top=n-popRect.height+"px",c.classList.add("above")):c.classList.remove("above"),c.classList.add("right")):c.classList.remove("right")}function d(){i[k].classList.add("on")}function g(){i[k].classList.remove("on")}function h(){setTimeout(function(){i[k].classList.remove("show")},100)}var j,k,l,m,n,o,p=a.getAttribute(e);p==f&&(j=a.getAttribute("href"),k=j.replace("#",""),l=b.doc.getElementById(k),a.addEventListener("mouseover",c,!1),a.addEventListener("mouseout",h,!1))}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").mathml=function(a,b){if(-1!==b.currentChapter.properties.indexOf("mathml")){b.iframe.contentWindow.mathmlCallback=a;var c=document.createElement("script");c.type="text/x-mathjax-config",c.innerHTML=' MathJax.Hub.Register.StartupHook("End",function () { window.mathmlCallback(); }); MathJax.Hub.Config({jax: ["input/TeX","input/MathML","output/SVG"],extensions: ["tex2jax.js","mml2jax.js","MathEvents.js"],TeX: {extensions: ["noErrors.js","noUndefined.js","autoload-all.js"]},MathMenu: {showRenderer: false},menuSettings: {zoom: "Click"},messageStyle: "none"}); ',b.doc.body.appendChild(c),EPUBJS.core.addScript("http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML",null,b.doc.head)}else a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").smartimages=function(a,b){var c=b.doc.querySelectorAll("img"),d=Array.prototype.slice.call(c),e=b.bodyEl.clientHeight;d.forEach(function(a){function c(){var c,d=a.getBoundingClientRect(),f=d.height,g=d.top,h=a.getAttribute("data-height"),i=h||f;e=b.docEl.clientHeight,0>g&&(g=0),i+g>=e?(e/2>g?(c=e-g,a.style.maxHeight=c+"px",a.style.width="auto"):(c=e>i?i:e,a.style.maxHeight=c+"px",a.style.marginTop=e-g+"px",a.style.width="auto",console.log(c)),a.setAttribute("data-height",c)):(a.style.removeProperty("max-height"),a.style.removeProperty("margin-top"))}a.addEventListener("load",c,!1),b.on("renderer:resized",c),b.on("renderer:chapterUnloaded",function(){a.removeEventListener("load",c),b.off("renderer:resized",c)}),c()}),a&&a()},EPUBJS.Hooks.register("beforeChapterDisplay").transculsions=function(a,b){var c=b.doc.querySelectorAll("[transclusion]"),d=Array.prototype.slice.call(c);d.forEach(function(a){function c(){j=g,k=h,j>b.colWidth&&(d=b.colWidth/j,j=b.colWidth,k*=d),f.width=j,f.height=k}var d,e=a.getAttribute("ref"),f=document.createElement("iframe"),g=a.getAttribute("width"),h=a.getAttribute("height"),i=a.parentNode,j=g,k=h;c(),b.book.listenUntil("book:resized","book:chapterDestroy",c),f.src=e,i.replaceChild(f,a)}),a&&a()};

File diff suppressed because one or more lines are too long

View file

@ -38,9 +38,6 @@
-moz-box-shadow: inset 10px 0 20px rgba(0,0,0,.1); -moz-box-shadow: inset 10px 0 20px rgba(0,0,0,.1);
-webkit-box-shadow: inset 10px 0 20px rgba(0,0,0,.1); -webkit-box-shadow: inset 10px 0 20px rgba(0,0,0,.1);
box-shadow: inset 10px 0 20px rgba(0,0,0,.1); box-shadow: inset 10px 0 20px rgba(0,0,0,.1);
}
#area iframe {
padding: 40px 40px; padding: 40px 40px;
} }

View file

@ -1,29 +0,0 @@
EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns = function(callback, renderer){
var lock = false;
$(renderer.docEl).keydown(function(e){
if(lock) return;
if (e.keyCode == 37) {
renderer.book.prevPage();
lock = true;
setTimeout(function(){
lock = false;
}, 100);
return false;
}
if (e.keyCode == 39) {
renderer.book.nextPage();
lock = true;
setTimeout(function(){
lock = false;
}, 100);
return false;
}
});
if(callback) callback();
}

View file

@ -1,8 +1,7 @@
EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns = function(callback, renderer){ EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns = function(callback, renderer){
var lock = false; var lock = false;
var arrowKeys = function(e){
$(renderer.docEl).keydown(function(e){
if(lock) return; if(lock) return;
if (e.keyCode == 37) { if (e.keyCode == 37) {
@ -23,7 +22,7 @@ EPUBJS.Hooks.register("beforeChapterDisplay").pageTurns = function(callback, ren
return false; return false;
} }
}); };
renderer.docEl.addEventListener('keydown', arrowKeys, false);
if(callback) callback(); if(callback) callback();
} }

View file

@ -21,6 +21,8 @@ EPUBJS.reader.ControlsController = function(book) {
// $store.attr("src", $icon.data("saved")); // $store.attr("src", $icon.data("saved"));
}; };
var fullscreen = false;
book.on("book:online", goOnline); book.on("book:online", goOnline);
book.on("book:offline", goOffline); book.on("book:offline", goOffline);
@ -38,10 +40,22 @@ EPUBJS.reader.ControlsController = function(book) {
$fullscreen.on("click", function() { $fullscreen.on("click", function() {
screenfull.toggle($('#container')[0]); screenfull.toggle($('#container')[0]);
$fullscreenicon.toggle();
$cancelfullscreenicon.toggle();
}); });
document.addEventListener(screenfull.raw.fullscreenchange, function() {
fullscreen = screenfull.isFullscreen;
if(fullscreen) {
$fullscreen
.addClass("icon-resize-small")
.removeClass("icon-resize-full");
} else {
$fullscreen
.addClass("icon-resize-full")
.removeClass("icon-resize-small");
}
});
$settings.on("click", function() { $settings.on("click", function() {
reader.SettingsController.show(); reader.SettingsController.show();
}); });

View file

@ -22,7 +22,7 @@ EPUBJS.reader.ReaderController = function(book) {
$loader.hide(); $loader.hide();
//-- If the book is using spreads, show the divider //-- If the book is using spreads, show the divider
if(!book.single) { if(book.settings.spreads) {
showDivider(); showDivider();
} }
}; };
@ -76,12 +76,21 @@ EPUBJS.reader.ReaderController = function(book) {
e.preventDefault(); e.preventDefault();
}); });
book.on("book:spreads", function(){
if(book.settings.spreads) {
showDivider();
} else {
hideDivider();
}
});
return { return {
"slideOut" : slideOut, "slideOut" : slideOut,
"slideIn" : slideIn, "slideIn" : slideIn,
"showLoader" : showLoader, "showLoader" : showLoader,
"hideLoader" : hideLoader, "hideLoader" : hideLoader,
"showDivider" : showDivider, "showDivider" : showDivider,
"hideDivider" : hideDivider "hideDivider" : hideDivider,
"arrowKeys" : arrowKeys
}; };
}; };

View file

@ -1,7 +1,7 @@
EPUBJS.reader = {}; EPUBJS.reader = {};
EPUBJS.reader.plugins = {}; //-- Attach extra Controllers as plugins (like search?) EPUBJS.reader.plugins = {}; //-- Attach extra Controllers as plugins (like search?)
(function(root) { (function(root, $) {
var previousReader = root.ePubReader || {}; var previousReader = root.ePubReader || {};
@ -24,16 +24,20 @@ EPUBJS.reader.plugins = {}; //-- Attach extra Controllers as plugins (like searc
//Node //Node
module.exports = ePubReader; module.exports = ePubReader;
})(window); })(window, jQuery);
EPUBJS.Reader = function(path, _options) { EPUBJS.Reader = function(path, _options) {
var reader = this; var reader = this;
var book; var book;
var plugin;
this.settings = _.defaults(_options || {}, { this.settings = _.defaults(_options || {}, {
restore: true, restore : true,
reload: false, reload : false,
bookmarks: null bookmarks : null,
contained : null,
bookKey : null,
styles : null
}); });
this.setBookKey(path); //-- This could be username + path or any unique string this.setBookKey(path); //-- This could be username + path or any unique string
@ -42,10 +46,17 @@ EPUBJS.Reader = function(path, _options) {
this.applySavedSettings(); this.applySavedSettings();
} }
this.settings.styles = this.settings.styles || {
fontSize : "100%"
};
this.book = book = new EPUBJS.Book({ this.book = book = new EPUBJS.Book({
bookPath: path, bookPath: path,
restore: this.settings.restore, restore: this.settings.restore,
reload: this.settings.reload reload: this.settings.reload,
contained: this.settings.contained,
bookKey: this.settings.bookKey,
styles: this.settings.styles
}); });
if(this.settings.previousLocationCfi) { if(this.settings.previousLocationCfi) {
@ -67,7 +78,7 @@ EPUBJS.Reader = function(path, _options) {
reader.BookmarksController = EPUBJS.reader.BookmarksController.call(reader, book); reader.BookmarksController = EPUBJS.reader.BookmarksController.call(reader, book);
// Call Plugins // Call Plugins
for(var plugin in EPUBJS.reader.plugins) { for(plugin in EPUBJS.reader.plugins) {
if(EPUBJS.reader.plugins.hasOwnProperty(plugin)) { if(EPUBJS.reader.plugins.hasOwnProperty(plugin)) {
reader[plugin] = EPUBJS.reader.plugins[plugin].call(reader, book); reader[plugin] = EPUBJS.reader.plugins[plugin].call(reader, book);
} }
@ -87,9 +98,48 @@ EPUBJS.Reader = function(path, _options) {
window.addEventListener("beforeunload", this.unload.bind(this), false); window.addEventListener("beforeunload", this.unload.bind(this), false);
document.addEventListener('keydown', this.adjustFontSize.bind(this), false);
book.on("renderer:keydown", this.adjustFontSize.bind(this));
book.on("renderer:keydown", reader.ReaderController.arrowKeys.bind(this));
return this; return this;
}; };
EPUBJS.Reader.prototype.adjustFontSize = function(e) {
var fontSize;
var interval = 2;
var PLUS = 187;
var MINUS = 189;
var ZERO = 48;
var MOD = (e.ctrlKey || e.metaKey );
if(!this.settings.styles) return;
if(!this.settings.styles.fontSize) {
this.settings.styles.fontSize = "100%";
}
fontSize = parseInt(this.settings.styles.fontSize.slice(0, -1));
if(MOD && e.keyCode == PLUS) {
e.preventDefault();
this.book.setStyle("fontSize", (fontSize + interval) + "%");
}
if(MOD && e.keyCode == MINUS){
e.preventDefault();
this.book.setStyle("fontSize", (fontSize - interval) + "%");
}
if(MOD && e.keyCode == ZERO){
e.preventDefault();
this.book.setStyle("fontSize", "100%");
}
};
EPUBJS.Reader.prototype.addBookmark = function(cfi) { EPUBJS.Reader.prototype.addBookmark = function(cfi) {
var present = this.isBookmarked(cfi); var present = this.isBookmarked(cfi);
if(present > -1 ) return; if(present > -1 ) return;
@ -117,9 +167,10 @@ EPUBJS.Reader.prototype.isBookmarked = function(cfi) {
/* /*
EPUBJS.Reader.prototype.searchBookmarked = function(cfi) { EPUBJS.Reader.prototype.searchBookmarked = function(cfi) {
var bookmarks = this.settings.bookmarks, var bookmarks = this.settings.bookmarks,
len = bookmarks.length; len = bookmarks.length,
i;
for(var i = 0; i < len; i++) { for(i = 0; i < len; i++) {
if (bookmarks[i]['cfi'] === cfi) return i; if (bookmarks[i]['cfi'] === cfi) return i;
} }
return -1; return -1;

View file

@ -171,6 +171,7 @@ EPUBJS.Book.prototype.loadPackage = function(_containerPath){
then(function(paths){ then(function(paths){
book.settings.contentsPath = book.bookUrl + paths.basePath; book.settings.contentsPath = book.bookUrl + paths.basePath;
book.settings.packageUrl = book.bookUrl + paths.packagePath; book.settings.packageUrl = book.bookUrl + paths.packagePath;
book.settings.encoding = paths.encoding;
return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata
}); });
} else { } else {
@ -271,9 +272,9 @@ EPUBJS.Book.prototype.networkListeners = function(){
//-- Choose between a request from store or a request from network //-- Choose between a request from store or a request from network
EPUBJS.Book.prototype.loadXml = function(url){ EPUBJS.Book.prototype.loadXml = function(url){
if(this.settings.fromStorage) { if(this.settings.fromStorage) {
return this.storage.getXml(url); return this.storage.getXml(url, this.settings.encoding);
} else if(this.settings.contained) { } else if(this.settings.contained) {
return this.zip.getXml(url); return this.zip.getXml(url, this.settings.encoding);
}else{ }else{
return EPUBJS.core.request(url, 'xml'); return EPUBJS.core.request(url, 'xml');
} }
@ -446,14 +447,16 @@ EPUBJS.Book.prototype.restore = function(identifier){
fetch = ['manifest', 'spine', 'metadata', 'cover', 'toc', 'spineNodeIndex', 'spineIndexByURL'], fetch = ['manifest', 'spine', 'metadata', 'cover', 'toc', 'spineNodeIndex', 'spineIndexByURL'],
reject = false, reject = false,
bookKey = this.setBookKey(identifier), bookKey = this.setBookKey(identifier),
fromStore = localStorage.getItem(bookKey); fromStore = localStorage.getItem(bookKey),
len = fetch.length,
i;
if(this.settings.clearSaved) reject = true; if(this.settings.clearSaved) reject = true;
if(!reject && fromStore != 'undefined' && fromStore != null){ if(!reject && fromStore != 'undefined' && fromStore != null){
book.contents = JSON.parse(fromStore); book.contents = JSON.parse(fromStore);
for(var i = 0, len = fetch.length; i < len; i++) { for(i = 0; i < len; i++) {
var item = fetch[i]; var item = fetch[i];
if(!book.contents[item]) { if(!book.contents[item]) {
@ -638,13 +641,16 @@ EPUBJS.Book.prototype.goto = function(url){
EPUBJS.Book.prototype.preloadNextChapter = function() { EPUBJS.Book.prototype.preloadNextChapter = function() {
var next; var next;
var chap = this.spinePos + 1;
if(this.spinePos >= this.spine.length){ if(chap >= this.spine.length){
return false; return false;
} }
next = new EPUBJS.Chapter(this.spine[this.spinePos + 1]); next = new EPUBJS.Chapter(this.spine[chap]);
if(next) {
EPUBJS.core.request(next.absolute); EPUBJS.core.request(next.absolute);
}
}; };
@ -690,17 +696,28 @@ EPUBJS.Book.prototype.fromStorage = function(stored) {
*/ */
EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) { EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) {
if(!this.isRendered) return this._enqueue("setStyle", arguments);
this.settings.styles[style] = val; this.settings.styles[style] = val;
if(this.render) this.render.setStyle(style, val, prefixed); this.render.setStyle(style, val, prefixed);
this.render.reformat();
}; };
EPUBJS.Book.prototype.removeStyle = function(style) { EPUBJS.Book.prototype.removeStyle = function(style) {
if(this.render) this.render.removeStyle(style); if(!this.isRendered) return this._enqueue("removeStyle", arguments);
this.render.removeStyle(style);
this.render.reformat();
delete this.settings.styles[style]; delete this.settings.styles[style];
}; };
EPUBJS.Book.prototype.useSpreads = function(use) {
if(!this.isRendered) return this._enqueue("useSpreads", arguments);
this.settings.spreads = use;
this.render.reformat();
this.trigger("book:spreads", use);
};
EPUBJS.Book.prototype.unload = function(){ EPUBJS.Book.prototype.unload = function(){
if(this.settings.restore) { if(this.settings.restore) {

View file

@ -7,12 +7,14 @@ EPUBJS.Parser.prototype.container = function(containerXml){
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/> //-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
var rootfile = containerXml.querySelector("rootfile"), var rootfile = containerXml.querySelector("rootfile"),
fullpath = rootfile.getAttribute('full-path'), fullpath = rootfile.getAttribute('full-path'),
folder = EPUBJS.core.folder(fullpath); folder = EPUBJS.core.folder(fullpath),
encoding = containerXml.xmlEncoding;
//-- Now that we have the path we can parse the contents //-- Now that we have the path we can parse the contents
return { return {
'packagePath' : fullpath, 'packagePath' : fullpath,
'basePath' : folder 'basePath' : folder,
'encoding' : encoding
}; };
}; };

View file

@ -10,6 +10,8 @@ EPUBJS.Renderer = function(book) {
this.epubcfi = new EPUBJS.EpubCFI(); this.epubcfi = new EPUBJS.EpubCFI();
this.initialize(); this.initialize();
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click"];
this.listeners(); this.listeners();
//-- Renderer events for listening //-- Renderer events for listening
@ -107,6 +109,50 @@ EPUBJS.Renderer.prototype.hideHashChanges = function(){
*/ */
//-- Listeners for events in the frame
EPUBJS.Renderer.prototype.addIframeListeners = function(){
this.listenedEvents.forEach(function(eventName){
this.doc.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this);
};
EPUBJS.Renderer.prototype.removeIframeListeners = function(){
this.listenedEvents.forEach(function(eventName){
this.doc.removeEventListener(eventName, this.triggerEvent, false);
}, this);
};
EPUBJS.Renderer.prototype.triggerEvent = function(e){
this.book.trigger("renderer:"+e.type, e);
};
EPUBJS.Renderer.prototype.addSelectionListeners = function(){
this.doc.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
this.contentWindow.addEventListener("mouseup", this.onMouseUp.bind(this), false);
};
EPUBJS.Renderer.prototype.removeSelectionListeners = function(){
this.doc.removeEventListener("selectionchange", this.onSelectionChange, false);
this.contentWindow.removeEventListener("mouseup", this.onMouseUp, false);
};
EPUBJS.Renderer.prototype.onSelectionChange = function(e){
this.highlighted = true;
};
EPUBJS.Renderer.prototype.onMouseUp = function(e){
var selection;
if(this.highlighted) {
selection = this.contentWindow.getSelection();
this.book.trigger("renderer:selected", selection);
this.highlighted = false;
}
};
EPUBJS.Renderer.prototype.onResized = function(e){ EPUBJS.Renderer.prototype.onResized = function(e){
var msg = { var msg = {
@ -175,7 +221,6 @@ EPUBJS.Renderer.prototype.crossBrowserColumnCss = function(){
}; };
EPUBJS.Renderer.prototype.setIframeSrc = function(url){ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
var renderer = this, var renderer = this,
deferred = new RSVP.defer(); deferred = new RSVP.defer();
@ -188,6 +233,7 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
renderer.doc = renderer.iframe.contentDocument; renderer.doc = renderer.iframe.contentDocument;
renderer.docEl = renderer.doc.documentElement; renderer.docEl = renderer.doc.documentElement;
renderer.bodyEl = renderer.doc.body; renderer.bodyEl = renderer.doc.body;
renderer.contentWindow = renderer.iframe.contentWindow;
renderer.applyStyles(); renderer.applyStyles();
@ -215,7 +261,9 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
}); });
renderer.iframe.contentWindow.addEventListener("resize", renderer.resized, false); renderer.contentWindow.addEventListener("resize", this.resized, false);
renderer.addIframeListeners();
renderer.addSelectionListeners();
}; };
return deferred.promise; return deferred.promise;
@ -300,6 +348,7 @@ EPUBJS.Renderer.prototype.setStyle = function(style, val, prefixed){
} }
if(this.bodyEl) this.bodyEl.style[style] = val; if(this.bodyEl) this.bodyEl.style[style] = val;
}; };
EPUBJS.Renderer.prototype.removeStyle = function(style){ EPUBJS.Renderer.prototype.removeStyle = function(style){
@ -415,9 +464,9 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
var items = this.doc.querySelectorAll(query), var items = this.doc.querySelectorAll(query),
resources = Array.prototype.slice.call(items), resources = Array.prototype.slice.call(items),
count = resources.length, count = resources.length,
after = function(result){ after = function(result, full){
count--; count--;
if(progress) progress(result, count); if(progress) progress(result, full, count);
if(count <= 0 && finished) finished(true); if(count <= 0 && finished) finished(true);
}; };
@ -442,11 +491,11 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_uri = EPUBJS.core.uri(this.book.chapter.absolute), _uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base, _chapterBase = _uri.base,
_attr = attr, _attr = attr,
_wait = 2000,
progress = function(url, full, count) { progress = function(url, full, count) {
_newUrls[full] = url; _newUrls[full] = url;
}, },
finished = function(notempty) { finished = function(notempty) {
if(callback) callback(); if(callback) callback();
_.each(_oldUrls, function(url){ _.each(_oldUrls, function(url){
@ -466,10 +515,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
full = EPUBJS.core.resolveUrl(_chapterBase, src); full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) { var replaceUrl = function(url) {
var timeout;
link.onload = function(){ link.onload = function(){
clearTimeout(timeout);
done(url, full); done(url, full);
}; };
link.onerror = function(e){ link.onerror = function(e){
clearTimeout(timeout);
done(url, full);
console.error(e); console.error(e);
}; };
@ -478,7 +533,18 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
link.setAttribute("externalResourcesRequired", "true"); link.setAttribute("externalResourcesRequired", "true");
} }
if(query == "link[href]") {
//-- Only Stylesheet links seem to have a load events, just continue others
done(url, full);
}
link.setAttribute(_attr, url); link.setAttribute(_attr, url);
//-- If elements never fire Load Event, should continue anyways
timeout = setTimeout(function(){
done(url, full);
}, _wait);
}; };
if(full in _oldUrls){ if(full in _oldUrls){
@ -684,11 +750,13 @@ EPUBJS.Renderer.prototype.height = function(el){
}; };
EPUBJS.Renderer.prototype.remove = function() { EPUBJS.Renderer.prototype.remove = function() {
this.iframe.contentWindow.removeEventListener("resize", this.resized); this.contentWindow.removeEventListener("resize", this.resized);
this.removeIframeListeners();
this.removeSelectionListeners();
this.el.removeChild(this.iframe); this.el.removeChild(this.iframe);
}; };
//-- Enable binding events to parser //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype); RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);

View file

@ -38,7 +38,6 @@ EPUBJS.replace.links = function(_store, full, done, link){
}else{ }else{
_store.getUrl(full).then(done); _store.getUrl(full).then(done);
} }
}; };
EPUBJS.replace.stylesheets = function(_store, full) { EPUBJS.replace.stylesheets = function(_store, full) {

View file

@ -36,9 +36,9 @@ EPUBJS.Unarchiver.prototype.openZip = function(zipUrl, callback){
return deferred.promise; return deferred.promise;
}; };
EPUBJS.Unarchiver.prototype.getXml = function(url){ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
return this.getText(url). return this.getText(url, encoding).
then(function(text){ then(function(text){
var parser = new DOMParser(); var parser = new DOMParser();
return parser.parseFromString(text, "application/xml"); return parser.parseFromString(text, "application/xml");
@ -68,7 +68,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
return deferred.promise; return deferred.promise;
}; };
EPUBJS.Unarchiver.prototype.getText = function(url){ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); var deferred = new RSVP.defer();
var entry = this.zipFs.find(url); var entry = this.zipFs.find(url);
@ -79,7 +79,7 @@ EPUBJS.Unarchiver.prototype.getText = function(url){
entry.getText(function(text){ entry.getText(function(text){
deferred.resolve(text); deferred.resolve(text);
}, null, null, 'ISO-8859-1'); }, null, null, encoding || 'UTF-8');
return deferred.promise; return deferred.promise;
}; };
@ -87,7 +87,6 @@ EPUBJS.Unarchiver.prototype.getText = function(url){
EPUBJS.Unarchiver.prototype.revokeUrl = function(url){ EPUBJS.Unarchiver.prototype.revokeUrl = function(url){
var _URL = window.URL || window.webkitURL || window.mozURL; var _URL = window.URL || window.webkitURL || window.mozURL;
var fromCache = unarchiver.urlCache[url]; var fromCache = unarchiver.urlCache[url];
console.log("revoke", fromCache);
if(fromCache) _URL.revokeObjectURL(fromCache); if(fromCache) _URL.revokeObjectURL(fromCache);
}; };

View file

@ -168,5 +168,50 @@ asyncTest("Display end of chapter 20 and go to prev page", 3, function() {
}); });
// asyncTest("Add styles to book", 3, function() { asyncTest("Add styles to book", 4, function() {
// });
var Book = ePub('../demo/moby-dick/', { width: 400, height: 600 });
var render = Book.renderTo("qunit-fixture");
var result = function(){
var $iframe = $( "iframe", "#qunit-fixture" ),
$body;
equal( Book.render.bodyEl.style.background, '', "background not set");
Book.setStyle("background", "purple");
equal( Book.render.bodyEl.style.background, "purple", "background is purple");
equal( Book.render.bodyEl.style.fontSize, '', "fontSize not set");
Book.setStyle("fontSize", "100px");
equal( Book.render.bodyEl.style.fontSize, "100px", "fontSize is purple");
start();
};
render.then(result);
});
asyncTest("Switch Spreads to Single", 3, function() {
var Book = ePub('../demo/moby-dick/', { width: 400, height: 600 });
var render = Book.renderTo("qunit-fixture");
var result = function(){
equal( Book.settings.spreads, true, "Use Spreads");
Book.useSpreads(false);
equal( Book.settings.spreads, false, "Don't Use Spreads");
equal( Book.render.docEl.style[EPUBJS.Renderer.columnWidth], "400px", "Don't Use Spreads");
start();
};
render.then(result);
});