From 624c9e823b5163e2754a71655a2e37f8bb5c04a0 Mon Sep 17 00:00:00 2001 From: Bala Clark Date: Thu, 8 Sep 2011 12:27:01 +0200 Subject: [PATCH] don't allow navigating to non-existant pages --- lib/ComicBook.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/ComicBook.js b/lib/ComicBook.js index 5843497..b224f4d 100755 --- a/lib/ComicBook.js +++ b/lib/ComicBook.js @@ -92,8 +92,11 @@ function ComicBook(id, srcs, opts) { var page_requested = false; // used to request non preloaded pages var shiv = false; + // page hash on first load + var hash = parseInt(location.hash.substring(1),10) - 1; + // the current page, can pass a default as a url hash - var pointer = (parseInt(location.hash.substring(1),10) - 1) || 0; + var pointer = (hash < srcs.length) ? hash : 0; /** * Gets the window.innerWidth - scrollbars @@ -388,7 +391,7 @@ function ComicBook(id, srcs, opts) { */ ComicBook.prototype.getPage = function(i) { - if (i < 0 || i > srcs.length) { + if (i < 0 || i > srcs.length-1) { throw ComicBookException.INVALID_PAGE+' '+i; } @@ -643,7 +646,13 @@ function ComicBook(id, srcs, opts) { */ ComicBook.prototype.drawNextPage = function () { - if (!this.getPage(pointer+1)) { return false; } + var page; + + try { + page = this.getPage(pointer+1); + } catch (e) {} + + if (!page) { return false; } if (pointer + 1 < pages.length) { pointer += (options.displayMode === "single" || is_double_page_spread) ? 1 : 2; @@ -658,7 +667,12 @@ function ComicBook(id, srcs, opts) { */ ComicBook.prototype.drawPrevPage = function () { - var page = this.getPage(pointer-1); + var page; + + try { + page = this.getPage(pointer-1); + } catch (e) {} + if (!page) { return false; } is_double_page_spread = (page.width > page.height); // need to run double page check again here as we are going backwards