mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
added queue to render events and displays
This commit is contained in:
parent
2d19ae683c
commit
5af9e6cd4c
11 changed files with 246 additions and 76 deletions
|
@ -25,7 +25,6 @@ module.exports = function(grunt) {
|
|||
'demo/js/epub.min.js': ['libs/underscore/underscore-min.js', 'build/epub.js'],
|
||||
'build/epub.min.js': ['libs/underscore/underscore-min.js', 'build/epub.js'],
|
||||
'demo/js/reader.min.js': 'build/reader.js',
|
||||
'demo/js/hooks.min.js': 'build/hooks.js',
|
||||
'demo/js/libs/zip.min.js': ['libs/zip/zip.js', 'libs/zip/zip-fs.js', 'libs/zip/zip-ext.js', 'libs/zip/mime-types.js'],
|
||||
'demo/js/libs/inflate.min.js': ['libs/zip/inflate.js'],
|
||||
'build/libs/zip.min.js': ['libs/zip/zip.js', 'libs/zip/zip-fs.js', 'libs/zip/zip-ext.js', 'libs/zip/mime-types.js'],
|
||||
|
|
111
build/epub.js
111
build/epub.js
|
@ -82,6 +82,7 @@ EPUBJS.Book = function(options){
|
|||
version: 1,
|
||||
restore: false,
|
||||
reload : false,
|
||||
goto : false,
|
||||
styles : {}
|
||||
});
|
||||
|
||||
|
@ -117,6 +118,8 @@ EPUBJS.Book = function(options){
|
|||
this.storage = new fileStorage.storage(this.settings.storage);
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.ready = {
|
||||
manifest: new RSVP.Promise(),
|
||||
spine: new RSVP.Promise(),
|
||||
|
@ -127,14 +130,17 @@ EPUBJS.Book = function(options){
|
|||
|
||||
this.ready.all = RSVP.all(_.values(this.ready));
|
||||
|
||||
this.ready.all.then(function(){
|
||||
this.trigger("book:ready");
|
||||
}.bind(this));
|
||||
this.ready.all.then(this._ready);
|
||||
|
||||
this._q = [];
|
||||
this.isRendered = false;
|
||||
this._rendering = false;
|
||||
this._displayQ = [];
|
||||
|
||||
this.opened = new RSVP.Promise();
|
||||
// BookUrl is optional, but if present start loading process
|
||||
if(this.settings.bookPath) {
|
||||
this.open(this.settings.bookPath);
|
||||
this.open(this.settings.bookPath, this.settings.reload);
|
||||
}
|
||||
|
||||
|
||||
|
@ -445,7 +451,7 @@ EPUBJS.Book.prototype.renderTo = function(elem){
|
|||
rendered = this.opened.
|
||||
then(function(){
|
||||
book.render = new EPUBJS.Renderer(book);
|
||||
|
||||
book._rendered();
|
||||
return book.startDisplay();
|
||||
}, function(error) { console.error(error) });
|
||||
|
||||
|
@ -456,7 +462,12 @@ EPUBJS.Book.prototype.renderTo = function(elem){
|
|||
|
||||
EPUBJS.Book.prototype.startDisplay = function(){
|
||||
var display;
|
||||
if( this.settings.restore && this.settings.previousLocationCfi) {
|
||||
|
||||
if( this.settings.restore && this.settings.goto) {
|
||||
|
||||
display = this.goto(this.settings.goto);
|
||||
|
||||
}else if( this.settings.restore && this.settings.previousLocationCfi) {
|
||||
|
||||
display = this.displayChapter(this.settings.previousLocationCfi);
|
||||
|
||||
|
@ -513,7 +524,12 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
|
|||
cfi,
|
||||
pos;
|
||||
|
||||
if(!this.render) return false;
|
||||
if(!this.isRendered) return this._enqueue("displayChapter", arguments);
|
||||
|
||||
if(this._rendering) {
|
||||
this._displayQ.push(arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
if(_.isNumber(chap)){
|
||||
pos = chap;
|
||||
|
@ -541,6 +557,8 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
|
|||
//-- Create a new chapter
|
||||
this.chapter = new EPUBJS.Chapter(this.spine[pos]);
|
||||
|
||||
this._rendering = true;
|
||||
|
||||
render = book.render.chapter(this.chapter);
|
||||
|
||||
if(cfi) {
|
||||
|
@ -562,13 +580,24 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
|
|||
});
|
||||
}
|
||||
|
||||
//-- Clear render queue
|
||||
render.then(function(){
|
||||
var inwait;
|
||||
|
||||
book._rendering = false;
|
||||
if(book._displayQ.length) {
|
||||
inwait = book._displayQ.unshift();
|
||||
book.displayChapter.apply(book, inwait);
|
||||
}
|
||||
|
||||
});
|
||||
return render;
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype.nextPage = function(){
|
||||
var next;
|
||||
|
||||
if(!this.render) return;
|
||||
if(!this.isRendered) return this._enqueue("nextPage", arguments);
|
||||
|
||||
next = this.render.nextPage();
|
||||
|
||||
|
@ -580,7 +609,7 @@ EPUBJS.Book.prototype.nextPage = function(){
|
|||
EPUBJS.Book.prototype.prevPage = function() {
|
||||
var prev;
|
||||
|
||||
if(!this.render) return;
|
||||
if(!this.isRendered) return this._enqueue("prevPage", arguments);
|
||||
|
||||
prev = this.render.prevPage();
|
||||
|
||||
|
@ -602,28 +631,29 @@ EPUBJS.Book.prototype.prevChapter = function() {
|
|||
}
|
||||
|
||||
EPUBJS.Book.prototype.goto = function(url){
|
||||
var split = url.split("#"),
|
||||
var split, chapter, section, absoluteURL, spinePos;
|
||||
|
||||
if(!this.isRendered) return this._enqueue("goto", arguments);
|
||||
|
||||
split = url.split("#"),
|
||||
chapter = split[0],
|
||||
section = split[1] || false,
|
||||
absoluteURL = (chapter.search("://") == -1) ? this.settings.contentsPath + chapter : chapter,
|
||||
spinePos = this.spineIndexByURL[absoluteURL],
|
||||
book;
|
||||
|
||||
if(!this.render) return;
|
||||
spinePos = this.spineIndexByURL[absoluteURL];
|
||||
|
||||
//-- If link fragment only stay on current chapter
|
||||
if(!chapter){
|
||||
spinePos = this.chapter.spinePos;
|
||||
spinePos = this.chapter ? this.chapter.spinePos : 0;
|
||||
}
|
||||
|
||||
//-- Check that URL is present in the index, or stop
|
||||
if(typeof(spinePos) != "number") return false;
|
||||
|
||||
if(spinePos != this.chapter.spinePos || !this.chapter){
|
||||
if(!this.chapter || spinePos != this.chapter.spinePos){
|
||||
//-- Load new chapter if different than current
|
||||
return this.displayChapter(spinePos).then(function(){
|
||||
if(section) book.render.section(section);
|
||||
});
|
||||
if(section) this.render.section(section);
|
||||
}.bind(this));
|
||||
}else{
|
||||
//-- Only goto section
|
||||
if(section) this.render.section(section);
|
||||
|
@ -700,7 +730,7 @@ EPUBJS.Book.prototype.removeStyle = function(style) {
|
|||
delete this.settings.styles[style];
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype.unload = function(bookPath, forceReload){
|
||||
EPUBJS.Book.prototype.unload = function(){
|
||||
|
||||
if(this.settings.restore) {
|
||||
this.saveSettings();
|
||||
|
@ -722,6 +752,33 @@ EPUBJS.Book.prototype.destroy = function() {
|
|||
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype._enqueue = function(command, arguments) {
|
||||
|
||||
this._q.push({
|
||||
'command': command,
|
||||
'arguments': arguments
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype._ready = function(err) {
|
||||
var book = this;
|
||||
|
||||
this.trigger("book:ready");
|
||||
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype._rendered = function(err) {
|
||||
var book = this;
|
||||
|
||||
this.isRendered = true;
|
||||
this.trigger("book:rendered");
|
||||
|
||||
this._q.forEach(function(item){
|
||||
book[item.command].apply(book, item.arguments);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
//-- Get pre-registered hooks
|
||||
EPUBJS.Book.prototype.getHooks = function(){
|
||||
|
@ -1619,7 +1676,8 @@ EPUBJS.Parser.prototype.toc = function(tocXml){
|
|||
"id": id,
|
||||
"href": src,
|
||||
"label": text,
|
||||
"subitems" : subitems
|
||||
"subitems" : subitems,
|
||||
"parent" : parent ? parent.getAttribute('id') : null
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -1627,7 +1685,6 @@ EPUBJS.Parser.prototype.toc = function(tocXml){
|
|||
return list;
|
||||
}
|
||||
|
||||
|
||||
return getTOC(navMap);
|
||||
|
||||
|
||||
|
@ -1660,6 +1717,7 @@ EPUBJS.Renderer = function(book) {
|
|||
EPUBJS.Renderer.prototype.initialize = function(){
|
||||
this.iframe = document.createElement('iframe');
|
||||
//this.iframe.id = "epubjs-iframe";
|
||||
this.iframe.scrolling = "no";
|
||||
|
||||
if(this.book.settings.width || this.book.settings.height){
|
||||
this.resizeIframe(false, this.book.settings.width || this.el.clientWidth, this.book.settings.height || this.el.clientHeight);
|
||||
|
@ -1829,7 +1887,6 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
|
|||
|
||||
this.iframe.src = url;
|
||||
|
||||
|
||||
this.iframe.onload = function() {
|
||||
renderer.doc = renderer.iframe.contentDocument;
|
||||
renderer.docEl = renderer.doc.documentElement;
|
||||
|
@ -1913,7 +1970,7 @@ EPUBJS.Renderer.prototype.formatSpread = function(){
|
|||
// this.bodyEl.style.fontSize = localStorage.getItem("fontSize") || "medium";
|
||||
|
||||
//-- Clear Margins
|
||||
this.bodyEl.style.margin = "0";
|
||||
if(this.bodyEl) this.bodyEl.style.margin = "0";
|
||||
|
||||
this.docEl.style.overflow = "hidden";
|
||||
|
||||
|
@ -2039,8 +2096,6 @@ EPUBJS.Renderer.prototype.prevPage = function(){
|
|||
|
||||
EPUBJS.Renderer.prototype.chapterEnd = function(){
|
||||
this.page(this.displayedPages);
|
||||
|
||||
this.currentLocationCfi = this.getPageCfi();
|
||||
}
|
||||
|
||||
EPUBJS.Renderer.prototype.setLeft = function(leftPos){
|
||||
|
@ -2181,6 +2236,10 @@ EPUBJS.Renderer.prototype.page = function(pg){
|
|||
this.leftPos = this.spreadWidth * (pg-1); //-- pages start at 1
|
||||
this.setLeft(this.leftPos);
|
||||
|
||||
this.currentLocationCfi = this.getPageCfi();
|
||||
|
||||
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
|
||||
|
||||
// localStorage.setItem("chapterPos", pg);
|
||||
return true;
|
||||
}
|
||||
|
@ -2316,7 +2375,7 @@ EPUBJS.Renderer.prototype.isElementVisible = function(el){
|
|||
left = el.getBoundingClientRect().left;
|
||||
|
||||
if( left >= 0 &&
|
||||
left <= this.spreadWidth ) {
|
||||
left < this.spreadWidth ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
4
build/epub.min.js
vendored
4
build/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -172,7 +172,7 @@ EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, c
|
|||
height = oHeight || rectHeight,
|
||||
newHeight;
|
||||
|
||||
iheight = chapter.bodyEl.clientHeight;
|
||||
iheight = chapter.docEl.clientHeight;
|
||||
if(top < 0) top = 0;
|
||||
|
||||
if(height + top >= iheight) {
|
||||
|
@ -182,10 +182,11 @@ EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, c
|
|||
item.style.maxHeight = newHeight + "px";
|
||||
item.style.width= "auto";
|
||||
}else{
|
||||
height = (height < iheight ? height : iheight);
|
||||
newHeight = (height < iheight ? height : iheight);
|
||||
item.style.maxHeight = newHeight + "px";
|
||||
item.style.marginTop = iheight - top + "px";
|
||||
item.style.width= "auto";
|
||||
console.log(newHeight)
|
||||
}
|
||||
|
||||
item.setAttribute('data-height', newHeight);
|
||||
|
|
4
demo/js/epub.min.js
vendored
4
demo/js/epub.min.js
vendored
File diff suppressed because one or more lines are too long
2
demo/js/hooks.min.js
vendored
2
demo/js/hooks.min.js
vendored
|
@ -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")),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("book:pageChanged",h,this),b.book.on("book: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").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.bodyEl.clientHeight,0>g&&(g=0),i+g>=e?(e/2>g?(c=e-g,a.style.maxHeight=c+"px",a.style.width="auto"):(i=e>i?i:e,a.style.maxHeight=c+"px",a.style.marginTop=e-g+"px",a.style.width="auto"),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")),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("book:pageChanged",h,this),b.book.on("book: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").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()};
|
|
@ -37,7 +37,7 @@
|
|||
<!-- Hooks -->
|
||||
<!-- <script src="../hooks/default/transculsions.js"></script> -->
|
||||
<!-- <script src="../hooks/default/endnotes.js"></script> -->
|
||||
<!-- <script src="../hooks/default/smartimages.js"></script> -->
|
||||
<script src="../hooks/default/smartimages.js"></script>
|
||||
|
||||
<script>
|
||||
|
||||
|
@ -104,7 +104,7 @@
|
|||
"use strict";
|
||||
|
||||
var Book = ePub("../demo/moby-dick.epub", {restore: true, reload: true });
|
||||
|
||||
Book.goto('chapter_008.xhtml');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -14,7 +14,7 @@ EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, c
|
|||
height = oHeight || rectHeight,
|
||||
newHeight;
|
||||
|
||||
iheight = chapter.bodyEl.clientHeight;
|
||||
iheight = chapter.docEl.clientHeight;
|
||||
if(top < 0) top = 0;
|
||||
|
||||
if(height + top >= iheight) {
|
||||
|
@ -24,10 +24,11 @@ EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, c
|
|||
item.style.maxHeight = newHeight + "px";
|
||||
item.style.width= "auto";
|
||||
}else{
|
||||
height = (height < iheight ? height : iheight);
|
||||
newHeight = (height < iheight ? height : iheight);
|
||||
item.style.maxHeight = newHeight + "px";
|
||||
item.style.marginTop = iheight - top + "px";
|
||||
item.style.width= "auto";
|
||||
console.log(newHeight)
|
||||
}
|
||||
|
||||
item.setAttribute('data-height', newHeight);
|
||||
|
|
91
src/book.js
91
src/book.js
|
@ -17,6 +17,7 @@ EPUBJS.Book = function(options){
|
|||
version: 1,
|
||||
restore: false,
|
||||
reload : false,
|
||||
goto : false,
|
||||
styles : {}
|
||||
});
|
||||
|
||||
|
@ -52,6 +53,8 @@ EPUBJS.Book = function(options){
|
|||
this.storage = new fileStorage.storage(this.settings.storage);
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.ready = {
|
||||
manifest: new RSVP.Promise(),
|
||||
spine: new RSVP.Promise(),
|
||||
|
@ -62,9 +65,12 @@ EPUBJS.Book = function(options){
|
|||
|
||||
this.ready.all = RSVP.all(_.values(this.ready));
|
||||
|
||||
this.ready.all.then(function(){
|
||||
this.trigger("book:ready");
|
||||
}.bind(this));
|
||||
this.ready.all.then(this._ready);
|
||||
|
||||
this._q = [];
|
||||
this.isRendered = false;
|
||||
this._rendering = false;
|
||||
this._displayQ = [];
|
||||
|
||||
this.opened = new RSVP.Promise();
|
||||
// BookUrl is optional, but if present start loading process
|
||||
|
@ -380,7 +386,7 @@ EPUBJS.Book.prototype.renderTo = function(elem){
|
|||
rendered = this.opened.
|
||||
then(function(){
|
||||
book.render = new EPUBJS.Renderer(book);
|
||||
|
||||
book._rendered();
|
||||
return book.startDisplay();
|
||||
}, function(error) { console.error(error) });
|
||||
|
||||
|
@ -391,7 +397,12 @@ EPUBJS.Book.prototype.renderTo = function(elem){
|
|||
|
||||
EPUBJS.Book.prototype.startDisplay = function(){
|
||||
var display;
|
||||
if( this.settings.restore && this.settings.previousLocationCfi) {
|
||||
|
||||
if( this.settings.restore && this.settings.goto) {
|
||||
|
||||
display = this.goto(this.settings.goto);
|
||||
|
||||
}else if( this.settings.restore && this.settings.previousLocationCfi) {
|
||||
|
||||
display = this.displayChapter(this.settings.previousLocationCfi);
|
||||
|
||||
|
@ -448,7 +459,12 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
|
|||
cfi,
|
||||
pos;
|
||||
|
||||
if(!this.render) return false;
|
||||
if(!this.isRendered) return this._enqueue("displayChapter", arguments);
|
||||
|
||||
if(this._rendering) {
|
||||
this._displayQ.push(arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
if(_.isNumber(chap)){
|
||||
pos = chap;
|
||||
|
@ -476,6 +492,8 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
|
|||
//-- Create a new chapter
|
||||
this.chapter = new EPUBJS.Chapter(this.spine[pos]);
|
||||
|
||||
this._rendering = true;
|
||||
|
||||
render = book.render.chapter(this.chapter);
|
||||
|
||||
if(cfi) {
|
||||
|
@ -497,13 +515,24 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
|
|||
});
|
||||
}
|
||||
|
||||
//-- Clear render queue
|
||||
render.then(function(){
|
||||
var inwait;
|
||||
|
||||
book._rendering = false;
|
||||
if(book._displayQ.length) {
|
||||
inwait = book._displayQ.unshift();
|
||||
book.displayChapter.apply(book, inwait);
|
||||
}
|
||||
|
||||
});
|
||||
return render;
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype.nextPage = function(){
|
||||
var next;
|
||||
|
||||
if(!this.render) return;
|
||||
if(!this.isRendered) return this._enqueue("nextPage", arguments);
|
||||
|
||||
next = this.render.nextPage();
|
||||
|
||||
|
@ -515,7 +544,7 @@ EPUBJS.Book.prototype.nextPage = function(){
|
|||
EPUBJS.Book.prototype.prevPage = function() {
|
||||
var prev;
|
||||
|
||||
if(!this.render) return;
|
||||
if(!this.isRendered) return this._enqueue("prevPage", arguments);
|
||||
|
||||
prev = this.render.prevPage();
|
||||
|
||||
|
@ -537,28 +566,29 @@ EPUBJS.Book.prototype.prevChapter = function() {
|
|||
}
|
||||
|
||||
EPUBJS.Book.prototype.goto = function(url){
|
||||
var split = url.split("#"),
|
||||
var split, chapter, section, absoluteURL, spinePos;
|
||||
|
||||
if(!this.isRendered) return this._enqueue("goto", arguments);
|
||||
|
||||
split = url.split("#"),
|
||||
chapter = split[0],
|
||||
section = split[1] || false,
|
||||
absoluteURL = (chapter.search("://") == -1) ? this.settings.contentsPath + chapter : chapter,
|
||||
spinePos = this.spineIndexByURL[absoluteURL],
|
||||
book;
|
||||
|
||||
if(!this.render) return;
|
||||
spinePos = this.spineIndexByURL[absoluteURL];
|
||||
|
||||
//-- If link fragment only stay on current chapter
|
||||
if(!chapter){
|
||||
spinePos = this.chapter.spinePos;
|
||||
spinePos = this.chapter ? this.chapter.spinePos : 0;
|
||||
}
|
||||
|
||||
//-- Check that URL is present in the index, or stop
|
||||
if(typeof(spinePos) != "number") return false;
|
||||
|
||||
if(spinePos != this.chapter.spinePos || !this.chapter){
|
||||
if(!this.chapter || spinePos != this.chapter.spinePos){
|
||||
//-- Load new chapter if different than current
|
||||
return this.displayChapter(spinePos).then(function(){
|
||||
if(section) book.render.section(section);
|
||||
});
|
||||
if(section) this.render.section(section);
|
||||
}.bind(this));
|
||||
}else{
|
||||
//-- Only goto section
|
||||
if(section) this.render.section(section);
|
||||
|
@ -657,6 +687,33 @@ EPUBJS.Book.prototype.destroy = function() {
|
|||
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype._enqueue = function(command, arguments) {
|
||||
|
||||
this._q.push({
|
||||
'command': command,
|
||||
'arguments': arguments
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype._ready = function(err) {
|
||||
var book = this;
|
||||
|
||||
this.trigger("book:ready");
|
||||
|
||||
}
|
||||
|
||||
EPUBJS.Book.prototype._rendered = function(err) {
|
||||
var book = this;
|
||||
|
||||
this.isRendered = true;
|
||||
this.trigger("book:rendered");
|
||||
|
||||
this._q.forEach(function(item){
|
||||
book[item.command].apply(book, item.arguments);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
//-- Get pre-registered hooks
|
||||
EPUBJS.Book.prototype.getHooks = function(){
|
||||
|
|
|
@ -202,7 +202,8 @@ EPUBJS.Parser.prototype.toc = function(tocXml){
|
|||
"id": id,
|
||||
"href": src,
|
||||
"label": text,
|
||||
"subitems" : subitems
|
||||
"subitems" : subitems,
|
||||
"parent" : parent ? parent.getAttribute('id') : null
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -210,7 +211,6 @@ EPUBJS.Parser.prototype.toc = function(tocXml){
|
|||
return list;
|
||||
}
|
||||
|
||||
|
||||
return getTOC(navMap);
|
||||
|
||||
|
||||
|
|
|
@ -77,6 +77,59 @@ asyncTest("Go to chapter 1 and advance to next page", 4, function() {
|
|||
|
||||
});
|
||||
|
||||
asyncTest("Go to chapter 10 at restore start", 2, function() {
|
||||
var Book = ePub('../demo/moby-dick/', { width: 400,
|
||||
height: 600,
|
||||
restore: true,
|
||||
goto: "chapter_010.xhtml" });
|
||||
|
||||
var render = Book.renderTo("qunit-fixture");
|
||||
|
||||
var result = function(){
|
||||
|
||||
var $iframe = $( "iframe", "#qunit-fixture" ),
|
||||
$body;
|
||||
|
||||
equal( $iframe.length, 1, "iframe added successfully" );
|
||||
|
||||
|
||||
|
||||
start();
|
||||
equal( Book.render.currentChapter.id, "xchapter_010", "on chapter 10");
|
||||
|
||||
};
|
||||
|
||||
render.then(result);
|
||||
|
||||
|
||||
});
|
||||
|
||||
asyncTest("Go to chapter 20 from queue", 2, function() {
|
||||
var Book = ePub('../demo/moby-dick/', { width: 400,
|
||||
height: 600 });
|
||||
|
||||
Book.goto("chapter_020.xhtml");
|
||||
|
||||
var render = Book.renderTo("qunit-fixture");
|
||||
|
||||
var result = function(){
|
||||
|
||||
var $iframe = $( "iframe", "#qunit-fixture" ),
|
||||
$body;
|
||||
|
||||
equal( $iframe.length, 1, "iframe added successfully" );
|
||||
|
||||
|
||||
|
||||
start();
|
||||
equal( Book.render.currentChapter.id, "xchapter_020", "on chapter 20");
|
||||
|
||||
};
|
||||
|
||||
render.then(result);
|
||||
|
||||
});
|
||||
|
||||
asyncTest("Display end of chapter 20 and go to prev page", 3, function() {
|
||||
|
||||
var Book = ePub('../demo/moby-dick/', { width: 400, height: 600 });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue