mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
89 lines
No EOL
2.1 KiB
JavaScript
89 lines
No EOL
2.1 KiB
JavaScript
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
|
|
var head;
|
|
base.setAttribute("href", this.url);
|
|
|
|
if(_document) {
|
|
head = _document.querySelector("head");
|
|
}
|
|
if(head) {
|
|
head.insertBefore(base, head.firstChild);
|
|
task.resolve();
|
|
} else {
|
|
task.reject(new Error("No head to insert into"));
|
|
}
|
|
|
|
|
|
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){
|
|
|
|
}; |