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

File Split

This commit is contained in:
Fred Chasen 2014-01-11 06:07:38 -08:00
parent 80051ad8b3
commit a5d970bca2
4 changed files with 395 additions and 391 deletions

127
src/content.js Normal file
View file

@ -0,0 +1,127 @@
EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
var items = this.doc.querySelectorAll(query),
resources = Array.prototype.slice.call(items),
count = resources.length,
after = function(result, full){
count--;
if(progress) progress(result, full, count);
if(count <= 0 && finished) finished(true);
};
if(count === 0) {
finished(false);
return;
}
resources.forEach(function(item){
func(item, after);
}.bind(this));
};
EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) {
var _oldUrls,
_newUrls = {},
_store = this.determineStore(),
_cache = this.caches[query],
_uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base,
_attr = attr,
_wait = 2000,
progress = function(url, full, count) {
_newUrls[full] = url;
},
finished = function(notempty) {
if(callback) callback();
_.each(_oldUrls, function(url){
_store.revokeUrl(url);
});
_cache = _newUrls;
};
if(!_store) return;
if(!_cache) _cache = {};
_oldUrls = _.clone(_cache);
this.replace(query, function(link, done){
var src = link.getAttribute(_attr),
full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) {
var timeout;
link.onload = function(){
clearTimeout(timeout);
done(url, full);
};
link.onerror = function(e){
clearTimeout(timeout);
done(url, full);
console.error(e);
};
if(query == "image") {
//-- SVG needs this to trigger a load event
link.setAttribute("externalResourcesRequired", "true");
}
if(query == "link[href]") {
//-- Only Stylesheet links seem to have a load events, just continue others
done(url, full);
}
link.setAttribute(_attr, url);
//-- If elements never fire Load Event, should continue anyways
timeout = setTimeout(function(){
done(url, full);
}, _wait);
};
if(full in _oldUrls){
replaceUrl(_oldUrls[full]);
_newUrls[full] = _oldUrls[full];
delete _oldUrls[full];
}else{
func(_store, full, replaceUrl, link);
}
}, finished, progress);
};
//-- Replaces the relative links within the book to use our internal page changer
EPUBJS.Renderer.prototype.replaceLinks = function(callback){
var renderer = this;
this.replace("a[href]", function(link, done){
var href = link.getAttribute("href"),
relative = href.search("://"),
fragment = href[0] == "#";
if(relative != -1){
link.setAttribute("target", "_blank");
}else{
link.onclick = function(){
renderer.book.goto(href);
return false;
};
}
done();
}, callback);
};

72
src/layout.js Normal file
View file

@ -0,0 +1,72 @@
EPUBJS.Renderer.prototype.formatSpread = function(){
var divisor = 2,
cutoff = 800;
//-- Check the width and decied on columns
//-- Todo: a better place for this?
this.elWidth = this.iframe.clientWidth;
if(this.elWidth % 2 !== 0){
this.elWidth -= 1;
}
// this.gap = this.gap || Math.ceil(this.elWidth / 8);
this.gap = Math.ceil(this.elWidth / 8);
if(this.gap % 2 !== 0){
this.gap += 1;
}
if(this.elWidth < cutoff || !this.book.settings.spreads) {
this.spread = false; //-- Single Page
divisor = 1;
this.colWidth = Math.floor(this.elWidth / divisor);
}else{
this.spread = true; //-- Double Page
this.colWidth = Math.floor((this.elWidth - this.gap) / divisor);
}
this.spreadWidth = (this.colWidth + this.gap) * divisor;
// if(this.bodyEl) this.bodyEl.style.margin = 0;
// this.bodyEl.style.fontSize = localStorage.getItem("fontSize") || "medium";
//-- Clear Margins
if(this.bodyEl) this.bodyEl.style.margin = "0";
this.docEl.style.overflow = "hidden";
this.docEl.style.width = this.elWidth + "px";
//-- Adjust height
this.docEl.style.height = this.iframe.clientHeight + "px";
//-- Add columns
this.docEl.style[EPUBJS.Renderer.columnAxis] = "horizontal";
this.docEl.style[EPUBJS.Renderer.columnGap] = this.gap+"px";
this.docEl.style[EPUBJS.Renderer.columnWidth] = this.colWidth+"px";
};
EPUBJS.Renderer.prototype.fixedLayout = function(){
this.paginated = false;
this.elWidth = this.iframe.width;
this.docEl.style.width = this.elWidth;
// this.setLeft(0);
this.docEl.style.width = this.elWidth;
//-- Adjust height
this.docEl.style.height = "auto";
//-- Remove columns
// this.docEl.style[EPUBJS.core.columnWidth] = "auto";
//-- Scroll
this.docEl.style.overflow = "auto";
this.iframe.scrolling = "yes";
// this.displayedPages = 1;
};

