5253e03238c76ce1bdb787ec8da053248129d26e
[edumed.git] / wtem / static / wtem / wtem.js
1 $(function() {
2
3     var to_submit;
4
5     $('#submit_answers').click(function(e) {
6         //e.preventDefault();
7         to_submit = {};
8
9         $('.exercise-wtem').each(function() {
10             var el = $(this);
11             if(el.hasClass('exercise')) {
12                 handlers.edumed(el);
13             } else {
14                 var type = el.attr('data-type');
15                 if(handlers[type]) {
16                     handlers[type](el);
17                 }
18             }
19         });
20         console.log(JSON.stringify(to_submit));
21         $('input[name=answers]').val(JSON.stringify(to_submit));
22     });
23
24     var push_answer = function(el, answer) {
25         to_submit[el.attr('data-id')] = answer
26     };
27
28     var handlers = {
29         edumed: function(el) {
30             var exercise = el.data('exercise'),
31                 to_push = {},
32                 open_part;
33             if(exercise.get_answers) {
34                 to_push.closed_part = exercise.get_answers()[0];
35             }
36             open_part = el.find('.open_part')
37             if(open_part.length) {
38                 to_push.open_part = open_part.find('textarea').val();
39             }
40
41             push_answer(el, to_push);
42         },
43
44         open: function(el) {
45             push_answer(el, el.find('textarea').val());
46         }
47     }
48
49 });