1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

Added URI method, check chapter base on replace

This commit is contained in:
Fred Chasen 2014-01-06 22:18:47 -08:00
parent 888b655104
commit 2c01da9259
11 changed files with 190 additions and 92 deletions

View file

@ -1890,7 +1890,7 @@ EPUBJS.Book.prototype.open = function(bookPath, forceReload){
// return; //-- TODO: this need to be fixed and tested before enabling
epubpackage = this.unarchive(bookPath).
then(function(){
return this.loadPackage();
return book.loadPackage();
});
} else {
@ -2756,13 +2756,63 @@ EPUBJS.core.toArray = function(obj) {
return arr;
};
//-- Parse out the origin
EPUBJS.core.uri = function(url){
var uri = {
protocol : '',
host : '',
path : '',
origin : '',
directory : '',
base : '',
filename : ''
},
doubleSlash = url.indexOf('://'),
search = url.indexOf('?'),
firstSlash;
if(search != -1) {
uri.search = url.slice(search + 1);
url = url.slice(0, search);
}
if(doubleSlash != -1) {
uri.protocol = url.slice(0, doubleSlash);
uri.path = url.slice(doubleSlash+3);
firstSlash = uri.path.indexOf('/');
if(firstSlash === -1) {
uri.host = uri.path;
} else {
uri.host = uri.path.slice(0, firstSlash);
}
uri.origin = uri.protocol + "://" + uri.host;
uri.directory = EPUBJS.core.folder(uri.path.replace(uri.host, ''));
uri.base = uri.origin + uri.directory;
// return origin;
} else {
uri.path = url;
uri.directory = EPUBJS.core.folder(url);
uri.base = uri.directory;
}
//-- Filename
uri.filename = url.replace(uri.base, '');
return uri;
};
//-- Parse out the folder
EPUBJS.core.folder = function(url){
var slash = url.lastIndexOf('/'),
folder = url.slice(0, slash + 1);
var lastSlash = url.lastIndexOf('/');
if(slash == -1) folder = '';
if(lastSlash == -1) folder = '';
folder = url.slice(0, lastSlash + 1);
return folder;
@ -3413,21 +3463,13 @@ EPUBJS.Parser.prototype.toc = function(tocXml, spineIndexByURL, bookSpine){
function getTOC(parent){
var list = [],
items = [],
nodes = parent.childNodes,
nodesArray = Array.prototype.slice.call(nodes),
length = nodesArray.length,
nodes = parent.querySelectorAll("navPoint"),
items = Array.prototype.slice.call(nodes).reverse(),
length = items.length,
iter = length,
node;
if(length === 0) return false;
while(iter--){
node = nodesArray[iter];
if(node.nodeName === "navPoint") {
items.push(node);
}
}
if(length === 0) return [];
items.forEach(function(item){
var id = item.getAttribute('id') || false,
@ -3677,6 +3719,7 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
renderer.trigger("renderer:chapterDisplayed", msg);
renderer.book.trigger("renderer:chapterDisplayed", msg);
renderer.book.trigger("renderer:pageChanged", renderer.currentLocationCfi);
renderer.visible(true);
@ -3906,7 +3949,8 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_newUrls = {},
_store = this.determineStore(),
_cache = this.caches[query],
_contentsPath = this.book.settings.contentsPath,
_uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base,
_attr = attr,
progress = function(url, full, count) {
_newUrls[full] = url;
@ -3928,17 +3972,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_oldUrls = _.clone(_cache);
this.replace(query, function(link, done){
var src = link.getAttribute(_attr),
full = EPUBJS.core.resolveUrl(_contentsPath, src),
replaceUrl = function(url) {
full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) {
link.setAttribute(_attr, url);
link.onload = function(){
done(url, full);
};
};
if(full in _oldUrls){
replaceUrl(_oldUrls[full]);
_newUrls[full] = _oldUrls[full];
@ -3981,7 +4024,7 @@ EPUBJS.Renderer.prototype.replaceLinks = function(callback){
EPUBJS.Renderer.prototype.page = function(pg){
if(pg >= 1 && pg <= this.displayedPages){
if(pg >= 1 && pg <= this.displayedPages && pg != this.chapterPos){
this.chapterPos = pg;
this.leftPos = this.spreadWidth * (pg-1); //-- pages start at 1
this.setLeft(this.leftPos);
@ -4302,7 +4345,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var entry = this.zipFs.find(url);
var _URL = window.URL || window.webkitURL || window.mozURL;
if(!entry) console.error(url);
if(!entry) console.error("File not found:", url);
if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]);

4
build/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -32,6 +32,7 @@ EPUBJS.Reader = function(path, _options) {
this.settings = _.defaults(_options || {}, {
restore: true,
reload: false,
bookmarks: null
});
@ -43,7 +44,8 @@ EPUBJS.Reader = function(path, _options) {
this.book = book = new EPUBJS.Book({
bookPath: path,
restore: this.settings.restore
restore: this.settings.restore,
reload: this.settings.reload
});
if(this.settings.previousLocationCfi) {
@ -540,7 +542,7 @@ EPUBJS.reader.TocController = function(toc) {
listitem.appendChild(link);
if(chapter.subitems) {
if(chapter.subitems.length > 0) {
level++;
subitems = generateTocItems(chapter.subitems, level);
toggle.classList.add('toc_toggle');

View file

@ -56,6 +56,7 @@
<script src="../src/book.js"></script>
<script src="../src/chapter.js"></script>
<script src="../src/renderer.js"></script>
<script src="../src/replace.js"></script>
<script src="../src/epubcfi.js"></script>
<!-- Hooks -->

4
demo/js/epub.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -32,6 +32,7 @@ EPUBJS.Reader = function(path, _options) {
this.settings = _.defaults(_options || {}, {
restore: true,
reload: false,
bookmarks: null
});
@ -43,7 +44,8 @@ EPUBJS.Reader = function(path, _options) {
this.book = book = new EPUBJS.Book({
bookPath: path,
restore: this.settings.restore
restore: this.settings.restore,
reload: this.settings.reload
});
if(this.settings.previousLocationCfi) {

View file

@ -117,7 +117,7 @@ EPUBJS.Book.prototype.open = function(bookPath, forceReload){
// return; //-- TODO: this need to be fixed and tested before enabling
epubpackage = this.unarchive(bookPath).
then(function(){
return this.loadPackage();
return book.loadPackage();
});
} else {

View file

@ -96,13 +96,63 @@ EPUBJS.core.toArray = function(obj) {
return arr;
};
//-- Parse out the origin
EPUBJS.core.uri = function(url){
var uri = {
protocol : '',
host : '',
path : '',
origin : '',
directory : '',
base : '',
filename : ''
},
doubleSlash = url.indexOf('://'),
search = url.indexOf('?'),
firstSlash;
if(search != -1) {
uri.search = url.slice(search + 1);
url = url.slice(0, search);
}
if(doubleSlash != -1) {
uri.protocol = url.slice(0, doubleSlash);
uri.path = url.slice(doubleSlash+3);
firstSlash = uri.path.indexOf('/');
if(firstSlash === -1) {
uri.host = uri.path;
} else {
uri.host = uri.path.slice(0, firstSlash);
}
uri.origin = uri.protocol + "://" + uri.host;
uri.directory = EPUBJS.core.folder(uri.path.replace(uri.host, ''));
uri.base = uri.origin + uri.directory;
// return origin;
} else {
uri.path = url;
uri.directory = EPUBJS.core.folder(url);
uri.base = uri.directory;
}
//-- Filename
uri.filename = url.replace(uri.base, '');
return uri;
};
//-- Parse out the folder
EPUBJS.core.folder = function(url){
var slash = url.lastIndexOf('/'),
folder = url.slice(0, slash + 1);
var lastSlash = url.lastIndexOf('/');
if(slash == -1) folder = '';
if(lastSlash == -1) folder = '';
folder = url.slice(0, lastSlash + 1);
return folder;

View file

@ -439,7 +439,8 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_newUrls = {},
_store = this.determineStore(),
_cache = this.caches[query],
_contentsPath = this.book.settings.contentsPath,
_uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base,
_attr = attr,
progress = function(url, full, count) {
_newUrls[full] = url;
@ -461,17 +462,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
_oldUrls = _.clone(_cache);
this.replace(query, function(link, done){
var src = link.getAttribute(_attr),
full = EPUBJS.core.resolveUrl(_contentsPath, src),
replaceUrl = function(url) {
full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) {
link.setAttribute(_attr, url);
link.onload = function(){
done(url, full);
};
};
if(full in _oldUrls){
replaceUrl(_oldUrls[full]);
_newUrls[full] = _oldUrls[full];

View file

@ -52,7 +52,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var entry = this.zipFs.find(url);
var _URL = window.URL || window.webkitURL || window.mozURL;
if(!entry) console.error(url);
if(!entry) console.error("File not found:", url);
if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]);