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');
53 var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
54 dstDoc.write(contents);
58 function setupAnnotator(noteElement, readOnly) {
59 noteElement.annotator({readOnly: readOnly});
60 noteElement.annotator('addPlugin', 'Store', {
61 prefix: '/ajax/annotations',
71 function injectRemoteScript(url, noteframe, onload) {
72 var injectScript = noteframe.document.createElement("script");
73 injectScript.src = url;
74 injectScript.onload = onload;
75 noteframe.document.head.appendChild(injectScript);
78 function injectScript(scriptText, noteframe) {
79 var injectScript = noteframe.document.createElement("script");
80 injectScript.innerHTML = scriptText;
81 noteframe.document.body.appendChild(injectScript);
84 function injectRemoteCSS(url, noteframe) {
85 var injectCSS = noteframe.document.createElement("link");
87 injectCSS.type = 'text/css';
88 injectCSS.rel = 'stylesheet';
89 noteframe.document.head.appendChild(injectCSS);
92 function tabHandler(event) {
94 // key pressed was TAB
95 // key was pressed in last row
96 if (event.which == 9) {
97 var totalForms = parseInt($('#id_form-TOTAL_FORMS').attr('value'));
98 var formIndex = parseInt($(this).closest('div.keyword-form-row').data('index'));
99 if (formIndex === totalForms-1) {
101 event.preventDefault();
106 function addForm(event) {
107 var prototypeForm = $('#keyword-form-prototype div.keyword-form-row').clone().appendTo('#keyword-form-rows');
108 var newForm = $('.keyword-form-row:last');
109 var totalForms = $('#id_form-TOTAL_FORMS').attr('value');
110 var newIdRoot = 'id_form-' + totalForms + '-';
111 var newNameRoot = 'form-' + totalForms + '-';
113 newForm.data('index', totalForms);
115 var keywordInput = newForm.find('.keyword');
116 keywordInput.attr('id', newIdRoot + 'keyword');
117 keywordInput.attr('name', newNameRoot + 'keyword');
118 keywordInput.focus();
120 var definitionInput = newForm.find('.definition');
121 definitionInput.attr('id', newIdRoot + 'definition');
122 definitionInput.attr('name', newNameRoot + 'definition');
123 definitionInput.keydown(tabHandler);
125 var objectIdInput = newForm.find('.object-id');
126 objectIdInput.attr('id', newIdRoot + 'id');
127 objectIdInput.attr('name', newNameRoot + 'id');
129 $('#id_form-TOTAL_FORMS').attr('value', parseInt(totalForms)+1);
131 keywordInput.focus();
134 function initNoteContentPage() {
136 $("#thank-button").click(function(event) {
137 event.preventDefault();
139 // increment number in page right away
140 var thankNumber = $("#thank-number");
141 thankNumber.text(parseInt(thankNumber.text()) + 1);
143 // disable thank button so it can't
146 $('#thank-button-disabled').show();
147 $(this).unbind('click');
149 // tell server that somebody thanked
158 $("#flag-button").click(function(event) {
159 event.preventDefault();
161 if (confirm('Do you wish to flag this note for deletion?')) {
162 // disable thank button so it can't
165 $('#flag-button-disabled').show();
166 $(this).unbind('click');
168 // tell server that somebody flagged
178 $('#save_note_tags').click(function(event) {
180 url: edit_note_tags_url,
182 data: $('#note_tags_input').val(),
184 success: function(data) {
185 $('#note_tags_form').slideUp();
187 $.each(data.fields.tags, function(index, tag) {
188 $('.tags').append($('<span>', { class: 'tag-span', text: tag }));
190 $('#note-tag-dialog').foundation('reveal', 'close');
195 $("#note-download-button").click(function(event) {
196 if (confirm('It costs 2 karma points to download a note. Are you sure?')) {
197 // disable handler so it won't be run again
198 $(this).unbind('click');
200 // tell server that somebody downloaded
203 url: note_downloaded_url,
211 $('#delete-note-button').click(function (event) {
212 if (!confirm("Are you sure you want to delete this note?")) {
213 event.preventDefault();
217 // Embed the converted markdown if it is on the page, else default to the iframe
218 if ($('#note-markdown').length > 0) {
219 var note_markdown = $('#note-markdown');
220 note_markdown.html(marked(note_markdown.data('markdown')));
221 setupAnnotator(note_markdown, !user_authenticated);
223 $.ajax(note_contents_url, {
226 onprogress: function (progress) {
227 var percentage = Math.floor((progress.loaded / progress.total) * 100);
228 writeNoteFrame("<h3 style='text-align: center'>" + percentage + "%</h3>");
231 success: function(data, textStatus, jqXHR) {
232 writeNoteFrame(data);
234 // run setupAnnotator in frame context
235 var noteframe = document.getElementById('noteframe').contentWindow;
237 injectRemoteCSS(annotator_css_url, noteframe);
238 injectScript("csrf_token = '" + csrf_token + "';", noteframe);
240 injectRemoteScript("https://code.jquery.com/jquery-2.1.0.min.js", noteframe,
242 injectRemoteScript(setup_ajax_url, noteframe);
243 injectRemoteScript(annotator_js_url, noteframe,
245 var js = "$(function() { \
246 var document_selector = $('body'); \
247 if ($('#page-container').length > 0) { \
248 document_selector = $('#page-container'); \
250 document_selector.annotator({readOnly: " + !user_authenticated + "}); \
251 document_selector.annotator('addPlugin', 'Store', { \
252 prefix: '/ajax/annotations', \
254 'uri': " + note_id + " \
257 'uri': " + note_id + " \
260 injectScript(js, noteframe);
262 if (pdfControls == true) {
263 var pdfViewer = noteframe.pdf2htmlEX.defaultViewer;
264 $(noteframe.document).ready(function() {
265 setupPdfViewer(noteframe, pdfViewer);
271 error: function(data, textStatus, jqXHR) {
272 writeNoteFrame("<h3 style='text-align: center'>Sorry, your note could not be retrieved.</h3>");
278 function initNoteKeywordsPage() {
279 $('.definition').keydown(tabHandler);
280 $('#add-row-btn').click(addForm);
282 $('#keywords-data-table').dataTable({
283 // don't provide a option for the user to change the table page length
284 'bLengthChange': false,
288 $('#edit-keywords-button').click(function(event) {
289 $('#keywords-data-table').toggle();
290 $('#keyword-form').toggle();