a65bb7f72f1f2cd67b0b2ae444ca758c66070d99
[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     var sms_handler = function() {
60         var textarea = $(this),
61             label_suffix = textarea.parent().find('.label_suffix'),
62             left = 140 - textarea.val().length;
63             to_insert = '(pozostaƂo: ' + left + ')';
64         if(left < 0) {
65             to_insert = '<span style="color:red">' + to_insert + '</span>';
66         }
67         label_suffix.html(to_insert);
68     };
69
70     $('#wtem_sms').change(sms_handler).keyup(sms_handler);
71
72 });