Commit 6f5e54b9 by ichuang

Merge pull request #147 from MITx/ps-fix-problem

Queue up MathML conversion to be after render
parents bf4f946f 4db0a701
...@@ -20,10 +20,12 @@ jasmine.stubRequests = -> ...@@ -20,10 +20,12 @@ jasmine.stubRequests = ->
settings.success data: jasmine.stubbedMetadata[match[1]] settings.success data: jasmine.stubbedMetadata[match[1]]
else if match = settings.url.match /static\/subs\/(.+)\.srt\.sjson/ else if match = settings.url.match /static\/subs\/(.+)\.srt\.sjson/
settings.success jasmine.stubbedCaption settings.success jasmine.stubbedCaption
else if settings.url.match /modx\/problem\/.+\/problem_get$/
settings.success html: readFixtures('problem_content.html')
else if settings.url == '/calculate' || else if settings.url == '/calculate' ||
settings.url == '/6002x/modx/sequence/1/goto_position' || settings.url == '/6002x/modx/sequence/1/goto_position' ||
settings.url.match(/event$/) || settings.url.match(/event$/) ||
settings.url.match(/6002x\/modx\/problem\/.+\/problem_(check|reset|show|save)$/) settings.url.match(/modx\/problem\/.+\/problem_(check|reset|show|save)$/)
# do nothing # do nothing
else else
throw "External request attempted for #{settings.url}, which is not defined." throw "External request attempted for #{settings.url}, which is not defined."
......
...@@ -13,6 +13,7 @@ describe 'Problem', -> ...@@ -13,6 +13,7 @@ describe 'Problem', ->
spyOn($.fn, 'load').andCallFake (url, callback) -> spyOn($.fn, 'load').andCallFake (url, callback) ->
$(@).html readFixtures('problem_content.html') $(@).html readFixtures('problem_content.html')
callback() callback()
jasmine.stubRequests()
describe 'constructor', -> describe 'constructor', ->
beforeEach -> beforeEach ->
...@@ -21,12 +22,6 @@ describe 'Problem', -> ...@@ -21,12 +22,6 @@ describe 'Problem', ->
it 'set the element', -> it 'set the element', ->
expect(@problem.element).toBe '#problem_1' expect(@problem.element).toBe '#problem_1'
it 'set the content url', ->
expect(@problem.content_url).toEqual '/problem/url/problem_get?id=1'
it 'render the content', ->
expect($.fn.load).toHaveBeenCalledWith @problem.content_url, @problem.bind
describe 'bind', -> describe 'bind', ->
beforeEach -> beforeEach ->
spyOn window, 'update_schematics' spyOn window, 'update_schematics'
...@@ -57,8 +52,11 @@ describe 'Problem', -> ...@@ -57,8 +52,11 @@ describe 'Problem', ->
it 'bind the math input', -> it 'bind the math input', ->
expect($('input.math')).toHandleWith 'keyup', @problem.refreshMath expect($('input.math')).toHandleWith 'keyup', @problem.refreshMath
it 'display the math input', -> it 'replace math content on the page', ->
expect(@stubbedJax.root.toMathML).toHaveBeenCalled() expect(MathJax.Hub.Queue.mostRecentCall.args).toEqual [
['Text', @stubbedJax, ''],
[@problem.updateMathML, @stubbedJax, $('#input_example_1').get(0)]
]
describe 'render', -> describe 'render', ->
beforeEach -> beforeEach ->
...@@ -77,12 +75,19 @@ describe 'Problem', -> ...@@ -77,12 +75,19 @@ describe 'Problem', ->
expect(@problem.bind).toHaveBeenCalled() expect(@problem.bind).toHaveBeenCalled()
describe 'with no content given', -> describe 'with no content given', ->
beforeEach ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) ->
callback html: "Hello World"
@problem.render()
it 'load the content via ajax', -> it 'load the content via ajax', ->
expect($.fn.load).toHaveBeenCalledWith @problem.content_url, @bind expect(@problem.element.html()).toEqual 'Hello World'
it 're-bind the content', ->
expect(@problem.bind).toHaveBeenCalled()
describe 'check', -> describe 'check', ->
beforeEach -> beforeEach ->
jasmine.stubRequests()
@problem = new Problem 1, '/problem/url/' @problem = new Problem 1, '/problem/url/'
@problem.answers = 'foo=1&bar=2' @problem.answers = 'foo=1&bar=2'
...@@ -116,7 +121,6 @@ describe 'Problem', -> ...@@ -116,7 +121,6 @@ describe 'Problem', ->
describe 'reset', -> describe 'reset', ->
beforeEach -> beforeEach ->
jasmine.stubRequests()
@problem = new Problem 1, '/problem/url/' @problem = new Problem 1, '/problem/url/'
it 'log the problem_reset event', -> it 'log the problem_reset event', ->
...@@ -130,13 +134,13 @@ describe 'Problem', -> ...@@ -130,13 +134,13 @@ describe 'Problem', ->
expect($.postWithPrefix).toHaveBeenCalledWith '/modx/problem/1/problem_reset', { id: 1 }, jasmine.any(Function) expect($.postWithPrefix).toHaveBeenCalledWith '/modx/problem/1/problem_reset', { id: 1 }, jasmine.any(Function)
it 'render the returned content', -> it 'render the returned content', ->
spyOn($, 'postWithPrefix').andCallFake (url, answers, callback) -> callback("Reset!") spyOn($, 'postWithPrefix').andCallFake (url, answers, callback) ->
callback html: "Reset!"
@problem.reset() @problem.reset()
expect(@problem.element.html()).toEqual 'Reset!' expect(@problem.element.html()).toEqual 'Reset!'
describe 'show', -> describe 'show', ->
beforeEach -> beforeEach ->
jasmine.stubRequests()
@problem = new Problem 1, '/problem/url/' @problem = new Problem 1, '/problem/url/'
@problem.element.prepend '<div id="answer_1_1" /><div id="answer_1_2" />' @problem.element.prepend '<div id="answer_1_1" /><div id="answer_1_2" />'
...@@ -154,18 +158,19 @@ describe 'Problem', -> ...@@ -154,18 +158,19 @@ describe 'Problem', ->
expect($.postWithPrefix).toHaveBeenCalledWith '/modx/problem/1/problem_show', jasmine.any(Function) expect($.postWithPrefix).toHaveBeenCalledWith '/modx/problem/1/problem_show', jasmine.any(Function)
it 'show the answers', -> it 'show the answers', ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback('1_1': 'One', '1_2': 'Two') spyOn($, 'postWithPrefix').andCallFake (url, callback) ->
callback answers: '1_1': 'One', '1_2': 'Two'
@problem.show() @problem.show()
expect($('#answer_1_1')).toHaveHtml 'One' expect($('#answer_1_1')).toHaveHtml 'One'
expect($('#answer_1_2')).toHaveHtml 'Two' expect($('#answer_1_2')).toHaveHtml 'Two'
it 'toggle the show answer button', -> it 'toggle the show answer button', ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback({}) spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback(answers: {})
@problem.show() @problem.show()
expect($('.show')).toHaveValue 'Hide Answer' expect($('.show')).toHaveValue 'Hide Answer'
it 'add the showed class to element', -> it 'add the showed class to element', ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback({}) spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback(answers: {})
@problem.show() @problem.show()
expect(@problem.element).toHaveClass 'showed' expect(@problem.element).toHaveClass 'showed'
...@@ -179,7 +184,8 @@ describe 'Problem', -> ...@@ -179,7 +184,8 @@ describe 'Problem', ->
''' '''
it 'set the correct_answer attribute on the choice', -> it 'set the correct_answer attribute on the choice', ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback('1_1': [2, 3]) spyOn($, 'postWithPrefix').andCallFake (url, callback) ->
callback answers: '1_1': [2, 3]
@problem.show() @problem.show()
expect($('label[for="input_1_1_1"]')).not.toHaveAttr 'correct_answer', 'true' expect($('label[for="input_1_1_1"]')).not.toHaveAttr 'correct_answer', 'true'
expect($('label[for="input_1_1_2"]')).toHaveAttr 'correct_answer', 'true' expect($('label[for="input_1_1_2"]')).toHaveAttr 'correct_answer', 'true'
...@@ -214,7 +220,6 @@ describe 'Problem', -> ...@@ -214,7 +220,6 @@ describe 'Problem', ->
describe 'save', -> describe 'save', ->
beforeEach -> beforeEach ->
jasmine.stubRequests()
@problem = new Problem 1, '/problem/url/' @problem = new Problem 1, '/problem/url/'
@problem.answers = 'foo=1&bar=2' @problem.answers = 'foo=1&bar=2'
...@@ -236,23 +241,29 @@ describe 'Problem', -> ...@@ -236,23 +241,29 @@ describe 'Problem', ->
describe 'refreshMath', -> describe 'refreshMath', ->
beforeEach -> beforeEach ->
@problem = new Problem 1, '/problem/url/' @problem = new Problem 1, '/problem/url/'
@stubbedJax.root.toMathML.andReturn '<MathML>'
$('#input_example_1').val 'E=mc^2' $('#input_example_1').val 'E=mc^2'
@problem.refreshMath target: $('#input_example_1').get(0)
it 'should queue the conversion and MathML element update', ->
expect(MathJax.Hub.Queue).toHaveBeenCalledWith ['Text', @stubbedJax, 'E=mc^2'],
[@problem.updateMathML, @stubbedJax, $('#input_example_1').get(0)]
describe 'updateMathML', ->
beforeEach ->
@problem = new Problem 1, '/problem/url/'
@stubbedJax.root.toMathML.andReturn '<MathML>'
describe 'when there is no exception', -> describe 'when there is no exception', ->
beforeEach -> beforeEach ->
@problem.refreshMath target: $('#input_example_1').get(0) @problem.updateMathML @stubbedJax, $('#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', -> it 'convert jax to MathML', ->
expect($('#input_example_1_dynamath')).toHaveValue '<MathML>' expect($('#input_example_1_dynamath')).toHaveValue '<MathML>'
describe 'when there is an exception', -> describe 'when there is an exception', ->
beforeEach -> beforeEach ->
@stubbedJax.root.toMathML.andThrow {restart: true} @stubbedJax.root.toMathML.andThrow {restart: true}
@problem.refreshMath target: $('#input_example_1').get(0) @problem.updateMathML @stubbedJax, $('#input_example_1').get(0)
it 'should queue up the exception', -> it 'should queue up the exception', ->
expect(MathJax.Callback.After).toHaveBeenCalledWith [@problem.refreshMath, @stubbedJax], true expect(MathJax.Callback.After).toHaveBeenCalledWith [@problem.refreshMath, @stubbedJax], true
......
class @Problem class @Problem
constructor: (@id, url) -> constructor: (@id, url) ->
@element = $("#problem_#{id}") @element = $("#problem_#{id}")
@content_url = "#{url}problem_get?id=#{@id}"
@render() @render()
$: (selector) -> $: (selector) ->
...@@ -17,7 +16,7 @@ class @Problem ...@@ -17,7 +16,7 @@ class @Problem
@$('section.action input.save').click @save @$('section.action input.save').click @save
@$('input.math').keyup(@refreshMath).each(@refreshMath) @$('input.math').keyup(@refreshMath).each(@refreshMath)
update_progress: (response) => updateProgress: (response) =>
if response.progress_changed if response.progress_changed
@element.attr progress: response.progress_status @element.attr progress: response.progress_status
@element.trigger('progressChanged') @element.trigger('progressChanged')
...@@ -27,10 +26,9 @@ class @Problem ...@@ -27,10 +26,9 @@ class @Problem
@element.html(content) @element.html(content)
@bind() @bind()
else else
$.postWithPrefix "/modx/problem/#{@id}/problem_get", '', (response) => $.postWithPrefix "/modx/problem/#{@id}/problem_get", (response) =>
@element.html(response.html) @element.html(response.html)
@bind() @bind()
check: => check: =>
Logger.log 'problem_check', @answers Logger.log 'problem_check', @answers
...@@ -38,7 +36,7 @@ class @Problem ...@@ -38,7 +36,7 @@ class @Problem
switch response.success switch response.success
when 'incorrect', 'correct' when 'incorrect', 'correct'
@render(response.contents) @render(response.contents)
@update_progress response @updateProgress response
else else
alert(response.success) alert(response.success)
...@@ -46,7 +44,7 @@ class @Problem ...@@ -46,7 +44,7 @@ class @Problem
Logger.log 'problem_reset', @answers Logger.log 'problem_reset', @answers
$.postWithPrefix "/modx/problem/#{@id}/problem_reset", id: @id, (response) => $.postWithPrefix "/modx/problem/#{@id}/problem_reset", id: @id, (response) =>
@render(response.html) @render(response.html)
@update_progress response @updateProgress response
show: => show: =>
if !@element.hasClass 'showed' if !@element.hasClass 'showed'
...@@ -62,7 +60,7 @@ class @Problem ...@@ -62,7 +60,7 @@ class @Problem
MathJax.Hub.Queue ["Typeset", MathJax.Hub] MathJax.Hub.Queue ["Typeset", MathJax.Hub]
@$('.show').val 'Hide Answer' @$('.show').val 'Hide Answer'
@element.addClass 'showed' @element.addClass 'showed'
@update_progress response @updateProgress response
else else
@$('[id^=answer_], [id^=solution_]').text '' @$('[id^=answer_], [id^=solution_]').text ''
@$('[correct_answer]').attr correct_answer: null @$('[correct_answer]').attr correct_answer: null
...@@ -74,21 +72,22 @@ class @Problem ...@@ -74,21 +72,22 @@ class @Problem
$.postWithPrefix "/modx/problem/#{@id}/problem_save", @answers, (response) => $.postWithPrefix "/modx/problem/#{@id}/problem_save", @answers, (response) =>
if response.success if response.success
alert 'Saved' alert 'Saved'
@update_progress response @updateProgress response
refreshMath: (event, element) => refreshMath: (event, element) =>
element = event.target unless element element = event.target unless element
target = "display_#{element.id.replace(/^input_/, '')}" target = "display_#{element.id.replace(/^input_/, '')}"
if jax = MathJax.Hub.getAllJax(target)[0] if jax = MathJax.Hub.getAllJax(target)[0]
MathJax.Hub.Queue ['Text', jax, $(element).val()] MathJax.Hub.Queue ['Text', jax, $(element).val()],
[@updateMathML, jax, element]
try updateMathML: (jax, element) =>
output = jax.root.toMathML '' try
$("##{element.id}_dynamath").val(output) $("##{element.id}_dynamath").val(jax.root.toMathML '')
catch exception catch exception
throw exception unless exception.restart throw exception unless exception.restart
MathJax.Callback.After [@refreshMath, jax], exception.restart MathJax.Callback.After [@refreshMath, jax], exception.restart
refreshAnswers: => refreshAnswers: =>
@$('input.schematic').each (index, element) -> @$('input.schematic').each (index, element) ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment