From fa00ea442ed4e73ca02ddd4d22b1509c2fd27207 Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Mon, 25 Feb 2013 01:26:34 -0500 Subject: [PATCH 1/5] add first pass at multi-chapter support. --- common/static/js/pdfviewer.js | 10 +++--- lms/djangoapps/staticbook/views.py | 10 ++++-- lms/templates/static_pdfbook.html | 55 ++++++++++++++++++++++++++++++ lms/urls.py | 9 +++-- 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/common/static/js/pdfviewer.js b/common/static/js/pdfviewer.js index c25b1b2e4e..720d3fe6d6 100644 --- a/common/static/js/pdfviewer.js +++ b/common/static/js/pdfviewer.js @@ -44,9 +44,6 @@ PDFJS.disableWorker = true; var currentScaleValue = "0"; var DEFAULT_SCALE_VALUE = "1"; - // TESTING: - var destinations = null; - var setupText = function setupText(textdiv, content, viewport) { function getPageNumberFromDest(dest) { @@ -270,7 +267,8 @@ PDFJS.disableWorker = true; // // Asynchronously download PDF as an ArrayBuffer // - PDFJS.getDocument(url).then( + loadUrl = function pdfViewLoadUrl(url_to_load) { + PDFJS.getDocument(url).then( function getDocument(_pdfDocument) { pdfDocument = _pdfDocument; // display the current page with a default scale value: @@ -282,6 +280,9 @@ PDFJS.disableWorker = true; function getDocumentProgress(progressData) { // placeholder: not yet ready to display loading progress }); + }; + + loadUrl(url); $("#previous").click(function(event) { prevPage(); @@ -308,5 +309,6 @@ PDFJS.disableWorker = true; renderPage(newPageVal); } }); + return pdfViewer; } })(jQuery); diff --git a/lms/djangoapps/staticbook/views.py b/lms/djangoapps/staticbook/views.py index be7e272596..319e2a9074 100644 --- a/lms/djangoapps/staticbook/views.py +++ b/lms/djangoapps/staticbook/views.py @@ -52,13 +52,19 @@ def pdf_index(request, course_id, book_index, chapter=None, page=None): # strip off the quotes again... return output_url[1:-1] - textbook['url'] = remap_static_url(textbook['url'], course) + if 'url' in textbook: + textbook['url'] = remap_static_url(textbook['url'], course) # then remap all the chapter URLs as well, if they are provided. - + if 'chapters' in textbook: + for entry in textbook['chapters']: + entry['url'] = remap_static_url(entry['url'], course) + + return render_to_response('static_pdfbook.html', {'book_index': book_index, 'course': course, 'textbook': textbook, + 'chapter': chapter, 'page': page, 'chapter': chapter, 'staff_access': staff_access}) diff --git a/lms/templates/static_pdfbook.html b/lms/templates/static_pdfbook.html index 386d921b90..051e752bdd 100644 --- a/lms/templates/static_pdfbook.html +++ b/lms/templates/static_pdfbook.html @@ -17,6 +17,7 @@ <%block name="js_extra"> @@ -35,6 +64,7 @@
+
@@ -88,6 +118,31 @@
+%if 'chapters' in textbook: +
+ +
    + <%def name="print_entry(entry)"> +
  • + + + ${entry.get('title')} + + +
  • + + + % for entry in textbook['chapters']: + ${print_entry(entry)} + % endfor + + ## Don't delete this empty list item. Without it, Jquery.TreeView won't + ## render the last list item as expandable. +
  • +
