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 // 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,14 +2756,64 @@ 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(lastSlash == -1) folder = '';
if(slash == -1) folder = '';
folder = url.slice(0, lastSlash + 1);
return folder; return folder;
}; };
@ -3413,22 +3463,14 @@ 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,
content = item.querySelector("content"), content = item.querySelector("content"),
@ -3443,7 +3485,7 @@ EPUBJS.Parser.prototype.toc = function(tocXml, spineIndexByURL, bookSpine){
if(!id) { if(!id) {
if(spinePos) { if(spinePos) {
spineItem = bookSpine[spinePos]; spineItem = bookSpine[spinePos];
id = spineItem.id id = spineItem.id
} else { } else {
id = 'epubjs-autogen-toc-id-' + (idCounter++); id = 'epubjs-autogen-toc-id-' + (idCounter++);
@ -3463,7 +3505,7 @@ EPUBJS.Parser.prototype.toc = function(tocXml, spineIndexByURL, bookSpine){
return list; return list;
} }
return getTOC(navMap); return getTOC(navMap);
}; };
EPUBJS.Renderer = function(book) { EPUBJS.Renderer = function(book) {
@ -3677,7 +3719,8 @@ 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);
}); });
@ -3894,7 +3937,7 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
} }
resources.forEach(function(item){ resources.forEach(function(item){
func(item, after); func(item, after);
}.bind(this)); }.bind(this));
@ -3903,24 +3946,25 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) { EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) {
var _oldUrls, var _oldUrls,
_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),
_attr = attr, _chapterBase = _uri.base,
progress = function(url, full, count) { _attr = attr,
_newUrls[full] = url; progress = function(url, full, count) {
}, _newUrls[full] = url;
finished = function(notempty) { },
finished = function(notempty) {
if(callback) callback();
_.each(_oldUrls, function(url){ if(callback) callback();
_store.revokeUrl(url);
}); _.each(_oldUrls, function(url){
_store.revokeUrl(url);
_cache = _newUrls; });
};
_cache = _newUrls;
};
if(!_store) return; if(!_store) return;
@ -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) {
link.setAttribute(_attr, url); var replaceUrl = function(url) {
link.onload = function(){ link.setAttribute(_attr, url);
done(url, full); link.onload = function(){
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

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 || {}, { 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');

View file

@ -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

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 || {}, { 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) {

View file

@ -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 {

View file

@ -96,14 +96,64 @@ 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(lastSlash == -1) folder = '';
if(slash == -1) folder = '';
folder = url.slice(0, lastSlash + 1);
return folder; return folder;
}; };

View file

@ -427,7 +427,7 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
} }
resources.forEach(function(item){ resources.forEach(function(item){
func(item, after); func(item, after);
}.bind(this)); }.bind(this));
@ -436,24 +436,25 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) { EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) {
var _oldUrls, var _oldUrls,
_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),
_attr = attr, _chapterBase = _uri.base,
progress = function(url, full, count) { _attr = attr,
_newUrls[full] = url; progress = function(url, full, count) {
}, _newUrls[full] = url;
finished = function(notempty) { },
finished = function(notempty) {
if(callback) callback();
_.each(_oldUrls, function(url){ if(callback) callback();
_store.revokeUrl(url);
}); _.each(_oldUrls, function(url){
_store.revokeUrl(url);
_cache = _newUrls; });
};
_cache = _newUrls;
};
if(!_store) return; if(!_store) return;
@ -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) {
link.setAttribute(_attr, url); var replaceUrl = function(url) {
link.onload = function(){ link.setAttribute(_attr, url);
done(url, full); link.onload = function(){
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];

View file

@ -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]);