X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/dd0b1138bec01e917fb3105be5ba41325370aa41..c411ffb02d0b387e2e3ca0be2dbad5e373eeed64:/wtem/static/wtem/wtem.js diff --git a/wtem/static/wtem/wtem.js b/wtem/static/wtem/wtem.js index f7dccdc..1c61498 100644 --- a/wtem/static/wtem/wtem.js +++ b/wtem/static/wtem/wtem.js @@ -1,15 +1,59 @@ $(function() { - $('#submit_answers').click(function(e) { - e.preventDefault(); - var to_submit = []; - $('.exercise').each(function() { - var exercise = $(this).data('exercise'); - if(exercise.get_answers) { - to_submit.push(exercise.get_answers()[0]); + var to_submit; + + $('form').submit(function(e) { + //e.preventDefault(); + to_submit = {}; + + $('.exercise-wtem').each(function() { + var el = $(this); + if(el.hasClass('exercise')) { + handlers.edumed(el); + } else { + var type = el.attr('data-type'); + if(handlers[type]) { + handlers[type](el); + } } }); - console.log(JSON.stringify(to_submit)); + $('input[name=answers]').val(JSON.stringify(to_submit)); }); + var push_answer = function(el, answer) { + to_submit[el.attr('data-id')] = answer + }; + + var handlers = { + edumed: function(el) { + var exercise = el.data('exercise'), + to_push = {}, + open_part; + if(exercise.get_answers) { + to_push.closed_part = exercise.get_answers()[0]; + } + open_part = el.find('.open_part') + if(open_part.length) { + to_push.open_part = open_part.find('textarea').val(); + } + + push_answer(el, to_push); + }, + + open: function(el) { + var textareas = el.find('textarea'), + to_push; + if(textareas.length === 1) { + to_push = el.find('textarea').val(); + } else { + to_push = []; + textareas.each(function() { + var textarea = $(this); + to_push.push({'id': textarea.attr('data-field-id'), 'text': textarea.val()}); + }); + } + push_answer(el, to_push); + } + } + }); \ No newline at end of file