7afbe0a5966758bf72d4b085c1931c5fd3443e53
[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             var textareas = el.find('textarea'),
46                 to_push;
47             if(textareas.length === 1) {
48                 to_push = el.find('textarea').val();
49             } else {
50                 to_push = [];
51                 textareas.each(function() {
52                     var textarea = $(this);
53                     to_push.push({'id': textarea.attr('data-field-id'), 'text': textarea.val()});
54                 });
55             }
56             push_answer(el, to_push);
57         }
58     }
59
60 });