mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Added error handles, sections, updated url resolving, use book.request for requests
This commit is contained in:
parent
c6465177c5
commit
c29e5f84ee
19 changed files with 3049 additions and 683 deletions
|
@ -2,28 +2,18 @@ EPUBJS.Renderer = function(book, _options) {
|
|||
var options = _options || {};
|
||||
this.settings = {
|
||||
hidden: options.hidden || false,
|
||||
viewsLimit: 4,
|
||||
viewsLimit: 6,
|
||||
width: options.width || false,
|
||||
height: options.height || false,
|
||||
};
|
||||
|
||||
this.book = book;
|
||||
|
||||
// Listen for load events
|
||||
// this.on("render:loaded", this.loaded.bind(this));
|
||||
|
||||
// Blank Cfi for Parsing
|
||||
this.epubcfi = new EPUBJS.EpubCFI();
|
||||
|
||||
this.layoutSettings = {};
|
||||
|
||||
//-- Adds Hook methods to the Book prototype
|
||||
// Hooks will all return before triggering the callback.
|
||||
EPUBJS.Hooks.mixin(this);
|
||||
|
||||
//-- Get pre-registered hooks for events
|
||||
this.getHooks("beforeChapterDisplay");
|
||||
|
||||
//-- Queue up page changes if page map isn't ready
|
||||
this._q = EPUBJS.core.queue(this);
|
||||
|
||||
|
@ -31,14 +21,18 @@ EPUBJS.Renderer = function(book, _options) {
|
|||
|
||||
this.initialize({
|
||||
"width" : this.settings.width,
|
||||
"height" : this.settings.height,
|
||||
"hidden" : true
|
||||
"height" : this.settings.height
|
||||
});
|
||||
|
||||
this.rendering = false;
|
||||
this.views = [];
|
||||
this.positions = [];
|
||||
|
||||
//-- Adds Hook methods to the Renderer prototype
|
||||
this.hooks = {};
|
||||
this.hooks.display = new EPUBJS.Hook(this);
|
||||
this.hooks.replacements = new EPUBJS.Hook(this);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -51,7 +45,6 @@ EPUBJS.Renderer.prototype.initialize = function(_options){
|
|||
var width = options.width ? options.width + "px" : "100%";
|
||||
var hidden = options.hidden || false;
|
||||
|
||||
|
||||
this.container = document.createElement("div");
|
||||
this.infinite = new EPUBJS.Infinite(this.container, this);
|
||||
|
||||
|
@ -69,7 +62,7 @@ EPUBJS.Renderer.prototype.initialize = function(_options){
|
|||
this.wrapper.appendChild(this.container);
|
||||
return this.wrapper;
|
||||
}
|
||||
|
||||
|
||||
return this.container;
|
||||
};
|
||||
|
||||
|
@ -126,15 +119,27 @@ EPUBJS.Renderer.prototype.attachTo = function(_element){
|
|||
this.infinite.start();
|
||||
|
||||
this.infinite.on("forwards", function(){
|
||||
if(!this.rendering) this.forwards();
|
||||
var next = this.last().section.index + 1;
|
||||
|
||||
if(!this.rendering && next < this.book.spine.length){
|
||||
this.forwards();
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
|
||||
this.infinite.on("backwards", function(){
|
||||
if(!this.rendering) this.backwards();
|
||||
var prev = this.first().section.index - 1;
|
||||
|
||||
if(!this.rendering && prev > 0){
|
||||
this.backwards();
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
|
||||
window.addEventListener("resize", this.onResized.bind(this), false);
|
||||
|
||||
this.hooks.replacements.register(this.replacements.bind(this));
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Renderer.prototype.clear = function(){
|
||||
|
@ -154,14 +159,20 @@ EPUBJS.Renderer.prototype.display = function(what){
|
|||
|
||||
this.book.opened.then(function(){
|
||||
var section = this.book.spine.get(what);
|
||||
var rendered = this.render(section);
|
||||
var rendered;
|
||||
|
||||
rendered.
|
||||
then(this.fill.bind(this)).
|
||||
then(function(){
|
||||
displaying.resolve(this);
|
||||
}.bind(this));
|
||||
|
||||
if(section){
|
||||
rendered = this.render(section);
|
||||
|
||||
rendered
|
||||
.then(this.fill.bind(this))
|
||||
.then(function(){
|
||||
displaying.resolve(this);
|
||||
}.bind(this));
|
||||
} else {
|
||||
displaying.reject(new Error("No Section Found"));
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
|
||||
return displayed;
|
||||
|
@ -169,28 +180,35 @@ EPUBJS.Renderer.prototype.display = function(what){
|
|||
|
||||
EPUBJS.Renderer.prototype.render = function(section){
|
||||
var rendered;
|
||||
var view = new EPUBJS.View();
|
||||
var view;
|
||||
|
||||
if(!section) {
|
||||
rendered.reject();
|
||||
return;
|
||||
};
|
||||
|
||||
rendered = section.render();
|
||||
view.index = section.index;
|
||||
rendered = new RSVP.defer();
|
||||
rendered.reject(new Error("No Section Provided"));
|
||||
return rendered.promise;
|
||||
};
|
||||
|
||||
view = new EPUBJS.View(section);
|
||||
|
||||
// Place view in correct position
|
||||
this.insert(view, section.index);
|
||||
|
||||
return rendered.
|
||||
then(function(contents){
|
||||
// Place view in correct position
|
||||
this.insert(view, section.index);
|
||||
rendered = view.render(this.book.request);
|
||||
|
||||
return view.load(contents);
|
||||
return rendered
|
||||
.then(function(){
|
||||
return this.hooks.display.trigger(view);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
return this.hooks.replacements.trigger(view, this);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
this.rendering = false;
|
||||
view.show();
|
||||
return view;
|
||||
}.bind(this))
|
||||
.catch(function(e){
|
||||
this.trigger("loaderror", e);
|
||||
}.bind(this));
|
||||
|
||||
};
|
||||
|
@ -201,14 +219,13 @@ EPUBJS.Renderer.prototype.forwards = function(){
|
|||
var rendered;
|
||||
var section;
|
||||
|
||||
next = this.last().index + 1;
|
||||
|
||||
next = this.last().section.index + 1;
|
||||
if(this.rendering || next === this.book.spine.length){
|
||||
rendered = new RSVP.defer();
|
||||
rendered.reject({message: "reject forwards"});
|
||||
rendered.reject(new Error("Reject Forwards"));
|
||||
return rendered.promise;
|
||||
}
|
||||
console.log("going forwards")
|
||||
// console.log("going forwards")
|
||||
|
||||
this.rendering = true;
|
||||
|
||||
|
@ -216,11 +233,8 @@ EPUBJS.Renderer.prototype.forwards = function(){
|
|||
rendered = this.render(section);
|
||||
|
||||
rendered.then(function(){
|
||||
// this.rendering = false;
|
||||
var first = this.first();
|
||||
var bounds = first.bounds();
|
||||
var container = this.container.getBoundingClientRect();
|
||||
var offset;
|
||||
var prev = this.container.scrollTop;
|
||||
if(this.views.length > this.settings.viewsLimit) {
|
||||
|
||||
|
@ -232,6 +246,7 @@ EPUBJS.Renderer.prototype.forwards = function(){
|
|||
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
|
||||
return rendered;
|
||||
};
|
||||
|
@ -242,13 +257,14 @@ EPUBJS.Renderer.prototype.backwards = function(view){
|
|||
var section;
|
||||
|
||||
|
||||
prev = this.first().index - 1;
|
||||
prev = this.first().section.index - 1;
|
||||
|
||||
if(this.rendering || prev < 0){
|
||||
rendered = new RSVP.defer();
|
||||
rendered.reject({message: "reject backwards"});
|
||||
rendered.reject(new Error("Reject Backwards"));
|
||||
return rendered.promise;
|
||||
}
|
||||
console.log("going backwards")
|
||||
// console.log("going backwards")
|
||||
|
||||
this.rendering = true;
|
||||
|
||||
|
@ -257,6 +273,7 @@ EPUBJS.Renderer.prototype.backwards = function(view){
|
|||
|
||||
rendered.then(function(){
|
||||
// this.container.scrollTop += this.first().height;
|
||||
|
||||
this.infinite.scrollBy(0, this.first().height, true);
|
||||
|
||||
if(this.views.length > this.settings.viewsLimit) {
|
||||
|
@ -277,21 +294,25 @@ EPUBJS.Renderer.prototype.fill = function() {
|
|||
var next = function(){
|
||||
var bottom = this.last().bounds().bottom;
|
||||
var defer = new RSVP.defer();
|
||||
var promise = defer.promise;
|
||||
|
||||
if (height && bottom && (bottom < height) && (this.last().index + 1 < this.book.spine.length)) {
|
||||
if (height && bottom && (bottom < height)) { //&& (this.last().section.index + 1 < this.book.spine.length)) {
|
||||
return this.forwards().then(next);
|
||||
} else {
|
||||
this.rendering = false;
|
||||
defer.resolve();
|
||||
return promise;
|
||||
return defer.promise;
|
||||
}
|
||||
}.bind(this);
|
||||
var prev = this.first().section.index - 1;
|
||||
var filling = next();
|
||||
|
||||
if(prev > 0){
|
||||
filling.then(this.backwards.bind(this));
|
||||
}
|
||||
|
||||
|
||||
return next().
|
||||
then(this.backwards.bind(this)).
|
||||
then(function(){
|
||||
return filling
|
||||
.then(function(){
|
||||
this.rendering = false;
|
||||
}.bind(this));
|
||||
|
||||
|
@ -319,12 +340,11 @@ EPUBJS.Renderer.prototype.insert = function(view, index){
|
|||
|
||||
if(!this.first()) {
|
||||
this.append(view);
|
||||
} else if(index - this.first().index >= 0) {
|
||||
} else if(index - this.first().section.index >= 0) {
|
||||
this.append(view);
|
||||
} else if(index - this.last().index <= 0) {
|
||||
} else if(index - this.last().section.index <= 0) {
|
||||
this.prepend(view);
|
||||
}
|
||||
console.log("insert")
|
||||
// return position;
|
||||
};
|
||||
|
||||
|
@ -345,5 +365,38 @@ EPUBJS.Renderer.prototype.last = function() {
|
|||
return this.views[this.views.length-1];
|
||||
};
|
||||
|
||||
EPUBJS.Renderer.prototype.replacements = function(view, renderer) {
|
||||
var task = new RSVP.defer();
|
||||
var links = view.document.querySelectorAll("a[href]");
|
||||
var replaceLinks = function(link){
|
||||
var href = link.getAttribute("href");
|
||||
var isRelative = href.search("://");
|
||||
// var directory = EPUBJS.core.uri(view.window.location.href).directory;
|
||||
// var relative;
|
||||
|
||||
if(isRelative != -1){
|
||||
|
||||
link.setAttribute("target", "_blank");
|
||||
|
||||
}else{
|
||||
|
||||
// relative = EPUBJS.core.resolveUrl(directory, href);
|
||||
|
||||
link.onclick = function(){
|
||||
renderer.display(href);
|
||||
return false;
|
||||
};
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
for (var i = 0; i < links.length; i++) {
|
||||
replaceLinks(links[i]);
|
||||
};
|
||||
|
||||
task.resolve();
|
||||
return task.promise;
|
||||
};
|
||||
|
||||
//-- Enable binding events to Renderer
|
||||
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
|
Loading…
Add table
Add a link
Reference in a new issue