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

121 lines
2.9 KiB
JavaScript

var RSVP = require('rsvp');
var core = require('./core');
var EpubCFI = require('./epubcfi');
var Section = require('./section');
function Spine(_request){
this.request = _request;
this.spineItems = [];
this.spineByHref = {};
this.spineById = {};
};
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 EpubCFI();
this.items.forEach(function(item, index){
var href, url;
var manifestItem = this.manifest[item.idref];
var spineItem;
item.cfiBase = this.epubcfi.generateChapterComponent(this.spineNodeIndex, item.index, item.idref);
if(manifestItem) {
item.href = manifestItem.href;
item.url = this.baseUrl + item.href;
if(manifestItem.properties.length){
item.properties.push.apply(item.properties, manifestItem.properties);
}
}
// if(index > 0) {
item.prev = function(){ return this.get(index-1); }.bind(this);
// }
// if(index+1 < this.items.length) {
item.next = function(){ return this.get(index+1); }.bind(this);
// }
spineItem = new Section(item);
this.append(spineItem);
}.bind(this));
};
// book.spine.get();
// book.spine.get(1);
// book.spine.get("chap1.html");
// book.spine.get("#id1234");
Spine.prototype.get = function(target) {
var index = 0;
if(this.epubcfi.isCfiString(target)) {
cfi = this.epubcfi.parse(target);
index = cfi.spinePos;
} else if(target && (typeof target === "number" || isNaN(target) === false)){
index = target;
} else if(target && target.indexOf("#") === 0) {
index = this.spineById[target.substring(1)];
} else if(target) {
// Remove fragments
target = target.split("#")[0];
index = this.spineByHref[target];
}
return this.spineItems[index] || null;
};
Spine.prototype.append = function(section) {
var index = this.spineItems.length;
section.index = index;
this.spineItems.push(section);
this.spineByHref[section.href] = index;
this.spineById[section.idref] = index;
return index;
};
Spine.prototype.prepend = function(section) {
var index = this.spineItems.unshift(section);
this.spineByHref[section.href] = 0;
this.spineById[section.idref] = 0;
// Re-index
this.spineItems.forEach(function(item, index){
item.index = index;
});
return 0;
};
Spine.prototype.insert = function(section, index) {
};
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);
}
};
Spine.prototype.each = function() {
return this.spineItems.forEach.apply(this.spineItems, arguments);
};
module.exports = Spine;