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:
parent
c6465177c5
commit
c29e5f84ee
19 changed files with 3049 additions and 683 deletions
81
lib/epubjs/section.js
Normal file
81
lib/epubjs/section.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
EPUBJS.Section = function(item){
|
||||
this.idref = item.idref;
|
||||
this.linear = item.linear;
|
||||
this.properties = item.properties;
|
||||
this.index = item.index;
|
||||
this.href = item.href;
|
||||
this.url = item.url;
|
||||
this.cfiBase = item.cfiBase;
|
||||
|
||||
this.hooks = {};
|
||||
this.hooks.replacements = new EPUBJS.Hook(this);
|
||||
|
||||
// Register replacements
|
||||
this.hooks.replacements.register(this.replacements);
|
||||
};
|
||||
|
||||
|
||||
EPUBJS.Section.prototype.load = function(_request){
|
||||
var request = _request || this.request || EPUBJS.core.request;
|
||||
var loading = new RSVP.defer();
|
||||
var loaded = loading.promise;
|
||||
|
||||
if(this.contents) {
|
||||
loading.resolve(this.contents);
|
||||
} else {
|
||||
request(this.url, 'xml')
|
||||
.then(function(xml){
|
||||
var base;
|
||||
var directory = EPUBJS.core.folder(this.url);
|
||||
|
||||
this.document = xml;
|
||||
this.contents = xml.documentElement;
|
||||
|
||||
return this.hooks.replacements.trigger(this.document);
|
||||
}.bind(this))
|
||||
.then(function(){
|
||||
loading.resolve(this.contents);
|
||||
}.bind(this))
|
||||
.catch(function(error){
|
||||
loading.reject(error);
|
||||
});
|
||||
}
|
||||
|
||||
return loaded;
|
||||
};
|
||||
|
||||
EPUBJS.Section.prototype.replacements = function(_document){
|
||||
var task = new RSVP.defer();
|
||||
var base = _document.createElement("base"); // TODO: check if exists
|
||||
|
||||
base.setAttribute("href", this.url);
|
||||
_document.head.insertBefore(base, _document.head.firstChild);
|
||||
|
||||
task.resolve();
|
||||
|
||||
return task.promise;
|
||||
};
|
||||
|
||||
EPUBJS.Section.prototype.beforeSectionLoad = function(){
|
||||
// Stub for a hook - replace me for now
|
||||
}
|
||||
|
||||
EPUBJS.Section.prototype.render = function(_request){
|
||||
var rendering = new RSVP.defer();
|
||||
var rendered = rendering.promise;
|
||||
|
||||
this.load(_request).then(function(contents){
|
||||
var serializer = new XMLSerializer();
|
||||
var output = serializer.serializeToString(contents);
|
||||
rendering.resolve(output);
|
||||
})
|
||||
.catch(function(error){
|
||||
rendering.reject(error);
|
||||
});
|
||||
|
||||
return rendered;
|
||||
};
|
||||
|
||||
EPUBJS.Section.prototype.find = function(_query){
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue