mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
Added basic pagination
This commit is contained in:
parent
1a89a9c3ff
commit
816880d328
12 changed files with 1289 additions and 373 deletions
|
@ -1,10 +1,12 @@
|
|||
EPUBJS.Renderer = function(book, _options) {
|
||||
var options = _options || {};
|
||||
this.settings = {
|
||||
hidden: options.hidden || false,
|
||||
viewsLimit: 6,
|
||||
width: options.width || false,
|
||||
height: options.height || false,
|
||||
infinite: typeof(options.infinite) === "undefined" ? true : options.infinite,
|
||||
hidden: typeof(options.hidden) === "undefined" ? false : options.hidden,
|
||||
axis: options.axis || "vertical",
|
||||
viewsLimit: options.viewsLimit || 5,
|
||||
width: typeof(options.width) === "undefined" ? false : options.width,
|
||||
height: typeof(options.height) === "undefined" ? false : options.height,
|
||||
};
|
||||
|
||||
this.book = book;
|
||||
|
@ -33,6 +35,11 @@ EPUBJS.Renderer = function(book, _options) {
|
|||
this.hooks.display = new EPUBJS.Hook(this);
|
||||
this.hooks.replacements = new EPUBJS.Hook(this);
|
||||
|
||||
if(!this.settings.infinite) {
|
||||
this.settings.viewsLimit = 1;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -41,12 +48,29 @@ EPUBJS.Renderer = function(book, _options) {
|
|||
*/
|
||||
EPUBJS.Renderer.prototype.initialize = function(_options){
|
||||
var options = _options || {};
|
||||
var height = options.height ? options.height + "px" : "100%";
|
||||
var width = options.width ? options.width + "px" : "100%";
|
||||
var height = options.height !== false ? options.height : "100%";
|
||||
var width = options.width !== false ? options.width : "100%";
|
||||
var hidden = options.hidden || false;
|
||||
|
||||
if(options.height && EPUBJS.core.isNumber(options.height)) {
|
||||
height = options.height + "px";
|
||||
}
|
||||
|
||||
if(options.width && EPUBJS.core.isNumber(options.width)) {
|
||||
width = options.width + "px";
|
||||
}
|
||||
|
||||
this.container = document.createElement("div");
|
||||
this.infinite = new EPUBJS.Infinite(this.container, this);
|
||||
|
||||
if(this.settings.infinite) {
|
||||
this.infinite = new EPUBJS.Infinite(this.container, this.settings.axis);
|
||||
}
|
||||
|
||||
if(this.settings.axis === "horizontal") {
|
||||
// this.container.style.display = "flex";
|
||||
// this.container.style.flexWrap = "nowrap";
|
||||
this.container.style.whiteSpace = "nowrap";
|
||||
}
|
||||
|
||||
this.container.style.width = width;
|
||||
this.container.style.height = height;
|
||||
|
@ -116,26 +140,29 @@ EPUBJS.Renderer.prototype.attachTo = function(_element){
|
|||
this.resize(bounds.width, bounds.height);
|
||||
}
|
||||
|
||||
this.infinite.start();
|
||||
if(this.settings.infinite) {
|
||||
|
||||
this.infinite.on("forwards", function(){
|
||||
var next = this.last().section.index + 1;
|
||||
|
||||
if(!this.rendering && next < this.book.spine.length){
|
||||
this.forwards();
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
this.infinite.start();
|
||||
|
||||
this.infinite.on("backwards", function(){
|
||||
var prev = this.first().section.index - 1;
|
||||
this.infinite.on("forwards", function(){
|
||||
var next = this.last().section.index + 1;
|
||||
|
||||
if(!this.rendering && prev > 0){
|
||||
this.backwards();
|
||||
}
|
||||
if(!this.rendering && next < this.book.spine.length){
|
||||
this.forwards();
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
|
||||
this.infinite.on("backwards", function(){
|
||||
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));
|
||||
|
@ -164,11 +191,14 @@ EPUBJS.Renderer.prototype.display = function(what){
|
|||
if(section){
|
||||
rendered = this.render(section);
|
||||
|
||||
rendered
|
||||
.then(this.fill.bind(this))
|
||||
.then(function(){
|
||||
displaying.resolve(this);
|
||||
}.bind(this));
|
||||
if(this.settings.infinite) {
|
||||
rendered.then(this.fill.bind(this))
|
||||
}
|
||||
|
||||
rendered.then(function(){
|
||||
displaying.resolve(this);
|
||||
}.bind(this));
|
||||
|
||||
} else {
|
||||
displaying.reject(new Error("No Section Found"));
|
||||
}
|
||||
|
@ -202,6 +232,9 @@ EPUBJS.Renderer.prototype.render = function(section){
|
|||
.then(function(){
|
||||
return this.hooks.replacements.trigger(view, this);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
return view.expand();
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
this.rendering = false;
|
||||
view.show();
|
||||
|
@ -235,16 +268,24 @@ EPUBJS.Renderer.prototype.forwards = function(){
|
|||
rendered.then(function(){
|
||||
var first = this.first();
|
||||
var bounds = first.bounds();
|
||||
var prev = this.container.scrollTop;
|
||||
var prevTop = this.container.scrollTop;
|
||||
var prevLeft = this.container.scrollLeft;
|
||||
|
||||
if(this.views.length > this.settings.viewsLimit) {
|
||||
|
||||
// Remove the item
|
||||
this.remove(first);
|
||||
|
||||
// Reset Position
|
||||
this.infinite.scroll(0, prev - bounds.height, true)
|
||||
|
||||
if(this.settings.infinite) {
|
||||
// Reset Position
|
||||
if(this.settings.axis === "vertical") {
|
||||
this.infinite.scroll(0, prevTop - bounds.height, true)
|
||||
} else {
|
||||
this.infinite.scroll(prevLeft - bounds.width, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
|
||||
|
||||
|
@ -272,11 +313,23 @@ EPUBJS.Renderer.prototype.backwards = function(view){
|
|||
rendered = this.render(section);
|
||||
|
||||
rendered.then(function(){
|
||||
// this.container.scrollTop += this.first().height;
|
||||
var last;
|
||||
|
||||
this.infinite.scrollBy(0, this.first().height, true);
|
||||
if(this.settings.infinite) {
|
||||
|
||||
// this.container.scrollTop += this.first().height;
|
||||
if(this.settings.axis === "vertical") {
|
||||
this.infinite.scrollBy(0, this.first().bounds().height, true);
|
||||
} else {
|
||||
this.infinite.scrollBy(this.first().bounds().width, 0, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(this.views.length > this.settings.viewsLimit) {
|
||||
|
||||
|
||||
|
||||
last = this.last();
|
||||
this.remove(last);
|
||||
}
|
||||
|
@ -290,21 +343,15 @@ EPUBJS.Renderer.prototype.backwards = function(view){
|
|||
|
||||
// -- this might want to be in infinite
|
||||
EPUBJS.Renderer.prototype.fill = function() {
|
||||
var height = this.container.getBoundingClientRect().height;
|
||||
var next = function(){
|
||||
var bottom = this.last().bounds().bottom;
|
||||
var defer = new RSVP.defer();
|
||||
|
||||
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 defer.promise;
|
||||
}
|
||||
}.bind(this);
|
||||
|
||||
var prev = this.first().section.index - 1;
|
||||
var filling = next();
|
||||
var filling = this.forwards();
|
||||
|
||||
if(this.settings.axis === "vertical") {
|
||||
filling.then(this.fillVertical.bind(this));
|
||||
} else {
|
||||
filling.then(this.fillHorizontal.bind(this));
|
||||
}
|
||||
|
||||
if(prev > 0){
|
||||
filling.then(this.backwards.bind(this));
|
||||
|
@ -319,6 +366,34 @@ EPUBJS.Renderer.prototype.fill = function() {
|
|||
|
||||
};
|
||||
|
||||
EPUBJS.Renderer.prototype.fillVertical = function() {
|
||||
var height = this.container.getBoundingClientRect().height;
|
||||
var bottom = this.last().bounds().bottom;
|
||||
var defer = new RSVP.defer();
|
||||
|
||||
if (height && bottom && (bottom < height)) { //&& (this.last().section.index + 1 < this.book.spine.length)) {
|
||||
return this.forwards().then(this.fillVertical.bind(this));
|
||||
} else {
|
||||
this.rendering = false;
|
||||
defer.resolve();
|
||||
return defer.promise;
|
||||
}
|
||||
};
|
||||
|
||||
EPUBJS.Renderer.prototype.fillHorizontal = function() {
|
||||
var width = this.container.getBoundingClientRect().width;
|
||||
var right = this.last().bounds().right;
|
||||
var defer = new RSVP.defer();
|
||||
|
||||
if (width && right && (right <= width)) { //&& (this.last().section.index + 1 < this.book.spine.length)) {
|
||||
return this.forwards().then(this.fillHorizontal.bind(this));
|
||||
} else {
|
||||
this.rendering = false;
|
||||
defer.resolve();
|
||||
return defer.promise;
|
||||
}
|
||||
};
|
||||
|
||||
EPUBJS.Renderer.prototype.append = function(view){
|
||||
var first, prevTop, prevHeight, offset;
|
||||
|
||||
|
@ -398,5 +473,13 @@ EPUBJS.Renderer.prototype.replacements = function(view, renderer) {
|
|||
return task.promise;
|
||||
};
|
||||
|
||||
EPUBJS.Renderer.prototype.paginate = function(options) {
|
||||
this.pagination = new EPUBJS.Paginate(this, {
|
||||
width: this.settings.width,
|
||||
height: this.settings.height
|
||||
});
|
||||
return this.pagination;
|
||||
};
|
||||
|
||||
//-- Enable binding events to Renderer
|
||||
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
|
Loading…
Add table
Add a link
Reference in a new issue