196
src/navigation.js Normal file
View file

@ -0,0 +1,196 @@
EPUBJS.Renderer.prototype.page = function(pg){
if(pg >= 1 && pg <= this.displayedPages){
this.chapterPos = pg;
this.leftPos = this.spreadWidth * (pg-1); //-- pages start at 1
this.setLeft(this.leftPos);
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
// localStorage.setItem("chapterPos", pg);
return true;
}
//-- Return false if page is greater than the total
return false;
};
EPUBJS.Renderer.prototype.nextPage = function(){
if(this.chapterPos < this.displayedPages){
this.chapterPos++;
this.leftPos += this.spreadWidth;
this.setLeft(this.leftPos);
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
}else{
return false;
}
};
EPUBJS.Renderer.prototype.prevPage = function(){
if(this.chapterPos > 1){
this.chapterPos--;
this.leftPos -= this.spreadWidth;
this.setLeft(this.leftPos);
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
}else{
return false;
}
};
EPUBJS.Renderer.prototype.gotoChapterEnd = function(){
this.chapterEnd();
};
EPUBJS.Renderer.prototype.chapterEnd = function(){
this.page(this.displayedPages);
};
//-- Find a section by fragement id
EPUBJS.Renderer.prototype.section = function(fragment){
var el = this.doc.getElementById(fragment),
left, pg;
if(el){
this.pageByElement(el);
}
};
//-- Show the page containing an Element
EPUBJS.Renderer.prototype.pageByElement = function(el){
var left, pg;
if(!el) return;
left = this.leftPos + el.getBoundingClientRect().left; //-- Calculate left offset compaired to scrolled position
pg = Math.floor(left / this.spreadWidth) + 1; //-- pages start at 1
this.page(pg);
};
EPUBJS.Renderer.prototype.walk = function(node) {
var r, children, leng,
startNode = node,
prevNode,
stack = [startNode];
var STOP = 10000, ITER=0;
while(!r && stack.length) {
node = stack.shift();
if( this.isElementVisible(node) ) {
r = node;
}
if(!r && node && node.childElementCount > 0){
children = node.children;
if (children && children.length) {
leng = children.length ? children.length : 0;
} else {
return r;
}
for (var i = 0; i < leng; i++) {
if(children[i] != prevNode) stack.push(children[i]);
}
}
if(!r && stack.length === 0 && startNode && startNode.parentNode !== null){
stack.push(startNode.parentNode);
prevNode = startNode;
startNode = startNode.parentNode;
}
ITER++;
if(ITER > STOP) {
console.error("ENDLESS LOOP");
break;
}
}
return r;
};
EPUBJS.Renderer.prototype.getPageCfi = function(){
var prevEl = this.visibileEl;
this.visibileEl = this.findFirstVisible(prevEl);
if(!this.visibileEl.id) {
this.visibileEl.id = "EPUBJS-PAGE-" + this.chapterPos;
}
this.pageIds[this.chapterPos] = this.visibileEl.id;
return this.epubcfi.generateFragment(this.visibileEl, this.currentChapterCfi);
};
EPUBJS.Renderer.prototype.gotoCfiFragment = function(cfi){
var element;
if(_.isString(cfi)){
cfi = this.epubcfi.parse(cfi);
}
element = this.epubcfi.getElement(cfi, this.doc);
this.pageByElement(element);
};
EPUBJS.Renderer.prototype.findFirstVisible = function(startEl){
var el = startEl || this.bodyEl,
found;
found = this.walk(el);
if(found) {
return found;
}else{
return startEl;
}
};
EPUBJS.Renderer.prototype.isElementVisible = function(el){
var rect;
if(el && typeof el.getBoundingClientRect === 'function'){
rect = el.getBoundingClientRect();
if( rect.width != 0 &&
rect.height != 0 &&
rect.left >= 0 &&
rect.left < this.spreadWidth ) {
return true;
}
}
return false;
};

View file

