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

Added error handles, sections, updated url resolving, use book.request for requests

This commit is contained in:
Fred Chasen 2014-08-21 17:39:35 -04:00
parent c6465177c5
commit c29e5f84ee
19 changed files with 3049 additions and 683 deletions

View file

@ -1,57 +1,9 @@
EPUBJS.View = function(width, height) {
EPUBJS.View = function(section) {
this.id = "epubjs-view:" + EPUBJS.core.uuid();
this.loading = new RSVP.defer();
this.loaded = this.loading.promise;
this.rendering = new RSVP.defer();
this.rendered = this.rendering.promise;
this.iframe = this.create();
this.height;
this.width;
};
EPUBJS.View.prototype.load = function(contents) {
var loading = new RSVP.defer();
var loaded = loading.promise;
this.document = this.iframe.contentDocument;
this.iframe.addEventListener("load", function(event) {
var layout;
this.window = this.iframe.contentWindow;
this.document = this.iframe.contentDocument;
this.iframe.style.display = "block";
// Reset Body Styles
this.document.body.style.margin = "0";
this.document.body.style.display = "inline-block";
this.layout();
this.iframe.style.visibility = "visible";
setTimeout(function(){
this.window.addEventListener("resize", this.resized.bind(this), false);
}.bind(this), 10); // Wait to listen for resize events
this.document.fonts.onloading = function(){
console.log("loaded fonts");
// this.layout();
}.bind(this);
// this.observer = this.observe(this.document);
loading.resolve(this);
this.loading.resolve(this);
}.bind(this));
// this.iframe.srcdoc = contents;
this.document.open();
this.document.write(contents);
this.document.close();
return loaded;
this.section = section;
};
EPUBJS.View.prototype.create = function() {
@ -83,9 +35,81 @@ EPUBJS.View.prototype.resized = function(e) {
};
EPUBJS.View.prototype.render = function(_request) {
return this.section.render(_request)
.then(function(contents){
return this.load(contents);
}.bind(this))
.then(this.display.bind(this))
.then(function(){
this.rendering.resolve(this);
}.bind(this));
};
EPUBJS.View.prototype.load = function(contents) {
var loading = new RSVP.defer();
var loaded = loading.promise;
this.document = this.iframe.contentDocument;
this.iframe.addEventListener("load", function(event) {
var layout;
this.window = this.iframe.contentWindow;
this.document = this.iframe.contentDocument;
loading.resolve(this);
}.bind(this));
// this.iframe.srcdoc = contents;
this.document.open();
this.document.write(contents);
this.document.close();
return loaded;
};
EPUBJS.View.prototype.display = function(contents) {
var displaying = new RSVP.defer();
var displayed = displaying.promise;
this.iframe.style.display = "block";
// Reset Body Styles
this.document.body.style.margin = "0";
this.document.body.style.display = "inline-block";
// Set Padding -> TODO: apply these from a function
this.document.body.style.padding = "0 20px 20px 20px";
setTimeout(function(){
this.window.addEventListener("resize", this.resized.bind(this), false);
}.bind(this), 10); // Wait to listen for resize events
if(this.document.fonts.status !== "loading") {
this.layout();
displaying.resolve(this);
} else {
this.document.fonts.onloading = function(){
this.layout();
displaying.resolve(this);
}.bind(this);
}
// this.observer = this.observe(this.document);
return displayed
};
EPUBJS.View.prototype.layout = function() {
var bounds;
console.log("layout")
// Check bounds
bounds = this.document.body.getBoundingClientRect();
@ -93,13 +117,8 @@ EPUBJS.View.prototype.layout = function() {
console.error("View not shown");
}
// Apply Changes
this.resizing = true;
this.iframe.style.height = bounds.height + "px";
// this.iframe.style.width = bounds.width + "px";
// Check again
bounds = this.document.body.getBoundingClientRect();
// Apply Changes
this.resizing = true;
this.iframe.style.height = bounds.height + "px";
// this.iframe.style.width = bounds.width + "px";
@ -142,10 +161,20 @@ EPUBJS.View.prototype.bounds = function() {
return this.iframe.getBoundingClientRect();
};
EPUBJS.View.prototype.show = function() {
this.iframe.style.display = "block";
this.iframe.style.visibility = "visible";
};
EPUBJS.View.prototype.hide = function() {
this.iframe.style.display = "none";
this.iframe.style.visibility = "hidden";
};
EPUBJS.View.prototype.destroy = function() {
// Stop observing
// this.observer.disconnect();
this.element.removeChild(this.iframe);
};