+
+%endif +
diff --git a/lms/urls.py b/lms/urls.py index 4d9798f360..e8b4b17c3c 100644 --- a/lms/urls.py +++ b/lms/urls.py @@ -280,11 +280,10 @@ if settings.COURSEWARE_ENABLED: url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/pdfbook/(?P[^/]*)/(?P[^/]*)$', 'staticbook.views.pdf_index'), -# Doesn't yet support loading individual chapters... -# url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/pdfbook/(?P[^/]*)/chapter/(?P[^/]*)/$', -# 'staticbook.views.pdf_index'), -# url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/pdfbook/(?P[^/]*)/chapter/(?P[^/]*)/(?P[^/]*)$', -# 'staticbook.views.pdf_index'), + url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/pdfbook/(?P[^/]*)/chapter/(?P[^/]*)/$', + 'staticbook.views.pdf_index'), + url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/pdfbook/(?P[^/]*)/chapter/(?P[^/]*)/(?P[^/]*)$', + 'staticbook.views.pdf_index'), url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/courseware/?$', 'courseware.views.index', name="courseware"), From dde2cd0bd0e14675c8eac186273ae9b9d8caa16f Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Mon, 25 Feb 2013 16:52:32 -0500 Subject: [PATCH 2/5] get chapter links to start working --- common/static/js/pdfviewer.js | 120 +++++++++++++++++++---------- lms/djangoapps/staticbook/views.py | 1 - lms/templates/static_pdfbook.html | 82 ++++++++------------ 3 files changed, 109 insertions(+), 94 deletions(-) diff --git a/common/static/js/pdfviewer.js b/common/static/js/pdfviewer.js index 720d3fe6d6..85570a4051 100644 --- a/common/static/js/pdfviewer.js +++ b/common/static/js/pdfviewer.js @@ -27,7 +27,20 @@ PDFJS.disableWorker = true; var pdfViewer = this; var pdfDocument = null; - var url = options['url']; + var url = null; + if (options.url) { + url = options.url; + } + var chapter_urls = null; + if (options.chapters) { + chapter_urls = options.chapters; + } + var chapterNum = 1; + if (options.chapterNum) { + chapterNum = options.chapterNum; + // TODO: this should only be specified if there are + // chapters, and it should be in-bounds. + } var pageNum = 1; if (options.pageNum) { pageNum = options.pageNum; @@ -47,25 +60,25 @@ PDFJS.disableWorker = true; 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; - } + 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) { - // get page number from dest: - destPage = getPageNumberFromDest(dest); + // get page number from dest: + destPage = getPageNumberFromDest(dest); link.href = '#page=' + destPage; link.onclick = function pageViewSetupLinksOnclick() { if (dest && dest instanceof Array ) @@ -135,10 +148,10 @@ PDFJS.disableWorker = true; // Get page info from document, resize canvas accordingly, and render page // renderPage = function(num) { - // don't try to render a page that cannot be rendered - if (num < 1 || num > pdfDocument.numPages) { - return; - } + // 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 }); @@ -268,21 +281,25 @@ PDFJS.disableWorker = true; // Asynchronously download PDF as an ArrayBuffer // loadUrl = function pdfViewLoadUrl(url_to_load) { - PDFJS.getDocument(url).then( - function getDocument(_pdfDocument) { - pdfDocument = _pdfDocument; - // display the current page with a default scale value: - parseScale(DEFAULT_SCALE_VALUE); - }, - function getDocumentError(message, exception) { - // placeholder: don't expect errors :) - }, - function getDocumentProgress(progressData) { - // placeholder: not yet ready to display loading progress - }); - }; + PDFJS.getDocument(url_to_load).then( + function getDocument(_pdfDocument) { + pdfDocument = _pdfDocument; + // display the current page with a default scale value: + currentScale = UNKNOWN_SCALE; + parseScale(DEFAULT_SCALE_VALUE); + }, + function getDocumentError(message, exception) { + // placeholder: don't expect errors :) + }, + function getDocumentProgress(progressData) { + // placeholder: not yet ready to display loading progress + }); + }; - loadUrl(url); + loadChapterUrl = function pdfViewLoadChapterUrl(chapter_index) { + var chapter_url = chapter_urls[chapter_index]; + loadUrl(chapter_url); + } $("#previous").click(function(event) { prevPage(); @@ -303,12 +320,33 @@ PDFJS.disableWorker = true; parseScale(this.value); }); + $('#pageNumber').change(function(event) { - var newPageVal = parseInt(this.value); - if (newPageVal) { - renderPage(newPageVal); - } + var newPageVal = parseInt(this.value); + if (newPageVal) { + renderPage(newPageVal); + } }); - return pdfViewer; + + // define navigation links for chapters: + if (chapter_urls != null) { + var loadChapterUrlHelper = function(i) { + return function(event) { + loadChapterUrl(i); + }; + }; + for (var index = 1; index <= chapter_urls.length; index += 1) { + $("#pdfchapter-" + index).click(loadChapterUrlHelper(index)); + } + } + + // finally, load the appropriate page + if (url != null) { + loadUrl(url); + } else { + loadChapterUrl(chapterNum); + } + + return pdfViewer; } })(jQuery); diff --git a/lms/djangoapps/staticbook/views.py b/lms/djangoapps/staticbook/views.py index 319e2a9074..a391b1cb32 100644 --- a/lms/djangoapps/staticbook/views.py +++ b/lms/djangoapps/staticbook/views.py @@ -66,5 +66,4 @@ def pdf_index(request, course_id, book_index, chapter=None, page=None): 'textbook': textbook, 'chapter': chapter, 'page': page, - 'chapter': chapter, 'staff_access': staff_access}) diff --git a/lms/templates/static_pdfbook.html b/lms/templates/static_pdfbook.html index 051e752bdd..526232802f 100644 --- a/lms/templates/static_pdfbook.html +++ b/lms/templates/static_pdfbook.html @@ -17,45 +17,27 @@ <%block name="js_extra"> @@ -121,25 +103,21 @@ %if 'chapters' in textbook:
-
    - <%def name="print_entry(entry)"> -
  • - - - ${entry.get('title')} - - -
  • +
    + <%def name="print_entry(entry, index_value)"> +
    + + ${entry.get('title')} + +
    + <% index = 0 %> % for entry in textbook['chapters']: - ${print_entry(entry)} + <% index += 1 %> + ${print_entry(entry, index)} % endfor - - ## Don't delete this empty list item. Without it, Jquery.TreeView won't - ## render the last list item as expandable. -
  • -
