From b6cf206fc72db36ac13e48a96541e2aa7902132e Mon Sep 17 00:00:00 2001 From: ichuang Date: Sun, 24 Feb 2013 01:54:29 +0000 Subject: [PATCH] fix latex high-level-source editor --- cms/templates/widgets/source-edit.html | 91 ++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 4 deletions(-) diff --git a/cms/templates/widgets/source-edit.html b/cms/templates/widgets/source-edit.html index f0922831e1..d3afbdf040 100644 --- a/cms/templates/widgets/source-edit.html +++ b/cms/templates/widgets/source-edit.html @@ -10,7 +10,7 @@

High Level Source Editing

-
+
@@ -18,6 +18,9 @@ +
+ +
@@ -32,12 +35,43 @@ $('#hls-trig-${hlskey}').click(function(){slow_refresh_hls($('#hls-modal-${hlskey}'))}) + // file upload button + $('#hls-modal-${hlskey}').find('.hls-upload').click(function(){ + $('#hls-modal-${hlskey}').find('#hlsfile').trigger('click'); + }); + + $('#hls-modal-${hlskey}').find('#hlsfile').change(function() { + console.log('handler for hlsfile input called'); + var file = $('#hls-modal-${hlskey}').find('#hlsfile')[0].files[0]; + el = $('#hls-modal-${hlskey}'); + console.log(file); + + var reader = new FileReader(); + reader.onload = function(event) { + var contents = event.target.result; + process_file_contents(el, contents); + }; + reader.readAsText(file); + }); + + // file upload processing + function process_file_contents(el, contents){ + // console.log("File contents: " + contents); + editor = el.data('editor'); + editor.setValue(contents); + console.log('doing compile and save'); + // trigger compile & save + el.find('.hls-compile').trigger('click'); + } + // refresh button $('#hls-modal-${hlskey}').find('.hls-refresh').click(function(){refresh_hls($('#hls-modal-${hlskey}'))}); function refresh_hls(el){ el.data('editor').refresh(); + // hide gray overlay + $('#lean_overlay').hide(); } function slow_refresh_hls(el){ @@ -52,15 +86,55 @@ // compile & save button - $('#hls-modal-${hlskey}').find('.hls-compile').click(compile_hls_${hlskey}); + // $('#hls-modal-${hlskey}').find('.hls-compile').click(compile_hls_${hlskey}); + $('#hls-modal-${hlskey}').find('.hls-compile').click(function(){ + el = $('#hls-modal-${hlskey}'); + compile_hls(el); + }); - function compile_hls_${hlskey}(){ + // connect to server using POST (requires cross-domain-access) + + function compile_hls(el){ + editor = el.data('editor') + hlsdata = editor.getValue(); + // console.log('hlsdata=' + hlsdata); + + $.ajax({ + url: "https://studio-input-filter.mitx.mit.edu/latex2edx?raw=1", + type: "POST", + data: ""+hlsdata, + crossDomain: true, + processData: false, + success: function(data){ + console.log('latex2edx success!'); + console.log(data); + xml = data.xml; + if (xml.length==0){ + alert('Conversion failed! error:'+ data.message); + //set_status(el, "Conversion failed - please try another file", false); + }else{ + //set_status(el, "Done!", true); + // post_images(el, location, data, file); + // post_source_code(el, location, file); + el.closest('.component').find('.CodeMirror-wrap')[0].CodeMirror.setValue(xml); + save_hls(el); + } + }, + error: function() { + alert('Error: cannot connect to word2edx server'); + console.log('error!'); + } + }); + } + + ## this version uses JSONP and GET, which limits file sizes + function old_compile_hls_${hlskey}(){ editor = $('#hls-modal-${hlskey}').data('editor') var myquery = { latexin: editor.getValue() }; $.ajax({ - url: '${metadata.get('source_processor_url','https://qisx.mit.edu:5443/latex2edx')}', + url: '${metadata.get('source_processor_url','https://studio-input-filter.mitx.mit.edu/latex2edx')}', type: 'GET', contentType: 'application/json', data: escape(JSON.stringify(myquery)), @@ -109,4 +183,13 @@ el.closest('.component').find('.save-button').click(); } + ## add upload and download links / buttons to component edit box + $('#hls-modal-${hlskey}').closest('.component').find('.component-actions').append(''); + $('#link-${hlskey}').html('upload'); + $('#upload-${hlskey}').click(function(){ + $('#hls-modal-${hlskey}').closest('.component').find('.edit-button').trigger('click'); // open up editor window + $('#hls-trig-${hlskey}').trigger('click'); // open up HLS editor window + $('#hls-modal-${hlskey}').find('#hlsfile').trigger('click'); + }); +