From 66515b2e9c234041175b3fa5a93d40a9d7d8cd35 Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Sun, 27 Jan 2013 19:12:03 -0800 Subject: [PATCH] route on start --- fpjs/render/book.js | 54 ++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/fpjs/render/book.js b/fpjs/render/book.js index 77d8b6e..488fc53 100644 --- a/fpjs/render/book.js +++ b/fpjs/render/book.js @@ -464,34 +464,38 @@ FP.Book.prototype.chapterTitle = function(){ return this.spine[this.spinePos].id; //-- TODO: clarify that this is returning title } -FP.Book.prototype.startDisplay = function(){ +FP.Book.prototype.startDisplay = function(chapter){ + var routed, + loaded = function(chapter){ + + //-- If there is a saved page, and the pages haven't changed go to it + if(this.prevChapterPos && this.prevDisplayedPages == chapter.displayedPages){ + chapter.page(this.prevChapterPos); + } + + //-- If there is network connection, store the books contents + if(this.online && !this.contained){ + this.storeOffline(); + } + + }.bind(this); this.tell("book:bookReady"); + //-- Go to hashed page if present + if(this.useHash){ + routed = this.route(loaded); + } + + if(!this.useHash || !routed){ + this.displayChapter(this.spinePos, loaded); + } - this.displayChapter(this.spinePos, function(chapter){ - - //-- If there is a saved page, and the pages haven't changed go to it - if(this.prevChapterPos && this.prevDisplayedPages == chapter.displayedPages){ - chapter.page(this.prevChapterPos); - } - - //-- If there is network connection, store the books contents - if(this.online && !this.contained){ - this.storeOffline(); - } - - //-- Go to hashed page if present - if(this.useHash){ - this.route(); - } - - }.bind(this)); } -FP.Book.prototype.show = function(url){ +FP.Book.prototype.show = function(url, callback){ var split = url.split("#"), chapter = split[0], section = split[1] || false, @@ -506,14 +510,16 @@ FP.Book.prototype.show = function(url){ //-- Check that URL is present in the index, or stop if(typeof(spinePos) != "number") return false; - if(spinePos != this.spinePos){ + if(spinePos != this.spinePos || !this.currentChapter){ //-- Load new chapter if different than current this.displayChapter(spinePos, function(chap){ if(section) chap.section(section); + if(callback) callback(chap); }); }else{ //-- Only goto section if(section) this.currentChapter.section(section); + if(callback) callback(this.currentChapter); } } @@ -646,12 +652,14 @@ FP.Book.prototype.determineStorageMethod = function(override) { FP.storage.storageMethod(method); } -FP.Book.prototype.route = function(){ +FP.Book.prototype.route = function(callback){ var location = window.location.hash.replace('#/', ''); if(this.useHash && location.length && location != this.prevLocation){ - this.show(location); + this.show(location, callback); this.prevLocation = location; + return true; } + return false; } FP.Book.prototype.hideHashChanges = function(){