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
|
@ -1,31 +1,33 @@
|
|||
EPUBJS.Spine = function(_package, _request){
|
||||
this.items = _package.spine;
|
||||
this.manifest = _package.manifest;
|
||||
this.spineNodeIndex = _package.spineNodeIndex;
|
||||
this.baseUrl = _package.baseUrl || '';
|
||||
EPUBJS.Spine = function(_request){
|
||||
this.request = _request;
|
||||
this.length = this.items.length;
|
||||
this.epubcfi = new EPUBJS.EpubCFI();
|
||||
this.spineItems = [];
|
||||
this.spineByHref = {};
|
||||
this.spineById = {};
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Spine.prototype.load = function(_package) {
|
||||
|
||||
this.items = _package.spine;
|
||||
this.manifest = _package.manifest;
|
||||
this.spineNodeIndex = _package.spineNodeIndex;
|
||||
this.baseUrl = _package.baseUrl || '';
|
||||
this.length = this.items.length;
|
||||
this.epubcfi = new EPUBJS.EpubCFI();
|
||||
|
||||
this.items.forEach(function(item, index){
|
||||
var cfiBase = this.epubcfi.generateChapterComponent(this.spineNodeIndex, item.index, item.idref);
|
||||
var href, url;
|
||||
var manifestItem = this.manifest[item.idref];
|
||||
var spineItem;
|
||||
item.cfiBase = this.epubcfi.generateChapterComponent(this.spineNodeIndex, item.index, item.idref);
|
||||
|
||||
if(manifestItem) {
|
||||
href = manifestItem.href;
|
||||
url = this.baseUrl + href;
|
||||
item.href = manifestItem.href;
|
||||
item.url = this.baseUrl + item.href;
|
||||
}
|
||||
|
||||
spineItem = new EPUBJS.SpineItem(item, href, url, cfiBase);
|
||||
this.spineItems.push(spineItem);
|
||||
|
||||
this.spineByHref[spineItem.href] = index;
|
||||
this.spineById[spineItem.idref] = index;
|
||||
spineItem = new EPUBJS.Section(item);
|
||||
this.append(spineItem);
|
||||
|
||||
|
||||
}.bind(this));
|
||||
|
@ -50,61 +52,42 @@ EPUBJS.Spine.prototype.get = function(target) {
|
|||
return this.spineItems[index];
|
||||
};
|
||||
|
||||
EPUBJS.Spine.prototype.append = function(section) {
|
||||
var index = this.spineItems.length;
|
||||
section.index = index;
|
||||
|
||||
EPUBJS.SpineItem = function(item, href, url, cfiBase){
|
||||
this.idref = item.idref;
|
||||
this.linear = item.linear;
|
||||
this.properties = item.properties;
|
||||
this.index = item.index;
|
||||
this.href = href;
|
||||
this.url = url;
|
||||
this.cfiBase = cfiBase;
|
||||
this.spineItems.push(section);
|
||||
|
||||
this.spineByHref[section.href] = index;
|
||||
this.spineById[section.idref] = index;
|
||||
|
||||
return index;
|
||||
};
|
||||
|
||||
EPUBJS.Spine.prototype.prepend = function(section) {
|
||||
var index = this.spineItems.unshift(section);
|
||||
this.spineByHref[section.href] = 0;
|
||||
this.spineById[section.idref] = 0;
|
||||
|
||||
EPUBJS.SpineItem.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;
|
||||
|
||||
this.replacements(this.document);
|
||||
|
||||
loading.resolve(this.contents);
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
return loaded;
|
||||
};
|
||||
|
||||
EPUBJS.SpineItem.prototype.replacements = function(_document){
|
||||
var base = _document.createElement("base");
|
||||
base.setAttribute("href", this.url);
|
||||
_document.head.insertBefore(base, _document.head.firstChild);
|
||||
};
|
||||
|
||||
EPUBJS.SpineItem.prototype.render = function(){
|
||||
var rendering = new RSVP.defer();
|
||||
var rendered = rendering.promise;
|
||||
|
||||
this.load().then(function(contents){
|
||||
var serializer = new XMLSerializer();
|
||||
var output = serializer.serializeToString(contents);
|
||||
rendering.resolve(output);
|
||||
// Re-index
|
||||
this.spineItems.forEach(function(item, index){
|
||||
item.index = index;
|
||||
});
|
||||
|
||||
return rendered;
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
EPUBJS.SpineItem.prototype.find = function(_query){
|
||||
EPUBJS.Spine.prototype.insert = function(section, index) {
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Spine.prototype.remove = function(section) {
|
||||
var index = this.spineItems.indexOf(section);
|
||||
|
||||
if(index > -1) {
|
||||
delete this.spineByHref[section.href];
|
||||
delete this.spineById[section.idref];
|
||||
|
||||
return this.spineItems.splice(index, 1);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue