1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

added hash changes

This commit is contained in:
Fred Chasen 2013-01-27 18:06:59 -08:00
parent 954aba9627
commit 1554dda914
4 changed files with 48 additions and 10 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -88,9 +88,12 @@ FPR.app.init = (function($){
//-- Provide the Book with the url to show //-- Provide the Book with the url to show
// The Url must be found in the books manifest // The Url must be found in the books manifest
Book.show(url);
e.preventDefault(); if(!Book.useHash){
Book.show(url);
e.preventDefault();
}
}); });
} }
@ -102,7 +105,7 @@ FPR.app.init = (function($){
contents.forEach(function(item){ contents.forEach(function(item){
var $subitems, var $subitems,
$wrapper = $("<li id='toc-"+item.id+"'>"), $wrapper = $("<li id='toc-"+item.id+"'>"),
$item = $("<a class='toc_link' href='#"+item.href+"' data-url='"+item.href+"'>"+item.label+"</a>"); $item = $("<a class='toc_link' href='#/"+item.href+"' data-url='"+item.href+"'>"+item.label+"</a>");
$wrapper.append($item); $wrapper.append($item);

View file

@ -26,11 +26,13 @@ FP.Book = function(elem, bookPath){
"beforeChapterDisplay" : [] "beforeChapterDisplay" : []
}; };
this.useHash = true;
this.initialize(this.el); this.initialize(this.el);
this.online = navigator.onLine; this.online = navigator.onLine;
this.listeners(); this.listeners();
//-- Determine storage method //-- Determine storage method
//-- Override options: none | ram | websql | indexedDB | filesystem //-- Override options: none | ram | websql | indexedDB | filesystem
this.determineStorageMethod(); this.determineStorageMethod();
@ -70,6 +72,9 @@ FP.Book.prototype.listeners = function(){
that.tell("book:online"); that.tell("book:online");
}, false); }, false);
window.addEventListener("hashchange", that.route.bind(this), false);
} }
//-- Check bookUrl and start parsing book Assets or load them from storage //-- Check bookUrl and start parsing book Assets or load them from storage
@ -462,6 +467,8 @@ FP.Book.prototype.chapterTitle = function(){
FP.Book.prototype.startDisplay = function(){ FP.Book.prototype.startDisplay = function(){
this.tell("book:bookReady"); this.tell("book:bookReady");
this.displayChapter(this.spinePos, function(chapter){ this.displayChapter(this.spinePos, function(chapter){
//-- If there is a saved page, and the pages haven't changed go to it //-- If there is a saved page, and the pages haven't changed go to it
@ -474,9 +481,14 @@ FP.Book.prototype.startDisplay = function(){
this.storeOffline(); this.storeOffline();
} }
//-- Go to hashed page if present
if(this.useHash){
this.route();
}
}.bind(this)); }.bind(this));
} }
FP.Book.prototype.show = function(url){ FP.Book.prototype.show = function(url){
@ -634,6 +646,18 @@ FP.Book.prototype.determineStorageMethod = function(override) {
FP.storage.storageMethod(method); FP.storage.storageMethod(method);
} }
FP.Book.prototype.route = function(){
var location = window.location.hash.replace('#/', '');
if(this.useHash && location.length && location != this.prevLocation){
this.show(location);
this.prevLocation = location;
}
}
FP.Book.prototype.hideHashChanges = function(){
this.useHash = false;
}
//-- Hooks allow for injecting async functions that must all complete before continuing //-- Hooks allow for injecting async functions that must all complete before continuing
// Functions must have a callback as their first argument. // Functions must have a callback as their first argument.
FP.Book.prototype.registerHook = function(type, toAdd){ FP.Book.prototype.registerHook = function(type, toAdd){

View file

@ -203,12 +203,17 @@ FP.Chapter.prototype.replaceLinks = function(callback){
links.forEach(function(link){ links.forEach(function(link){
var path, var path,
href = link.getAttribute("href"), href = link.getAttribute("href"),
relative = href.search("://"); relative = href.search("://"),
fragment = href[0] == "#";
if(relative != -1) return; //-- Only replace relative links if(relative != -1) return; //-- Only replace relative links
link.onclick = function(){ link.onclick = function(){
that.book.show(href); if(that.book.useHash){
window.location.hash = "#/"+href;
}else{
that.book.show(href);
}
} }
}); });
@ -266,10 +271,16 @@ FP.Chapter.prototype.page = function(pg){
//-- Find a section by fragement id //-- Find a section by fragement id
FP.Chapter.prototype.section = function(fragment){ FP.Chapter.prototype.section = function(fragment){
var el = this.doc.getElementById(fragment), var el = this.doc.getElementById(fragment),
left, pg;
if(el){
left = this.leftPos + el.offsetLeft, //-- Calculate left offset compaired to scrolled position left = this.leftPos + el.offsetLeft, //-- Calculate left offset compaired to scrolled position
pg = Math.floor(left / this.spreadWidth) + 1; //-- pages start at 1 pg = Math.floor(left / this.spreadWidth) + 1; //-- pages start at 1
this.page(pg); this.page(pg);
}
} }
FP.Chapter.prototype.beforeDisplay = function(callback){ FP.Chapter.prototype.beforeDisplay = function(callback){