edumed quiz pt 1
[redakcja.git] / redakcja / static / edumed / js / edumed.js
1 // Generated by CoffeeScript 1.4.0
2 (function() {
3   var $, Binding, EduModule, Excercise, Wybor, excercise,
4     __hasProp = {}.hasOwnProperty,
5     __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
6
7   $ = jQuery;
8
9   Binding = (function() {
10
11     function Binding(handler, element) {
12       this.handler = handler;
13       this.element = element;
14       $(this.element).data(this.handler, this);
15     }
16
17     return Binding;
18
19   })();
20
21   EduModule = (function(_super) {
22
23     __extends(EduModule, _super);
24
25     function EduModule(element) {
26       var _this = this;
27       EduModule.__super__.constructor.call(this, 'edumodule', element);
28       $("[name=teacher-toggle]").change(function(ev) {
29         if ($(ev.target).is(":checked")) {
30           return $(".teacher", _this.element).addClass("show");
31         } else {
32           return $(".teacher", _this.element).removeClass("show");
33         }
34       });
35     }
36
37     return EduModule;
38
39   })(Binding);
40
41   Excercise = (function(_super) {
42
43     __extends(Excercise, _super);
44
45     function Excercise(element) {
46       var _this = this;
47       Excercise.__super__.constructor.call(this, 'excercise', element);
48       $(".check", this.element).click(function() {
49         return _this.check();
50       });
51       $('.solutions', this.element).click(function() {
52         return _this.show_solutions();
53       });
54     }
55
56     return Excercise;
57
58   })(Binding);
59
60   Wybor = (function(_super) {
61
62     __extends(Wybor, _super);
63
64     function Wybor(element) {
65       Wybor.__super__.constructor.call(this, element);
66     }
67
68     Wybor.prototype.check = function() {
69       var score, scores,
70         _this = this;
71       scores = [];
72       $(".question").each(function(i, question) {
73         return scores.push(_this.check_question(question));
74       });
75       score = [0, 0];
76       $.each(scores, function(i, s) {
77         score[0] += s[0];
78         return score[1] += s[1];
79       });
80       return this.show_score(score);
81     };
82
83     Wybor.prototype.check_question = function(question) {
84       var all, good, solution,
85         _this = this;
86       all = 0;
87       good = 0;
88       solution = $(question).attr('data-solution').split(',').map($.trim).map(function(x) {
89         return parseInt(x);
90       });
91       $(".question-piece", question).each(function(i, qpiece) {
92         var is_checked, piece_no, should_be_checked;
93         piece_no = parseInt($(qpiece).attr('data-no'));
94         should_be_checked = solution.indexOf(piece_no) >= 0;
95         is_checked = $("input", qpiece).is(":checked");
96         if (should_be_checked) {
97           all += 1;
98         }
99         if (is_checked) {
100           if (should_be_checked) {
101             good += 1;
102             return _this.piece_correct(qpiece);
103           } else {
104             return _this.piece_incorrect(qpiece);
105           }
106         } else {
107           return $(qpiece).removeClass("correct,incorrect");
108         }
109       });
110       return [good, all];
111     };
112
113     Wybor.prototype.piece_correct = function(qpiece) {
114       return $(qpiece).removeClass('incorrect').addClass('correct');
115     };
116
117     Wybor.prototype.piece_incorrect = function(qpiece) {
118       return $(qpiece).removeClass('correct').addClass('incorrect');
119     };
120
121     Wybor.prototype.show_solutions = function() {};
122
123     Wybor.prototype.show_score = function(score) {
124       return $(".message", this.element).text("Wynik: " + score[0] + " / " + score[1]);
125     };
126
127     return Wybor;
128
129   })(Excercise);
130
131   excercise = function(ele) {
132     var cls, es;
133     es = {
134       'wybor': Wybor
135     };
136     cls = es[$(ele).attr('data-type')];
137     return new cls(ele);
138   };
139
140   window.edumed = {
141     'EduModule': EduModule
142   };
143
144   $(document).ready(function() {
145     new EduModule($("#book-text"));
146     return $(".excercise").each(function(i, el) {
147       return excercise(this);
148     });
149   });
150
151 }).call(this);