From d5e34b0a44670604534a4bbc44a99a650e898204 Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Thu, 21 Feb 2013 15:35:03 -0500 Subject: [PATCH] fix initialization of scale dropdown, and check for valid values of page number --- common/static/css/pdfviewer.css | 2 +- common/static/js/pdfviewer.js | 60 ++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/common/static/css/pdfviewer.css b/common/static/css/pdfviewer.css index 744c53ca92..716db251f0 100644 --- a/common/static/css/pdfviewer.css +++ b/common/static/css/pdfviewer.css @@ -590,7 +590,7 @@ select { margin: 4px 2px 4px 0; border: 1px solid transparent; border-radius: 2px; -/* color: hsl(0,0%,85%); can't get background to work, so let this be black... */ + color: hsl(0,0%,85%); font-size: 12px; line-height: 14px; text-align: left; diff --git a/common/static/js/pdfviewer.js b/common/static/js/pdfviewer.js index 227d1c584c..c25b1b2e4e 100644 --- a/common/static/js/pdfviewer.js +++ b/common/static/js/pdfviewer.js @@ -32,8 +32,6 @@ PDFJS.disableWorker = true; if (options.pageNum) { pageNum = options.pageNum; } - var currentScale = 1.0; - var currentScaleValue = "1"; var viewerElement = document.getElementById('viewer'); var ANNOT_MIN_SIZE = 10; @@ -42,15 +40,41 @@ PDFJS.disableWorker = true; var MIN_SCALE = 0.25; var MAX_SCALE = 4.0; + var currentScale = UNKNOWN_SCALE; + var currentScaleValue = "0"; + var DEFAULT_SCALE_VALUE = "1"; + + // TESTING: + var destinations = null; + var setupText = function setupText(textdiv, content, viewport) { + + function getPageNumberFromDest(dest) { + var destPage = 1; + if (dest instanceof Array) { + var destRef = dest[0]; + if (destRef instanceof Object) { + // we would need to look this up in the + // list of all pages that have been loaded, + // but we're trying to not have to load all the pages + // right now. + // destPage = this.pagesRefMap[destRef.num + ' ' + destRef.gen + ' R']; + } else { + destPage = (destRef + 1); + } + } + return destPage; + } + function bindLink(link, dest) { - // TODO: get this to work for document-internal links - // link.href = PDFView.getDestinationHash(dest); - // link.onclick = function pageViewSetupLinksOnclick() { - // if (dest) - // PDFView.navigateTo(dest); - // return false; - // }; + // get page number from dest: + destPage = getPageNumberFromDest(dest); + link.href = '#page=' + destPage; + link.onclick = function pageViewSetupLinksOnclick() { + if (dest && dest instanceof Array ) + renderPage(destPage); + return false; + }; } function createElementWithStyle(tagName, item, rect) { @@ -109,11 +133,16 @@ PDFJS.disableWorker = true; } }); } + // // Get page info from document, resize canvas accordingly, and render page // - // function renderPage(num) { renderPage = function(num) { + // don't try to render a page that cannot be rendered + if (num < 1 || num > pdfDocument.numPages) { + return; + } + // Update logging: log_event("book", { "type" : "gotopage", "old" : pageNum, "new" : num }); @@ -176,7 +205,6 @@ PDFJS.disableWorker = true; log_event("book", { "type" : "nextpage", "new" : pageNum }); } - // Check to see if selectScaleOption = function(value) { var options = $('#scaleSelect options'); var predefinedValueFound = false; @@ -245,7 +273,8 @@ PDFJS.disableWorker = true; PDFJS.getDocument(url).then( function getDocument(_pdfDocument) { pdfDocument = _pdfDocument; - renderPage(pageNum); + // display the current page with a default scale value: + parseScale(DEFAULT_SCALE_VALUE); }, function getDocumentError(message, exception) { // placeholder: don't expect errors :) @@ -272,5 +301,12 @@ PDFJS.disableWorker = true; $('#scaleSelect').change(function(event) { parseScale(this.value); }); + + $('#pageNumber').change(function(event) { + var newPageVal = parseInt(this.value); + if (newPageVal) { + renderPage(newPageVal); + } + }); } })(jQuery);