+
%endif From fc9ab346e16d27c1b787e515c389d071c6b2f305 Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Tue, 26 Feb 2013 01:03:01 -0500 Subject: [PATCH 3/5] 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); From 19567cc3d5a594a3897b0b538451262a865a4f6d Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Tue, 26 Feb 2013 15:19:23 -0500 Subject: [PATCH 4/5] update styling of pdfs to be more like textbooks --- lms/templates/static_pdfbook.html | 32 +++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lms/templates/static_pdfbook.html b/lms/templates/static_pdfbook.html index 176f25d1f4..53b018302d 100644 --- a/lms/templates/static_pdfbook.html +++ b/lms/templates/static_pdfbook.html @@ -41,17 +41,20 @@ +<% use_nav_images = False %> + <%include file="/courseware/course_navigation.html" args="active_page='pdftextbook/{0}'.format(book_index)" />
-
+
+%if use_nav_images != True: @@ -59,6 +62,7 @@ +%endif
@@ -103,13 +107,13 @@ %if 'chapters' in textbook:
-
+
    <%def name="print_entry(entry, index_value)"> - + + <% index = 0 %> @@ -117,11 +121,23 @@ <% index += 1 %> ${print_entry(entry, index)} % endfor -
+
%endif -
+
+%if use_nav_images: + +%endif
From 310073ddefc78b116f485729f90d0014c22b9179 Mon Sep 17 00:00:00 2001 From: Brian Wilson Date: Tue, 26 Feb 2013 16:43:48 -0500 Subject: [PATCH 5/5] use same images for prev/next as used in image-based textbooks --- common/static/css/pdfviewer.css | 5 ++++- lms/static/sass/course/_textbook.scss | 1 + lms/templates/static_pdfbook.html | 24 ++++-------------------- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/common/static/css/pdfviewer.css b/common/static/css/pdfviewer.css index 716db251f0..656bc47c29 100644 --- a/common/static/css/pdfviewer.css +++ b/common/static/css/pdfviewer.css @@ -85,13 +85,16 @@ select { } #viewerContainer { - overflow: auto; +/* overflow: auto; */ box-shadow: inset 1px 0 0 hsla(0,0%,100%,.05); /* position: absolute; top: 32px; right: 0; bottom: 0; left: 0; */ +/* switch to using these instead: */ + position: relative; + overflow: hidden; } .toolbar { diff --git a/lms/static/sass/course/_textbook.scss b/lms/static/sass/course/_textbook.scss index 4bda5a46d6..af9c2493fd 100644 --- a/lms/static/sass/course/_textbook.scss +++ b/lms/static/sass/course/_textbook.scss @@ -102,6 +102,7 @@ div.book-wrapper { position: absolute; height: 100%; width: flex-grid(2, 8); + z-index: 1; a { background-color: rgba(#000, .7); diff --git a/lms/templates/static_pdfbook.html b/lms/templates/static_pdfbook.html index 53b018302d..013413b62d 100644 --- a/lms/templates/static_pdfbook.html +++ b/lms/templates/static_pdfbook.html @@ -41,11 +41,8 @@ -<% use_nav_images = False %> - <%include file="/courseware/course_navigation.html" args="active_page='pdftextbook/{0}'.format(book_index)" /> -
@@ -53,17 +50,6 @@
-
-%if use_nav_images != True: - -
- -%endif -
@@ -116,17 +102,15 @@ - <% index = 0 %> - % for entry in textbook['chapters']: - <% index += 1 %> - ${print_entry(entry, index)} + % for (index, entry) in enumerate(textbook['chapters']): + ${print_entry(entry, index+1)} % endfor %endif
-%if use_nav_images: + -%endif +