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

Goto will take you back to first page of chapter

This commit is contained in:
Fred Chasen 2014-05-12 10:03:12 -07:00
parent d84203711a
commit 48612637d4
12 changed files with 752 additions and 722 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

4
build/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

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -6,7 +6,7 @@
<title>Basic ePubJS Example</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- EPUBJS Renderer -->
<!-- zip -->
@ -15,8 +15,8 @@
<script src="../libs/zip/zip-ext.js"></script>
<script src="../libs/zip/inflate.js"></script>
<script src="../libs/zip/mime-types.js"></script>
<!-- Render -->
<script src="../libs/underscore/underscore.js"></script>
<script src="../libs/rsvp/rsvp.js"></script>
@ -39,9 +39,9 @@
<!-- <script src="../hooks/default/transculsions.js"></script> -->
<!-- <script src="../hooks/default/endnotes.js"></script> -->
<script src="../hooks/default/smartimages.js"></script>
<script>
EPUBJS.filePath = "../reader/js/libs/";
EPUBJS.cssPath = "../reader/css/";
@ -50,7 +50,7 @@
<style type="text/css">
body {
}
#main {
@ -75,7 +75,7 @@
left: 40px;
}
#next {
#next {
right: 40px;
}
@ -96,11 +96,11 @@
.arrow:hover {
color: #777;
}
.arrow:active {
color: #000;
}
#controls {
position: absolute;
bottom: 16px;
@ -110,15 +110,15 @@
text-align: center;
display: none;
}
#controls > input[type=range] {
width: 400px;
}
</style>
<script>
"use strict";
var book = ePub("../reader/moby-dick/", { width: 1076, height: 588 });
</script>
@ -133,7 +133,7 @@
</div>
</div>
<script>
<script>
var controls = document.getElementById("controls");
var currentPage = document.getElementById("currentpg");
var totalPages = document.getElementById("totalpg");
@ -144,23 +144,23 @@
};
var throttledSlide = _.throttle(slide, 200);
var mouseDown = false;
var rendered = book.renderTo("area");
// Load in stored pageList from json or local storage
///*
EPUBJS.core.request("page_list.json").then(function(storedPageList){
pageList = storedPageList;
console.log(storedPageList)
book.loadPagination(pageList);
// EPUBJS.core.request("page_list.json").then(function(storedPageList){
// pageList = storedPageList;
// console.log(storedPageList)
// book.loadPagination(pageList);
// });
// Or generate the pageList on the fly
book.ready.all.then(function(){
book.generatePagination();
});
// Or generate the pageList on the fly
// book.ready.all.then(function(){
// book.generatePagination();
// });
// Wait for the pageList to be ready and then show slider
book.pageListReady.then(function(pageList){
controls.style.display = "block";
@ -170,7 +170,7 @@
slider.setAttribute("max", book.pagination.lastPage);
slider.setAttribute("step", 1);
slider.setAttribute("value", 0);
slider.addEventListener("change", throttledSlide, false);
slider.addEventListener("mousedown", function(){
mouseDown = true;
@ -178,7 +178,7 @@
slider.addEventListener("mouseup", function(){
mouseDown = false;
}, false);
// Wait for book to be rendered to get current page
rendered.then(function(){
var currentLocation = book.getCurrentLocationCfi();
@ -186,15 +186,15 @@
slider.value = currentPage;
currentPage.value = currentPage;
});
controls.appendChild(slider);
totalPages.innerText = book.pagination.totalPages;
currentPage.addEventListener("change", function(){
book.gotoPage(currentPage.value);
}, false);
});
book.on('book:pageChanged', function(location){
if(!mouseDown) {
slider.value = location.anchorPage;

File diff suppressed because one or more lines are too long

350
reader/js/epub.min.js vendored

File diff suppressed because it is too large Load diff

View file

@ -28,7 +28,7 @@ EPUBJS.Book = function(options){
});
this.settings.EPUBJSVERSION = EPUBJS.VERSION;
this.spinePos = 0;
this.stored = false;
@ -42,25 +42,25 @@ EPUBJS.Book = function(options){
book:loadFailed
book:loadChapterFailed
*/
//-- Adds Hook methods to the Book prototype
// Hooks will all return before triggering the callback.
// EPUBJS.Hooks.mixin(this);
//-- Get pre-registered hooks for events
// this.getHooks("beforeChapterDisplay");
this.online = this.settings.online || navigator.onLine;
this.networkListeners();
this.store = false; //-- False if not using storage;
//-- Determine storage method
//-- Override options: none | ram | websqldatabase | indexeddb | filesystem
if(this.settings.storage !== false){
this.storage = new fileStorage.storage(this.settings.storage);
}
this.ready = {
manifest: new RSVP.defer(),
spine: new RSVP.defer(),
@ -69,7 +69,7 @@ EPUBJS.Book = function(options){
toc: new RSVP.defer(),
pageList: new RSVP.defer()
};
this.readyPromises = [
this.ready.manifest.promise,
this.ready.spine.promise,
@ -77,15 +77,15 @@ EPUBJS.Book = function(options){
this.ready.cover.promise,
this.ready.toc.promise
];
this.pageList = [];
this.pagination = new EPUBJS.Pagination();
this.pageListReady = this.ready.pageList.promise;
this.ready.all = RSVP.all(this.readyPromises);
this.ready.all.then(this._ready.bind(this));
// Queue for methods used before rendering
this.isRendered = false;
this._q = EPUBJS.core.queue(this);
@ -97,7 +97,7 @@ EPUBJS.Book = function(options){
this._gotoQ = EPUBJS.core.queue(this);
/**
* Creates a new renderer.
* Creates a new renderer.
* The renderer will handle displaying the content using the method provided in the settings
*/
this.renderer = new EPUBJS.Renderer(this.settings.render_method);
@ -106,14 +106,14 @@ EPUBJS.Book = function(options){
this.renderer.setGap(this.settings.gap);
//-- Pass through the renderer events
this.listenToRenderer(this.renderer);
this.defer_opened = new RSVP.defer();
this.opened = this.defer_opened.promise;
// BookUrl is optional, but if present start loading process
if(typeof this.settings.bookPath === 'string') {
this.open(this.settings.bookPath, this.settings.reload);
}
window.addEventListener("beforeunload", this.unload.bind(this), false);
//-- Listen for these promises:
@ -121,7 +121,7 @@ EPUBJS.Book = function(options){
//-- book.rendered.then()
};
//-- Check bookUrl and start parsing book Assets or load them from storage
//-- Check bookUrl and start parsing book Assets or load them from storage
EPUBJS.Book.prototype.open = function(bookPath, forceReload){
var book = this,
epubpackage,
@ -159,9 +159,9 @@ EPUBJS.Book.prototype.open = function(bookPath, forceReload){
opened.resolve();
book.defer_opened.resolve();
});
}else{
//-- Get package information from epub opf
epubpackage.then(function(packageXml) {
book.unpack(packageXml);
@ -169,7 +169,7 @@ EPUBJS.Book.prototype.open = function(bookPath, forceReload){
book.defer_opened.resolve();
});
}
//-- If there is network connection, store the books contents
if(this.online && this.settings.storage && !this.settings.contained){
if(!this.settings.stored) opened.then(book.storeOffline());
@ -187,7 +187,7 @@ EPUBJS.Book.prototype.loadPackage = function(_containerPath){
containerPath = _containerPath || "META-INF/container.xml",
containerXml,
packageXml;
if(!this.settings.packageUrl) { //-- provide the packageUrl to skip this step
packageXml = book.loadXml(book.bookUrl + containerPath).
then(function(containerXml){
@ -197,12 +197,12 @@ EPUBJS.Book.prototype.loadPackage = function(_containerPath){
book.settings.contentsPath = book.bookUrl + paths.basePath;
book.settings.packageUrl = book.bookUrl + paths.packagePath;
book.settings.encoding = paths.encoding;
return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata
return book.loadXml(book.settings.packageUrl); // Containes manifest, spine and metadata
});
} else {
packageXml = book.loadXml(book.settings.packageUrl);
}
packageXml.catch(function(error) {
// handle errors in either of the two requests
console.error("Could not load book at: "+ containerPath);
@ -221,7 +221,7 @@ EPUBJS.Book.prototype.packageIdentifier = function(packageXml){
EPUBJS.Book.prototype.unpack = function(packageXml){
var book = this,
parse = new EPUBJS.Parser();
book.contents = parse.packageContents(packageXml, book.settings.contentsPath); // Extract info from contents
book.manifest = book.contents.manifest;
@ -236,9 +236,9 @@ EPUBJS.Book.prototype.unpack = function(packageXml){
book.globalLayoutProperties = book.parseLayoutProperties(book.metadata);
book.cover = book.contents.cover = book.settings.contentsPath + book.contents.coverPath;
book.spineNodeIndex = book.contents.spineNodeIndex;
book.ready.manifest.resolve(book.contents.manifest);
book.ready.spine.resolve(book.contents.spine);
book.ready.metadata.resolve(book.contents.metadata);
@ -258,7 +258,7 @@ EPUBJS.Book.prototype.unpack = function(packageXml){
}, function(error) {
book.ready.toc.resolve(false);
});
// Load the optional pageList
book.loadXml(book.settings.navUrl).
then(function(navHtml){
@ -290,7 +290,7 @@ EPUBJS.Book.prototype.unpack = function(packageXml){
});
}
});
if(!wait) {
book.pagination.process(book.pageList);
book.ready.pageList.resolve(book.pageList);
@ -331,7 +331,7 @@ EPUBJS.Book.prototype.createHiddenRender = function(renderer, _width, _height) {
if(this.settings.forceSingle) {
renderer.forceSingle(true);
}
hiddenContainer = document.createElement("div");
hiddenContainer.style.visibility = "hidden";
hiddenContainer.style.overflow = "hidden";
@ -345,7 +345,7 @@ EPUBJS.Book.prototype.createHiddenRender = function(renderer, _width, _height) {
hiddenEl.style.width = width + "px";//"0";
hiddenEl.style.height = height +"px"; //"0";
hiddenContainer.appendChild(hiddenEl);
renderer.initialize(hiddenEl);
return hiddenContainer;
};
@ -377,9 +377,9 @@ EPUBJS.Book.prototype.generatePageList = function(width, height){
"cfi" : item.start,
"page" : currentPage
});
});
if(pager.pageMap.length % 2 > 0 &&
pager.spreads) {
currentPage += 1; // Handle Spreads
@ -397,13 +397,13 @@ EPUBJS.Book.prototype.generatePageList = function(width, height){
}
return done.promise;
}.bind(this);
var finished = nextChapter().then(function(){
pager.remove();
this.element.removeChild(hiddenContainer);
deferred.resolve(pageList);
}.bind(this));
return deferred.promise;
};
@ -421,7 +421,7 @@ EPUBJS.Book.prototype.generatePagination = function(width, height) {
defered.resolve(book.pageList);
});
});
return defered.promise;
};
@ -464,7 +464,7 @@ EPUBJS.Book.prototype.networkListeners = function(){
book.online = true;
book.trigger("book:online");
}, false);
};
// Listen to all events the renderer triggers and pass them as book events
@ -496,12 +496,12 @@ EPUBJS.Book.prototype.listenToRenderer = function(renderer){
"percentage": percent,
"pageRange" : pageRange
});
// TODO: Add event for first and last page.
// TODO: Add event for first and last page.
// (though last is going to be hard, since it could be several reflowed pages long)
}
}.bind(this));
renderer.on("render:loaded", this.loadChange.bind(this));
};
@ -538,18 +538,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
absolute = uri.protocol,
fromRoot = uri.path[0] == "/",
location = window.location,
//-- Get URL orgin, try for native or combine
//-- Get URL orgin, try for native or combine
origin = location.origin || location.protocol + "//" + location.host,
baseTag = document.getElementsByTagName('base'),
base;
//-- Check is Base tag is set
if(baseTag.length) {
base = baseTag[0].href;
}
//-- 1. Check if url is absolute
if(uri.protocol){
return uri.origin + uri.path;
@ -571,13 +571,13 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
EPUBJS.Book.prototype.unarchive = function(bookPath){
var book = this,
unarchived;
//-- Must use storage
// if(this.settings.storage == false ){
// this.settings.storage = true;
// this.storage = new fileStorage.storage();
// }
this.zip = new EPUBJS.Unarchiver();
this.store = this.zip; // Use zip storaged in ram
return this.zip.openZip(bookPath);
@ -637,7 +637,7 @@ EPUBJS.Book.prototype.removeSavedContents = function() {
EPUBJS.Book.prototype.renderTo = function(elem){
var book = this,
rendered;
if(_.isElement(elem)) {
this.element = elem;
} else if (typeof elem == "string") {
@ -646,7 +646,7 @@ EPUBJS.Book.prototype.renderTo = function(elem){
console.error("Not an Element");
return;
}
rendered = this.opened.
then(function(){
// book.render = new EPUBJS.Renderer[this.settings.renderer](book);
@ -721,9 +721,9 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
pos,
store,
defer = deferred || new RSVP.defer();
var chapter;
if(!this.isRendered) {
this._q.enqueue("displayChapter", arguments);
// Reject for now. TODO: pass promise to queue
@ -733,7 +733,7 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
});
return defer.promise;
}
if(this._rendering) {
// Pass along the current defer
@ -747,7 +747,7 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
cfi = new EPUBJS.EpubCFI(chap);
pos = cfi.spinePos;
}
if(pos < 0 || pos >= this.spine.length){
console.warn("Not A Valid Location");
pos = 0;
@ -755,11 +755,11 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
cfi = false;
}
//-- Create a new chapter
//-- Create a new chapter
chapter = new EPUBJS.Chapter(this.spine[pos], this.store);
this._rendering = true;
render = book.renderer.displayChapter(chapter, this.globalLayoutProperties);
//-- Success, Clear render queue
@ -767,7 +767,7 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
// var inwait;
//-- Set the book's spine position
book.spinePos = pos;
if(cfi) {
rendered.gotoCfi(cfi);
defer.resolve(book.renderer);
@ -777,13 +777,13 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
} else {
defer.resolve(book.renderer);
}
if(!book.settings.fromStorage &&
!book.settings.contained) {
book.preloadNextChapter();
}
book.currentChapter = chapter;
book._rendering = false;
book._displayQ.dequeue();
@ -795,7 +795,7 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end, deferred){
book._rendering = false;
defer.reject(error);
});
return defer.promise;
};
@ -803,7 +803,7 @@ EPUBJS.Book.prototype.nextPage = function(){
var next;
if(!this.isRendered) return this._q.enqueue("nextPage", arguments);
next = this.renderer.nextPage();
if(!next){
@ -817,7 +817,7 @@ EPUBJS.Book.prototype.prevPage = function() {
if(!this.isRendered) return this._q.enqueue("prevPage", arguments);
prev = this.renderer.prevPage();
if(!prev){
return this.prevChapter();
}
@ -875,7 +875,7 @@ EPUBJS.Book.prototype.goto = function(target){
} else {
return this.gotoHref(target);
}
};
EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
@ -884,7 +884,7 @@ EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
spineItem,
rendered,
deferred = defer || new RSVP.defer();
if(!this.isRendered) {
this.settings.previousLocationCfi = cfiString;
return false;
@ -895,10 +895,10 @@ EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
this._gotoQ.enqueue("gotoCfi", [cfiString, deferred]);
return false;
}
cfi = new EPUBJS.EpubCFI(cfiString);
spinePos = cfi.spinePos;
if(spinePos == -1) {
return false;
}
@ -912,18 +912,18 @@ EPUBJS.Book.prototype.gotoCfi = function(cfiString, defer){
this._moving = false;
deferred.resolve(this.renderer.currentLocationCfi);
} else {
if(!spineItem || spinePos == -1) {
spinePos = 0;
spineItem = this.spine[spinePos];
}
this.currentChapter = new EPUBJS.Chapter(spineItem, this.store);
if(this.currentChapter) {
this.spinePos = spinePos;
render = this.renderer.displayChapter(this.currentChapter, this.globalLayoutProperties);
render.then(function(rendered){
rendered.gotoCfi(cfi);
this._moving = false;
@ -967,7 +967,7 @@ EPUBJS.Book.prototype.gotoHref = function(url, defer){
//-- Check that URL is present in the index, or stop
if(typeof(spinePos) != "number") return false;
if(!this.currentChapter || spinePos != this.currentChapter.spinePos){
//-- Load new chapter if different than current
return this.displayChapter(spinePos).then(function(){
@ -977,9 +977,12 @@ EPUBJS.Book.prototype.gotoHref = function(url, defer){
deferred.resolve(this.renderer.currentLocationCfi);
}.bind(this));
}else{
//-- Only goto section
//-- Goto section
if(section) {
this.renderer.section(section);
} else {
// Or jump to the start
this.renderer.firstPage();
}
deferred.resolve(this.renderer.currentLocationCfi);
}
@ -1004,11 +1007,11 @@ EPUBJS.Book.prototype.gotoPercentage = function(percent){
EPUBJS.Book.prototype.preloadNextChapter = function() {
var next;
var chap = this.spinePos + 1;
if(chap >= this.spine.length){
return false;
}
next = new EPUBJS.Chapter(this.spine[chap]);
if(next) {
EPUBJS.core.request(next.absolute);
@ -1019,7 +1022,7 @@ EPUBJS.Book.prototype.preloadNextChapter = function() {
EPUBJS.Book.prototype.storeOffline = function() {
var book = this,
assets = _.values(this.manifest);
//-- Creates a queue of all items to load
return EPUBJS.storage.batch(assets).
then(function(){
@ -1034,9 +1037,9 @@ EPUBJS.Book.prototype.availableOffline = function() {
/*
EPUBJS.Book.prototype.fromStorage = function(stored) {
if(this.contained) return;
if(!stored){
this.online = true;
this.tell("book:online");
@ -1047,19 +1050,19 @@ EPUBJS.Book.prototype.fromStorage = function(stored) {
this.online = false;
this.tell("book:offline");
}.bind(this));
}else{
this.online = false;
this.tell("book:offline");
}
}
}
*/
EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) {
if(!this.isRendered) return this._q.enqueue("setStyle", arguments);
this.settings.styles[style] = val;
this.renderer.setStyle(style, val, prefixed);
@ -1112,7 +1115,7 @@ EPUBJS.Book.prototype.setGap = function(gap) {
};
EPUBJS.Book.prototype.unload = function(){
if(this.settings.restore && localStorage) {
this.saveContents();
}

View file

@ -10,7 +10,7 @@ EPUBJS.Layout.Reflowable.prototype.format = function(documentElement, _width, _h
var columnAxis = EPUBJS.core.prefixed('columnAxis');
var columnGap = EPUBJS.core.prefixed('columnGap');
var columnWidth = EPUBJS.core.prefixed('columnWidth');
//-- Check the width and create even width columns
var width = Math.floor(_width);
// var width = (fullWidth % 2 === 0) ? fullWidth : fullWidth - 0; // Not needed for single
@ -19,23 +19,23 @@ EPUBJS.Layout.Reflowable.prototype.format = function(documentElement, _width, _h
this.documentElement = documentElement;
//-- Single Page
this.spreadWidth = (width + gap);
documentElement.style.overflow = "hidden";
// Must be set to the new calculated width or the columns will be off
documentElement.style.width = width + "px";
//-- Adjust height
documentElement.style.height = _height + "px";
//-- Add columns
documentElement.style[columnAxis] = "horizontal";
documentElement.style[columnWidth] = width+"px";
documentElement.style[columnGap] = gap+"px";
this.colWidth = width;
this.gap = gap;
return {
pageWidth : this.spreadWidth,
pageHeight : _height
@ -63,14 +63,14 @@ EPUBJS.Layout.ReflowableSpreads.prototype.format = function(documentElement, _wi
var columnAxis = EPUBJS.core.prefixed('columnAxis');
var columnGap = EPUBJS.core.prefixed('columnGap');
var columnWidth = EPUBJS.core.prefixed('columnWidth');
var divisor = 2,
cutoff = 800;
//-- Check the width and create even width columns
var fullWidth = Math.floor(_width);
var width = (fullWidth % 2 === 0) ? fullWidth : fullWidth - 1;
var section = Math.floor(width / 8);
var gap = (_gap >= 0) ? _gap : ((section % 2 === 0) ? section : section - 1);
@ -79,8 +79,8 @@ EPUBJS.Layout.ReflowableSpreads.prototype.format = function(documentElement, _wi
this.documentElement = documentElement;
this.spreadWidth = (colWidth + gap) * divisor;
documentElement.style.overflow = "hidden";
// Must be set to the new calculated width or the columns will be off
@ -123,7 +123,7 @@ EPUBJS.Layout.Fixed = function(documentElement, _width, _height, _gap){
var content;
var contents;
var width, height;
this.documentElement = documentElement;
/**
* check for the viewport size
@ -139,7 +139,7 @@ EPUBJS.Layout.Fixed = function(documentElement, _width, _height, _gap){
height = contents[1].replace("height=", '');
}
}
//-- Adjust width and height
documentElement.style.width = width + "px" || "auto";
documentElement.style.height = height + "px" || "auto";
@ -152,12 +152,12 @@ EPUBJS.Layout.Fixed = function(documentElement, _width, _height, _gap){
this.colWidth = width;
this.gap = 0;
return {
pageWidth : width,
pageHeight : height
};
};
EPUBJS.Layout.Fixed.prototype.calculatePages = function(){
@ -165,4 +165,4 @@ EPUBJS.Layout.Fixed.prototype.calculatePages = function(){
displayedPages : 1,
pageCount : 1
};
};
};

View file

@ -20,26 +20,26 @@ EPUBJS.Renderer = function(renderMethod, hidden) {
// Listen for load events
this.render.on("render:loaded", this.loaded.bind(this));
// Cached for replacement urls from storage
this.caches = {};
// Blank Cfi for Parsing
this.epubcfi = new EPUBJS.EpubCFI();
this.spreads = true;
this.isForcedSingle = false;
this.resized = _.throttle(this.onResized.bind(this), 10);
this.layoutSettings = {};
this.hidden = hidden || false;
//-- Adds Hook methods to the Book prototype
// Hooks will all return before triggering the callback.
EPUBJS.Hooks.mixin(this);
//-- Get pre-registered hooks for events
this.getHooks("beforeChapterDisplay");
//-- Queue up page changes if page map isn't ready
this._q = EPUBJS.core.queue(this);
@ -66,15 +66,15 @@ EPUBJS.Renderer.prototype.Events = [
/**
* Creates an element to render to.
* Resizes to passed width and height or to the elements size
* Resizes to passed width and height or to the elements size
*/
EPUBJS.Renderer.prototype.initialize = function(element, width, height){
this.container = element;
this.element = this.render.create();
this.initWidth = width;
this.initHeight = height;
this.width = width || this.container.clientWidth;
this.height = height || this.container.clientHeight;
@ -98,7 +98,7 @@ EPUBJS.Renderer.prototype.displayChapter = function(chapter, globalLayout){
// Get the url string from the chapter (may be from storage)
return chapter.url().
then(function(url) {
// Unload the previous chapter listener
if(this.currentChapter) {
this.currentChapter.unload(); // Remove stored blobs
@ -110,15 +110,15 @@ EPUBJS.Renderer.prototype.displayChapter = function(chapter, globalLayout){
this.doc = null;
this.pageMap = null;
}
this.currentChapter = chapter;
this.chapterPos = 1;
this.pageMap = null;
this.currentChapterCfiBase = chapter.cfiBase;
this.layoutSettings = this.reconcileLayoutSettings(globalLayout, chapter.properties);
return this.load(url);
}.bind(this));
};
@ -155,7 +155,7 @@ EPUBJS.Renderer.prototype.load = function(url){
this.render.window.addEventListener("resize", this.resized, false);
}
this.addEventListeners();
this.addSelectionListeners();
@ -172,12 +172,12 @@ EPUBJS.Renderer.prototype.load = function(url){
msg.cfi = this.currentLocationCfi;
this.trigger("renderer:chapterDisplayed", msg);
this.visible(true);
deferred.resolve(this); //-- why does this return the renderer?
}.bind(this));
}.bind(this));
return deferred.promise;
@ -210,11 +210,11 @@ EPUBJS.Renderer.prototype.reconcileLayoutSettings = function(global, chapter){
var rendition = prop.replace("rendition:", '');
var split = rendition.indexOf("-");
var property, value;
if(split != -1){
property = rendition.slice(0, split);
value = rendition.slice(split+1);
settings[property] = value;
}
});
@ -232,7 +232,7 @@ EPUBJS.Renderer.prototype.determineLayout = function(settings){
var spreads = this.determineSpreads(this.minSpreadWidth);
var layoutMethod = spreads ? "ReflowableSpreads" : "Reflowable";
var scroll = false;
if(settings.layout === "pre-paginated") {
layoutMethod = "Fixed";
scroll = true;
@ -244,7 +244,7 @@ EPUBJS.Renderer.prototype.determineLayout = function(settings){
scroll = false;
spreads = false;
}
if(settings.layout === "reflowable" && settings.spread === "both") {
layoutMethod = "ReflowableSpreads";
scroll = false;
@ -267,7 +267,7 @@ EPUBJS.Renderer.prototype.updatePages = function(layout){
this.pageMap = this.mapPage();
this.displayedPages = layout.displayedPages;
this.currentChapter.pages = layout.pageCount;
this._q.flush();
};
@ -276,10 +276,10 @@ EPUBJS.Renderer.prototype.reformat = function(){
var renderer = this;
var formated, pages;
if(!this.contents) return;
this.layoutMethod = this.determineLayout(this.layoutSettings);
this.layout = new EPUBJS.Layout[this.layoutMethod]();
this.formated = this.layout.format(this.contents, this.render.width, this.render.height, this.gap);
this.render.setPageDimensions(this.formated.pageWidth, this.formated.pageHeight);
@ -319,7 +319,7 @@ EPUBJS.Renderer.prototype.remove = function() {
this.removeEventListeners();
this.removeSelectionListeners();
}
this.container.removeChild(this.element);
};
@ -348,8 +348,8 @@ EPUBJS.Renderer.prototype.applyHeadTags = function(headTags) {
//-- NAVIGATION
EPUBJS.Renderer.prototype.page = function(pg){
EPUBJS.Renderer.prototype.page = function(pg){
if(!this.pageMap) {
this._q.enqueue("page", arguments);
return true;
@ -376,9 +376,9 @@ EPUBJS.Renderer.prototype.nextPage = function(){
var pg = this.chapterPos + 1;
if(pg <= this.displayedPages){
this.chapterPos = pg;
this.render.page(pg);
this.currentLocationCfi = this.getPageCfi(this.visibileEl);
this.trigger("renderer:locationChanged", this.currentLocationCfi);
@ -410,6 +410,11 @@ EPUBJS.Renderer.prototype.lastPage = function(){
this.page(this.displayedPages);
};
// Jump to the first page of the chapter
EPUBJS.Renderer.prototype.firstPage = function(){
this.page(1);
};
//-- Find a section by fragement id
EPUBJS.Renderer.prototype.section = function(fragment){
var el = this.doc.getElementById(fragment),
@ -424,7 +429,7 @@ EPUBJS.Renderer.prototype.section = function(fragment){
EPUBJS.Renderer.prototype.firstElementisTextNode = function(node) {
var children = node.childNodes;
var leng = children.length;
if(leng &&
children[0] && // First Child
children[0].nodeType === 3 && // This is a textNodes
@ -540,33 +545,31 @@ EPUBJS.Renderer.prototype.mapPage = function(){
var children = Array.prototype.slice.call(node.childNodes);
if (node.nodeType == Node.ELEMENT_NODE) {
elPos = node.getBoundingClientRect();
if(!elPos || (elPos.width === 0 && elPos.height === 0)) {
return;
}
if(elPos.left + elPos.width > elLimit) {
// console.log("checking text node of: ", node);
children.forEach(function(node){
if(node.nodeType == Node.TEXT_NODE &&
node.textContent.trim().length) {
checkText(node);
}
});
// renderer.textSprint(node, checkText);
});
}
}
// if (node.nodeType != Node.TEXT_NODE) {
// renderer.handleTextNode(node, map, prevRange, limit, cfi);
// }
};
var checkText = function(node){
var ranges = renderer.splitTextNodeIntoWordsRanges(node);
ranges.forEach(function(range){
var pos = range.getBoundingClientRect();
// console.log(pos.left, pos.top, node);
// if(!pos || (pos.width === 0 && pos.height === 0)) {
// return;
// }
if(!pos || (pos.width === 0 && pos.height === 0)) {
return;
}
if(pos.left + pos.width < limit) {
if(!map[page-1].start){
range.collapse(true);
@ -579,7 +582,7 @@ EPUBJS.Renderer.prototype.mapPage = function(){
cfi = renderer.currentChapter.cfiFromRange(prevRange);
map[page-1].end = cfi;
}
range.collapse(true);
cfi = renderer.currentChapter.cfiFromRange(range);
map.push({
@ -590,23 +593,23 @@ EPUBJS.Renderer.prototype.mapPage = function(){
limit = (width * page) - offset;
elLimit = limit;
}
prevRange = range;
});
};
this.sprint(root, check);
// this.textSprint(root, checkText);
if(prevRange){
prevRange.collapse(true);
cfi = renderer.currentChapter.cfiFromRange(prevRange);
map[page-1].end = cfi;
}
// Handle empty map
if(map.length === 1 && !map[0].start) {
range = this.doc.createRange();
@ -617,7 +620,7 @@ EPUBJS.Renderer.prototype.mapPage = function(){
map[0].start = cfi;
map[0].end = cfi;
}
// clean up
prevRange = null;
ranges = null;
@ -634,53 +637,53 @@ EPUBJS.Renderer.prototype.splitTextNodeIntoWordsRanges = function(node){
var rect;
var list;
pos = text.indexOf(" ");
if(pos === -1) {
range = this.doc.createRange();
range.selectNodeContents(node);
return [range];
}
range = this.doc.createRange();
range.setStart(node, 0);
range.setEnd(node, pos);
ranges.push(range);
range = false;
while ( pos != -1 ) {
pos = text.indexOf(" ", pos + 1);
if(pos > 0) {
if(range) {
range.setEnd(node, pos);
ranges.push(range);
}
range = this.doc.createRange();
range.setStart(node, pos+1);
}
}
if(range) {
range.setEnd(node, text.length);
ranges.push(range);
}
return ranges;
};
EPUBJS.Renderer.prototype.rangePosition = function(range){
var rect;
var list;
list = range.getClientRects();
if(list.length) {
rect = list[0];
return rect;
}
return null;
};
@ -694,14 +697,14 @@ EPUBJS.Renderer.prototype.getPageCfi = function(prevEl){
var y = 1;//;this.formated.pageHeight/2;
range = this.getRange(x, y);
// var test = this.doc.defaultView.getSelection();
// var r = this.doc.createRange();
// test.removeAllRanges();
// r.setStart(range.startContainer, range.startOffset);
// r.setEnd(range.startContainer, range.startOffset + 1);
// test.addRange(r);
return this.currentChapter.cfiFromRange(range);
};
*/
@ -732,7 +735,7 @@ EPUBJS.Renderer.prototype.getRange = function(x, y, forceElement){
this.visibileEl = this.findElementAfter(x, y);
range.setStart(this.visibileEl, 1);
}
// var test = this.doc.defaultView.getSelection();
// var r = this.doc.createRange();
// test.removeAllRanges();
@ -755,7 +758,7 @@ EPUBJS.Renderer.prototype.getVisibleRangeCfi = function(prevEl){
if(endRange) {
endCfi = this.currentChapter.cfiFromRange(endRange);
}
return {
start: startCfi,
end: endCfi || false
@ -809,9 +812,9 @@ EPUBJS.Renderer.prototype.getRenderedPagesLeft = function(){
console.warn("page map not loaded");
return false;
}
lastPage = this.pageMap.length;
if (this.spreads) {
pg = this.chapterPos*2;
} else {
@ -820,7 +823,7 @@ EPUBJS.Renderer.prototype.getRenderedPagesLeft = function(){
pagesLeft = lastPage - pg;
return pagesLeft;
};
EPUBJS.Renderer.prototype.getVisibleRangeCfi = function(){
@ -831,12 +834,12 @@ EPUBJS.Renderer.prototype.getVisibleRangeCfi = function(){
console.warn("page map not loaded");
return false;
}
if (this.spreads) {
pg = this.chapterPos*2;
startRange = this.pageMap[pg-2];
endRange = startRange;
if(this.layout.pageCount > 1) {
endRange = this.pageMap[pg-1];
}
@ -845,13 +848,13 @@ EPUBJS.Renderer.prototype.getVisibleRangeCfi = function(){
startRange = this.pageMap[pg-1];
endRange = startRange;
}
if(!startRange) {
console.warn("page range miss:", pg);
startRange = this.pageMap[this.pageMap.length-1];
endRange = startRange;
}
return {
start: startRange.start,
end: endRange.end
@ -867,7 +870,7 @@ EPUBJS.Renderer.prototype.gotoCfi = function(cfi){
if(_.isString(cfi)){
cfi = this.epubcfi.parse(cfi);
}
if(typeof document.evaluate === 'undefined') {
marker = this.epubcfi.addMarker(cfi, this.doc);
if(marker) {
@ -880,7 +883,7 @@ EPUBJS.Renderer.prototype.gotoCfi = function(cfi){
range = this.epubcfi.generateRangeFromCfi(cfi, this.doc);
if(range) {
pg = this.render.getPageNumberByRect(range.getBoundingClientRect());
this.page(pg);
this.page(pg);
}
}
};
@ -930,14 +933,14 @@ EPUBJS.Renderer.prototype.hideHashChanges = function(){
EPUBJS.Renderer.prototype.resize = function(width, height, setSize){
var spreads;
this.width = width;
this.height = height;
if(setSize !== false) {
this.render.resize(this.width, this.height);
}
spreads = this.determineSpreads(this.minSpreadWidth);
// Only re-layout if the spreads have switched
if(spreads != this.spreads){
@ -945,11 +948,11 @@ EPUBJS.Renderer.prototype.resize = function(width, height, setSize){
this.layoutMethod = this.determineLayout(this.layoutSettings);
this.layout = new EPUBJS.Layout[this.layoutMethod]();
}
if(this.contents){
this.reformat();
}
this.trigger("renderer:resized", {
width: this.width,
height: this.height
@ -1040,7 +1043,7 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
var items = this.contents.querySelectorAll(query),
resources = Array.prototype.slice.call(items),
count = resources.length;
if(count === 0) {
finished(false);
@ -1056,7 +1059,7 @@ EPUBJS.Renderer.prototype.replace = function(query, func, finished, progress){
called = true;
}
};
func(item, after);
}.bind(this));
@ -1138,4 +1141,4 @@ EPUBJS.Renderer.prototype.replaceWithStored = function(query, attr, func, callba
};
//-- Enable binding events to Renderer
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);
RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype);