diff --git a/common/lib/capa/templates/textinput_dynamath.html b/common/lib/capa/templates/textinput_dynamath.html index c50658f713..e8b26c5fcc 100644 --- a/common/lib/capa/templates/textinput_dynamath.html +++ b/common/lib/capa/templates/textinput_dynamath.html @@ -2,49 +2,32 @@ ### version of textline.html which does dynammic math ###
- + + + + + +
- - + + + + -
+ + + - - - % if state == 'unsubmitted': - - % elif state == 'correct': - - % elif state == 'incorrect': - - % elif state == 'incomplete': - - % endif - -
- `{::}` - - -
-## -## javascript for dynamic math: add this math element to the MathJax rendering queue -## also adds to global jaxset js array -## - - % if msg: -
- ${msg|n} - % endif + % if state == 'unsubmitted': + + % elif state == 'correct': + + % elif state == 'incorrect': + + % elif state == 'incomplete': + + % endif +
+ `{::}` + + +
diff --git a/lms/static/coffee/fixtures/problem_content.html b/lms/static/coffee/fixtures/problem_content.html index d2e89fed2b..ffcb5e4682 100644 --- a/lms/static/coffee/fixtures/problem_content.html +++ b/lms/static/coffee/fixtures/problem_content.html @@ -6,6 +6,10 @@
+ + + + diff --git a/lms/static/coffee/spec/modules/problem_spec.coffee b/lms/static/coffee/spec/modules/problem_spec.coffee index 6bca63cfe1..78047db3ba 100644 --- a/lms/static/coffee/spec/modules/problem_spec.coffee +++ b/lms/static/coffee/spec/modules/problem_spec.coffee @@ -1,7 +1,11 @@ describe 'Problem', -> beforeEach -> # Stub MathJax - window.MathJax = { Hub: { Queue: -> } } + window.MathJax = + Hub: jasmine.createSpyObj('MathJax.Hub', ['getAllJax', 'Queue']) + Callback: jasmine.createSpyObj('MathJax.Callback', ['After']) + @stubbedJax = root: jasmine.createSpyObj('jax.root', ['toMathML']) + MathJax.Hub.getAllJax.andReturn [@stubbedJax] window.update_schematics = -> loadFixtures 'problem.html' @@ -25,8 +29,8 @@ describe 'Problem', -> describe 'bind', -> beforeEach -> - spyOn MathJax.Hub, 'Queue' spyOn window, 'update_schematics' + MathJax.Hub.getAllJax.andReturn [@stubbedJax] @problem = new Problem 1, '/problem/url/' it 'set mathjax typeset', -> @@ -50,6 +54,12 @@ describe 'Problem', -> it 'bind the save button', -> expect($('section.action input.save')).toHandleWith 'click', @problem.save + it 'bind the math input', -> + expect($('input.math')).toHandleWith 'keyup', @problem.refreshMath + + it 'display the math input', -> + expect(@stubbedJax.root.toMathML).toHaveBeenCalled() + describe 'render', -> beforeEach -> @problem = new Problem 1, '/problem/url/' @@ -223,6 +233,30 @@ describe 'Problem', -> @problem.save() expect(window.alert).toHaveBeenCalledWith 'Saved' + describe 'refreshMath', -> + beforeEach -> + @problem = new Problem 1, '/problem/url/' + @stubbedJax.root.toMathML.andReturn '' + $('#input_example_1').val 'E=mc^2' + + describe 'when there is no exception', -> + beforeEach -> + @problem.refreshMath target: $('#input_example_1').get(0) + + it 'should convert and display the MathML object', -> + expect(MathJax.Hub.Queue).toHaveBeenCalledWith ['Text', @stubbedJax, 'E=mc^2'] + + it 'should display debug output in hidden div', -> + expect($('#input_example_1_dynamath')).toHaveValue '' + + describe 'when there is an exception', -> + beforeEach -> + @stubbedJax.root.toMathML.andThrow {restart: true} + @problem.refreshMath target: $('#input_example_1').get(0) + + it 'should queue up the exception', -> + expect(MathJax.Callback.After).toHaveBeenCalledWith [@problem.refreshMath, @stubbedJax], true + describe 'refreshAnswers', -> beforeEach -> @problem = new Problem 1, '/problem/url/' diff --git a/lms/static/coffee/src/modules/problem.coffee b/lms/static/coffee/src/modules/problem.coffee index e1fac3a1a5..c1a801d2b2 100644 --- a/lms/static/coffee/src/modules/problem.coffee +++ b/lms/static/coffee/src/modules/problem.coffee @@ -15,6 +15,7 @@ class @Problem @$('section.action input.reset').click @reset @$('section.action input.show').click @show @$('section.action input.save').click @save + @$('input.math').keyup(@refreshMath).each(@refreshMath) render: (content) -> if content @@ -62,6 +63,20 @@ class @Problem if response.success alert 'Saved' + refreshMath: (event, element) => + element = event.target unless element + target = "display_#{element.id.replace(/^input_/, '')}" + + if jax = MathJax.Hub.getAllJax(target)[0] + MathJax.Hub.Queue ['Text', jax, $(element).val()] + + try + output = jax.root.toMathML '' + $("##{element.id}_dynamath").val(output) + catch exception + throw exception unless exception.restart + MathJax.Callback.After [@refreshMath, jax], exception.restart + refreshAnswers: => @$('input.schematic').each (index, element) -> element.schematic.update_value() diff --git a/lms/templates/mathjax_include.html b/lms/templates/mathjax_include.html index 681893cadd..c789e4d299 100644 --- a/lms/templates/mathjax_include.html +++ b/lms/templates/mathjax_include.html @@ -5,82 +5,22 @@ ## ## This enables ASCIIMathJAX, and is used by js_textbox - -// })(); - -function DoUpdateMath(inputId) { - var str = document.getElementById("input_"+inputId).value; - - // make sure the input field is in the jaxset - if ($.inArray(inputId,jaxset) == -1){ - //alert('missing '+inputId); - if (document.getElementById("display_" + inputId)){ - MathJax.Hub.queue.Push(function () { - math = MathJax.Hub.getAllJax("display_" + inputId)[0]; - if (math){ - jaxset[inputId] = math; - } - }); - }; - } - - UpdateMath(str,inputId) -} - - - - - + +