Commit dde2cd0b by Brian Wilson

get chapter links to start working

parent fa00ea44
......@@ -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
});
};
loadUrl(url);
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
});
};
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);
......@@ -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})
......@@ -17,45 +17,27 @@
<%block name="js_extra">
<script type="text/javascript">
%if 'url' in textbook:
var url = "${textbook['url']}";
$(document).ready(function() {
$('#outerContainer').PDFViewer( {
% if page is not None:
'pageNum' : ${page},
% endif
'url' : url
});
var options = {};
%if 'url' in textbook:
options.url = "${textbook['url']}";
%endif
%if 'chapters' in textbook:
var chptrs = [];
%for chap in textbook['chapters']:
chptrs.push("${chap['url']}");
%endfor
options.chapters = chptrs;
%endif
%if chapter is not None:
options.chapterNum : ${chapter};
%endif
%if page is not None:
options.pageNum : ${page};
%endif
$('#outerContainer').PDFViewer(options);
});
%else:
var my_pdfviewer = null;
function load_url(url_to_load, page_to_load) {
// $('#outerContainer').PDFViewer( {
// 'pageNum' : page_to_load,
// 'url' : url_to_load
// });
my_pdfviewer.loadUrl(url_to_load, page_to_load);
}
// since we have no url, we must rely on chapter display,
// so make sure we have a value.
var url = "${ textbook['chapters'][chapter-1 if chapter is not None else 0]['url'] }";
$(document).ready(function() {
// load_url(url, ${page if page is not None else 1});
var my_pdfviewer = $('#outerContainer').PDFViewer( {
'pageNum' : ${page if page is not None else 1},
'url' : url
});
if (my_pdfviewer) {
}
} );
%endif
</script>
</%block>
......@@ -121,25 +103,21 @@
%if 'chapters' in textbook:
<section aria-label="Textbook Navigation" class="book-sidebar">
<ul id="pdfbooknav" class="treeview-booknav">
<%def name="print_entry(entry)">
<li>
<a href="javascript:load_url(${entry.get('url')}, 1)">
<span class="chapter">
${entry.get('title')}
</span>
</a>
</li>
<div id="pdfbooknav" class="treeview-booknav">
<%def name="print_entry(entry, index_value)">
<div id="pdfchapter-${index_value}">
<span class="chapter">
${entry.get('title')}
</span>
</div>
</%def>
<% 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.
<li></li>
</ul>
</div>
</section>
%endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment