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

Added Views function to manage collections of views

This commit is contained in:
fchasen 2015-06-28 15:40:04 -04:00
parent 6676590225
commit 23d88d2e99
8 changed files with 432 additions and 490 deletions

456
dist/epub.js vendored
View file

@ -2182,17 +2182,20 @@ EPUBJS.Queue.prototype.enqueue = function() {
var queued; var queued;
var task = [].shift.call(arguments); var task = [].shift.call(arguments);
var args = arguments; var args = arguments;
// Handle single args without context // Handle single args without context
// if(args && !Array.isArray(args)) { // if(args && !Array.isArray(args)) {
// args = [args]; // args = [args];
// } // }
if(!task) {
return console.error("No Task Provided");
}
if(typeof task === "function"){ if(typeof task === "function"){
deferred = new RSVP.defer(); deferred = new RSVP.defer();
promise = deferred.promise; promise = deferred.promise;
queued = { queued = {
"task" : task, "task" : task,
"args" : args, "args" : args,
@ -2200,15 +2203,15 @@ EPUBJS.Queue.prototype.enqueue = function() {
"deferred" : deferred, "deferred" : deferred,
"promise" : promise "promise" : promise
}; };
} else { } else {
// Task is a promise // Task is a promise
queued = { queued = {
"promise" : task "promise" : task
}; };
} }
this._q.push(queued); this._q.push(queued);
// Wait to start queue flush // Wait to start queue flush
@ -2240,8 +2243,8 @@ EPUBJS.Queue.prototype.dequeue = function(){
return inwait.promise; return inwait.promise;
} }
} else if(inwait.promise) { } else if(inwait.promise) {
// Task is a promise // Task is a promise
return inwait.promise; return inwait.promise;
@ -2271,7 +2274,7 @@ EPUBJS.Queue.prototype.run = function(){
this.running = false; this.running = false;
}.bind(this)); }.bind(this));
} }
this.tick.call(window, this.run.bind(this)); this.tick.call(window, this.run.bind(this));
}; };
@ -2308,7 +2311,7 @@ EPUBJS.Task = function(task, args, context){
return function(){ return function(){
var toApply = arguments || []; var toApply = arguments || [];
return new RSVP.Promise(function(resolve, reject) { return new RSVP.Promise(function(resolve, reject) {
var callback = function(value){ var callback = function(value){
resolve(value); resolve(value);
@ -2318,12 +2321,13 @@ EPUBJS.Task = function(task, args, context){
// Apply all arguments to the functions // Apply all arguments to the functions
task.apply(this, toApply); task.apply(this, toApply);
}.bind(this)); }.bind(this));
}; };
}; };
EPUBJS.Hook = function(context){ EPUBJS.Hook = function(context){
this.context = context || this; this.context = context || this;
this.hooks = []; this.hooks = [];
@ -4518,6 +4522,159 @@ EPUBJS.View.prototype.addScript = function(src) {
RSVP.EventTarget.mixin(EPUBJS.View.prototype); RSVP.EventTarget.mixin(EPUBJS.View.prototype);
EPUBJS.Views = function(container) {
this.container = container;
this._views = [];
this.length = 0;
this.hidden = false;
};
EPUBJS.Views.prototype.first = function() {
return this._views[0];
};
EPUBJS.Views.prototype.last = function() {
return this._views[this._views.length-1];
};
EPUBJS.Views.prototype.each = function() {
return this._views.forEach.apply(this._views, arguments);
};
EPUBJS.Views.prototype.indexOf = function(view) {
return this._views.indexOf(view);
};
EPUBJS.Views.prototype.slice = function() {
return this._views.slice.apply(this._views, arguments);
};
EPUBJS.Views.prototype.get = function(i) {
return this._views[i];
};
EPUBJS.Views.prototype.append = function(view){
this._views.push(view);
this.container.appendChild(view.element);
this.length++;
return view;
};
EPUBJS.Views.prototype.prepend = function(view){
this._views.unshift(view);
this.container.insertBefore(view.element, this.container.firstChild);
this.length++;
return view;
};
EPUBJS.Views.prototype.insert = function(view, index) {
this._views.splice(index, 0, view);
if(index < this.container.children.length){
this.container.insertBefore(view.element, this.container.children[index]);
} else {
this.container.appendChild(view.element);
}
this.length++;
return view;
};
EPUBJS.Views.prototype.remove = function(view) {
var index = this._views.indexOf(view);
if(index > -1) {
this._views.splice(index, 1);
}
this.destroy(view);
this.length--;
};
EPUBJS.Views.prototype.destroy = function(view) {
view.off("resized");
if(view.displayed){
view.destroy();
}
this.container.removeChild(view.element);
view = null;
};
// Iterators
EPUBJS.Views.prototype.clear = function(){
// Remove all views
var view;
var len = this.length;
if(!this.length) return;
for (var i = 0; i < len; i++) {
view = this._views[i];
this.destroy(view);
}
this._views = [];
this.length = 0;
};
EPUBJS.Views.prototype.find = function(section){
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed && view.section.index == section.index) {
return view;
}
}
};
EPUBJS.Views.prototype.displayed = function(){
var displayed = [];
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed){
displayed.push(view);
}
}
return displayed;
};
EPUBJS.Views.prototype.show = function(){
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed){
view.show();
}
}
this.hidden = false;
};
EPUBJS.Views.prototype.hide = function(){
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed){
view.hide();
}
}
this.hidden = true;
};
EPUBJS.Layout = EPUBJS.Layout || {}; EPUBJS.Layout = EPUBJS.Layout || {};
EPUBJS.Layout.Reflowable = function(){ EPUBJS.Layout.Reflowable = function(){
@ -4691,7 +4848,7 @@ EPUBJS.Rendition = function(book, options) {
this.book = book; this.book = book;
this.views = []; this.views = null;
//-- Adds Hook methods to the Rendition prototype //-- Adds Hook methods to the Rendition prototype
this.hooks = {}; this.hooks = {};
@ -4804,6 +4961,8 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
this.element.appendChild(this.container); this.element.appendChild(this.container);
} }
this.views = new EPUBJS.Views(this.container);
// Attach Listeners // Attach Listeners
this.attachListeners(); this.attachListeners();
@ -4862,7 +5021,7 @@ EPUBJS.Rendition.prototype._display = function(target){
} }
// Check to make sure the section we want isn't already shown // Check to make sure the section we want isn't already shown
visible = this.find(section); visible = this.views.find(section);
if(visible) { if(visible) {
offset = view.locationOf(target); offset = view.locationOf(target);
@ -4871,7 +5030,7 @@ EPUBJS.Rendition.prototype._display = function(target){
} else { } else {
// Hide all current views // Hide all current views
this.hide(); this.views.hide();
// Create a new view // Create a new view
view = new EPUBJS.View(section, this.viewSettings); view = new EPUBJS.View(section, this.viewSettings);
@ -4889,16 +5048,14 @@ EPUBJS.Rendition.prototype._display = function(target){
// Move to correct place within the section, if needed // Move to correct place within the section, if needed
if(cfi || fragment) { if(cfi || fragment) {
offset = view.locationOf(target); offset = view.locationOf(target);
this.q.enqueue(this.moveTo, offset); return this.q.enqueue(this.moveTo, offset);
} }
if(typeof this.check === 'function') { if(typeof this.check === 'function') {
this.q.enqueue(this.check); return this.q.enqueue(this.check);
} }
}.bind(this))
this.q.enqueue(this.show); .then(this.views.show.bind(this.views));
}.bind(this));
} }
@ -4910,8 +5067,6 @@ EPUBJS.Rendition.prototype._display = function(target){
displaying.resolve(this); displaying.resolve(this);
}.bind(this)); }.bind(this));
return displayed; return displayed;
}; };
@ -4944,8 +5099,7 @@ EPUBJS.Rendition.prototype.render = function(view, show) {
return this.hooks.render.trigger(view, this); return this.hooks.render.trigger(view, this);
}.bind(this)) }.bind(this))
.then(function(){ .then(function(){
if(show !== false && this.views.hidden === false) {
if(show !== false && this.hidden === false) {
this.q.enqueue(function(view){ this.q.enqueue(function(view){
view.show(); view.show();
}, view); }, view);
@ -4969,13 +5123,9 @@ EPUBJS.Rendition.prototype.afterDisplayed = function(view){
EPUBJS.Rendition.prototype.fill = function(view){ EPUBJS.Rendition.prototype.fill = function(view){
if(this.views.length){ this.views.clear();
this.clear();
}
this.views.push(view); this.views.append(view);
this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onDisplayed = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
@ -4983,37 +5133,6 @@ EPUBJS.Rendition.prototype.fill = function(view){
return this.render(view); return this.render(view);
}; };
EPUBJS.Rendition.prototype.clear = function(){
// Remove all views
this.views.forEach(function(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._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) { EPUBJS.Rendition.prototype.resizeView = function(view) {
if(this.globalLayoutProperties.layout === "pre-paginated") { if(this.globalLayoutProperties.layout === "pre-paginated") {
@ -5103,7 +5222,7 @@ EPUBJS.Rendition.prototype.resize = function(width, height){
this.updateLayout(); this.updateLayout();
this.views.forEach(this.resizeView.bind(this)); this.views.each(this.resizeView.bind(this));
this.trigger("resized", { this.trigger("resized", {
width: this.stage.width, width: this.stage.width,
@ -5129,7 +5248,7 @@ EPUBJS.Rendition.prototype.next = function(){
if(!this.views.length) return; if(!this.views.length) return;
next = this.views[0].section.next(); next = this.views.last().section.next();
if(next) { if(next) {
view = this.createView(next); view = this.createView(next);
@ -5149,7 +5268,7 @@ EPUBJS.Rendition.prototype.prev = function(){
if(!this.views.length) return; if(!this.views.length) return;
prev = this.views[0].section.prev(); prev = this.views.first().section.prev();
if(prev) { if(prev) {
view = this.createView(prev); view = this.createView(prev);
return this.append(view); return this.append(view);
@ -5206,12 +5325,13 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c
EPUBJS.Rendition.prototype.visible = function(){ EPUBJS.Rendition.prototype.visible = function(){
var container = this.bounds(); var container = this.bounds();
var displayedViews = this.views.displayed();
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 < displayedViews.length; i++) {
view = this.views[i]; view = displayedViews[i];
isVisible = this.isVisible(view, 0, 0, container); isVisible = this.isVisible(view, 0, 0, container);
if(isVisible === true) { if(isVisible === true) {
@ -5219,24 +5339,10 @@ EPUBJS.Rendition.prototype.visible = function(){
} }
} }
return visible; return visible;
}; };
EPUBJS.Rendition.prototype.find = function(section){
var view;
for (var i = 0; i < this.views.length; i++) {
view = this.views[i];
if(view.displayed && view.section.index == section.index) {
return view;
}
}
};
EPUBJS.Rendition.prototype.bounds = function(func) { EPUBJS.Rendition.prototype.bounds = function(func) {
var bounds; var bounds;
@ -5249,45 +5355,11 @@ EPUBJS.Rendition.prototype.bounds = function(func) {
return bounds; 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(){ EPUBJS.Rendition.prototype.destroy = function(){
// Clear the queue // Clear the queue
this.q.clear(); this.q.clear();
this.clear(); this.views.clear();
clearTimeout(this.trimTimeout); clearTimeout(this.trimTimeout);
if(this.settings.hidden) { if(this.settings.hidden) {
@ -5310,7 +5382,7 @@ EPUBJS.Rendition.prototype.currentLocation = function(){
var start, end; var start, end;
if(this.views.length) { if(this.views.length) {
view = this.views[0]; view = this.views.first();
// start = container.left - view.position().left; // start = container.left - view.position().left;
// end = start + this.layout.spread; // end = start + this.layout.spread;
@ -5432,7 +5504,6 @@ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
var next = currView.section.next(); var next = currView.section.next();
var prev = currView.section.prev(); var prev = currView.section.prev();
var index = this.views.indexOf(currView); var index = this.views.indexOf(currView);
var prevView, nextView; var prevView, nextView;
if(index + 1 === this.views.length && next) { if(index + 1 === this.views.length && next) {
@ -5463,25 +5534,24 @@ EPUBJS.Continuous.prototype.removeShownListeners = function(view){
}; };
EPUBJS.Continuous.prototype.append = function(view){ EPUBJS.Continuous.prototype.append = function(view){
this.views.push(view);
this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onDisplayed = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
this.views.append(view);
//this.q.enqueue(this.check); //this.q.enqueue(this.check);
return this.check(); return this.check();
}; };
EPUBJS.Continuous.prototype.prepend = function(view){ EPUBJS.Continuous.prototype.prepend = function(view){
this.views.unshift(view);
this.container.insertBefore(view.element, this.container.firstChild);
// view.on("shown", this.afterDisplayedAbove.bind(this)); // view.on("shown", this.afterDisplayedAbove.bind(this));
view.onDisplayed = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
view.on("resized", this.counter.bind(this)); view.on("resized", this.counter.bind(this));
this.views.prepend(view);
// this.q.enqueue(this.check); // this.q.enqueue(this.check);
return this.check(); return this.check();
}; };
@ -5496,58 +5566,13 @@ EPUBJS.Continuous.prototype.counter = function(bounds){
}; };
EPUBJS.Continuous.prototype.insert = function(view, index) {
this.views.splice(index, 0, view);
if(index < this.cotainer.children.length){
this.container.insertBefore(view.element, this.container.children[index]);
} else {
this.container.appendChild(view.element);
}
// this.q.enqueue(this.check);
return this.check();
};
// // Remove the render element and clean up listeners
// EPUBJS.Continuous.prototype.remove = function(view) {
// var index = this.views.indexOf(view);
// if(index > -1) {
// this.views.splice(index, 1);
// }
// this.container.removeChild(view.element);
// view.off("resized");
// if(view.displayed){
// view.destroy();
// }
// view = null;
// };
EPUBJS.Continuous.prototype.first = function() {
return this.views[0];
};
EPUBJS.Continuous.prototype.last = function() {
return this.views[this.views.length-1];
};
EPUBJS.Continuous.prototype.each = function(func) {
return this.views.forEach(func);
};
EPUBJS.Continuous.prototype.check = function(_offset){ EPUBJS.Continuous.prototype.check = function(_offset){
var checking = new RSVP.defer(); var checking = new RSVP.defer();
var container = this.bounds(); var container = this.bounds();
var promises = []; var promises = [];
var offset = _offset || this.settings.offset; var offset = _offset || this.settings.offset;
this.views.each(function(view){
this.views.forEach(function(view){
var visible = this.isVisible(view, offset, offset, container); var visible = this.isVisible(view, offset, offset, container);
if(visible) { if(visible) {
@ -5592,30 +5617,9 @@ EPUBJS.Continuous.prototype.check = function(_offset){
}; };
// 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 displayed = this.displayed(); var displayed = this.views.displayed();
var first = displayed[0]; var first = displayed[0];
var last = displayed[displayed.length-1]; var last = displayed[displayed.length-1];
var firstIndex = this.views.indexOf(first); var firstIndex = this.views.indexOf(first);
@ -5652,7 +5656,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
var bounds = view.bounds(); var bounds = view.bounds();
this.remove(view); this.views.remove(view);
if(above) { if(above) {
@ -5665,39 +5669,6 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
}; };
EPUBJS.Continuous.prototype.checkCurrent = function(position) {
var view, top;
var container = this.container.getBoundingClientRect();
var length = this.views.length - 1;
if(this.rendering) {
return;
}
if(this.settings.axis === "horizontal") {
// TODO: Check for current horizontal
} else {
for (var i = length; i >= 0; i--) {
view = this.views[i];
top = view.bounds().top;
if(top < container.bottom) {
if(this.current == view.section) {
break;
}
this.current = view.section;
this.trigger("current", this.current);
break;
}
}
}
};
EPUBJS.Continuous.prototype.start = function() { EPUBJS.Continuous.prototype.start = function() {
var scroller; var scroller;
@ -5902,7 +5873,7 @@ EPUBJS.Paginate = function(book, options) {
this.viewSettings = { this.viewSettings = {
axis: this.settings.axis axis: this.settings.axis
}; };
this.start(); this.start();
}; };
@ -5941,31 +5912,31 @@ EPUBJS.Paginate.prototype.forceSingle = function(bool){
// console.log("spreads", spreads, this.settings.minSpreadWidth) // console.log("spreads", spreads, this.settings.minSpreadWidth)
// var layoutMethod = spreads ? "ReflowableSpreads" : "Reflowable"; // var layoutMethod = spreads ? "ReflowableSpreads" : "Reflowable";
// var scroll = false; // var scroll = false;
// //
// if(settings.layout === "pre-paginated") { // if(settings.layout === "pre-paginated") {
// layoutMethod = "Fixed"; // layoutMethod = "Fixed";
// scroll = true; // scroll = true;
// spreads = false; // spreads = false;
// } // }
// //
// if(settings.layout === "reflowable" && settings.spread === "none") { // if(settings.layout === "reflowable" && settings.spread === "none") {
// layoutMethod = "Reflowable"; // layoutMethod = "Reflowable";
// scroll = false; // scroll = false;
// spreads = false; // spreads = false;
// } // }
// //
// if(settings.layout === "reflowable" && settings.spread === "both") { // if(settings.layout === "reflowable" && settings.spread === "both") {
// layoutMethod = "ReflowableSpreads"; // layoutMethod = "ReflowableSpreads";
// scroll = false; // scroll = false;
// spreads = true; // spreads = true;
// } // }
// //
// this.spreads = spreads; // this.spreads = spreads;
// //
// return layoutMethod; // return layoutMethod;
// }; // };
EPUBJS.Paginate.prototype.start = function(){ EPUBJS.Paginate.prototype.start = function(){
// On display // On display
// this.layoutSettings = this.reconcileLayoutSettings(globalLayout, chapter.properties); // this.layoutSettings = this.reconcileLayoutSettings(globalLayout, chapter.properties);
// this.layoutMethod = this.determineLayout(this.layoutSettings); // this.layoutMethod = this.determineLayout(this.layoutSettings);
@ -5973,7 +5944,7 @@ EPUBJS.Paginate.prototype.start = function(){
//this.hooks.display.register(this.registerLayoutMethod.bind(this)); //this.hooks.display.register(this.registerLayoutMethod.bind(this));
// this.hooks.display.register(this.reportLocation); // this.hooks.display.register(this.reportLocation);
this.on('displayed', this.reportLocation.bind(this)); this.on('displayed', this.reportLocation.bind(this));
this.hooks.content.register(this.adjustImages.bind(this)); this.hooks.content.register(this.adjustImages.bind(this));
this.currentPage = 0; this.currentPage = 0;
@ -6013,17 +5984,17 @@ EPUBJS.Paginate.prototype.applyLayoutMethod = function() {
}; };
EPUBJS.Paginate.prototype.updateLayout = function() { EPUBJS.Paginate.prototype.updateLayout = function() {
this.spreads = this.determineSpreads(this.settings.minSpreadWidth); this.spreads = this.determineSpreads(this.settings.minSpreadWidth);
this.layout.calculate( this.layout.calculate(
this.stage.width, this.stage.width,
this.stage.height, this.stage.height,
this.settings.gap, this.settings.gap,
this.spreads this.spreads
); );
this.settings.offset = this.layout.delta; this.settings.offset = this.layout.delta;
}; };
@ -6035,7 +6006,7 @@ EPUBJS.Paginate.prototype.moveTo = function(offset){
}; };
EPUBJS.Paginate.prototype.page = function(pg){ EPUBJS.Paginate.prototype.page = function(pg){
// this.currentPage = pg; // this.currentPage = pg;
// this.renderer.infinite.scrollTo(this.currentPage * this.formated.pageWidth, 0); // this.renderer.infinite.scrollTo(this.currentPage * this.formated.pageWidth, 0);
//-- Return false if page is greater than the total //-- Return false if page is greater than the total
@ -6101,7 +6072,7 @@ EPUBJS.Paginate.prototype.currentLocation = function(){
end: pageRight.end end: pageRight.end
}; };
} }
}; };
EPUBJS.Paginate.prototype.resize = function(width, height){ EPUBJS.Paginate.prototype.resize = function(width, height){
@ -6123,7 +6094,7 @@ EPUBJS.Paginate.prototype.resize = function(width, height){
EPUBJS.Paginate.prototype.onResized = function(e) { EPUBJS.Paginate.prototype.onResized = function(e) {
this.clear(); this.views.clear();
clearTimeout(this.resizeTimeout); clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(function(){ this.resizeTimeout = setTimeout(function(){
@ -6132,9 +6103,9 @@ EPUBJS.Paginate.prototype.onResized = function(e) {
}; };
EPUBJS.Paginate.prototype.adjustImages = function(view) { EPUBJS.Paginate.prototype.adjustImages = function(view) {
view.addStylesheetRules([ view.addStylesheetRules([
["img", ["img",
["max-width", (this.layout.spread) + "px"], ["max-width", (this.layout.spread) + "px"],
["max-height", (this.layout.height) + "px"] ["max-height", (this.layout.height) + "px"]
] ]
@ -6150,6 +6121,7 @@ EPUBJS.Paginate.prototype.adjustImages = function(view) {
// EPUBJS.Paginate.prototype.display = function(what){ // EPUBJS.Paginate.prototype.display = function(what){
// return this.display(what); // return this.display(what);
// }; // };
EPUBJS.Map = function(layout){ EPUBJS.Map = function(layout){
this.layout = layout; this.layout = layout;
}; };

6
dist/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -24,6 +24,7 @@ var files = [
'src/replacements.js', 'src/replacements.js',
'src/book.js', 'src/book.js',
'src/view.js', 'src/view.js',
'src/views.js',
'src/layout.js', 'src/layout.js',
'src/rendition.js', 'src/rendition.js',
'src/continuous.js', 'src/continuous.js',
@ -63,4 +64,4 @@ gulp.task('default', ['lint', 'minify']);
// gulp.task('default', function() { // gulp.task('default', function() {
// // place code for your default task here // // place code for your default task here
// }); // });

View file

@ -70,7 +70,6 @@ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
var next = currView.section.next(); var next = currView.section.next();
var prev = currView.section.prev(); var prev = currView.section.prev();
var index = this.views.indexOf(currView); var index = this.views.indexOf(currView);
var prevView, nextView; var prevView, nextView;
if(index + 1 === this.views.length && next) { if(index + 1 === this.views.length && next) {
@ -101,25 +100,24 @@ EPUBJS.Continuous.prototype.removeShownListeners = function(view){
}; };
EPUBJS.Continuous.prototype.append = function(view){ EPUBJS.Continuous.prototype.append = function(view){
this.views.push(view);
this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onDisplayed = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
this.views.append(view);
//this.q.enqueue(this.check); //this.q.enqueue(this.check);
return this.check(); return this.check();
}; };
EPUBJS.Continuous.prototype.prepend = function(view){ EPUBJS.Continuous.prototype.prepend = function(view){
this.views.unshift(view);
this.container.insertBefore(view.element, this.container.firstChild);
// view.on("shown", this.afterDisplayedAbove.bind(this)); // view.on("shown", this.afterDisplayedAbove.bind(this));
view.onDisplayed = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
view.on("resized", this.counter.bind(this)); view.on("resized", this.counter.bind(this));
this.views.prepend(view);
// this.q.enqueue(this.check); // this.q.enqueue(this.check);
return this.check(); return this.check();
}; };
@ -134,58 +132,13 @@ EPUBJS.Continuous.prototype.counter = function(bounds){
}; };
EPUBJS.Continuous.prototype.insert = function(view, index) {
this.views.splice(index, 0, view);
if(index < this.cotainer.children.length){
this.container.insertBefore(view.element, this.container.children[index]);
} else {
this.container.appendChild(view.element);
}
// this.q.enqueue(this.check);
return this.check();
};
// // Remove the render element and clean up listeners
// EPUBJS.Continuous.prototype.remove = function(view) {
// var index = this.views.indexOf(view);
// if(index > -1) {
// this.views.splice(index, 1);
// }
// this.container.removeChild(view.element);
// view.off("resized");
// if(view.displayed){
// view.destroy();
// }
// view = null;
// };
EPUBJS.Continuous.prototype.first = function() {
return this.views[0];
};
EPUBJS.Continuous.prototype.last = function() {
return this.views[this.views.length-1];
};
EPUBJS.Continuous.prototype.each = function(func) {
return this.views.forEach(func);
};
EPUBJS.Continuous.prototype.check = function(_offset){ EPUBJS.Continuous.prototype.check = function(_offset){
var checking = new RSVP.defer(); var checking = new RSVP.defer();
var container = this.bounds(); var container = this.bounds();
var promises = []; var promises = [];
var offset = _offset || this.settings.offset; var offset = _offset || this.settings.offset;
this.views.each(function(view){
this.views.forEach(function(view){
var visible = this.isVisible(view, offset, offset, container); var visible = this.isVisible(view, offset, offset, container);
if(visible) { if(visible) {
@ -230,30 +183,9 @@ EPUBJS.Continuous.prototype.check = function(_offset){
}; };
// 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 displayed = this.displayed(); var displayed = this.views.displayed();
var first = displayed[0]; var first = displayed[0];
var last = displayed[displayed.length-1]; var last = displayed[displayed.length-1];
var firstIndex = this.views.indexOf(first); var firstIndex = this.views.indexOf(first);
@ -290,7 +222,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
var bounds = view.bounds(); var bounds = view.bounds();
this.remove(view); this.views.remove(view);
if(above) { if(above) {
@ -303,39 +235,6 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
}; };
EPUBJS.Continuous.prototype.checkCurrent = function(position) {
var view, top;
var container = this.container.getBoundingClientRect();
var length = this.views.length - 1;
if(this.rendering) {
return;
}
if(this.settings.axis === "horizontal") {
// TODO: Check for current horizontal
} else {
for (var i = length; i >= 0; i--) {
view = this.views[i];
top = view.bounds().top;
if(top < container.bottom) {
if(this.current == view.section) {
break;
}
this.current = view.section;
this.trigger("current", this.current);
break;
}
}
}
};
EPUBJS.Continuous.prototype.start = function() { EPUBJS.Continuous.prototype.start = function() {
var scroller; var scroller;

View file

@ -20,7 +20,7 @@ EPUBJS.Paginate = function(book, options) {
this.viewSettings = { this.viewSettings = {
axis: this.settings.axis axis: this.settings.axis
}; };
this.start(); this.start();
}; };
@ -59,31 +59,31 @@ EPUBJS.Paginate.prototype.forceSingle = function(bool){
// console.log("spreads", spreads, this.settings.minSpreadWidth) // console.log("spreads", spreads, this.settings.minSpreadWidth)
// var layoutMethod = spreads ? "ReflowableSpreads" : "Reflowable"; // var layoutMethod = spreads ? "ReflowableSpreads" : "Reflowable";
// var scroll = false; // var scroll = false;
// //
// if(settings.layout === "pre-paginated") { // if(settings.layout === "pre-paginated") {
// layoutMethod = "Fixed"; // layoutMethod = "Fixed";
// scroll = true; // scroll = true;
// spreads = false; // spreads = false;
// } // }
// //
// if(settings.layout === "reflowable" && settings.spread === "none") { // if(settings.layout === "reflowable" && settings.spread === "none") {
// layoutMethod = "Reflowable"; // layoutMethod = "Reflowable";
// scroll = false; // scroll = false;
// spreads = false; // spreads = false;
// } // }
// //
// if(settings.layout === "reflowable" && settings.spread === "both") { // if(settings.layout === "reflowable" && settings.spread === "both") {
// layoutMethod = "ReflowableSpreads"; // layoutMethod = "ReflowableSpreads";
// scroll = false; // scroll = false;
// spreads = true; // spreads = true;
// } // }
// //
// this.spreads = spreads; // this.spreads = spreads;
// //
// return layoutMethod; // return layoutMethod;
// }; // };
EPUBJS.Paginate.prototype.start = function(){ EPUBJS.Paginate.prototype.start = function(){
// On display // On display
// this.layoutSettings = this.reconcileLayoutSettings(globalLayout, chapter.properties); // this.layoutSettings = this.reconcileLayoutSettings(globalLayout, chapter.properties);
// this.layoutMethod = this.determineLayout(this.layoutSettings); // this.layoutMethod = this.determineLayout(this.layoutSettings);
@ -91,7 +91,7 @@ EPUBJS.Paginate.prototype.start = function(){
//this.hooks.display.register(this.registerLayoutMethod.bind(this)); //this.hooks.display.register(this.registerLayoutMethod.bind(this));
// this.hooks.display.register(this.reportLocation); // this.hooks.display.register(this.reportLocation);
this.on('displayed', this.reportLocation.bind(this)); this.on('displayed', this.reportLocation.bind(this));
this.hooks.content.register(this.adjustImages.bind(this)); this.hooks.content.register(this.adjustImages.bind(this));
this.currentPage = 0; this.currentPage = 0;
@ -131,17 +131,17 @@ EPUBJS.Paginate.prototype.applyLayoutMethod = function() {
}; };
EPUBJS.Paginate.prototype.updateLayout = function() { EPUBJS.Paginate.prototype.updateLayout = function() {
this.spreads = this.determineSpreads(this.settings.minSpreadWidth); this.spreads = this.determineSpreads(this.settings.minSpreadWidth);
this.layout.calculate( this.layout.calculate(
this.stage.width, this.stage.width,
this.stage.height, this.stage.height,
this.settings.gap, this.settings.gap,
this.spreads this.spreads
); );
this.settings.offset = this.layout.delta; this.settings.offset = this.layout.delta;
}; };
@ -153,7 +153,7 @@ EPUBJS.Paginate.prototype.moveTo = function(offset){
}; };
EPUBJS.Paginate.prototype.page = function(pg){ EPUBJS.Paginate.prototype.page = function(pg){
// this.currentPage = pg; // this.currentPage = pg;
// this.renderer.infinite.scrollTo(this.currentPage * this.formated.pageWidth, 0); // this.renderer.infinite.scrollTo(this.currentPage * this.formated.pageWidth, 0);
//-- Return false if page is greater than the total //-- Return false if page is greater than the total
@ -219,7 +219,7 @@ EPUBJS.Paginate.prototype.currentLocation = function(){
end: pageRight.end end: pageRight.end
}; };
} }
}; };
EPUBJS.Paginate.prototype.resize = function(width, height){ EPUBJS.Paginate.prototype.resize = function(width, height){
@ -241,7 +241,7 @@ EPUBJS.Paginate.prototype.resize = function(width, height){
EPUBJS.Paginate.prototype.onResized = function(e) { EPUBJS.Paginate.prototype.onResized = function(e) {
this.clear(); this.views.clear();
clearTimeout(this.resizeTimeout); clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(function(){ this.resizeTimeout = setTimeout(function(){
@ -250,9 +250,9 @@ EPUBJS.Paginate.prototype.onResized = function(e) {
}; };
EPUBJS.Paginate.prototype.adjustImages = function(view) { EPUBJS.Paginate.prototype.adjustImages = function(view) {
view.addStylesheetRules([ view.addStylesheetRules([
["img", ["img",
["max-width", (this.layout.spread) + "px"], ["max-width", (this.layout.spread) + "px"],
["max-height", (this.layout.height) + "px"] ["max-height", (this.layout.height) + "px"]
] ]
@ -267,4 +267,4 @@ EPUBJS.Paginate.prototype.adjustImages = function(view) {
// EPUBJS.Paginate.prototype.display = function(what){ // EPUBJS.Paginate.prototype.display = function(what){
// return this.display(what); // return this.display(what);
// }; // };

View file

@ -11,17 +11,20 @@ EPUBJS.Queue.prototype.enqueue = function() {
var queued; var queued;
var task = [].shift.call(arguments); var task = [].shift.call(arguments);
var args = arguments; var args = arguments;
// Handle single args without context // Handle single args without context
// if(args && !Array.isArray(args)) { // if(args && !Array.isArray(args)) {
// args = [args]; // args = [args];
// } // }
if(!task) {
return console.error("No Task Provided");
}
if(typeof task === "function"){ if(typeof task === "function"){
deferred = new RSVP.defer(); deferred = new RSVP.defer();
promise = deferred.promise; promise = deferred.promise;
queued = { queued = {
"task" : task, "task" : task,
"args" : args, "args" : args,
@ -29,15 +32,15 @@ EPUBJS.Queue.prototype.enqueue = function() {
"deferred" : deferred, "deferred" : deferred,
"promise" : promise "promise" : promise
}; };
} else { } else {
// Task is a promise // Task is a promise
queued = { queued = {
"promise" : task "promise" : task
}; };
} }
this._q.push(queued); this._q.push(queued);
// Wait to start queue flush // Wait to start queue flush
@ -69,8 +72,8 @@ EPUBJS.Queue.prototype.dequeue = function(){
return inwait.promise; return inwait.promise;
} }
} else if(inwait.promise) { } else if(inwait.promise) {
// Task is a promise // Task is a promise
return inwait.promise; return inwait.promise;
@ -100,7 +103,7 @@ EPUBJS.Queue.prototype.run = function(){
this.running = false; this.running = false;
}.bind(this)); }.bind(this));
} }
this.tick.call(window, this.run.bind(this)); this.tick.call(window, this.run.bind(this));
}; };
@ -137,7 +140,7 @@ EPUBJS.Task = function(task, args, context){
return function(){ return function(){
var toApply = arguments || []; var toApply = arguments || [];
return new RSVP.Promise(function(resolve, reject) { return new RSVP.Promise(function(resolve, reject) {
var callback = function(value){ var callback = function(value){
resolve(value); resolve(value);
@ -147,9 +150,9 @@ EPUBJS.Task = function(task, args, context){
// Apply all arguments to the functions // Apply all arguments to the functions
task.apply(this, toApply); task.apply(this, toApply);
}.bind(this)); }.bind(this));
}; };
}; };

View file

@ -15,7 +15,7 @@ EPUBJS.Rendition = function(book, options) {
this.book = book; this.book = book;
this.views = []; this.views = null;
//-- Adds Hook methods to the Rendition prototype //-- Adds Hook methods to the Rendition prototype
this.hooks = {}; this.hooks = {};
@ -128,6 +128,8 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
this.element.appendChild(this.container); this.element.appendChild(this.container);
} }
this.views = new EPUBJS.Views(this.container);
// Attach Listeners // Attach Listeners
this.attachListeners(); this.attachListeners();
@ -186,7 +188,7 @@ EPUBJS.Rendition.prototype._display = function(target){
} }
// Check to make sure the section we want isn't already shown // Check to make sure the section we want isn't already shown
visible = this.find(section); visible = this.views.find(section);
if(visible) { if(visible) {
offset = view.locationOf(target); offset = view.locationOf(target);
@ -195,7 +197,7 @@ EPUBJS.Rendition.prototype._display = function(target){
} else { } else {
// Hide all current views // Hide all current views
this.hide(); this.views.hide();
// Create a new view // Create a new view
view = new EPUBJS.View(section, this.viewSettings); view = new EPUBJS.View(section, this.viewSettings);
@ -213,16 +215,14 @@ EPUBJS.Rendition.prototype._display = function(target){
// Move to correct place within the section, if needed // Move to correct place within the section, if needed
if(cfi || fragment) { if(cfi || fragment) {
offset = view.locationOf(target); offset = view.locationOf(target);
this.q.enqueue(this.moveTo, offset); return this.q.enqueue(this.moveTo, offset);
} }
if(typeof this.check === 'function') { if(typeof this.check === 'function') {
this.q.enqueue(this.check); return this.q.enqueue(this.check);
} }
}.bind(this))
this.q.enqueue(this.show); .then(this.views.show.bind(this.views));
}.bind(this));
} }
@ -234,8 +234,6 @@ EPUBJS.Rendition.prototype._display = function(target){
displaying.resolve(this); displaying.resolve(this);
}.bind(this)); }.bind(this));
return displayed; return displayed;
}; };
@ -268,8 +266,7 @@ EPUBJS.Rendition.prototype.render = function(view, show) {
return this.hooks.render.trigger(view, this); return this.hooks.render.trigger(view, this);
}.bind(this)) }.bind(this))
.then(function(){ .then(function(){
if(show !== false && this.views.hidden === false) {
if(show !== false && this.hidden === false) {
this.q.enqueue(function(view){ this.q.enqueue(function(view){
view.show(); view.show();
}, view); }, view);
@ -293,13 +290,9 @@ EPUBJS.Rendition.prototype.afterDisplayed = function(view){
EPUBJS.Rendition.prototype.fill = function(view){ EPUBJS.Rendition.prototype.fill = function(view){
if(this.views.length){ this.views.clear();
this.clear();
}
this.views.push(view); this.views.append(view);
this.container.appendChild(view.element);
// view.on("shown", this.afterDisplayed.bind(this)); // view.on("shown", this.afterDisplayed.bind(this));
view.onDisplayed = this.afterDisplayed.bind(this); view.onDisplayed = this.afterDisplayed.bind(this);
@ -307,37 +300,6 @@ EPUBJS.Rendition.prototype.fill = function(view){
return this.render(view); return this.render(view);
}; };
EPUBJS.Rendition.prototype.clear = function(){
// Remove all views
this.views.forEach(function(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._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) { EPUBJS.Rendition.prototype.resizeView = function(view) {
if(this.globalLayoutProperties.layout === "pre-paginated") { if(this.globalLayoutProperties.layout === "pre-paginated") {
@ -427,7 +389,7 @@ EPUBJS.Rendition.prototype.resize = function(width, height){
this.updateLayout(); this.updateLayout();
this.views.forEach(this.resizeView.bind(this)); this.views.each(this.resizeView.bind(this));
this.trigger("resized", { this.trigger("resized", {
width: this.stage.width, width: this.stage.width,
@ -453,7 +415,7 @@ EPUBJS.Rendition.prototype.next = function(){
if(!this.views.length) return; if(!this.views.length) return;
next = this.views[0].section.next(); next = this.views.last().section.next();
if(next) { if(next) {
view = this.createView(next); view = this.createView(next);
@ -473,7 +435,7 @@ EPUBJS.Rendition.prototype.prev = function(){
if(!this.views.length) return; if(!this.views.length) return;
prev = this.views[0].section.prev(); prev = this.views.first().section.prev();
if(prev) { if(prev) {
view = this.createView(prev); view = this.createView(prev);
return this.append(view); return this.append(view);
@ -530,12 +492,13 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c
EPUBJS.Rendition.prototype.visible = function(){ EPUBJS.Rendition.prototype.visible = function(){
var container = this.bounds(); var container = this.bounds();
var displayedViews = this.views.displayed();
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 < displayedViews.length; i++) {
view = this.views[i]; view = displayedViews[i];
isVisible = this.isVisible(view, 0, 0, container); isVisible = this.isVisible(view, 0, 0, container);
if(isVisible === true) { if(isVisible === true) {
@ -543,24 +506,10 @@ EPUBJS.Rendition.prototype.visible = function(){
} }
} }
return visible; return visible;
}; };
EPUBJS.Rendition.prototype.find = function(section){
var view;
for (var i = 0; i < this.views.length; i++) {
view = this.views[i];
if(view.displayed && view.section.index == section.index) {
return view;
}
}
};
EPUBJS.Rendition.prototype.bounds = function(func) { EPUBJS.Rendition.prototype.bounds = function(func) {
var bounds; var bounds;
@ -573,45 +522,11 @@ EPUBJS.Rendition.prototype.bounds = function(func) {
return bounds; 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(){ EPUBJS.Rendition.prototype.destroy = function(){
// Clear the queue // Clear the queue
this.q.clear(); this.q.clear();
this.clear(); this.views.clear();
clearTimeout(this.trimTimeout); clearTimeout(this.trimTimeout);
if(this.settings.hidden) { if(this.settings.hidden) {
@ -634,7 +549,7 @@ EPUBJS.Rendition.prototype.currentLocation = function(){
var start, end; var start, end;
if(this.views.length) { if(this.views.length) {
view = this.views[0]; view = this.views.first();
// start = container.left - view.position().left; // start = container.left - view.position().left;
// end = start + this.layout.spread; // end = start + this.layout.spread;

152
src/views.js Normal file
View file

@ -0,0 +1,152 @@
EPUBJS.Views = function(container) {
this.container = container;
this._views = [];
this.length = 0;
this.hidden = false;
};
EPUBJS.Views.prototype.first = function() {
return this._views[0];
};
EPUBJS.Views.prototype.last = function() {
return this._views[this._views.length-1];
};
EPUBJS.Views.prototype.each = function() {
return this._views.forEach.apply(this._views, arguments);
};
EPUBJS.Views.prototype.indexOf = function(view) {
return this._views.indexOf(view);
};
EPUBJS.Views.prototype.slice = function() {
return this._views.slice.apply(this._views, arguments);
};
EPUBJS.Views.prototype.get = function(i) {
return this._views[i];
};
EPUBJS.Views.prototype.append = function(view){
this._views.push(view);
this.container.appendChild(view.element);
this.length++;
return view;
};
EPUBJS.Views.prototype.prepend = function(view){
this._views.unshift(view);
this.container.insertBefore(view.element, this.container.firstChild);
this.length++;
return view;
};
EPUBJS.Views.prototype.insert = function(view, index) {
this._views.splice(index, 0, view);
if(index < this.container.children.length){
this.container.insertBefore(view.element, this.container.children[index]);
} else {
this.container.appendChild(view.element);
}
this.length++;
return view;
};
EPUBJS.Views.prototype.remove = function(view) {
var index = this._views.indexOf(view);
if(index > -1) {
this._views.splice(index, 1);
}
this.destroy(view);
this.length--;
};
EPUBJS.Views.prototype.destroy = function(view) {
view.off("resized");
if(view.displayed){
view.destroy();
}
this.container.removeChild(view.element);
view = null;
};
// Iterators
EPUBJS.Views.prototype.clear = function(){
// Remove all views
var view;
var len = this.length;
if(!this.length) return;
for (var i = 0; i < len; i++) {
view = this._views[i];
this.destroy(view);
}
this._views = [];
this.length = 0;
};
EPUBJS.Views.prototype.find = function(section){
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed && view.section.index == section.index) {
return view;
}
}
};
EPUBJS.Views.prototype.displayed = function(){
var displayed = [];
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed){
displayed.push(view);
}
}
return displayed;
};
EPUBJS.Views.prototype.show = function(){
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed){
view.show();
}
}
this.hidden = false;
};
EPUBJS.Views.prototype.hide = function(){
var view;
var len = this.length;
for (var i = 0; i < len; i++) {
view = this._views[i];
if(view.displayed){
view.hide();
}
}
this.hidden = true;
};