excercises: uporzadkuj, luki, zastap
[redakcja.git] / redakcja / static / edumed / js / edumed.js
1 (function() {
2   var $, Binding, EduModule, Excercise, Luki, Uporzadkuj, Wybor, Zastap, excercise,
3     __hasProp = Object.prototype.hasOwnProperty,
4     __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; };
5
6   $ = jQuery;
7
8   Binding = (function() {
9
10     function Binding(handler, element) {
11       this.handler = handler;
12       this.element = element;
13       $(this.element).data(this.handler, this);
14     }
15
16     return Binding;
17
18   })();
19
20   EduModule = (function(_super) {
21
22     __extends(EduModule, _super);
23
24     function EduModule(element) {
25       var _this = this;
26       EduModule.__super__.constructor.call(this, 'edumodule', element);
27       $("[name=teacher-toggle]").change(function(ev) {
28         if ($(ev.target).is(":checked")) {
29           return $(".teacher", _this.element).addClass("show");
30         } else {
31           return $(".teacher", _this.element).removeClass("show");
32         }
33       });
34     }
35
36     return EduModule;
37
38   })(Binding);
39
40   Excercise = (function(_super) {
41
42     __extends(Excercise, _super);
43
44     function Excercise(element) {
45       var _this = this;
46       Excercise.__super__.constructor.call(this, 'excercise', element);
47       $(".check", this.element).click(function() {
48         return _this.check();
49       });
50       $('.solutions', this.element).click(function() {
51         return _this.show_solutions();
52       });
53     }
54
55     Excercise.prototype.piece_correct = function(qpiece) {
56       return $(qpiece).removeClass('incorrect').addClass('correct');
57     };
58
59     Excercise.prototype.piece_incorrect = function(qpiece) {
60       return $(qpiece).removeClass('correct').addClass('incorrect');
61     };
62
63     Excercise.prototype.check = function() {
64       var score, scores,
65         _this = this;
66       scores = [];
67       $(".question").each(function(i, question) {
68         return scores.push(_this.check_question(question));
69       });
70       score = [0, 0];
71       $.each(scores, function(i, s) {
72         score[0] += s[0];
73         return score[1] += s[1];
74       });
75       return this.show_score(score);
76     };
77
78     Excercise.prototype.get_value_list = function(elem, data_key) {
79       return $(elem).data(data_key).split(',').map($.trim).map(function(x) {
80         return parseInt(x);
81       });
82     };
83
84     Excercise.prototype.show_score = function(score) {
85       return $(".message", this.element).text("Wynik: " + score[0] + " / " + score[1]);
86     };
87
88     return Excercise;
89
90   })(Binding);
91
92   Wybor = (function(_super) {
93
94     __extends(Wybor, _super);
95
96     function Wybor(element) {
97       Wybor.__super__.constructor.call(this, element);
98     }
99
100     Wybor.prototype.check_question = function(question) {
101       var all, good, solution,
102         _this = this;
103       all = 0;
104       good = 0;
105       solution = this.get_value_list(question, 'solution');
106       $(".question-piece", question).each(function(i, qpiece) {
107         var is_checked, piece_no, should_be_checked;
108         piece_no = parseInt($(qpiece).attr('data-no'));
109         should_be_checked = solution.indexOf(piece_no) >= 0;
110         is_checked = $("input", qpiece).is(":checked");
111         if (should_be_checked) all += 1;
112         if (is_checked) {
113           if (should_be_checked) {
114             good += 1;
115             return _this.piece_correct(qpiece);
116           } else {
117             return _this.piece_incorrect(qpiece);
118           }
119         } else {
120           return $(qpiece).removeClass("correct,incorrect");
121         }
122       });
123       return [good, all];
124     };
125
126     Wybor.prototype.show_solutions = function() {};
127
128     return Wybor;
129
130   })(Excercise);
131
132   Uporzadkuj = (function(_super) {
133
134     __extends(Uporzadkuj, _super);
135
136     function Uporzadkuj(element) {
137       Uporzadkuj.__super__.constructor.call(this, element);
138       $('ol, ul', this.element).sortable({
139         items: "> li"
140       });
141     }
142
143     Uporzadkuj.prototype.check_question = function(question) {
144       var all, correct, pkt, pkts, positions, sorted, _ref;
145       positions = this.get_value_list(question, 'original');
146       sorted = positions.sort();
147       pkts = $('.question-piece', question);
148       correct = 0;
149       all = 0;
150       for (pkt = 0, _ref = pkts.length; 0 <= _ref ? pkt < _ref : pkt > _ref; 0 <= _ref ? pkt++ : pkt--) {
151         all += 1;
152         if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
153           correct += 1;
154           this.piece_correct(pkts.eq(pkt));
155         } else {
156           this.piece_incorrect(pkts.eq(pkt));
157         }
158       }
159       return [correct, all];
160     };
161
162     return Uporzadkuj;
163
164   })(Excercise);
165
166   Luki = (function(_super) {
167
168     __extends(Luki, _super);
169
170     function Luki(element) {
171       Luki.__super__.constructor.call(this, element);
172     }
173
174     Luki.prototype.check = function() {
175       var all, correct,
176         _this = this;
177       all = 0;
178       correct = 0;
179       $(".question-piece", this.element).each(function(i, qpiece) {
180         if ($(qpiece).data('solution') === $(qpiece).val()) {
181           _this.piece_correct(qpiece);
182           correct += 1;
183         } else {
184           _this.piece_incorrect(qpiece);
185         }
186         return all += 1;
187       });
188       return this.show_score([correct, all]);
189     };
190
191     return Luki;
192
193   })(Excercise);
194
195   Zastap = (function(_super) {
196
197     __extends(Zastap, _super);
198
199     function Zastap(element) {
200       var _this = this;
201       Zastap.__super__.constructor.call(this, element);
202       $(".paragraph", this.element).each(function(i, par) {
203         var spans;
204         _this.wrap_words($(par), $('<span class="zastap question-piece"/>'));
205         spans = $("> span", par).attr("contenteditable", "true");
206         return spans.click(function(ev) {
207           spans.filter(':not(:empty)').removeClass('editing');
208           return $(ev.target).addClass('editing');
209         });
210       });
211     }
212
213     Zastap.prototype.check = function() {
214       var all, correct,
215         _this = this;
216       all = 0;
217       correct = 0;
218       $(".question-piece", this.element).each(function(i, qpiece) {
219         var should_be_changed, txt;
220         txt = $(qpiece).data('original');
221         should_be_changed = false;
222         if (!(txt != null)) {
223           txt = $(qpiece).data('solution');
224           should_be_changed = true;
225         }
226         if (!(txt != null)) return;
227         if (should_be_changed) all += 1;
228         if (txt !== $(qpiece).text()) {
229           return _this.piece_incorrect(qpiece);
230         } else {
231           if (should_be_changed) {
232             _this.piece_correct(qpiece);
233             return correct += 1;
234           }
235         }
236       });
237       return this.show_score([correct, all]);
238     };
239
240     Zastap.prototype.wrap_words = function(element, wrapper) {
241       var chld, i, ignore, insertWrapped, j, len, space, wordb, _ref, _results;
242       ignore = /^[ \t.,:;()]+/;
243       insertWrapped = function(txt, elem) {
244         var nw;
245         nw = wrapper.clone();
246         return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
247       };
248       _results = [];
249       for (j = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? j <= 0 : j >= 0; _ref <= 0 ? j++ : j--) {
250         chld = element.get(0).childNodes[j];
251         if (chld.nodeType === document.TEXT_NODE) {
252           len = chld.textContent.length;
253           wordb = 0;
254           i = 0;
255           while (i < len) {
256             space = ignore.exec(chld.textContent.substr(i));
257             if (space != null) {
258               if (wordb < i) {
259                 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
260               }
261               $(document.createTextNode(space[0])).insertBefore(chld);
262               i += space[0].length;
263               wordb = i;
264             } else {
265               i = i + 1;
266             }
267           }
268           if (wordb < len - 1) {
269             insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
270           }
271           _results.push($(chld).remove());
272         } else {
273           _results.push(void 0);
274         }
275       }
276       return _results;
277     };
278
279     return Zastap;
280
281   })(Excercise);
282
283   excercise = function(ele) {
284     var cls, es;
285     es = {
286       wybor: Wybor,
287       uporzadkuj: Uporzadkuj,
288       luki: Luki,
289       zastap: Zastap
290     };
291     cls = es[$(ele).attr('data-type')];
292     return new cls(ele);
293   };
294
295   window.edumed = {
296     'EduModule': EduModule
297   };
298
299   $(document).ready(function() {
300     new EduModule($("#book-text"));
301     return $(".excercise").each(function(i, el) {
302       return excercise(this);
303     });
304   });
305
306 }).call(this);