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:
parent
888b655104
commit
2c01da9259
11 changed files with 190 additions and 92 deletions
|
@ -1890,7 +1890,7 @@ EPUBJS.Book.prototype.open = function(bookPath, forceReload){
|
||||||
// return; //-- TODO: this need to be fixed and tested before enabling
|
// return; //-- TODO: this need to be fixed and tested before enabling
|
||||||
epubpackage = this.unarchive(bookPath).
|
epubpackage = this.unarchive(bookPath).
|
||||||
then(function(){
|
then(function(){
|
||||||
return this.loadPackage();
|
return book.loadPackage();
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -2756,13 +2756,63 @@ EPUBJS.core.toArray = function(obj) {
|
||||||
return arr;
|
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
|
//-- Parse out the folder
|
||||||
EPUBJS.core.folder = function(url){
|
EPUBJS.core.folder = function(url){
|
||||||
|
|
||||||
var slash = url.lastIndexOf('/'),
|
var lastSlash = url.lastIndexOf('/');
|
||||||
folder = url.slice(0, slash + 1);
|
|
||||||
|
|
||||||
if(slash == -1) folder = '';
|
if(lastSlash == -1) folder = '';
|
||||||
|
|
||||||
|
folder = url.slice(0, lastSlash + 1);
|
||||||
|
|
||||||
return folder;
|
return folder;
|
||||||
|
|
||||||
|
@ -3413,21 +3463,13 @@ EPUBJS.Parser.prototype.toc = function(tocXml, spineIndexByURL, bookSpine){
|
||||||
function getTOC(parent){
|
function getTOC(parent){
|
||||||
var list = [],
|
var list = [],
|
||||||
items = [],
|
items = [],
|
||||||
nodes = parent.childNodes,
|
nodes = parent.querySelectorAll("navPoint"),
|
||||||
nodesArray = Array.prototype.slice.call(nodes),
|
items = Array.prototype.slice.call(nodes).reverse(),
|
||||||
length = nodesArray.length,
|
length = items.length,
|
||||||
iter = length,
|
iter = length,
|
||||||
node;
|
node;
|
||||||
|
|
||||||
|
if(length === 0) return [];
|
||||||
if(length === 0) return false;
|
|
||||||
|
|
||||||
while(iter--){
|
|
||||||
node = nodesArray[iter];
|
|
||||||
if(node.nodeName === "navPoint") {
|
|
||||||
items.push(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
items.forEach(function(item){
|
items.forEach(function(item){
|
||||||
var id = item.getAttribute('id') || false,
|
var id = item.getAttribute('id') || false,
|
||||||
|
@ -3677,6 +3719,7 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
|
||||||
|
|
||||||
renderer.trigger("renderer:chapterDisplayed", msg);
|
renderer.trigger("renderer:chapterDisplayed", msg);
|
||||||
renderer.book.trigger("renderer:chapterDisplayed", msg);
|
renderer.book.trigger("renderer:chapterDisplayed", msg);
|
||||||
|
renderer.book.trigger("renderer:pageChanged", renderer.currentLocationCfi);
|
||||||
|
|
||||||
renderer.visible(true);
|
renderer.visible(true);
|
||||||
|
|
||||||
|
@ -3906,7 +3949,8 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
|
||||||
_newUrls = {},
|
_newUrls = {},
|
||||||
_store = this.determineStore(),
|
_store = this.determineStore(),
|
||||||
_cache = this.caches[query],
|
_cache = this.caches[query],
|
||||||
_contentsPath = this.book.settings.contentsPath,
|
_uri = EPUBJS.core.uri(this.book.chapter.absolute),
|
||||||
|
_chapterBase = _uri.base,
|
||||||
_attr = attr,
|
_attr = attr,
|
||||||
progress = function(url, full, count) {
|
progress = function(url, full, count) {
|
||||||
_newUrls[full] = url;
|
_newUrls[full] = url;
|
||||||
|
@ -3928,17 +3972,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
|
||||||
_oldUrls = _.clone(_cache);
|
_oldUrls = _.clone(_cache);
|
||||||
|
|
||||||
this.replace(query, function(link, done){
|
this.replace(query, function(link, done){
|
||||||
|
|
||||||
var src = link.getAttribute(_attr),
|
var src = link.getAttribute(_attr),
|
||||||
full = EPUBJS.core.resolveUrl(_contentsPath, src),
|
full = EPUBJS.core.resolveUrl(_chapterBase, src);
|
||||||
replaceUrl = function(url) {
|
|
||||||
|
var replaceUrl = function(url) {
|
||||||
link.setAttribute(_attr, url);
|
link.setAttribute(_attr, url);
|
||||||
link.onload = function(){
|
link.onload = function(){
|
||||||
done(url, full);
|
done(url, full);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
if(full in _oldUrls){
|
if(full in _oldUrls){
|
||||||
replaceUrl(_oldUrls[full]);
|
replaceUrl(_oldUrls[full]);
|
||||||
_newUrls[full] = _oldUrls[full];
|
_newUrls[full] = _oldUrls[full];
|
||||||
|
@ -3981,7 +4024,7 @@ EPUBJS.Renderer.prototype.replaceLinks = function(callback){
|
||||||
|
|
||||||
|
|
||||||
EPUBJS.Renderer.prototype.page = function(pg){
|
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.chapterPos = pg;
|
||||||
this.leftPos = this.spreadWidth * (pg-1); //-- pages start at 1
|
this.leftPos = this.spreadWidth * (pg-1); //-- pages start at 1
|
||||||
this.setLeft(this.leftPos);
|
this.setLeft(this.leftPos);
|
||||||
|
@ -4302,7 +4345,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
||||||
var entry = this.zipFs.find(url);
|
var entry = this.zipFs.find(url);
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
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) {
|
if(url in this.urlCache) {
|
||||||
deferred.resolve(this.urlCache[url]);
|
deferred.resolve(this.urlCache[url]);
|
||||||
|
|
4
build/epub.min.js
vendored
4
build/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -32,6 +32,7 @@ EPUBJS.Reader = function(path, _options) {
|
||||||
|
|
||||||
this.settings = _.defaults(_options || {}, {
|
this.settings = _.defaults(_options || {}, {
|
||||||
restore: true,
|
restore: true,
|
||||||
|
reload: false,
|
||||||
bookmarks: null
|
bookmarks: null
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -43,7 +44,8 @@ EPUBJS.Reader = function(path, _options) {
|
||||||
|
|
||||||
this.book = book = new EPUBJS.Book({
|
this.book = book = new EPUBJS.Book({
|
||||||
bookPath: path,
|
bookPath: path,
|
||||||
restore: this.settings.restore
|
restore: this.settings.restore,
|
||||||
|
reload: this.settings.reload
|
||||||
});
|
});
|
||||||
|
|
||||||
if(this.settings.previousLocationCfi) {
|
if(this.settings.previousLocationCfi) {
|
||||||
|
@ -540,7 +542,7 @@ EPUBJS.reader.TocController = function(toc) {
|
||||||
|
|
||||||
listitem.appendChild(link);
|
listitem.appendChild(link);
|
||||||
|
|
||||||
if(chapter.subitems) {
|
if(chapter.subitems.length > 0) {
|
||||||
level++;
|
level++;
|
||||||
subitems = generateTocItems(chapter.subitems, level);
|
subitems = generateTocItems(chapter.subitems, level);
|
||||||
toggle.classList.add('toc_toggle');
|
toggle.classList.add('toc_toggle');
|
||||||
|
|
|
@ -56,6 +56,7 @@
|
||||||
<script src="../src/book.js"></script>
|
<script src="../src/book.js"></script>
|
||||||
<script src="../src/chapter.js"></script>
|
<script src="../src/chapter.js"></script>
|
||||||
<script src="../src/renderer.js"></script>
|
<script src="../src/renderer.js"></script>
|
||||||
|
<script src="../src/replace.js"></script>
|
||||||
<script src="../src/epubcfi.js"></script>
|
<script src="../src/epubcfi.js"></script>
|
||||||
|
|
||||||
<!-- Hooks -->
|
<!-- Hooks -->
|
||||||
|
|
4
demo/js/epub.min.js
vendored
4
demo/js/epub.min.js
vendored
File diff suppressed because one or more lines are too long
2
demo/js/reader.min.js
vendored
2
demo/js/reader.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -32,6 +32,7 @@ EPUBJS.Reader = function(path, _options) {
|
||||||
|
|
||||||
this.settings = _.defaults(_options || {}, {
|
this.settings = _.defaults(_options || {}, {
|
||||||
restore: true,
|
restore: true,
|
||||||
|
reload: false,
|
||||||
bookmarks: null
|
bookmarks: null
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -43,7 +44,8 @@ EPUBJS.Reader = function(path, _options) {
|
||||||
|
|
||||||
this.book = book = new EPUBJS.Book({
|
this.book = book = new EPUBJS.Book({
|
||||||
bookPath: path,
|
bookPath: path,
|
||||||
restore: this.settings.restore
|
restore: this.settings.restore,
|
||||||
|
reload: this.settings.reload
|
||||||
});
|
});
|
||||||
|
|
||||||
if(this.settings.previousLocationCfi) {
|
if(this.settings.previousLocationCfi) {
|
||||||
|
|
|
@ -117,7 +117,7 @@ EPUBJS.Book.prototype.open = function(bookPath, forceReload){
|
||||||
// return; //-- TODO: this need to be fixed and tested before enabling
|
// return; //-- TODO: this need to be fixed and tested before enabling
|
||||||
epubpackage = this.unarchive(bookPath).
|
epubpackage = this.unarchive(bookPath).
|
||||||
then(function(){
|
then(function(){
|
||||||
return this.loadPackage();
|
return book.loadPackage();
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
56
src/core.js
56
src/core.js
|
@ -96,13 +96,63 @@ EPUBJS.core.toArray = function(obj) {
|
||||||
return arr;
|
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
|
//-- Parse out the folder
|
||||||
EPUBJS.core.folder = function(url){
|
EPUBJS.core.folder = function(url){
|
||||||
|
|
||||||
var slash = url.lastIndexOf('/'),
|
var lastSlash = url.lastIndexOf('/');
|
||||||
folder = url.slice(0, slash + 1);
|
|
||||||
|
|
||||||
if(slash == -1) folder = '';
|
if(lastSlash == -1) folder = '';
|
||||||
|
|
||||||
|
folder = url.slice(0, lastSlash + 1);
|
||||||
|
|
||||||
return folder;
|
return folder;
|
||||||
|
|
||||||
|
|
|
@ -439,7 +439,8 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
|
||||||
_newUrls = {},
|
_newUrls = {},
|
||||||
_store = this.determineStore(),
|
_store = this.determineStore(),
|
||||||
_cache = this.caches[query],
|
_cache = this.caches[query],
|
||||||
_contentsPath = this.book.settings.contentsPath,
|
_uri = EPUBJS.core.uri(this.book.chapter.absolute),
|
||||||
|
_chapterBase = _uri.base,
|
||||||
_attr = attr,
|
_attr = attr,
|
||||||
progress = function(url, full, count) {
|
progress = function(url, full, count) {
|
||||||
_newUrls[full] = url;
|
_newUrls[full] = url;
|
||||||
|
@ -461,17 +462,16 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
|
||||||
_oldUrls = _.clone(_cache);
|
_oldUrls = _.clone(_cache);
|
||||||
|
|
||||||
this.replace(query, function(link, done){
|
this.replace(query, function(link, done){
|
||||||
|
|
||||||
var src = link.getAttribute(_attr),
|
var src = link.getAttribute(_attr),
|
||||||
full = EPUBJS.core.resolveUrl(_contentsPath, src),
|
full = EPUBJS.core.resolveUrl(_chapterBase, src);
|
||||||
replaceUrl = function(url) {
|
|
||||||
|
var replaceUrl = function(url) {
|
||||||
link.setAttribute(_attr, url);
|
link.setAttribute(_attr, url);
|
||||||
link.onload = function(){
|
link.onload = function(){
|
||||||
done(url, full);
|
done(url, full);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
if(full in _oldUrls){
|
if(full in _oldUrls){
|
||||||
replaceUrl(_oldUrls[full]);
|
replaceUrl(_oldUrls[full]);
|
||||||
_newUrls[full] = _oldUrls[full];
|
_newUrls[full] = _oldUrls[full];
|
||||||
|
|
|
@ -52,7 +52,7 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
||||||
var entry = this.zipFs.find(url);
|
var entry = this.zipFs.find(url);
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
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) {
|
if(url in this.urlCache) {
|
||||||
deferred.resolve(this.urlCache[url]);
|
deferred.resolve(this.urlCache[url]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue