2 function rescalePdf(viewer) {
4 var outlineWidth = 250;
5 var frameWidth = parseInt($('#note_container')[0].clientWidth);
6 var pdfWidth = frameWidth;
8 if ($(viewer.sidebar).hasClass('opened')){
9 pdfWidth = pdfWidth - 250;
12 var newPdfScale = pdfWidth / scaleBase;
13 viewer.rescale(newPdfScale);
16 function setupPdfViewer(noteframe, pdfViewer) {
18 $('#plus-btn').click(function (){
19 pdfViewer.rescale(1.20, true, [0,0]);
22 $('#minus-btn').click(function (){
23 pdfViewer.rescale(0.80, true, [0,0]);
26 // detect if the PDF viewer wants to show an outline
28 if ($(pdfViewer.sidebar).hasClass('opened')) {
29 var body = $(document.body);
30 // if the screen is less than 64em wide, hide the outline
31 if (parseInt($(body.width()).toEm({scope: body})) < 64) {
32 $(pdfViewer.sidebar).removeClass('opened');
36 $('#outline-btn').click(function() {
37 $(pdfViewer.sidebar).toggleClass('opened');
38 // rescale the PDF to fit the available space
39 rescalePdf(pdfViewer);
42 $('#scroll-to').change(function() {
43 page = parseInt($(this).val());
44 pdfViewer.scroll_to(page, [0,0]);
47 // rescale the PDF to fit the available space
48 rescalePdf(pdfViewer);
51 function writeNoteFrame(contents) {
52 var dstFrame = document.getElementById('noteframe');
54 var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
55 dstDoc.write(contents);
60 function setupAnnotator(noteElement, readOnly) {
61 noteElement.annotator({readOnly: readOnly});
62 noteElement.annotator('addPlugin', 'Store', {
63 prefix: '/ajax/annotations',
73 function injectRemoteScript(url, noteframe, onload) {
74 var injectScript = noteframe.document.createElement("script");
75 injectScript.src = url;
76 injectScript.onload = onload;
77 noteframe.document.head.appendChild(injectScript);
80 function injectScript(scriptText, noteframe) {
81 var injectScript = noteframe.document.createElement("script");
82 injectScript.innerHTML = scriptText;
83 noteframe.document.body.appendChild(injectScript);
86 function injectRemoteCSS(url, noteframe) {
87 var injectCSS = noteframe.document.createElement("link");
89 injectCSS.type = 'text/css';
90 injectCSS.rel = 'stylesheet';
91 noteframe.document.head.appendChild(injectCSS);
94 function tabHandler(event) {
96 // key pressed was TAB
97 // key was pressed in last row
98 if (event.which == 9) {
99 var totalForms = parseInt($('#id_form-TOTAL_FORMS').attr('value'));
100 var formIndex = parseInt($(this).closest('div.keyword-form-row').data('index'));
101 if (formIndex === totalForms-1) {
103 event.preventDefault();
108 function addForm(event) {
109 var prototypeForm = $('#keyword-form-prototype div.keyword-form-row').clone().appendTo('#keyword-form-rows');
110 var newForm = $('.keyword-form-row:last');
111 var totalForms = $('#id_form-TOTAL_FORMS').attr('value');
112 var newIdRoot = 'id_form-' + totalForms + '-';
113 var newNameRoot = 'form-' + totalForms + '-';
115 newForm.data('index', totalForms);
117 var keywordInput = newForm.find('.keyword');
118 keywordInput.attr('id', newIdRoot + 'keyword');
119 keywordInput.attr('name', newNameRoot + 'keyword');
120 keywordInput.focus();
122 var definitionInput = newForm.find('.definition');
123 definitionInput.attr('id', newIdRoot + 'definition');
124 definitionInput.attr('name', newNameRoot + 'definition');
125 definitionInput.keydown(tabHandler);
127 var objectIdInput = newForm.find('.object-id');
128 objectIdInput.attr('id', newIdRoot + 'id');
129 objectIdInput.attr('name', newNameRoot + 'id');
131 $('#id_form-TOTAL_FORMS').attr('value', parseInt(totalForms)+1);
133 keywordInput.focus();
136 function initNoteContentPage() {
138 $("#thank-button").click(function(event) {
139 event.preventDefault();
141 // increment number in page right away
142 var thankNumber = $("#thank-number");
143 thankNumber.text(parseInt(thankNumber.text()) + 1);
145 // disable thank button so it can't
148 $('#thank-button-disabled').show();
149 $(this).unbind('click');
151 // tell server that somebody thanked
160 $("#flag-button").click(function(event) {
161 event.preventDefault();
163 if (confirm('Do you wish to flag this note for deletion?')) {
164 // disable thank button so it can't
167 $('#flag-button-disabled').show();
168 $(this).unbind('click');
170 // tell server that somebody flagged
180 $('#save_note_tags').click(function(event) {
182 url: edit_note_tags_url,
184 data: $('#note_tags_input').val(),
186 success: function(data) {
187 $('#note_tags_form').slideUp();
189 $.each(data.fields.tags, function(index, tag) {
190 $('.tags').append($('<span>', { class: 'tag-span', text: tag }));
192 $('#note-tag-dialog').foundation('reveal', 'close');
197 $("#note-download-button").click(function(event) {
198 if (confirm('It costs 2 karma points to download a note. Are you sure?')) {
199 // disable handler so it won't be run again
200 $(this).unbind('click');
202 // tell server that somebody downloaded
205 url: note_downloaded_url,
213 $('#delete-note-button').click(function (event) {
214 if (!confirm("Are you sure you want to delete this note?")) {
215 event.preventDefault();
219 // Embed the converted markdown if it is on the page, else default to the iframe
220 if ($('#note-markdown').length > 0) {
221 var note_markdown = $('#note-markdown');
222 note_markdown.html(marked(note_markdown.data('markdown')));
223 setupAnnotator(note_markdown, !user_authenticated);
225 $.ajax(note_contents_url, {
228 onprogress: function (progress) {
229 var percentage = Math.floor((progress.loaded / progress.total) * 100);
230 writeNoteFrame("<h3 style='text-align: center'>" + percentage + "%</h3>");
233 success: function(data, textStatus, jqXHR) {
234 writeNoteFrame(data);
236 // run setupAnnotator in frame context
237 var parentFrame = document.getElementById('noteframe');
241 var noteframe = parentFrame.contentWindow;
243 injectRemoteCSS(annotator_css_url, noteframe);
244 injectScript("csrf_token = '" + csrf_token + "';", noteframe);
246 injectRemoteScript("https://code.jquery.com/jquery-2.1.0.min.js", noteframe,
248 injectRemoteScript(setup_ajax_url, noteframe);
249 injectRemoteScript(annotator_js_url, noteframe,
251 var js = "$(function() { \
252 var document_selector = $('body'); \
253 if ($('#page-container').length > 0) { \
254 document_selector = $('#page-container'); \
256 document_selector.annotator({readOnly: " + !user_authenticated + "}); \
257 document_selector.annotator('addPlugin', 'Store', { \
258 prefix: '/ajax/annotations', \
260 'uri': " + note_id + " \
263 'uri': " + note_id + " \
266 injectScript(js, noteframe);
268 if (pdfControls == true) {
269 var pdfViewer = noteframe.pdf2htmlEX.defaultViewer;
270 $(noteframe.document).ready(function() {
271 setupPdfViewer(noteframe, pdfViewer);
277 error: function(data, textStatus, jqXHR) {
278 writeNoteFrame("<h3 style='text-align: center'>Sorry, your note could not be retrieved.</h3>");
284 function initNoteKeywordsPage() {
285 $('.definition').keydown(tabHandler);
286 $('#add-row-btn').click(addForm);
288 $('#keywords-data-table').dataTable({
289 // don't provide a option for the user to change the table page length
290 'bLengthChange': false,
294 $('#edit-keywords-button').click(function(event) {
295 $('#keywords-data-table').toggle();
296 $('#keyword-form').toggle();
301 function markQuestionCorrect(question) {
302 question.find('.quiz-question').addClass('correct');
303 question.find('.quiz-question').removeClass('incorrect');
304 question.find('.correct-label').show();
305 question.find('.incorrect-label').hide();
308 function markQuestionIncorrect(question) {
309 question.find('.quiz-question').addClass('incorrect');
310 question.find('.quiz-question').removeClass('correct');
311 question.find('.incorrect-label').show();
312 question.find('.correct-label').hide();
315 function initQuizPage() {
316 $('#check-answers-button').click(function() {
317 $('.quiz-question-wrapper').each(function() {
318 var choice = $(this).find('input:checked');
320 if (choice.data('correct') == true) {
321 markQuestionCorrect($(this));
323 markQuestionIncorrect($(this));
328 var options_selected = $(this).find('option:selected');
329 if (options_selected.length > 0) {
330 var matching_correct = true;
331 options_selected.each(function() {
332 if ($(this).data('correct') == false) {
333 matching_correct = false;
336 if (matching_correct) {
337 markQuestionCorrect($(this));
339 markQuestionIncorrect($(this));