1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

New queue flush, handle cfi for display, re-display on resize

This commit is contained in:
Fred Chasen 2015-06-05 00:01:50 -04:00
parent 42efc34517
commit ac926479cf
12 changed files with 941 additions and 399 deletions

539
dist/epub.js vendored
View file

@ -2806,6 +2806,8 @@ EPUBJS.Queue.prototype.enqueue = function() {
this._q.push(queued); this._q.push(queued);
// Wait to start queue flush
setTimeout(this.flush.bind(this), 0);
return queued.promise; return queued.promise;
}; };
@ -2849,13 +2851,14 @@ EPUBJS.Queue.prototype.dequeue = function(){
}; };
// Run All Immediately // Run All Immediately
EPUBJS.Queue.prototype.flush = function(){ EPUBJS.Queue.prototype.dump = function(){
while(this._q.length) { while(this._q.length) {
this.dequeue(); this.dequeue();
} }
}; };
// Run all sequentially, at convince // Run all sequentially, at convince
EPUBJS.Queue.prototype.run = function(){ EPUBJS.Queue.prototype.run = function(){
if(!this.running && this._q.length) { if(!this.running && this._q.length) {
this.running = true; this.running = true;
@ -2867,9 +2870,28 @@ EPUBJS.Queue.prototype.run = function(){
this.tick.call(window, this.run.bind(this)); this.tick.call(window, this.run.bind(this));
}; };
// Flush all, as quickly as possible
EPUBJS.Queue.prototype.flush = function(){
if(this.running){
return this.running;
}
if(this._q.length) {
this.running = this.dequeue().
then(function(){
this.running = undefined;
return this.flush();
}.bind(this));
return this.running;
}
};
// Clear all items in wait // Clear all items in wait
EPUBJS.Queue.prototype.clear = function(){ EPUBJS.Queue.prototype.clear = function(){
this._q = []; this._q = [];
this.running = false;
}; };
EPUBJS.Queue.prototype.length = function(){ EPUBJS.Queue.prototype.length = function(){
@ -4089,7 +4111,10 @@ EPUBJS.Spine.prototype.load = function(_package) {
EPUBJS.Spine.prototype.get = function(target) { EPUBJS.Spine.prototype.get = function(target) {
var index = 0; var index = 0;
if(target && (typeof target === "number" || isNaN(target) === false)){ if(typeof target === "string" && target.indexOf("epubcfi(") === 0) {
cfi = new EPUBJS.EpubCFI(target);
index = cfi.spinePos;
} if(target && (typeof target === "number" || isNaN(target) === false)){
index = target; index = target;
} else if(target && target.indexOf("#") === 0) { } else if(target && target.indexOf("#") === 0) {
index = this.spineById[target.substring(1)]; index = this.spineById[target.substring(1)];
@ -4415,12 +4440,14 @@ EPUBJS.View = function(section, options) {
this.element.style.width = "0px"; this.element.style.width = "0px";
this.element.style.overflow = "hidden"; this.element.style.overflow = "hidden";
this.shown = false; this.displayed = false;
this.rendered = false; this.rendered = false;
//this.width = 0; //this.width = 0;
//this.height = 0; //this.height = 0;
// Blank Cfi for Parsing
this.epubcfi = new EPUBJS.EpubCFI();
if(this.settings.axis && this.settings.axis == "horizontal"){ if(this.settings.axis && this.settings.axis == "horizontal"){
this.element.style.display = "inline-block"; this.element.style.display = "inline-block";
@ -4488,7 +4515,6 @@ EPUBJS.View.prototype.lock = function(width, height) {
iframeBorders = {width: 0, height: 0}; iframeBorders = {width: 0, height: 0};
} }
if(EPUBJS.core.isNumber(width)){ if(EPUBJS.core.isNumber(width)){
this.lockedWidth = width - elBorders.width - iframeBorders.width; this.lockedWidth = width - elBorders.width - iframeBorders.width;
this.resize(this.lockedWidth, width); // width keeps ratio correct this.resize(this.lockedWidth, width); // width keeps ratio correct
@ -4508,7 +4534,7 @@ EPUBJS.View.prototype.lock = function(width, height) {
this.resize(this.lockedWidth, this.lockedHeight); this.resize(this.lockedWidth, this.lockedHeight);
} }
if(this.shown && this.iframe) { if(this.displayed && this.iframe) {
this.layout(); this.layout();
this.expand(); this.expand();
@ -4540,7 +4566,7 @@ EPUBJS.View.prototype.resize = function(width, height) {
EPUBJS.View.prototype.reframe = function(width, height) { EPUBJS.View.prototype.reframe = function(width, height) {
//var prevBounds; //var prevBounds;
if(!this.shown) return; if(!this.displayed) return;
if(EPUBJS.core.isNumber(width)){ if(EPUBJS.core.isNumber(width)){
this.element.style.width = width + "px"; this.element.style.width = width + "px";
@ -4575,21 +4601,20 @@ EPUBJS.View.prototype.resized = function(e) {
}; };
EPUBJS.View.prototype.display = function(_request) { EPUBJS.View.prototype.render = function(_request) {
// if(this.rendering){ // if(this.rendering){
// return this.displayed; // return this.displayed;
// } // }
this.rendering = true; this.rendering = true;
this.displaying = new RSVP.defer(); // this.displayingDefer = new RSVP.defer();
this.displayed = this.displaying.promise; // this.displayedPromise = this.displaying.promise;
return this.section.render(_request) return this.section.render(_request)
.then(function(contents){ .then(function(contents){
return this.load(contents); return this.load(contents);
}.bind(this)) }.bind(this));
.then(this.displaying.resolve.call(this));
}; };
EPUBJS.View.prototype.load = function(contents) { EPUBJS.View.prototype.load = function(contents) {
@ -4764,10 +4789,10 @@ EPUBJS.View.prototype.imageLoadListeners = function(target) {
} }
}; };
EPUBJS.View.prototype.show = function() { EPUBJS.View.prototype.display = function() {
var shown = new RSVP.defer(); var displayed = new RSVP.defer();
this.shown = true; this.displayed = true;
this.layout(); this.layout();
@ -4775,15 +4800,23 @@ EPUBJS.View.prototype.show = function() {
this.expand(); this.expand();
this.trigger("displayed", this);
this.onDisplayed(this);
displayed.resolve(this);
return displayed.promise;
};
EPUBJS.View.prototype.show = function() {
this.element.style.visibility = "visible"; this.element.style.visibility = "visible";
if(this.iframe){
this.iframe.style.visibility = "visible"; this.iframe.style.visibility = "visible";
}
this.trigger("shown", this); this.trigger("shown", this);
this.onShown(this);
shown.resolve(this);
return shown.promise;
}; };
EPUBJS.View.prototype.hide = function() { EPUBJS.View.prototype.hide = function() {
@ -4792,14 +4825,14 @@ EPUBJS.View.prototype.hide = function() {
this.iframe.style.visibility = "hidden"; this.iframe.style.visibility = "hidden";
this.stopExpanding = true; this.stopExpanding = true;
this.trigger("hidden"); this.trigger("hidden", this);
}; };
EPUBJS.View.prototype.position = function() { EPUBJS.View.prototype.position = function() {
return this.element.getBoundingClientRect(); return this.element.getBoundingClientRect();
}; };
EPUBJS.View.prototype.onShown = function(view) { EPUBJS.View.prototype.onDisplayed = function(view) {
// Stub, override with a custom functions // Stub, override with a custom functions
}; };
@ -4816,10 +4849,10 @@ EPUBJS.View.prototype.destroy = function() {
this.observer.disconnect(); this.observer.disconnect();
} }
if(this.iframe){ if(this.displayed){
this.stopExpanding = true; this.stopExpanding = true;
this.element.removeChild(this.iframe); this.element.removeChild(this.iframe);
this.shown = false; this.displayed = false;
this.iframe = null; this.iframe = null;
} }
@ -4832,6 +4865,41 @@ EPUBJS.View.prototype.root = function() {
return this.document.documentElement; return this.document.documentElement;
}; };
EPUBJS.View.prototype.locationOf = function(target) {
if(!this.document) return;
if(target.indexOf("epubcfi(") === 0) {
cfi = this.epubcfi.parse(target);
if(typeof document.evaluate === 'undefined') {
marker = this.epubcfi.addMarker(cfi, this.document);
if(marker) {
// Must Clean up Marker before going to page
this.epubcfi.removeMarker(marker, this.document);
return marker.getBoundingClientRect();
}
} else {
range = this.epubcfi.generateRangeFromCfi(cfi, this.document);
if(range) {
return range.getBoundingClientRect();
}
}
} else if(target.indexOf("#") > -1) {
id = target.substring(target.indexOf("#"));
el = this.document.getElementById(id);
if(el) {
return el.getBoundingClientRect();
}
}
return {"left": 0, "top": 0};
};
RSVP.EventTarget.mixin(EPUBJS.View.prototype); RSVP.EventTarget.mixin(EPUBJS.View.prototype);
EPUBJS.Layout = EPUBJS.Layout || {}; EPUBJS.Layout = EPUBJS.Layout || {};
@ -4852,6 +4920,7 @@ EPUBJS.Layout.Reflowable.prototype.calculate = function(_width, _height, _gap, _
var colWidth; var colWidth;
var spreadWidth; var spreadWidth;
var delta;
//-- Double Page //-- Double Page
if(divisor > 1) { if(divisor > 1) {
@ -5019,9 +5088,7 @@ EPUBJS.Rendition = function(book, options) {
this.q.enqueue(this.book.opened); this.q.enqueue(this.book.opened);
this.book.opened.then(function(){ this.q.enqueue(this.parseLayoutProperties);
this.globalLayoutProperties = this.parseLayoutProperties(this.book.package.metadata);
}.bind(this));
}; };
@ -5127,7 +5194,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
this.trigger("attached"); this.trigger("attached");
// Start processing queue // Start processing queue
this.q.run(); // this.q.run();
}; };
@ -5145,18 +5212,34 @@ EPUBJS.Rendition.prototype.bounds = function() {
return this.container.getBoundingClientRect(); return this.container.getBoundingClientRect();
}; };
EPUBJS.Rendition.prototype.display = function(what){ EPUBJS.Rendition.prototype.display = function(target){
return this.q.enqueue(function(what){ return this.q.enqueue(this._display, target);
};
EPUBJS.Rendition.prototype._display = function(target){
var displaying = new RSVP.defer(); var displaying = new RSVP.defer();
var displayed = displaying.promise; var displayed = displaying.promise;
var section = this.book.spine.get(what); var section;
var view; 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; this.displaying = true;
// Hide current views
this.hide();
if(section){ if(section){
view = this.createView(section); view = this.createView(section);
@ -5166,6 +5249,9 @@ EPUBJS.Rendition.prototype.display = function(what){
// Move to correct place within the section, if needed // Move to correct place within the section, if needed
// this.moveTo(what) // this.moveTo(what)
// Show views
this.show();
this.hooks.display.trigger(view); this.hooks.display.trigger(view);
displaying.resolve(this); displaying.resolve(this);
@ -5175,16 +5261,14 @@ EPUBJS.Rendition.prototype.display = function(what){
} }
return displayed; return displayed;
}, what);
}; };
// Takes a cfi, fragment or page? // Takes a cfi, fragment or page?
EPUBJS.Rendition.prototype.moveTo = function(what){ EPUBJS.Rendition.prototype.moveTo = function(what){
}; };
EPUBJS.Rendition.prototype.render = function(view) { EPUBJS.Rendition.prototype.render = function(view, show) {
view.create(); view.create();
@ -5194,7 +5278,7 @@ EPUBJS.Rendition.prototype.render = function(view) {
this.resizeView(view); this.resizeView(view);
// Render Chain // Render Chain
return view.display(this.book.request) return view.render(this.book.request)
.then(function(){ .then(function(){
return this.hooks.replacements.trigger(view, this); return this.hooks.replacements.trigger(view, this);
}.bind(this)) }.bind(this))
@ -5202,10 +5286,17 @@ EPUBJS.Rendition.prototype.render = function(view) {
return this.hooks.layout.trigger(view); return this.hooks.layout.trigger(view);
}.bind(this)) }.bind(this))
.then(function(){ .then(function(){
return view.show() return view.display()
}.bind(this)) }.bind(this))
.then(function(view){ .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.map = new EPUBJS.Map(view, this.layout);
this.trigger("rendered", view.section); this.trigger("rendered", view.section);
@ -5229,35 +5320,43 @@ EPUBJS.Rendition.prototype.append = function(view){
// view.appendTo(this.container); // view.appendTo(this.container);
this.container.appendChild(view.element); this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("displayed", this.afterDisplayed.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
// this.resizeView(view); // this.resizeView(view);
return this.render(view); return this.render(view);
}; };
EPUBJS.Rendition.prototype.clear = function(){ EPUBJS.Rendition.prototype.clear = function(){
// Remove all views
this.views.forEach(function(view){ this.views.forEach(function(view){
this.remove(view); this._remove(view);
}.bind(this)); }.bind(this));
this.views = [];
}; };
EPUBJS.Rendition.prototype.remove = function(view) { EPUBJS.Rendition.prototype.remove = function(view) {
var index = this.views.indexOf(view); var index = this.views.indexOf(view);
if(index > -1) { if(index > -1) {
this.views.splice(index, 1); this.views.splice(index, 1);
} }
this.container.removeChild(view.element);
if(view.shown){ this._remove(view);
view.destroy();
}
view = null;
}; };
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) { EPUBJS.Rendition.prototype.resizeView = function(view) {
@ -5332,6 +5431,8 @@ EPUBJS.Rendition.prototype.layoutMethod = function() {
this.layout = new EPUBJS.Layout.Scroll(); this.layout = new EPUBJS.Layout.Scroll();
this.layoutUpdate(); this.layoutUpdate();
this.map = new EPUBJS.Map(this.layout);
}; };
EPUBJS.Rendition.prototype.layoutUpdate = function() { EPUBJS.Rendition.prototype.layoutUpdate = function() {
@ -5340,7 +5441,6 @@ EPUBJS.Rendition.prototype.layoutUpdate = function() {
}; };
EPUBJS.Rendition.prototype.resize = function(width, height){ EPUBJS.Rendition.prototype.resize = function(width, height){
this.stageSize(width, height); this.stageSize(width, height);
@ -5404,15 +5504,17 @@ EPUBJS.Rendition.prototype.prev = function(){
}; };
//-- http://www.idpf.org/epub/fxl/ //-- 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 layout = (this.layoutOveride && this.layoutOveride.layout) || metadata.layout || "reflowable";
var spread = (this.layoutOveride && this.layoutOveride.spread) || metadata.spread || "auto"; var spread = (this.layoutOveride && this.layoutOveride.spread) || metadata.spread || "auto";
var orientation = (this.layoutOveride && this.layoutOveride.orientation) || metadata.orientation || "auto"; var orientation = (this.layoutOveride && this.layoutOveride.orientation) || metadata.orientation || "auto";
return { this.globalLayoutProperties = {
layout : layout, layout : layout,
spread : spread, spread : spread,
orientation : orientation orientation : orientation
}; };
return this.globalLayoutProperties;
}; };
@ -5425,19 +5527,19 @@ EPUBJS.Rendition.prototype.current = function(){
return null; return null;
}; };
EPUBJS.Rendition.prototype.isVisible = function(view, offset, _container){ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _container){
var position = view.position(); var position = view.position();
var container = _container || this.container.getBoundingClientRect(); var container = _container || this.container.getBoundingClientRect();
if(this.settings.axis === "horizontal" && if(this.settings.axis === "horizontal" &&
(position.right > container.left - offset) && (position.right > container.left - offsetPrev) &&
!(position.left >= container.right + offset)) { !(position.left >= container.right + offsetNext)) {
return true; return true;
} else if(this.settings.axis === "vertical" && } else if(this.settings.axis === "vertical" &&
(position.bottom > container.top - offset) && (position.bottom > container.top - offsetPrev) &&
!(position.top >= container.bottom + offset)) { !(position.top >= container.bottom + offsetNext)) {
return true; return true;
} }
@ -5447,14 +5549,14 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offset, _container){
}; };
EPUBJS.Rendition.prototype.visible = function(){ EPUBJS.Rendition.prototype.visible = function(){
var container = this.container.getBoundingClientRect(); var container = this.bounds();
var visible = []; var visible = [];
var isVisible; var isVisible;
var view; var view;
for (var i = 0; i < this.views.length; i++) { for (var i = 0; i < this.views.length; i++) {
view = this.views[i]; view = this.views[i];
isVisible = this.isVisible(view, 0, container); isVisible = this.isVisible(view, 0, 0, container);
if(isVisible === true) { if(isVisible === true) {
visible.push(view); visible.push(view);
@ -5466,6 +5568,88 @@ 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 //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype); RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype);
@ -5478,7 +5662,7 @@ EPUBJS.Continuous = function(book, options) {
overflow: "auto", overflow: "auto",
axis: "vertical", axis: "vertical",
offset: 500, offset: 500,
offsetDelta: 100 offsetDelta: 250
}); });
EPUBJS.core.extend(this.settings, options); EPUBJS.core.extend(this.settings, options);
@ -5510,33 +5694,49 @@ EPUBJS.Continuous.prototype.attachListeners = function(){
}; };
EPUBJS.Continuous.prototype.display = function(what){ EPUBJS.Continuous.prototype._display = function(target){
return this.q.enqueue(function(what){
var displaying = new RSVP.defer(); var displaying = new RSVP.defer();
var displayed = displaying.promise; var displayed = displaying.promise;
var section = this.book.spine.get(what); var section;
var view; 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; this.displaying = true;
this.hide();
if(section){ if(section){
view = new EPUBJS.View(section, this.viewSettings); view = new EPUBJS.View(section, this.viewSettings);
// This will clear all previous views // This will clear all previous views
this.q.enqueue(this.fill, view); this.q.enqueue(this.fill, view).then(function(){
//this.q.enqueue(function(){
// Move to correct place within the section, if needed // Move to correct place within the section, if needed
// return this.moveTo(what) this.q.enqueue(function(){
//});
var offset = view.locationOf(target);
return this.moveTo(offset);
});
this.q.enqueue(this.check); this.q.enqueue(this.check);
this.q.enqueue(this.show);
this.hooks.display.trigger(view); this.hooks.display.trigger(view);
}.bind(this));
// view.displayed.then(function(){ // view.displayed.then(function(){
// this.trigger("displayed", section); // this.trigger("displayed", section);
// this.displaying = false; // this.displaying = false;
@ -5549,14 +5749,22 @@ EPUBJS.Continuous.prototype.display = function(what){
} }
return displayed; return displayed;
}, what);
}; };
// Takes a cfi, fragment or page? EPUBJS.Continuous.prototype.moveTo = function(offset){
EPUBJS.Continuous.prototype.moveTo = function(what){ var bounds = this.bounds();
var dist = Math.floor(offset.top / bounds.height) * bounds.height;
return this.check(0, dist+this.settings.offset).then(function(){
if(this.settings.axis === "vertical") {
this.scrollBy(0, dist);
} else {
this.scrollBy(dist, 0);
}
}.bind(this));
}; };
EPUBJS.Continuous.prototype.afterDisplayed = function(currView){ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
@ -5577,7 +5785,7 @@ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
} }
// this.removeShownListeners(currView); // this.removeShownListeners(currView);
currView.onShown = this.afterDisplayed.bind(this); // currView.onShown = this.afterDisplayed.bind(this);
this.trigger("displayed", currView.section); this.trigger("displayed", currView.section);
@ -5589,7 +5797,7 @@ EPUBJS.Continuous.prototype.removeShownListeners = function(view){
// view.off("shown", this.afterDisplayed); // view.off("shown", this.afterDisplayed);
// view.off("shown", this.afterDisplayedAbove); // view.off("shown", this.afterDisplayedAbove);
view.onShown = function(){}; view.onDisplayed = function(){};
}; };
@ -5598,7 +5806,7 @@ EPUBJS.Continuous.prototype.append = function(view){
this.container.appendChild(view.element); this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
//this.q.enqueue(this.check); //this.q.enqueue(this.check);
return this.check(); return this.check();
@ -5609,7 +5817,7 @@ EPUBJS.Continuous.prototype.prepend = function(view){
this.container.insertBefore(view.element, this.container.firstChild); this.container.insertBefore(view.element, this.container.firstChild);
// view.on("shown", this.afterDisplayedAbove.bind(this)); // view.on("shown", this.afterDisplayedAbove.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
view.on("resized", this.counter.bind(this)); view.on("resized", this.counter.bind(this));
@ -5638,9 +5846,12 @@ EPUBJS.Continuous.prototype.fill = function(view){
this.container.appendChild(view.element); this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
return this.render(view); return this.render(view)
.then(function(){
return this.check();
}.bind(this));
}; };
EPUBJS.Continuous.prototype.insert = function(view, index) { EPUBJS.Continuous.prototype.insert = function(view, index) {
@ -5656,32 +5867,24 @@ EPUBJS.Continuous.prototype.insert = function(view, index) {
return this.check(); return this.check();
}; };
// Remove the render element and clean up listeners // // Remove the render element and clean up listeners
EPUBJS.Continuous.prototype.remove = function(view) { // EPUBJS.Continuous.prototype.remove = function(view) {
var index = this.views.indexOf(view); // var index = this.views.indexOf(view);
if(index > -1) { // if(index > -1) {
this.views.splice(index, 1); // this.views.splice(index, 1);
} // }
this.container.removeChild(view.element); // this.container.removeChild(view.element);
view.off("resized"); // view.off("resized");
if(view.shown){ // if(view.displayed){
view.destroy(); // view.destroy();
} // }
view = null; // view = null;
}; // };
EPUBJS.Continuous.prototype.clear = function(){
this.views.forEach(function(view){
view.destroy();
});
this.views = [];
};
EPUBJS.Continuous.prototype.first = function() { EPUBJS.Continuous.prototype.first = function() {
return this.views[0]; return this.views[0];
@ -5695,44 +5898,40 @@ EPUBJS.Continuous.prototype.each = function(func) {
return this.views.forEach(func); return this.views.forEach(func);
}; };
EPUBJS.Continuous.prototype.check = function(_offset){
EPUBJS.Continuous.prototype.check = function(){
var checking = new RSVP.defer(); var checking = new RSVP.defer();
var container;//this.container.getBoundingClientRect(); var container = this.bounds();
var promises = []; var promises = [];
var offset = _offset || this.settings.offset;
if(!this.settings.height) {
container = EPUBJS.core.windowBounds();
} else {
container = this.container.getBoundingClientRect();
}
this.views.forEach(function(view){ this.views.forEach(function(view){
var visible = this.isVisible(view, this.settings.offset, container); var visible = this.isVisible(view, offset, offset, container);
if(visible) { if(visible) {
if(!view.shown && !view.rendering) { if(!view.displayed && !view.rendering) {
// console.log("render",view.section.index) // console.log("render",view.section.index)
promises.push(this.render(view)); promises.push(this.render(view));
} }
} else { } else {
if(view.shown) { if(view.displayed) {
// console.log("destroy", view.section.index) // console.log("destroy", view.section.index)
view.destroy(); this.q.enqueue(view.destroy.bind(view));
this.q.enqueue(this.trim); // view.destroy();
//this.q.enqueue(this.trim);
} }
} }
}.bind(this)); }.bind(this));
// clearTimeout(this.trimTimeout); clearTimeout(this.trimTimeout);
// this.trimTimeout = setTimeout(function(){ this.trimTimeout = setTimeout(function(){
// this.q.enqueue(this.trim); this.q.enqueue(this.trim);
// }.bind(this), 250); }.bind(this), 250);
if(promises.length){ if(promises.length){
@ -5750,22 +5949,44 @@ EPUBJS.Continuous.prototype.check = function(){
}; };
// EPUBJS.Continuous.prototype.trim = function(){
// var task = new RSVP.defer();
// var above = true;
// this.views.forEach(function(view, i){
// // var view = this.views[i];
// var prevShown = i > 0 ? this.views[i-1].displayed : false;
// var nextShown = (i+1 < this.views.length) ? this.views[i+1].displayed : false;
// if(!view.displayed && !prevShown && !nextShown) {
// // Remove
// this.erase(view, above);
// }
// if(nextShown) {
// above = false;
// }
// }.bind(this));
// task.resolve();
// return task.promise;
// };
EPUBJS.Continuous.prototype.trim = function(){ EPUBJS.Continuous.prototype.trim = function(){
var task = new RSVP.defer(); var task = new RSVP.defer();
var above = true; var displayed = this.displayed();
var first = displayed[0];
var last = displayed[displayed.length-1];
var firstIndex = this.views.indexOf(first);
var lastIndex = this.views.indexOf(last);
var above = this.views.slice(0, firstIndex);
var below = this.views.slice(lastIndex+1);
this.views.forEach(function(view, i){ for (var i = 0; i < above.length-1; i++) {
// var view = this.views[i]; this.erase(above[i], above);
var prevShown = i > 0 ? this.views[i-1].shown : false; };
var nextShown = (i+1 < this.views.length) ? this.views[i+1].shown : false;
if(!view.shown && !prevShown && !nextShown) { for (var i = 1; i < below.length; i++) {
// Remove this.erase(below[i]);
this.erase(view, above); };
}
if(nextShown) {
above = false;
}
}.bind(this));
task.resolve(); task.resolve();
return task.promise; return task.promise;
@ -5947,6 +6168,7 @@ EPUBJS.Continuous.prototype.scrollBy = function(x, y, silent){
} else { } else {
window.scrollBy(x,y); window.scrollBy(x,y);
} }
// console.log("scrollBy", x, y);
this.scrolled = true; this.scrolled = true;
}; };
@ -5961,7 +6183,7 @@ EPUBJS.Continuous.prototype.scrollTo = function(x, y, silent){
} else { } else {
window.scrollTo(x,y); window.scrollTo(x,y);
} }
// console.log("scrollTo", x, y);
this.scrolled = true; this.scrolled = true;
// if(this.container.scrollLeft != x){ // if(this.container.scrollLeft != x){
// setTimeout(function() { // setTimeout(function() {
@ -5981,11 +6203,26 @@ EPUBJS.Continuous.prototype.scrollTo = function(x, y, silent){
}; };
EPUBJS.Continuous.prototype.destroy = function() { EPUBJS.Continuous.prototype.currentLocation = function(){
var visible = this.visible();
var startPage, endPage;
this.clear(); var container = this.container.getBoundingClientRect()
this.element.removeChild(this.container); if(visible.length === 1) {
return this.map.page(visible[0]);
}
if(visible.length > 1) {
startPage = this.map.page(visible[0]);
endPage = this.map.page(visible[visible.length-1]);
return {
start: startPage.start,
end: endPage.end
}
}
}; };
@ -6177,6 +6414,13 @@ EPUBJS.Paginate.prototype.layoutUpdate = function() {
}; };
EPUBJS.Paginate.prototype.moveTo = function(offset){
var dist = Math.floor(offset.left / this.layout.delta) * this.layout.delta;
return this.check(0, dist+this.settings.offset).then(function(){
this.scrollBy(dist, 0);
}.bind(this));
};
EPUBJS.Paginate.prototype.page = function(pg){ EPUBJS.Paginate.prototype.page = function(pg){
// this.currentPage = pg; // this.currentPage = pg;
@ -6208,16 +6452,16 @@ EPUBJS.Paginate.prototype.prev = function(){
EPUBJS.Paginate.prototype.reportLocation = function(){ EPUBJS.Paginate.prototype.reportLocation = function(){
return this.q.enqueue(function(){ return this.q.enqueue(function(){
var location = this.currentLocation(); this.location = this.currentLocation();
this.trigger("locationChanged", location); this.trigger("locationChanged", this.location);
}.bind(this)); }.bind(this));
}; };
EPUBJS.Paginate.prototype.currentLocation = function(){ EPUBJS.Paginate.prototype.currentLocation = function(){
var visible = this.visible(); var visible = this.visible();
var startA, startB, endA, endB; var startA, startB, endA, endB;
var pageLeft, pageRight;
container = this.container.getBoundingClientRect() var container = this.container.getBoundingClientRect()
if(visible.length === 1) { if(visible.length === 1) {
startA = container.left - visible[0].position().left; startA = container.left - visible[0].position().left;
@ -6247,6 +6491,33 @@ EPUBJS.Paginate.prototype.currentLocation = function(){
}; };
EPUBJS.Paginate.prototype.resize = function(width, height){
// Clear the queue
this.q.clear();
this.stageSize(width, height);
this.layoutUpdate();
this.display(this.location.start);
this.trigger("resized", {
width: this.stage.width,
height: this.stage.height
});
};
EPUBJS.Paginate.prototype.onResized = function(e) {
this.clear();
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(function(){
this.resize();
}.bind(this), 150);
};
// EPUBJS.Paginate.prototype.display = function(what){ // EPUBJS.Paginate.prototype.display = function(what){
// return this.display(what); // return this.display(what);
// }; // };
@ -6440,7 +6711,7 @@ EPUBJS.Map.prototype.findTextEndRange = function(node, start, end){
pos = range.getBoundingClientRect(); pos = range.getBoundingClientRect();
if(pos.left > end) { if(pos.left > end && prev) {
return prev; return prev;
} else if(pos.right > end) { } else if(pos.right > end) {
return range; return range;
@ -6467,7 +6738,7 @@ EPUBJS.Map.prototype.splitTextNodeIntoRanges = function(node, _splitter){
pos = text.indexOf(splitter); pos = text.indexOf(splitter);
if(pos === -1) { if(pos === -1 || node.nodeType != Node.TEXT_NODE) {
range = doc.createRange(); range = doc.createRange();
range.selectNodeContents(node); range.selectNodeContents(node);
return [range]; return [range];

6
dist/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -87,7 +87,7 @@
// Load the opf // Load the opf
var book = ePub("../books/moby-dick/OPS/package.opf"); var book = ePub("../books/moby-dick/OPS/package.opf");
var rendition = book.renderTo(document.body, { method: "continuous", width: "60%" }); var rendition = book.renderTo(document.body, { method: "continuous", width: "60%" });
var displayed = rendition.display("chapter_008.xhtml"); var displayed = rendition.display("epubcfi(/6/14[xchapter_001]!4/2/24/2[c001p0011]/1:799)");
displayed.then(function(renderer){ displayed.then(function(renderer){

View file

@ -126,7 +126,7 @@
height: 600 height: 600
}); });
var displayed = rendition.display(6); var displayed = rendition.display("epubcfi(/6/14[xchapter_001]!4/2/30/2[c001p0016]/1:47)");
displayed.then(function(renderer){ displayed.then(function(renderer){

View file

@ -7,7 +7,7 @@ EPUBJS.Continuous = function(book, options) {
overflow: "auto", overflow: "auto",
axis: "vertical", axis: "vertical",
offset: 500, offset: 500,
offsetDelta: 100 offsetDelta: 250
}); });
EPUBJS.core.extend(this.settings, options); EPUBJS.core.extend(this.settings, options);
@ -39,33 +39,49 @@ EPUBJS.Continuous.prototype.attachListeners = function(){
}; };
EPUBJS.Continuous.prototype.display = function(what){ EPUBJS.Continuous.prototype._display = function(target){
return this.q.enqueue(function(what){
var displaying = new RSVP.defer(); var displaying = new RSVP.defer();
var displayed = displaying.promise; var displayed = displaying.promise;
var section = this.book.spine.get(what); var section;
var view; 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; this.displaying = true;
this.hide();
if(section){ if(section){
view = new EPUBJS.View(section, this.viewSettings); view = new EPUBJS.View(section, this.viewSettings);
// This will clear all previous views // This will clear all previous views
this.q.enqueue(this.fill, view); this.q.enqueue(this.fill, view).then(function(){
//this.q.enqueue(function(){
// Move to correct place within the section, if needed // Move to correct place within the section, if needed
// return this.moveTo(what) this.q.enqueue(function(){
//});
var offset = view.locationOf(target);
return this.moveTo(offset);
});
this.q.enqueue(this.check); this.q.enqueue(this.check);
this.q.enqueue(this.show);
this.hooks.display.trigger(view); this.hooks.display.trigger(view);
}.bind(this));
// view.displayed.then(function(){ // view.displayed.then(function(){
// this.trigger("displayed", section); // this.trigger("displayed", section);
// this.displaying = false; // this.displaying = false;
@ -78,14 +94,22 @@ EPUBJS.Continuous.prototype.display = function(what){
} }
return displayed; return displayed;
}, what);
}; };
// Takes a cfi, fragment or page? EPUBJS.Continuous.prototype.moveTo = function(offset){
EPUBJS.Continuous.prototype.moveTo = function(what){ var bounds = this.bounds();
var dist = Math.floor(offset.top / bounds.height) * bounds.height;
return this.check(0, dist+this.settings.offset).then(function(){
if(this.settings.axis === "vertical") {
this.scrollBy(0, dist);
} else {
this.scrollBy(dist, 0);
}
}.bind(this));
}; };
EPUBJS.Continuous.prototype.afterDisplayed = function(currView){ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
@ -106,7 +130,7 @@ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
} }
// this.removeShownListeners(currView); // this.removeShownListeners(currView);
currView.onShown = this.afterDisplayed.bind(this); // currView.onShown = this.afterDisplayed.bind(this);
this.trigger("displayed", currView.section); this.trigger("displayed", currView.section);
@ -118,7 +142,7 @@ EPUBJS.Continuous.prototype.removeShownListeners = function(view){
// view.off("shown", this.afterDisplayed); // view.off("shown", this.afterDisplayed);
// view.off("shown", this.afterDisplayedAbove); // view.off("shown", this.afterDisplayedAbove);
view.onShown = function(){}; view.onDisplayed = function(){};
}; };
@ -127,7 +151,7 @@ EPUBJS.Continuous.prototype.append = function(view){
this.container.appendChild(view.element); this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
//this.q.enqueue(this.check); //this.q.enqueue(this.check);
return this.check(); return this.check();
@ -138,7 +162,7 @@ EPUBJS.Continuous.prototype.prepend = function(view){
this.container.insertBefore(view.element, this.container.firstChild); this.container.insertBefore(view.element, this.container.firstChild);
// view.on("shown", this.afterDisplayedAbove.bind(this)); // view.on("shown", this.afterDisplayedAbove.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
view.on("resized", this.counter.bind(this)); view.on("resized", this.counter.bind(this));
@ -167,9 +191,12 @@ EPUBJS.Continuous.prototype.fill = function(view){
this.container.appendChild(view.element); this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
return this.render(view); return this.render(view)
.then(function(){
return this.check();
}.bind(this));
}; };
EPUBJS.Continuous.prototype.insert = function(view, index) { EPUBJS.Continuous.prototype.insert = function(view, index) {
@ -185,32 +212,24 @@ EPUBJS.Continuous.prototype.insert = function(view, index) {
return this.check(); return this.check();
}; };
// Remove the render element and clean up listeners // // Remove the render element and clean up listeners
EPUBJS.Continuous.prototype.remove = function(view) { // EPUBJS.Continuous.prototype.remove = function(view) {
var index = this.views.indexOf(view); // var index = this.views.indexOf(view);
if(index > -1) { // if(index > -1) {
this.views.splice(index, 1); // this.views.splice(index, 1);
} // }
this.container.removeChild(view.element); // this.container.removeChild(view.element);
view.off("resized"); // view.off("resized");
if(view.shown){ // if(view.displayed){
view.destroy(); // view.destroy();
} // }
view = null; // view = null;
}; // };
EPUBJS.Continuous.prototype.clear = function(){
this.views.forEach(function(view){
view.destroy();
});
this.views = [];
};
EPUBJS.Continuous.prototype.first = function() { EPUBJS.Continuous.prototype.first = function() {
return this.views[0]; return this.views[0];
@ -224,44 +243,40 @@ EPUBJS.Continuous.prototype.each = function(func) {
return this.views.forEach(func); return this.views.forEach(func);
}; };
EPUBJS.Continuous.prototype.check = function(_offset){
EPUBJS.Continuous.prototype.check = function(){
var checking = new RSVP.defer(); var checking = new RSVP.defer();
var container;//this.container.getBoundingClientRect(); var container = this.bounds();
var promises = []; var promises = [];
var offset = _offset || this.settings.offset;
if(!this.settings.height) {
container = EPUBJS.core.windowBounds();
} else {
container = this.container.getBoundingClientRect();
}
this.views.forEach(function(view){ this.views.forEach(function(view){
var visible = this.isVisible(view, this.settings.offset, container); var visible = this.isVisible(view, offset, offset, container);
if(visible) { if(visible) {
if(!view.shown && !view.rendering) { if(!view.displayed && !view.rendering) {
// console.log("render",view.section.index) // console.log("render",view.section.index)
promises.push(this.render(view)); promises.push(this.render(view));
} }
} else { } else {
if(view.shown) { if(view.displayed) {
// console.log("destroy", view.section.index) // console.log("destroy", view.section.index)
view.destroy(); this.q.enqueue(view.destroy.bind(view));
this.q.enqueue(this.trim); // view.destroy();
//this.q.enqueue(this.trim);
} }
} }
}.bind(this)); }.bind(this));
// clearTimeout(this.trimTimeout); clearTimeout(this.trimTimeout);
// this.trimTimeout = setTimeout(function(){ this.trimTimeout = setTimeout(function(){
// this.q.enqueue(this.trim); this.q.enqueue(this.trim);
// }.bind(this), 250); }.bind(this), 250);
if(promises.length){ if(promises.length){
@ -279,22 +294,44 @@ EPUBJS.Continuous.prototype.check = function(){
}; };
// EPUBJS.Continuous.prototype.trim = function(){
// var task = new RSVP.defer();
// var above = true;
// this.views.forEach(function(view, i){
// // var view = this.views[i];
// var prevShown = i > 0 ? this.views[i-1].displayed : false;
// var nextShown = (i+1 < this.views.length) ? this.views[i+1].displayed : false;
// if(!view.displayed && !prevShown && !nextShown) {
// // Remove
// this.erase(view, above);
// }
// if(nextShown) {
// above = false;
// }
// }.bind(this));
// task.resolve();
// return task.promise;
// };
EPUBJS.Continuous.prototype.trim = function(){ EPUBJS.Continuous.prototype.trim = function(){
var task = new RSVP.defer(); var task = new RSVP.defer();
var above = true; var displayed = this.displayed();
var first = displayed[0];
var last = displayed[displayed.length-1];
var firstIndex = this.views.indexOf(first);
var lastIndex = this.views.indexOf(last);
var above = this.views.slice(0, firstIndex);
var below = this.views.slice(lastIndex+1);
this.views.forEach(function(view, i){ for (var i = 0; i < above.length-1; i++) {
// var view = this.views[i]; this.erase(above[i], above);
var prevShown = i > 0 ? this.views[i-1].shown : false; };
var nextShown = (i+1 < this.views.length) ? this.views[i+1].shown : false;
if(!view.shown && !prevShown && !nextShown) { for (var i = 1; i < below.length; i++) {
// Remove this.erase(below[i]);
this.erase(view, above); };
}
if(nextShown) {
above = false;
}
}.bind(this));
task.resolve(); task.resolve();
return task.promise; return task.promise;
@ -476,6 +513,7 @@ EPUBJS.Continuous.prototype.scrollBy = function(x, y, silent){
} else { } else {
window.scrollBy(x,y); window.scrollBy(x,y);
} }
// console.log("scrollBy", x, y);
this.scrolled = true; this.scrolled = true;
}; };
@ -490,7 +528,7 @@ EPUBJS.Continuous.prototype.scrollTo = function(x, y, silent){
} else { } else {
window.scrollTo(x,y); window.scrollTo(x,y);
} }
// console.log("scrollTo", x, y);
this.scrolled = true; this.scrolled = true;
// if(this.container.scrollLeft != x){ // if(this.container.scrollLeft != x){
// setTimeout(function() { // setTimeout(function() {
@ -510,11 +548,26 @@ EPUBJS.Continuous.prototype.scrollTo = function(x, y, silent){
}; };
EPUBJS.Continuous.prototype.destroy = function() { EPUBJS.Continuous.prototype.currentLocation = function(){
var visible = this.visible();
var startPage, endPage;
this.clear(); var container = this.container.getBoundingClientRect()
this.element.removeChild(this.container); if(visible.length === 1) {
return this.map.page(visible[0]);
}
if(visible.length > 1) {
startPage = this.map.page(visible[0]);
endPage = this.map.page(visible[visible.length-1]);
return {
start: startPage.start,
end: endPage.end
}
}
}; };

View file

@ -17,6 +17,7 @@ EPUBJS.Layout.Reflowable.prototype.calculate = function(_width, _height, _gap, _
var colWidth; var colWidth;
var spreadWidth; var spreadWidth;
var delta;
//-- Double Page //-- Double Page
if(divisor > 1) { if(divisor > 1) {

View file

@ -188,7 +188,7 @@ EPUBJS.Map.prototype.findTextEndRange = function(node, start, end){
pos = range.getBoundingClientRect(); pos = range.getBoundingClientRect();
if(pos.left > end) { if(pos.left > end && prev) {
return prev; return prev;
} else if(pos.right > end) { } else if(pos.right > end) {
return range; return range;
@ -215,7 +215,7 @@ EPUBJS.Map.prototype.splitTextNodeIntoRanges = function(node, _splitter){
pos = text.indexOf(splitter); pos = text.indexOf(splitter);
if(pos === -1) { if(pos === -1 || node.nodeType != Node.TEXT_NODE) {
range = doc.createRange(); range = doc.createRange();
range.selectNodeContents(node); range.selectNodeContents(node);
return [range]; return [range];

View file

@ -141,6 +141,13 @@ EPUBJS.Paginate.prototype.layoutUpdate = function() {
}; };
EPUBJS.Paginate.prototype.moveTo = function(offset){
var dist = Math.floor(offset.left / this.layout.delta) * this.layout.delta;
return this.check(0, dist+this.settings.offset).then(function(){
this.scrollBy(dist, 0);
}.bind(this));
};
EPUBJS.Paginate.prototype.page = function(pg){ EPUBJS.Paginate.prototype.page = function(pg){
// this.currentPage = pg; // this.currentPage = pg;
@ -172,16 +179,16 @@ EPUBJS.Paginate.prototype.prev = function(){
EPUBJS.Paginate.prototype.reportLocation = function(){ EPUBJS.Paginate.prototype.reportLocation = function(){
return this.q.enqueue(function(){ return this.q.enqueue(function(){
var location = this.currentLocation(); this.location = this.currentLocation();
this.trigger("locationChanged", location); this.trigger("locationChanged", this.location);
}.bind(this)); }.bind(this));
}; };
EPUBJS.Paginate.prototype.currentLocation = function(){ EPUBJS.Paginate.prototype.currentLocation = function(){
var visible = this.visible(); var visible = this.visible();
var startA, startB, endA, endB; var startA, startB, endA, endB;
var pageLeft, pageRight;
container = this.container.getBoundingClientRect() var container = this.container.getBoundingClientRect()
if(visible.length === 1) { if(visible.length === 1) {
startA = container.left - visible[0].position().left; startA = container.left - visible[0].position().left;
@ -211,6 +218,33 @@ EPUBJS.Paginate.prototype.currentLocation = function(){
}; };
EPUBJS.Paginate.prototype.resize = function(width, height){
// Clear the queue
this.q.clear();
this.stageSize(width, height);
this.layoutUpdate();
this.display(this.location.start);
this.trigger("resized", {
width: this.stage.width,
height: this.stage.height
});
};
EPUBJS.Paginate.prototype.onResized = function(e) {
this.clear();
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(function(){
this.resize();
}.bind(this), 150);
};
// EPUBJS.Paginate.prototype.display = function(what){ // EPUBJS.Paginate.prototype.display = function(what){
// return this.display(what); // return this.display(what);
// }; // };

View file

@ -40,6 +40,8 @@ EPUBJS.Queue.prototype.enqueue = function() {
this._q.push(queued); this._q.push(queued);
// Wait to start queue flush
setTimeout(this.flush.bind(this), 0);
return queued.promise; return queued.promise;
}; };
@ -83,13 +85,14 @@ EPUBJS.Queue.prototype.dequeue = function(){
}; };
// Run All Immediately // Run All Immediately
EPUBJS.Queue.prototype.flush = function(){ EPUBJS.Queue.prototype.dump = function(){
while(this._q.length) { while(this._q.length) {
this.dequeue(); this.dequeue();
} }
}; };
// Run all sequentially, at convince // Run all sequentially, at convince
EPUBJS.Queue.prototype.run = function(){ EPUBJS.Queue.prototype.run = function(){
if(!this.running && this._q.length) { if(!this.running && this._q.length) {
this.running = true; this.running = true;
@ -101,9 +104,28 @@ EPUBJS.Queue.prototype.run = function(){
this.tick.call(window, this.run.bind(this)); this.tick.call(window, this.run.bind(this));
}; };
// Flush all, as quickly as possible
EPUBJS.Queue.prototype.flush = function(){
if(this.running){
return this.running;
}
if(this._q.length) {
this.running = this.dequeue().
then(function(){
this.running = undefined;
return this.flush();
}.bind(this));
return this.running;
}
};
// Clear all items in wait // Clear all items in wait
EPUBJS.Queue.prototype.clear = function(){ EPUBJS.Queue.prototype.clear = function(){
this._q = []; this._q = [];
this.running = false;
}; };
EPUBJS.Queue.prototype.length = function(){ EPUBJS.Queue.prototype.length = function(){

View file

@ -30,9 +30,7 @@ EPUBJS.Rendition = function(book, options) {
this.q.enqueue(this.book.opened); this.q.enqueue(this.book.opened);
this.book.opened.then(function(){ this.q.enqueue(this.parseLayoutProperties);
this.globalLayoutProperties = this.parseLayoutProperties(this.book.package.metadata);
}.bind(this));
}; };
@ -138,7 +136,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
this.trigger("attached"); this.trigger("attached");
// Start processing queue // Start processing queue
this.q.run(); // this.q.run();
}; };
@ -156,18 +154,34 @@ EPUBJS.Rendition.prototype.bounds = function() {
return this.container.getBoundingClientRect(); return this.container.getBoundingClientRect();
}; };
EPUBJS.Rendition.prototype.display = function(what){ EPUBJS.Rendition.prototype.display = function(target){
return this.q.enqueue(function(what){ return this.q.enqueue(this._display, target);
};
EPUBJS.Rendition.prototype._display = function(target){
var displaying = new RSVP.defer(); var displaying = new RSVP.defer();
var displayed = displaying.promise; var displayed = displaying.promise;
var section = this.book.spine.get(what); var section;
var view; 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; this.displaying = true;
// Hide current views
this.hide();
if(section){ if(section){
view = this.createView(section); view = this.createView(section);
@ -177,6 +191,9 @@ EPUBJS.Rendition.prototype.display = function(what){
// Move to correct place within the section, if needed // Move to correct place within the section, if needed
// this.moveTo(what) // this.moveTo(what)
// Show views
this.show();
this.hooks.display.trigger(view); this.hooks.display.trigger(view);
displaying.resolve(this); displaying.resolve(this);
@ -186,16 +203,14 @@ EPUBJS.Rendition.prototype.display = function(what){
} }
return displayed; return displayed;
}, what);
}; };
// Takes a cfi, fragment or page? // Takes a cfi, fragment or page?
EPUBJS.Rendition.prototype.moveTo = function(what){ EPUBJS.Rendition.prototype.moveTo = function(what){
}; };
EPUBJS.Rendition.prototype.render = function(view) { EPUBJS.Rendition.prototype.render = function(view, show) {
view.create(); view.create();
@ -205,7 +220,7 @@ EPUBJS.Rendition.prototype.render = function(view) {
this.resizeView(view); this.resizeView(view);
// Render Chain // Render Chain
return view.display(this.book.request) return view.render(this.book.request)
.then(function(){ .then(function(){
return this.hooks.replacements.trigger(view, this); return this.hooks.replacements.trigger(view, this);
}.bind(this)) }.bind(this))
@ -213,10 +228,17 @@ EPUBJS.Rendition.prototype.render = function(view) {
return this.hooks.layout.trigger(view); return this.hooks.layout.trigger(view);
}.bind(this)) }.bind(this))
.then(function(){ .then(function(){
return view.show() return view.display()
}.bind(this)) }.bind(this))
.then(function(view){ .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.map = new EPUBJS.Map(view, this.layout);
this.trigger("rendered", view.section); this.trigger("rendered", view.section);
@ -240,35 +262,43 @@ EPUBJS.Rendition.prototype.append = function(view){
// view.appendTo(this.container); // view.appendTo(this.container);
this.container.appendChild(view.element); this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("displayed", this.afterDisplayed.bind(this));
view.onShown = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
// this.resizeView(view); // this.resizeView(view);
return this.render(view); return this.render(view);
}; };
EPUBJS.Rendition.prototype.clear = function(){ EPUBJS.Rendition.prototype.clear = function(){
// Remove all views
this.views.forEach(function(view){ this.views.forEach(function(view){
this.remove(view); this._remove(view);
}.bind(this)); }.bind(this));
this.views = [];
}; };
EPUBJS.Rendition.prototype.remove = function(view) { EPUBJS.Rendition.prototype.remove = function(view) {
var index = this.views.indexOf(view); var index = this.views.indexOf(view);
if(index > -1) { if(index > -1) {
this.views.splice(index, 1); this.views.splice(index, 1);
} }
this.container.removeChild(view.element);
if(view.shown){ this._remove(view);
view.destroy();
}
view = null;
}; };
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) { EPUBJS.Rendition.prototype.resizeView = function(view) {
@ -343,6 +373,8 @@ EPUBJS.Rendition.prototype.layoutMethod = function() {
this.layout = new EPUBJS.Layout.Scroll(); this.layout = new EPUBJS.Layout.Scroll();
this.layoutUpdate(); this.layoutUpdate();
this.map = new EPUBJS.Map(this.layout);
}; };
EPUBJS.Rendition.prototype.layoutUpdate = function() { EPUBJS.Rendition.prototype.layoutUpdate = function() {
@ -351,7 +383,6 @@ EPUBJS.Rendition.prototype.layoutUpdate = function() {
}; };
EPUBJS.Rendition.prototype.resize = function(width, height){ EPUBJS.Rendition.prototype.resize = function(width, height){
this.stageSize(width, height); this.stageSize(width, height);
@ -415,15 +446,17 @@ EPUBJS.Rendition.prototype.prev = function(){
}; };
//-- http://www.idpf.org/epub/fxl/ //-- 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 layout = (this.layoutOveride && this.layoutOveride.layout) || metadata.layout || "reflowable";
var spread = (this.layoutOveride && this.layoutOveride.spread) || metadata.spread || "auto"; var spread = (this.layoutOveride && this.layoutOveride.spread) || metadata.spread || "auto";
var orientation = (this.layoutOveride && this.layoutOveride.orientation) || metadata.orientation || "auto"; var orientation = (this.layoutOveride && this.layoutOveride.orientation) || metadata.orientation || "auto";
return { this.globalLayoutProperties = {
layout : layout, layout : layout,
spread : spread, spread : spread,
orientation : orientation orientation : orientation
}; };
return this.globalLayoutProperties;
}; };
@ -436,19 +469,19 @@ EPUBJS.Rendition.prototype.current = function(){
return null; return null;
}; };
EPUBJS.Rendition.prototype.isVisible = function(view, offset, _container){ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _container){
var position = view.position(); var position = view.position();
var container = _container || this.container.getBoundingClientRect(); var container = _container || this.container.getBoundingClientRect();
if(this.settings.axis === "horizontal" && if(this.settings.axis === "horizontal" &&
(position.right > container.left - offset) && (position.right > container.left - offsetPrev) &&
!(position.left >= container.right + offset)) { !(position.left >= container.right + offsetNext)) {
return true; return true;
} else if(this.settings.axis === "vertical" && } else if(this.settings.axis === "vertical" &&
(position.bottom > container.top - offset) && (position.bottom > container.top - offsetPrev) &&
!(position.top >= container.bottom + offset)) { !(position.top >= container.bottom + offsetNext)) {
return true; return true;
} }
@ -458,14 +491,14 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offset, _container){
}; };
EPUBJS.Rendition.prototype.visible = function(){ EPUBJS.Rendition.prototype.visible = function(){
var container = this.container.getBoundingClientRect(); var container = this.bounds();
var visible = []; var visible = [];
var isVisible; var isVisible;
var view; var view;
for (var i = 0; i < this.views.length; i++) { for (var i = 0; i < this.views.length; i++) {
view = this.views[i]; view = this.views[i];
isVisible = this.isVisible(view, 0, container); isVisible = this.isVisible(view, 0, 0, container);
if(isVisible === true) { if(isVisible === true) {
visible.push(view); 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 //-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype); RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype);

View file

@ -54,7 +54,10 @@ EPUBJS.Spine.prototype.load = function(_package) {
EPUBJS.Spine.prototype.get = function(target) { EPUBJS.Spine.prototype.get = function(target) {
var index = 0; var index = 0;
if(target && (typeof target === "number" || isNaN(target) === false)){ if(typeof target === "string" && target.indexOf("epubcfi(") === 0) {
cfi = new EPUBJS.EpubCFI(target);
index = cfi.spinePos;
} if(target && (typeof target === "number" || isNaN(target) === false)){
index = target; index = target;
} else if(target && target.indexOf("#") === 0) { } else if(target && target.indexOf("#") === 0) {
index = this.spineById[target.substring(1)]; index = this.spineById[target.substring(1)];

View file

@ -14,12 +14,14 @@ EPUBJS.View = function(section, options) {
this.element.style.width = "0px"; this.element.style.width = "0px";
this.element.style.overflow = "hidden"; this.element.style.overflow = "hidden";
this.shown = false; this.displayed = false;
this.rendered = false; this.rendered = false;
//this.width = 0; //this.width = 0;
//this.height = 0; //this.height = 0;
// Blank Cfi for Parsing
this.epubcfi = new EPUBJS.EpubCFI();
if(this.settings.axis && this.settings.axis == "horizontal"){ if(this.settings.axis && this.settings.axis == "horizontal"){
this.element.style.display = "inline-block"; this.element.style.display = "inline-block";
@ -87,7 +89,6 @@ EPUBJS.View.prototype.lock = function(width, height) {
iframeBorders = {width: 0, height: 0}; iframeBorders = {width: 0, height: 0};
} }
if(EPUBJS.core.isNumber(width)){ if(EPUBJS.core.isNumber(width)){
this.lockedWidth = width - elBorders.width - iframeBorders.width; this.lockedWidth = width - elBorders.width - iframeBorders.width;
this.resize(this.lockedWidth, width); // width keeps ratio correct this.resize(this.lockedWidth, width); // width keeps ratio correct
@ -107,7 +108,7 @@ EPUBJS.View.prototype.lock = function(width, height) {
this.resize(this.lockedWidth, this.lockedHeight); this.resize(this.lockedWidth, this.lockedHeight);
} }
if(this.shown && this.iframe) { if(this.displayed && this.iframe) {
this.layout(); this.layout();
this.expand(); this.expand();
@ -139,7 +140,7 @@ EPUBJS.View.prototype.resize = function(width, height) {
EPUBJS.View.prototype.reframe = function(width, height) { EPUBJS.View.prototype.reframe = function(width, height) {
//var prevBounds; //var prevBounds;
if(!this.shown) return; if(!this.displayed) return;
if(EPUBJS.core.isNumber(width)){ if(EPUBJS.core.isNumber(width)){
this.element.style.width = width + "px"; this.element.style.width = width + "px";
@ -174,21 +175,20 @@ EPUBJS.View.prototype.resized = function(e) {
}; };
EPUBJS.View.prototype.display = function(_request) { EPUBJS.View.prototype.render = function(_request) {
// if(this.rendering){ // if(this.rendering){
// return this.displayed; // return this.displayed;
// } // }
this.rendering = true; this.rendering = true;
this.displaying = new RSVP.defer(); // this.displayingDefer = new RSVP.defer();
this.displayed = this.displaying.promise; // this.displayedPromise = this.displaying.promise;
return this.section.render(_request) return this.section.render(_request)
.then(function(contents){ .then(function(contents){
return this.load(contents); return this.load(contents);
}.bind(this)) }.bind(this));
.then(this.displaying.resolve.call(this));
}; };
EPUBJS.View.prototype.load = function(contents) { EPUBJS.View.prototype.load = function(contents) {
@ -363,10 +363,10 @@ EPUBJS.View.prototype.imageLoadListeners = function(target) {
} }
}; };
EPUBJS.View.prototype.show = function() { EPUBJS.View.prototype.display = function() {
var shown = new RSVP.defer(); var displayed = new RSVP.defer();
this.shown = true; this.displayed = true;
this.layout(); this.layout();
@ -374,15 +374,23 @@ EPUBJS.View.prototype.show = function() {
this.expand(); this.expand();
this.trigger("displayed", this);
this.onDisplayed(this);
displayed.resolve(this);
return displayed.promise;
};
EPUBJS.View.prototype.show = function() {
this.element.style.visibility = "visible"; this.element.style.visibility = "visible";
if(this.iframe){
this.iframe.style.visibility = "visible"; this.iframe.style.visibility = "visible";
}
this.trigger("shown", this); this.trigger("shown", this);
this.onShown(this);
shown.resolve(this);
return shown.promise;
}; };
EPUBJS.View.prototype.hide = function() { EPUBJS.View.prototype.hide = function() {
@ -391,14 +399,14 @@ EPUBJS.View.prototype.hide = function() {
this.iframe.style.visibility = "hidden"; this.iframe.style.visibility = "hidden";
this.stopExpanding = true; this.stopExpanding = true;
this.trigger("hidden"); this.trigger("hidden", this);
}; };
EPUBJS.View.prototype.position = function() { EPUBJS.View.prototype.position = function() {
return this.element.getBoundingClientRect(); return this.element.getBoundingClientRect();
}; };
EPUBJS.View.prototype.onShown = function(view) { EPUBJS.View.prototype.onDisplayed = function(view) {
// Stub, override with a custom functions // Stub, override with a custom functions
}; };
@ -415,10 +423,10 @@ EPUBJS.View.prototype.destroy = function() {
this.observer.disconnect(); this.observer.disconnect();
} }
if(this.iframe){ if(this.displayed){
this.stopExpanding = true; this.stopExpanding = true;
this.element.removeChild(this.iframe); this.element.removeChild(this.iframe);
this.shown = false; this.displayed = false;
this.iframe = null; this.iframe = null;
} }
@ -431,4 +439,39 @@ EPUBJS.View.prototype.root = function() {
return this.document.documentElement; return this.document.documentElement;
}; };
EPUBJS.View.prototype.locationOf = function(target) {
if(!this.document) return;
if(target.indexOf("epubcfi(") === 0) {
cfi = this.epubcfi.parse(target);
if(typeof document.evaluate === 'undefined') {
marker = this.epubcfi.addMarker(cfi, this.document);
if(marker) {
// Must Clean up Marker before going to page
this.epubcfi.removeMarker(marker, this.document);
return marker.getBoundingClientRect();
}
} else {
range = this.epubcfi.generateRangeFromCfi(cfi, this.document);
if(range) {
return range.getBoundingClientRect();
}
}
} else if(target.indexOf("#") > -1) {
id = target.substring(target.indexOf("#"));
el = this.document.getElementById(id);
if(el) {
return el.getBoundingClientRect();
}
}
return {"left": 0, "top": 0};
};
RSVP.EventTarget.mixin(EPUBJS.View.prototype); RSVP.EventTarget.mixin(EPUBJS.View.prototype);