@ -272,80 +272,7 @@ EPUBJS.Renderer.prototype.setIframeSrc = function(url){
};
EPUBJS.Renderer.prototype.formatSpread = function(){
var divisor = 2,
cutoff = 800;
//-- Check the width and decied on columns
//-- Todo: a better place for this?
this.elWidth = this.iframe.clientWidth;
if(this.elWidth % 2 !== 0){
this.elWidth -= 1;
}
// this.gap = this.gap || Math.ceil(this.elWidth / 8);
this.gap = Math.ceil(this.elWidth / 8);
if(this.gap % 2 !== 0){
this.gap += 1;
}
if(this.elWidth < cutoff || !this.book.settings.spreads) {
this.spread = false; //-- Single Page
divisor = 1;
this.colWidth = Math.floor(this.elWidth / divisor);
}else{
this.spread = true; //-- Double Page
this.colWidth = Math.floor((this.elWidth - this.gap) / divisor);
}
this.spreadWidth = (this.colWidth + this.gap) * divisor;
// if(this.bodyEl) this.bodyEl.style.margin = 0;
// this.bodyEl.style.fontSize = localStorage.getItem("fontSize") || "medium";
//-- Clear Margins
if(this.bodyEl) {
this.bodyEl.style.margin = "0";
this.docEl.style.overflow = "hidden";
this.docEl.style.width = this.elWidth + "px";
//-- Adjust height
this.docEl.style.height = this.iframe.clientHeight + "px";
//-- Add columns
this.docEl.style[EPUBJS.Renderer.columnAxis] = "horizontal";
this.docEl.style[EPUBJS.Renderer.columnGap] = this.gap+"px";
this.docEl.style[EPUBJS.Renderer.columnWidth] = this.colWidth+"px";
}
};
EPUBJS.Renderer.prototype.fixedLayout = function(){
this.paginated = false;
this.elWidth = this.iframe.width;
this.docEl.style.width = this.elWidth;
// this.setLeft(0);
this.docEl.style.width = this.elWidth;
//-- Adjust height
this.docEl.style.height = "auto";
//-- Remove columns
// this.docEl.style[EPUBJS.core.columnWidth] = "auto";
//-- Scroll
this.docEl.style.overflow = "auto";
this.iframe.scrolling = "yes";
// this.displayedPages = 1;
};
EPUBJS.Renderer.prototype.setStyle = function(style, val, prefixed){
if(prefixed) {
@ -413,48 +340,6 @@ EPUBJS.Renderer.prototype.calcPages = function() {
this.currentChapter.pages = this.displayedPages;
};
EPUBJS.Renderer.prototype.nextPage = function(){
if(this.chapterPos < this.displayedPages){
this.chapterPos++;
this.leftPos += this.spreadWidth;
this.setLeft(this.leftPos);
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
}else{
return false;
}
};
EPUBJS.Renderer.prototype.prevPage = function(){
if(this.chapterPos > 1){
this.chapterPos--;
this.leftPos -= this.spreadWidth;
this.setLeft(this.leftPos);
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
}else{
return false;
}
};
EPUBJS.Renderer.prototype.chapterEnd = function(){
this.page(this.displayedPages);
};
EPUBJS.Renderer.prototype.setLeft = function(leftPos){
// this.bodyEl.style.marginLeft = -leftPos + "px";
// this.docEl.style.marginLeft = -leftPos + "px";
@ -483,287 +368,11 @@ EPUBJS.Renderer.prototype.determineStore = function(callback){
}
};
EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
var items = this.doc.querySelectorAll(query),
resources = Array.prototype.slice.call(items),
count = resources.length,
after = function(result, full){
count--;
if(progress) progress(result, full, count);
if(count <= 0 && finished) finished(true);
};
if(count === 0) {
finished(false);
return;
}
resources.forEach(function(item){
func(item, after);
}.bind(this));
};
EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callback) {
var _oldUrls,
_newUrls = {},
_store = this.determineStore(),
_cache = this.caches[query],
_uri = EPUBJS.core.uri(this.book.chapter.absolute),
_chapterBase = _uri.base,
_attr = attr,
_wait = 2000,
progress = function(url, full, count) {
_newUrls[full] = url;
},
finished = function(notempty) {
if(callback) callback();
_.each(_oldUrls, function(url){
_store.revokeUrl(url);
});
_cache = _newUrls;
};
if(!_store) return;
if(!_cache) _cache = {};
_oldUrls = _.clone(_cache);
this.replace(query, function(link, done){
var src = link.getAttribute(_attr),
full = EPUBJS.core.resolveUrl(_chapterBase, src);
var replaceUrl = function(url) {
var timeout;
link.onload = function(){
clearTimeout(timeout);
done(url, full);
};
link.onerror = function(e){
clearTimeout(timeout);
done(url, full);
console.error(e);
};
if(query == "image") {
//-- SVG needs this to trigger a load event
link.setAttribute("externalResourcesRequired", "true");
}
if(query == "link[href]") {
//-- Only Stylesheet links seem to have a load events, just continue others
done(url, full);
}
link.setAttribute(_attr, url);
//-- If elements never fire Load Event, should continue anyways
timeout = setTimeout(function(){
done(url, full);
}, _wait);
};
if(full in _oldUrls){
replaceUrl(_oldUrls[full]);
_newUrls[full] = _oldUrls[full];
delete _oldUrls[full];
}else{
func(_store, full, replaceUrl, link);
}
}, finished, progress);
};
//-- Replaces the relative links within the book to use our internal page changer
EPUBJS.Renderer.prototype.replaceLinks = function(callback){
var renderer = this;
this.replace("a[href]", function(link, done){
var href = link.getAttribute("href"),
relative = href.search("://"),
fragment = href[0] == "#";
if(relative != -1){
link.setAttribute("target", "_blank");
}else{
link.onclick = function(){
renderer.book.goto(href);
return false;
};
}
done();
}, callback);
};
EPUBJS.Renderer.prototype.page = function(pg){
if(pg >= 1 && pg <= this.displayedPages){
this.chapterPos = pg;
this.leftPos = this.spreadWidth * (pg-1); //-- pages start at 1
this.setLeft(this.leftPos);
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
// localStorage.setItem("chapterPos", pg);
return true;
}
//-- Return false if page is greater than the total
return false;
};
//-- Find a section by fragement id
EPUBJS.Renderer.prototype.section = function(fragment){
var el = this.doc.getElementById(fragment),
left, pg;
if(el){
this.pageByElement(el);
}
};
//-- Show the page containing an Element
EPUBJS.Renderer.prototype.pageByElement = function(el){
var left, pg;
if(!el) return;
left = this.leftPos + el.getBoundingClientRect().left; //-- Calculate left offset compaired to scrolled position
pg = Math.floor(left / this.spreadWidth) + 1; //-- pages start at 1
this.page(pg);
};
EPUBJS.Renderer.prototype.beforeDisplay = function(callback){
this.book.triggerHooks("beforeChapterDisplay", callback.bind(this), this);
};
EPUBJS.Renderer.prototype.walk = function(node) {
var r, children, leng,
startNode = node,
prevNode,
stack = [startNode];
var STOP = 10000, ITER=0;
while(!r && stack.length) {
node = stack.shift();
if( this.isElementVisible(node) ) {
r = node;
}
if(!r && node && node.childElementCount > 0){
children = node.children;
if (children && children.length) {
leng = children.length ? children.length : 0;
} else {
return r;
}
for (var i = 0; i < leng; i++) {
if(children[i] != prevNode) stack.push(children[i]);
}
}
if(!r && stack.length === 0 && startNode && startNode.parentNode !== null){
stack.push(startNode.parentNode);
prevNode = startNode;
startNode = startNode.parentNode;
}
ITER++;
if(ITER > STOP) {
console.error("ENDLESS LOOP");
break;
}
}
return r;
};
EPUBJS.Renderer.prototype.getPageCfi = function(){
var prevEl = this.visibileEl;
this.visibileEl = this.findFirstVisible(prevEl);
if(!this.visibileEl.id) {
this.visibileEl.id = "EPUBJS-PAGE-" + this.chapterPos;
}
this.pageIds[this.chapterPos] = this.visibileEl.id;
return this.epubcfi.generateFragment(this.visibileEl, this.currentChapterCfi);
};
EPUBJS.Renderer.prototype.gotoCfiFragment = function(cfi){
var element;
if(_.isString(cfi)){
cfi = this.epubcfi.parse(cfi);
}
element = this.epubcfi.getElement(cfi, this.doc);
this.pageByElement(element);
};
EPUBJS.Renderer.prototype.findFirstVisible = function(startEl){
var el = startEl || this.bodyEl,
found;
found = this.walk(el);
if(found) {
return found;
}else{
return startEl;
}
};
EPUBJS.Renderer.prototype.isElementVisible = function(el){
var rect;
if(el && typeof el.getBoundingClientRect === 'function'){
rect = el.getBoundingClientRect();
if( rect.width != 0 &&
rect.height != 0 &&
rect.left >= 0 &&
rect.left < this.spreadWidth ) {
return true;
}
}
return false;
};
EPUBJS.Renderer.prototype.height = function(el){