mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Added Views function to manage collections of views
This commit is contained in:
parent
6676590225
commit
23d88d2e99
8 changed files with 432 additions and 490 deletions
400
dist/epub.js
vendored
400
dist/epub.js
vendored
|
@ -2187,6 +2187,9 @@ EPUBJS.Queue.prototype.enqueue = function() {
|
|||
// if(args && !Array.isArray(args)) {
|
||||
// args = [args];
|
||||
// }
|
||||
if(!task) {
|
||||
return console.error("No Task Provided");
|
||||
}
|
||||
|
||||
if(typeof task === "function"){
|
||||
|
||||
|
@ -2324,6 +2327,7 @@ EPUBJS.Task = function(task, args, context){
|
|||
};
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Hook = function(context){
|
||||
this.context = context || this;
|
||||
this.hooks = [];
|
||||
|
@ -4518,6 +4522,159 @@ EPUBJS.View.prototype.addScript = function(src) {
|
|||
|
||||
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.Reflowable = function(){
|
||||
|
@ -4691,7 +4848,7 @@ EPUBJS.Rendition = function(book, options) {
|
|||
|
||||
this.book = book;
|
||||
|
||||
this.views = [];
|
||||
this.views = null;
|
||||
|
||||
//-- Adds Hook methods to the Rendition prototype
|
||||
this.hooks = {};
|
||||
|
@ -4804,6 +4961,8 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
|
|||
this.element.appendChild(this.container);
|
||||
}
|
||||
|
||||
this.views = new EPUBJS.Views(this.container);
|
||||
|
||||
// Attach Listeners
|
||||
this.attachListeners();
|
||||
|
||||
|
@ -4862,7 +5021,7 @@ EPUBJS.Rendition.prototype._display = function(target){
|
|||
}
|
||||
|
||||
// Check to make sure the section we want isn't already shown
|
||||
visible = this.find(section);
|
||||
visible = this.views.find(section);
|
||||
|
||||
if(visible) {
|
||||
offset = view.locationOf(target);
|
||||
|
@ -4871,7 +5030,7 @@ EPUBJS.Rendition.prototype._display = function(target){
|
|||
} else {
|
||||
|
||||
// Hide all current views
|
||||
this.hide();
|
||||
this.views.hide();
|
||||
|
||||
// Create a new view
|
||||
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
|
||||
if(cfi || fragment) {
|
||||
offset = view.locationOf(target);
|
||||
this.q.enqueue(this.moveTo, offset);
|
||||
return this.q.enqueue(this.moveTo, offset);
|
||||
}
|
||||
|
||||
if(typeof this.check === 'function') {
|
||||
this.q.enqueue(this.check);
|
||||
return this.q.enqueue(this.check);
|
||||
}
|
||||
|
||||
this.q.enqueue(this.show);
|
||||
|
||||
}.bind(this));
|
||||
}.bind(this))
|
||||
.then(this.views.show.bind(this.views));
|
||||
}
|
||||
|
||||
|
||||
|
@ -4910,8 +5067,6 @@ EPUBJS.Rendition.prototype._display = function(target){
|
|||
displaying.resolve(this);
|
||||
}.bind(this));
|
||||
|
||||
|
||||
|
||||
return displayed;
|
||||
};
|
||||
|
||||
|
@ -4944,8 +5099,7 @@ EPUBJS.Rendition.prototype.render = function(view, show) {
|
|||
return this.hooks.render.trigger(view, this);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
|
||||
if(show !== false && this.hidden === false) {
|
||||
if(show !== false && this.views.hidden === false) {
|
||||
this.q.enqueue(function(view){
|
||||
view.show();
|
||||
}, view);
|
||||
|
@ -4969,13 +5123,9 @@ EPUBJS.Rendition.prototype.afterDisplayed = function(view){
|
|||
|
||||
EPUBJS.Rendition.prototype.fill = function(view){
|
||||
|
||||
if(this.views.length){
|
||||
this.clear();
|
||||
}
|
||||
this.views.clear();
|
||||
|
||||
this.views.push(view);
|
||||
|
||||
this.container.appendChild(view.element);
|
||||
this.views.append(view);
|
||||
|
||||
// view.on("shown", this.afterDisplayed.bind(this));
|
||||
view.onDisplayed = this.afterDisplayed.bind(this);
|
||||
|
@ -4983,37 +5133,6 @@ EPUBJS.Rendition.prototype.fill = function(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) {
|
||||
|
||||
if(this.globalLayoutProperties.layout === "pre-paginated") {
|
||||
|
@ -5103,7 +5222,7 @@ EPUBJS.Rendition.prototype.resize = function(width, height){
|
|||
|
||||
this.updateLayout();
|
||||
|
||||
this.views.forEach(this.resizeView.bind(this));
|
||||
this.views.each(this.resizeView.bind(this));
|
||||
|
||||
this.trigger("resized", {
|
||||
width: this.stage.width,
|
||||
|
@ -5129,7 +5248,7 @@ EPUBJS.Rendition.prototype.next = function(){
|
|||
|
||||
if(!this.views.length) return;
|
||||
|
||||
next = this.views[0].section.next();
|
||||
next = this.views.last().section.next();
|
||||
|
||||
if(next) {
|
||||
view = this.createView(next);
|
||||
|
@ -5149,7 +5268,7 @@ EPUBJS.Rendition.prototype.prev = function(){
|
|||
|
||||
if(!this.views.length) return;
|
||||
|
||||
prev = this.views[0].section.prev();
|
||||
prev = this.views.first().section.prev();
|
||||
if(prev) {
|
||||
view = this.createView(prev);
|
||||
return this.append(view);
|
||||
|
@ -5206,12 +5325,13 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c
|
|||
|
||||
EPUBJS.Rendition.prototype.visible = function(){
|
||||
var container = this.bounds();
|
||||
var displayedViews = this.views.displayed();
|
||||
var visible = [];
|
||||
var isVisible;
|
||||
var view;
|
||||
|
||||
for (var i = 0; i < this.views.length; i++) {
|
||||
view = this.views[i];
|
||||
for (var i = 0; i < displayedViews.length; i++) {
|
||||
view = displayedViews[i];
|
||||
isVisible = this.isVisible(view, 0, 0, container);
|
||||
|
||||
if(isVisible === true) {
|
||||
|
@ -5219,24 +5339,10 @@ EPUBJS.Rendition.prototype.visible = function(){
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
var bounds;
|
||||
|
||||
|
@ -5249,45 +5355,11 @@ EPUBJS.Rendition.prototype.bounds = function(func) {
|
|||
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();
|
||||
this.views.clear();
|
||||
|
||||
clearTimeout(this.trimTimeout);
|
||||
if(this.settings.hidden) {
|
||||
|
@ -5310,7 +5382,7 @@ EPUBJS.Rendition.prototype.currentLocation = function(){
|
|||
var start, end;
|
||||
|
||||
if(this.views.length) {
|
||||
view = this.views[0];
|
||||
view = this.views.first();
|
||||
// start = container.left - view.position().left;
|
||||
// end = start + this.layout.spread;
|
||||
|
||||
|
@ -5432,7 +5504,6 @@ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
|
|||
var next = currView.section.next();
|
||||
var prev = currView.section.prev();
|
||||
var index = this.views.indexOf(currView);
|
||||
|
||||
var prevView, nextView;
|
||||
|
||||
if(index + 1 === this.views.length && next) {
|
||||
|
@ -5463,25 +5534,24 @@ EPUBJS.Continuous.prototype.removeShownListeners = 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.onDisplayed = this.afterDisplayed.bind(this);
|
||||
|
||||
this.views.append(view);
|
||||
|
||||
//this.q.enqueue(this.check);
|
||||
return this.check();
|
||||
};
|
||||
|
||||
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.onDisplayed = this.afterDisplayed.bind(this);
|
||||
|
||||
view.on("resized", this.counter.bind(this));
|
||||
|
||||
this.views.prepend(view);
|
||||
|
||||
// this.q.enqueue(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){
|
||||
var checking = new RSVP.defer();
|
||||
var container = this.bounds();
|
||||
var promises = [];
|
||||
var offset = _offset || this.settings.offset;
|
||||
|
||||
|
||||
this.views.forEach(function(view){
|
||||
this.views.each(function(view){
|
||||
var visible = this.isVisible(view, offset, offset, container);
|
||||
|
||||
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(){
|
||||
var task = new RSVP.defer();
|
||||
var displayed = this.displayed();
|
||||
var displayed = this.views.displayed();
|
||||
var first = displayed[0];
|
||||
var last = displayed[displayed.length-1];
|
||||
var firstIndex = this.views.indexOf(first);
|
||||
|
@ -5652,7 +5656,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
|
|||
|
||||
var bounds = view.bounds();
|
||||
|
||||
this.remove(view);
|
||||
this.views.remove(view);
|
||||
|
||||
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() {
|
||||
var scroller;
|
||||
|
||||
|
@ -6123,7 +6094,7 @@ EPUBJS.Paginate.prototype.resize = function(width, height){
|
|||
|
||||
EPUBJS.Paginate.prototype.onResized = function(e) {
|
||||
|
||||
this.clear();
|
||||
this.views.clear();
|
||||
|
||||
clearTimeout(this.resizeTimeout);
|
||||
this.resizeTimeout = setTimeout(function(){
|
||||
|
@ -6150,6 +6121,7 @@ EPUBJS.Paginate.prototype.adjustImages = function(view) {
|
|||
// EPUBJS.Paginate.prototype.display = function(what){
|
||||
// return this.display(what);
|
||||
// };
|
||||
|
||||
EPUBJS.Map = function(layout){
|
||||
this.layout = layout;
|
||||
};
|
||||
|
|
6
dist/epub.min.js
vendored
6
dist/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -24,6 +24,7 @@ var files = [
|
|||
'src/replacements.js',
|
||||
'src/book.js',
|
||||
'src/view.js',
|
||||
'src/views.js',
|
||||
'src/layout.js',
|
||||
'src/rendition.js',
|
||||
'src/continuous.js',
|
||||
|
|
|
@ -70,7 +70,6 @@ EPUBJS.Continuous.prototype.afterDisplayed = function(currView){
|
|||
var next = currView.section.next();
|
||||
var prev = currView.section.prev();
|
||||
var index = this.views.indexOf(currView);
|
||||
|
||||
var prevView, nextView;
|
||||
|
||||
if(index + 1 === this.views.length && next) {
|
||||
|
@ -101,25 +100,24 @@ EPUBJS.Continuous.prototype.removeShownListeners = 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.onDisplayed = this.afterDisplayed.bind(this);
|
||||
|
||||
this.views.append(view);
|
||||
|
||||
//this.q.enqueue(this.check);
|
||||
return this.check();
|
||||
};
|
||||
|
||||
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.onDisplayed = this.afterDisplayed.bind(this);
|
||||
|
||||
view.on("resized", this.counter.bind(this));
|
||||
|
||||
this.views.prepend(view);
|
||||
|
||||
// this.q.enqueue(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){
|
||||
var checking = new RSVP.defer();
|
||||
var container = this.bounds();
|
||||
var promises = [];
|
||||
var offset = _offset || this.settings.offset;
|
||||
|
||||
|
||||
this.views.forEach(function(view){
|
||||
this.views.each(function(view){
|
||||
var visible = this.isVisible(view, offset, offset, container);
|
||||
|
||||
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(){
|
||||
var task = new RSVP.defer();
|
||||
var displayed = this.displayed();
|
||||
var displayed = this.views.displayed();
|
||||
var first = displayed[0];
|
||||
var last = displayed[displayed.length-1];
|
||||
var firstIndex = this.views.indexOf(first);
|
||||
|
@ -290,7 +222,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim
|
|||
|
||||
var bounds = view.bounds();
|
||||
|
||||
this.remove(view);
|
||||
this.views.remove(view);
|
||||
|
||||
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() {
|
||||
var scroller;
|
||||
|
||||
|
|
|
@ -241,7 +241,7 @@ EPUBJS.Paginate.prototype.resize = function(width, height){
|
|||
|
||||
EPUBJS.Paginate.prototype.onResized = function(e) {
|
||||
|
||||
this.clear();
|
||||
this.views.clear();
|
||||
|
||||
clearTimeout(this.resizeTimeout);
|
||||
this.resizeTimeout = setTimeout(function(){
|
||||
|
|
|
@ -16,6 +16,9 @@ EPUBJS.Queue.prototype.enqueue = function() {
|
|||
// if(args && !Array.isArray(args)) {
|
||||
// args = [args];
|
||||
// }
|
||||
if(!task) {
|
||||
return console.error("No Task Provided");
|
||||
}
|
||||
|
||||
if(typeof task === "function"){
|
||||
|
||||
|
|
125
src/rendition.js
125
src/rendition.js
|
@ -15,7 +15,7 @@ EPUBJS.Rendition = function(book, options) {
|
|||
|
||||
this.book = book;
|
||||
|
||||
this.views = [];
|
||||
this.views = null;
|
||||
|
||||
//-- Adds Hook methods to the Rendition prototype
|
||||
this.hooks = {};
|
||||
|
@ -128,6 +128,8 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
|
|||
this.element.appendChild(this.container);
|
||||
}
|
||||
|
||||
this.views = new EPUBJS.Views(this.container);
|
||||
|
||||
// Attach Listeners
|
||||
this.attachListeners();
|
||||
|
||||
|
@ -186,7 +188,7 @@ EPUBJS.Rendition.prototype._display = function(target){
|
|||
}
|
||||
|
||||
// Check to make sure the section we want isn't already shown
|
||||
visible = this.find(section);
|
||||
visible = this.views.find(section);
|
||||
|
||||
if(visible) {
|
||||
offset = view.locationOf(target);
|
||||
|
@ -195,7 +197,7 @@ EPUBJS.Rendition.prototype._display = function(target){
|
|||
} else {
|
||||
|
||||
// Hide all current views
|
||||
this.hide();
|
||||
this.views.hide();
|
||||
|
||||
// Create a new view
|
||||
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
|
||||
if(cfi || fragment) {
|
||||
offset = view.locationOf(target);
|
||||
this.q.enqueue(this.moveTo, offset);
|
||||
return this.q.enqueue(this.moveTo, offset);
|
||||
}
|
||||
|
||||
if(typeof this.check === 'function') {
|
||||
this.q.enqueue(this.check);
|
||||
return this.q.enqueue(this.check);
|
||||
}
|
||||
|
||||
this.q.enqueue(this.show);
|
||||
|
||||
}.bind(this));
|
||||
}.bind(this))
|
||||
.then(this.views.show.bind(this.views));
|
||||
}
|
||||
|
||||
|
||||
|
@ -234,8 +234,6 @@ EPUBJS.Rendition.prototype._display = function(target){
|
|||
displaying.resolve(this);
|
||||
}.bind(this));
|
||||
|
||||
|
||||
|
||||
return displayed;
|
||||
};
|
||||
|
||||
|
@ -268,8 +266,7 @@ EPUBJS.Rendition.prototype.render = function(view, show) {
|
|||
return this.hooks.render.trigger(view, this);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
|
||||
if(show !== false && this.hidden === false) {
|
||||
if(show !== false && this.views.hidden === false) {
|
||||
this.q.enqueue(function(view){
|
||||
view.show();
|
||||
}, view);
|
||||
|
@ -293,13 +290,9 @@ EPUBJS.Rendition.prototype.afterDisplayed = function(view){
|
|||
|
||||
EPUBJS.Rendition.prototype.fill = function(view){
|
||||
|
||||
if(this.views.length){
|
||||
this.clear();
|
||||
}
|
||||
this.views.clear();
|
||||
|
||||
this.views.push(view);
|
||||
|
||||
this.container.appendChild(view.element);
|
||||
this.views.append(view);
|
||||
|
||||
// view.on("shown", this.afterDisplayed.bind(this));
|
||||
view.onDisplayed = this.afterDisplayed.bind(this);
|
||||
|
@ -307,37 +300,6 @@ EPUBJS.Rendition.prototype.fill = function(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) {
|
||||
|
||||
if(this.globalLayoutProperties.layout === "pre-paginated") {
|
||||
|
@ -427,7 +389,7 @@ EPUBJS.Rendition.prototype.resize = function(width, height){
|
|||
|
||||
this.updateLayout();
|
||||
|
||||
this.views.forEach(this.resizeView.bind(this));
|
||||
this.views.each(this.resizeView.bind(this));
|
||||
|
||||
this.trigger("resized", {
|
||||
width: this.stage.width,
|
||||
|
@ -453,7 +415,7 @@ EPUBJS.Rendition.prototype.next = function(){
|
|||
|
||||
if(!this.views.length) return;
|
||||
|
||||
next = this.views[0].section.next();
|
||||
next = this.views.last().section.next();
|
||||
|
||||
if(next) {
|
||||
view = this.createView(next);
|
||||
|
@ -473,7 +435,7 @@ EPUBJS.Rendition.prototype.prev = function(){
|
|||
|
||||
if(!this.views.length) return;
|
||||
|
||||
prev = this.views[0].section.prev();
|
||||
prev = this.views.first().section.prev();
|
||||
if(prev) {
|
||||
view = this.createView(prev);
|
||||
return this.append(view);
|
||||
|
@ -530,12 +492,13 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c
|
|||
|
||||
EPUBJS.Rendition.prototype.visible = function(){
|
||||
var container = this.bounds();
|
||||
var displayedViews = this.views.displayed();
|
||||
var visible = [];
|
||||
var isVisible;
|
||||
var view;
|
||||
|
||||
for (var i = 0; i < this.views.length; i++) {
|
||||
view = this.views[i];
|
||||
for (var i = 0; i < displayedViews.length; i++) {
|
||||
view = displayedViews[i];
|
||||
isVisible = this.isVisible(view, 0, 0, container);
|
||||
|
||||
if(isVisible === true) {
|
||||
|
@ -543,24 +506,10 @@ EPUBJS.Rendition.prototype.visible = function(){
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
var bounds;
|
||||
|
||||
|
@ -573,45 +522,11 @@ EPUBJS.Rendition.prototype.bounds = function(func) {
|
|||
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();
|
||||
this.views.clear();
|
||||
|
||||
clearTimeout(this.trimTimeout);
|
||||
if(this.settings.hidden) {
|
||||
|
@ -634,7 +549,7 @@ EPUBJS.Rendition.prototype.currentLocation = function(){
|
|||
var start, end;
|
||||
|
||||
if(this.views.length) {
|
||||
view = this.views[0];
|
||||
view = this.views.first();
|
||||
// start = container.left - view.position().left;
|
||||
// end = start + this.layout.spread;
|
||||
|
||||
|
|
152
src/views.js
Normal file
152
src/views.js
Normal 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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue