1c614988691cae98358e83a34bd390ebd5248d6f
[edumed.git] / wtem / static / wtem / wtem.js
1 $(function() {
2
3     var to_submit;
4
5     $('form').submit(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         $('input[name=answers]').val(JSON.stringify(to_submit));
21     });
22
23     var push_answer = function(el, answer) {
24         to_submit[el.attr('data-id')] = answer
25     };
26
27     var handlers = {
28         edumed: function(el) {
29             var exercise = el.data('exercise'),
30                 to_push = {},
31                 open_part;
32             if(exercise.get_answers) {
33                 to_push.closed_part = exercise.get_answers()[0];
34             }
35             open_part = el.find('.open_part')
36             if(open_part.length) {
37                 to_push.open_part = open_part.find('textarea').val();
38             }
39
40             push_answer(el, to_push);
41         },
42
43         open: function(el) {
44             var textareas = el.find('textarea'),
45                 to_push;
46             if(textareas.length === 1) {
47                 to_push = el.find('textarea').val();
48             } else {
49                 to_push = [];
50                 textareas.each(function() {
51                     var textarea = $(this);
52                     to_push.push({'id': textarea.attr('data-field-id'), 'text': textarea.val()});
53                 });
54             }
55             push_answer(el, to_push);
56         }
57     }
58
59 });