From fc9ab346e16d27c1b787e515c389d071c6b2f305 Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Tue, 26 Feb 2013 01:03:01 -0500 Subject: [PATCH] fix loading of specific pages. --- common/static/js/pdfviewer.js | 61 +++++++++++++++++++------------ lms/templates/static_pdfbook.html | 4 +- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/common/static/js/pdfviewer.js b/common/static/js/pdfviewer.js index 85570a4051..c81709fa0f 100644 --- a/common/static/js/pdfviewer.js +++ b/common/static/js/pdfviewer.js @@ -27,25 +27,28 @@ PDFJS.disableWorker = true; var pdfViewer = this; var pdfDocument = null; - var url = null; + var urlToLoad = null; if (options.url) { - url = options.url; + urlToLoad = options.url; } - var chapter_urls = null; + var chapterUrls = null; if (options.chapters) { - chapter_urls = options.chapters; + chapterUrls = options.chapters; } - var chapterNum = 1; + var chapterToLoad = 1; if (options.chapterNum) { - chapterNum = options.chapterNum; // TODO: this should only be specified if there are // chapters, and it should be in-bounds. + chapterToLoad = options.chapterNum; } - var pageNum = 1; + var pageToLoad = 1; if (options.pageNum) { - pageNum = options.pageNum; + pageToLoad = options.pageNum; } + var chapterNum = 1; + var pageNum = 1; + var viewerElement = document.getElementById('viewer'); var ANNOT_MIN_SIZE = 10; var DEFAULT_SCALE_DELTA = 1.1; @@ -280,13 +283,21 @@ PDFJS.disableWorker = true; // // Asynchronously download PDF as an ArrayBuffer // - loadUrl = function pdfViewLoadUrl(url_to_load) { - PDFJS.getDocument(url_to_load).then( + loadUrl = function pdfViewLoadUrl(url, page) { + PDFJS.getDocument(url).then( function getDocument(_pdfDocument) { pdfDocument = _pdfDocument; - // display the current page with a default scale value: - currentScale = UNKNOWN_SCALE; - parseScale(DEFAULT_SCALE_VALUE); + pageNum = page; + // if the scale has not been set before, set it now. + // Otherwise, don't change the current scale, + // but make sure it gets refreshed. + if (currentScale == UNKNOWN_SCALE) { + parseScale(DEFAULT_SCALE_VALUE); + } else { + var preservedScale = currentScale; + currentScale = UNKNOWN_SCALE; + parseScale(preservedScale); + } }, function getDocumentError(message, exception) { // placeholder: don't expect errors :) @@ -296,9 +307,12 @@ PDFJS.disableWorker = true; }); }; - loadChapterUrl = function pdfViewLoadChapterUrl(chapter_index) { - var chapter_url = chapter_urls[chapter_index]; - loadUrl(chapter_url); + loadChapterUrl = function pdfViewLoadChapterUrl(chapterNum, pageVal) { + if (chapterNum < 1 || chapterNum > chapterUrls.length) { + return; + } + var chapterUrl = chapterUrls[chapterNum-1]; + loadUrl(chapterUrl, pageVal); } $("#previous").click(function(event) { @@ -329,22 +343,23 @@ PDFJS.disableWorker = true; }); // define navigation links for chapters: - if (chapter_urls != null) { + if (chapterUrls != null) { var loadChapterUrlHelper = function(i) { return function(event) { - loadChapterUrl(i); + // when opening a new chapter, always open the first page: + loadChapterUrl(i, 1); }; }; - for (var index = 1; index <= chapter_urls.length; index += 1) { + for (var index = 1; index <= chapterUrls.length; index += 1) { $("#pdfchapter-" + index).click(loadChapterUrlHelper(index)); } } - // finally, load the appropriate page - if (url != null) { - loadUrl(url); + // finally, load the appropriate url/page + if (urlToLoad != null) { + loadUrl(urlToLoad, pageToLoad); } else { - loadChapterUrl(chapterNum); + loadChapterUrl(chapterToLoad, pageToLoad); } return pdfViewer; diff --git a/lms/templates/static_pdfbook.html b/lms/templates/static_pdfbook.html index 526232802f..176f25d1f4 100644 --- a/lms/templates/static_pdfbook.html +++ b/lms/templates/static_pdfbook.html @@ -30,10 +30,10 @@ options.chapters = chptrs; %endif %if chapter is not None: - options.chapterNum : ${chapter}; + options.chapterNum = ${chapter}; %endif %if page is not None: - options.pageNum : ${page}; + options.pageNum = ${page}; %endif $('#outerContainer').PDFViewer(options);