mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-05 15:32:55 +02:00
New queue flush, handle cfi for display, re-display on resize
This commit is contained in:
parent
42efc34517
commit
ac926479cf
12 changed files with 941 additions and 399 deletions
|
@ -30,9 +30,7 @@ EPUBJS.Rendition = function(book, options) {
|
|||
|
||||
this.q.enqueue(this.book.opened);
|
||||
|
||||
this.book.opened.then(function(){
|
||||
this.globalLayoutProperties = this.parseLayoutProperties(this.book.package.metadata);
|
||||
}.bind(this));
|
||||
this.q.enqueue(this.parseLayoutProperties);
|
||||
|
||||
};
|
||||
|
||||
|
@ -138,7 +136,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
|
|||
this.trigger("attached");
|
||||
|
||||
// Start processing queue
|
||||
this.q.run();
|
||||
// this.q.run();
|
||||
|
||||
};
|
||||
|
||||
|
@ -156,46 +154,63 @@ EPUBJS.Rendition.prototype.bounds = function() {
|
|||
return this.container.getBoundingClientRect();
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.display = function(what){
|
||||
EPUBJS.Rendition.prototype.display = function(target){
|
||||
|
||||
return this.q.enqueue(function(what){
|
||||
|
||||
var displaying = new RSVP.defer();
|
||||
var displayed = displaying.promise;
|
||||
|
||||
var section = this.book.spine.get(what);
|
||||
var view;
|
||||
|
||||
this.displaying = true;
|
||||
|
||||
if(section){
|
||||
view = this.createView(section);
|
||||
|
||||
// Show view
|
||||
this.q.enqueue(this.append, view);
|
||||
|
||||
// Move to correct place within the section, if needed
|
||||
// this.moveTo(what)
|
||||
|
||||
this.hooks.display.trigger(view);
|
||||
|
||||
displaying.resolve(this);
|
||||
|
||||
} else {
|
||||
displaying.reject(new Error("No Section Found"));
|
||||
}
|
||||
|
||||
return displayed;
|
||||
}, what);
|
||||
return this.q.enqueue(this._display, target);
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype._display = function(target){
|
||||
|
||||
var displaying = new RSVP.defer();
|
||||
var displayed = displaying.promise;
|
||||
|
||||
var section;
|
||||
var view;
|
||||
var cfi, spinePos;
|
||||
|
||||
if(target.indexOf("epubcfi(") === 0) {
|
||||
cfi = new EPUBJS.EpubCFI(target);
|
||||
spinePos = cfi.spinePos;
|
||||
section = this.book.spine.get(spinePos);
|
||||
} else {
|
||||
section = this.book.spine.get(target);
|
||||
}
|
||||
|
||||
this.displaying = true;
|
||||
|
||||
// Hide current views
|
||||
this.hide();
|
||||
|
||||
if(section){
|
||||
view = this.createView(section);
|
||||
|
||||
// Show view
|
||||
this.q.enqueue(this.append, view);
|
||||
|
||||
// Move to correct place within the section, if needed
|
||||
// this.moveTo(what)
|
||||
|
||||
// Show views
|
||||
this.show();
|
||||
|
||||
this.hooks.display.trigger(view);
|
||||
|
||||
displaying.resolve(this);
|
||||
|
||||
} else {
|
||||
displaying.reject(new Error("No Section Found"));
|
||||
}
|
||||
|
||||
return displayed;
|
||||
|
||||
};
|
||||
// Takes a cfi, fragment or page?
|
||||
EPUBJS.Rendition.prototype.moveTo = function(what){
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.render = function(view) {
|
||||
EPUBJS.Rendition.prototype.render = function(view, show) {
|
||||
|
||||
view.create();
|
||||
|
||||
|
@ -205,7 +220,7 @@ EPUBJS.Rendition.prototype.render = function(view) {
|
|||
this.resizeView(view);
|
||||
|
||||
// Render Chain
|
||||
return view.display(this.book.request)
|
||||
return view.render(this.book.request)
|
||||
.then(function(){
|
||||
return this.hooks.replacements.trigger(view, this);
|
||||
}.bind(this))
|
||||
|
@ -213,9 +228,16 @@ EPUBJS.Rendition.prototype.render = function(view) {
|
|||
return this.hooks.layout.trigger(view);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
return view.show()
|
||||
return view.display()
|
||||
}.bind(this))
|
||||
.then(function(view){
|
||||
|
||||
if(show != false && this.hidden === false) {
|
||||
this.q.enqueue(function(view){
|
||||
view.show();
|
||||
}, view);
|
||||
}
|
||||
|
||||
|
||||
// this.map = new EPUBJS.Map(view, this.layout);
|
||||
this.trigger("rendered", view.section);
|
||||
|
@ -240,35 +262,43 @@ EPUBJS.Rendition.prototype.append = function(view){
|
|||
// view.appendTo(this.container);
|
||||
this.container.appendChild(view.element);
|
||||
|
||||
// view.on("shown", this.afterDisplayed.bind(this));
|
||||
view.onShown = this.afterDisplayed.bind(this);
|
||||
// view.on("displayed", this.afterDisplayed.bind(this));
|
||||
view.onDisplayed = this.afterDisplayed.bind(this);
|
||||
// this.resizeView(view);
|
||||
|
||||
return this.render(view);
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.clear = function(){
|
||||
// Remove all views
|
||||
this.views.forEach(function(view){
|
||||
this.remove(view);
|
||||
this._remove(view);
|
||||
}.bind(this));
|
||||
this.views = [];
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.remove = function(view) {
|
||||
var index = this.views.indexOf(view);
|
||||
|
||||
if(index > -1) {
|
||||
this.views.splice(index, 1);
|
||||
}
|
||||
|
||||
this.container.removeChild(view.element);
|
||||
|
||||
if(view.shown){
|
||||
view.destroy();
|
||||
}
|
||||
|
||||
view = null;
|
||||
|
||||
this._remove(view);
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype._remove = function(view) {
|
||||
view.off("resized");
|
||||
|
||||
if(view.displayed){
|
||||
view.destroy();
|
||||
}
|
||||
|
||||
this.container.removeChild(view.element);
|
||||
view = null;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.resizeView = function(view) {
|
||||
|
||||
|
@ -343,6 +373,8 @@ EPUBJS.Rendition.prototype.layoutMethod = function() {
|
|||
|
||||
this.layout = new EPUBJS.Layout.Scroll();
|
||||
this.layoutUpdate();
|
||||
|
||||
this.map = new EPUBJS.Map(this.layout);
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.layoutUpdate = function() {
|
||||
|
@ -351,9 +383,8 @@ EPUBJS.Rendition.prototype.layoutUpdate = function() {
|
|||
|
||||
};
|
||||
|
||||
|
||||
EPUBJS.Rendition.prototype.resize = function(width, height){
|
||||
|
||||
|
||||
this.stageSize(width, height);
|
||||
|
||||
this.layoutUpdate();
|
||||
|
@ -415,15 +446,17 @@ EPUBJS.Rendition.prototype.prev = function(){
|
|||
};
|
||||
|
||||
//-- http://www.idpf.org/epub/fxl/
|
||||
EPUBJS.Rendition.prototype.parseLayoutProperties = function(metadata){
|
||||
EPUBJS.Rendition.prototype.parseLayoutProperties = function(_metadata){
|
||||
var metadata = _metadata || this.book.package.metadata;
|
||||
var layout = (this.layoutOveride && this.layoutOveride.layout) || metadata.layout || "reflowable";
|
||||
var spread = (this.layoutOveride && this.layoutOveride.spread) || metadata.spread || "auto";
|
||||
var orientation = (this.layoutOveride && this.layoutOveride.orientation) || metadata.orientation || "auto";
|
||||
return {
|
||||
this.globalLayoutProperties = {
|
||||
layout : layout,
|
||||
spread : spread,
|
||||
orientation : orientation
|
||||
};
|
||||
return this.globalLayoutProperties;
|
||||
};
|
||||
|
||||
|
||||
|
@ -436,19 +469,19 @@ EPUBJS.Rendition.prototype.current = function(){
|
|||
return null;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.isVisible = function(view, offset, _container){
|
||||
EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _container){
|
||||
var position = view.position();
|
||||
var container = _container || this.container.getBoundingClientRect();
|
||||
|
||||
if(this.settings.axis === "horizontal" &&
|
||||
(position.right > container.left - offset) &&
|
||||
!(position.left >= container.right + offset)) {
|
||||
(position.right > container.left - offsetPrev) &&
|
||||
!(position.left >= container.right + offsetNext)) {
|
||||
|
||||
return true;
|
||||
|
||||
} else if(this.settings.axis === "vertical" &&
|
||||
(position.bottom > container.top - offset) &&
|
||||
!(position.top >= container.bottom + offset)) {
|
||||
(position.bottom > container.top - offsetPrev) &&
|
||||
!(position.top >= container.bottom + offsetNext)) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -458,14 +491,14 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offset, _container){
|
|||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.visible = function(){
|
||||
var container = this.container.getBoundingClientRect();
|
||||
var container = this.bounds();
|
||||
var visible = [];
|
||||
var isVisible;
|
||||
var view;
|
||||
|
||||
for (var i = 0; i < this.views.length; i++) {
|
||||
view = this.views[i];
|
||||
isVisible = this.isVisible(view, 0, container);
|
||||
isVisible = this.isVisible(view, 0, 0, container);
|
||||
|
||||
if(isVisible === true) {
|
||||
visible.push(view);
|
||||
|
@ -477,5 +510,87 @@ EPUBJS.Rendition.prototype.visible = function(){
|
|||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.bounds = function(func) {
|
||||
var bounds;
|
||||
|
||||
if(!this.settings.height) {
|
||||
bounds = EPUBJS.core.windowBounds();
|
||||
} else {
|
||||
bounds = this.container.getBoundingClientRect();
|
||||
}
|
||||
|
||||
return bounds;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.displayed = function(){
|
||||
var displayed = [];
|
||||
var view;
|
||||
for (var i = 0; i < this.views.length; i++) {
|
||||
view = this.views[i];
|
||||
if(view.displayed){
|
||||
displayed.push(view);
|
||||
}
|
||||
};
|
||||
return displayed;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.show = function(){
|
||||
var view;
|
||||
for (var i = 0; i < this.views.length; i++) {
|
||||
view = this.views[i];
|
||||
if(view.displayed){
|
||||
view.show();
|
||||
}
|
||||
};
|
||||
this.hidden = false;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.hide = function(){
|
||||
var view;
|
||||
for (var i = 0; i < this.views.length; i++) {
|
||||
view = this.views[i];
|
||||
if(view.displayed){
|
||||
view.hide();
|
||||
}
|
||||
};
|
||||
this.hidden = true;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.destroy = function(){
|
||||
// Clear the queue
|
||||
this.q.clear();
|
||||
|
||||
this.clear();
|
||||
|
||||
clearTimeout(this.trimTimeout);
|
||||
if(this.settings.hidden) {
|
||||
this.element.removeChild(this.wrapper);
|
||||
} else {
|
||||
this.element.removeChild(this.container);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.reportLocation = function(){
|
||||
return this.q.enqueue(function(){
|
||||
this.location = this.currentLocation();
|
||||
this.trigger("locationChanged", this.location);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.currentLocation = function(){
|
||||
var view;
|
||||
var start, end;
|
||||
|
||||
if(this.views.length) {
|
||||
view = this.views[0];
|
||||
// start = container.left - view.position().left;
|
||||
// end = start + this.layout.spread;
|
||||
|
||||
return this.map.page(view);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//-- Enable binding events to Renderer
|
||||
RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue