-// resize the iframe based on internal contents on page load
-function autoResize(id){
- var newheight;
- var newwidth;
-
- if(document.getElementById){
- newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
- newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
- }
-
- document.getElementById(id).height = (newheight + 10) + "px";
- document.getElementById(id).width= (newwidth + 5) + "px";
-}
function rescalePdf(viewer, frameWidth) {
var scaleBase = 750;
viewer.rescale(newPdfScale);
}
-function setupPdfViewer() {
- var noteFrame = document.getElementById("noteframe")
- var pdfViewer = noteFrame.contentWindow.pdf2htmlEX.defaultViewer;
+function setupPdfViewer(noteframe) {
+ var pdfViewer = noteframe.pdf2htmlEX.defaultViewer;
$('#plus-btn').click(function (){
pdfViewer.rescale(1.20, true, [0,0]);
// detect if the PDF viewer wants to show an outline
// at all
if ($(pdfViewer.sidebar).hasClass('opened')) {
- var body = $('body');
+ var body = $(document.body);
// if the screen is less than 64em wide, hide the outline
if (parseInt($(body.width()).toEm({scope: body})) < 64) {
$(pdfViewer.sidebar).removeClass('opened');
$('#outline-btn').click(function() {
$(pdfViewer.sidebar).toggleClass('opened');
// rescale the PDF to fit the available space
- rescalePdf(pdfViewer, parseInt(noteFrame.width));
+ rescalePdf(pdfViewer, parseInt(noteframe.frameElement.clientWidth));
});
$('#scroll-to').change(function() {
});
// rescale the PDF to fit the available space
- rescalePdf(pdfViewer, parseInt(noteFrame.width));
+ rescalePdf(pdfViewer, parseInt(noteframe.frameElement.clientWidth));
}
function writeNoteFrame(contents) {
dstDoc.close();
}
+function setupAnnotator(noteElement) {
+ noteElement.annotator();
+ noteElement.annotator('addPlugin', 'Store', {
+ prefix: '/ajax/annotations',
+ loadFromSearch: {
+ 'uri': note_id
+ },
+ annotationData: {
+ 'uri': note_id
+ }
+ });
+}
+
+function injectRemoteScript(url, noteframe, onload) {
+ var injectScript = noteframe.document.createElement("script");
+ injectScript.src = url;
+ injectScript.onload = onload;
+ noteframe.document.head.appendChild(injectScript);
+}
+
+function injectScript(scriptText, noteframe) {
+ var injectScript = noteframe.document.createElement("script");
+ injectScript.innerHTML = scriptText;
+ noteframe.document.body.appendChild(injectScript);
+}
+
+function injectRemoteCSS(url, noteframe) {
+ var injectCSS = noteframe.document.createElement("link");
+ injectCSS.href = url;
+ injectCSS.type = 'text/css';
+ injectCSS.rel = 'stylesheet';
+ noteframe.document.head.appendChild(injectCSS);
+}
+
$(function() {
$("#thank-button").click(function(event) {
if ($('#note-markdown').length > 0) {
var note_markdown = $('#note-markdown');
note_markdown.html(marked(note_markdown.data('markdown')));
- note_markdown.annotator();
- note_markdown.annotator('addPlugin', 'Store', {
- prefix: '/ajax/annotations',
- loadFromSearch: {
- 'uri': note_id
- },
- annotationData: {
- 'uri': note_id
- }
- });
+ setupAnnotator(note_markdown);
} else {
$.ajax(note_contents_url, {
type: 'GET',
},
success: function(data, textStatus, jqXHR) {
writeNoteFrame(data);
- autoResize('noteframe');
- if (pdfControls == true) {
- setupPdfViewer();
- }
+
+ // run setupAnnotator in frame context
+ var noteframe = document.getElementById('noteframe').contentWindow;
+
+ injectRemoteCSS(annotator_css_url, noteframe);
+ injectScript("csrf_token = '" + csrf_token + "';", noteframe);
+
+ injectRemoteScript("http://code.jquery.com/jquery-2.1.0.min.js", noteframe,
+ function() {
+ injectRemoteScript(setup_ajax_url, noteframe);
+ injectRemoteScript(annotator_js_url, noteframe,
+ function() {
+ var js = "$(function() { $('#page-container').annotator(); \
+ $('#page-container').annotator('addPlugin', 'Store', { \
+ prefix: '/ajax/annotations', \
+ loadFromSearch: { \
+ 'uri': " + note_id + " \
+ }, \
+ annotationData: { \
+ 'uri': " + note_id + " \
+ } \
+ }); })";
+ injectScript(js, noteframe);
+ if (pdfControls == true) {
+ $(noteframe.document).ready(function() {
+ setupPdfViewer(noteframe);
+ });
+ }
+ });
+ });
+
+
+
},
error: function(data, textStatus, jqXHR) {
writeNoteFrame("<h3 style='text-align: center'>Sorry, your note could not be retrieved.</h3>");