Introducing edumed.coffee from redakcja repository
[edumed.git] / wtem / static / wtem / edumed.js
1 // Generated by CoffeeScript 1.6.3
2 (function() {
3   var $, Binding, EduModule, Exercise, Luki, PrawdaFalsz, Przyporzadkuj, Uporzadkuj, Wybor, Zastap, exercise,
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     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     __extends(EduModule, _super);
22
23     function EduModule(element) {
24       EduModule.__super__.constructor.call(this, 'edumodule', element);
25     }
26
27     return EduModule;
28
29   })(Binding);
30
31   Exercise = (function(_super) {
32     __extends(Exercise, _super);
33
34     function Exercise(element) {
35       var _this = this;
36       Exercise.__super__.constructor.call(this, 'exercise', element);
37       $(this.element).data("exercise-html", $(this.element).html());
38       $(".check", this.element).click(function(ev) {
39         _this.check();
40         $(".retry", _this.element).show();
41         return $(".check", _this.element).hide();
42       });
43       $(".retry", this.element).click(function(ev) {
44         return _this.retry();
45       });
46       $('.solutions', this.element).click(function() {
47         _this.show_solutions();
48         return $(".comment", _this.element).show();
49       });
50       $('.reset', this.element).click(function() {
51         return _this.reset();
52       });
53     }
54
55     Exercise.prototype.retry = function() {
56       $(".correct, .incorrect", this.element).removeClass("correct incorrect");
57       $(".check", this.element).show();
58       return $(".retry", this.element).hide();
59     };
60
61     Exercise.prototype.reset = function() {
62       $(this.element).html($(this.element).data('exercise-html'));
63       return exercise(this.element);
64     };
65
66     Exercise.prototype.piece_correct = function(qpiece) {
67       return $(qpiece).removeClass('incorrect').addClass('correct');
68     };
69
70     Exercise.prototype.piece_incorrect = function(qpiece) {
71       return $(qpiece).removeClass('correct').addClass('incorrect');
72     };
73
74     Exercise.prototype.check = function() {
75       var score, scores,
76         _this = this;
77       scores = [];
78       $(".question", this.element).each(function(i, question) {
79         return scores.push(_this.check_question(question));
80       });
81       score = [0, 0, 0];
82       $.each(scores, function(i, s) {
83         score[0] += s[0];
84         score[1] += s[1];
85         return score[2] += s[2];
86       });
87       return this.show_score(score);
88     };
89
90     Exercise.prototype.show_solutions = function() {
91       var _this = this;
92       this.reset();
93       return $(".question", this.element).each(function(i, question) {
94         return _this.solve_question(question);
95       });
96     };
97
98     Exercise.prototype.get_value_list = function(elem, data_key, numbers) {
99       var vl;
100       vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
101       if (numbers) {
102         vl = vl.map(function(x) {
103           return parseInt(x);
104         });
105       }
106       return vl;
107     };
108
109     Exercise.prototype.get_value_optional_list = function(elem, data_key) {
110       var mandat, opt, v, vals, _i, _len;
111       vals = this.get_value_list(elem, data_key);
112       mandat = [];
113       opt = [];
114       for (_i = 0, _len = vals.length; _i < _len; _i++) {
115         v = vals[_i];
116         if (v.slice(-1) === "?") {
117           opt.push(v.slice(0, -1));
118         } else {
119           mandat.push(v);
120         }
121       }
122       return [mandat, opt];
123     };
124
125     Exercise.prototype.show_score = function(score) {
126       var $msg;
127       $msg = $(".message", this.element);
128       $msg.text("Wynik: " + score[0] + " / " + score[2]);
129       if (score[0] >= score[2] && score[1] === 0) {
130         return $msg.addClass("maxscore");
131       } else {
132         return $msg.removeClass("maxscore");
133       }
134     };
135
136     Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
137       return false;
138     };
139
140     Exercise.prototype.draggable_accept = function($draggable, $droppable) {
141       var d, dropped, _i, _len;
142       dropped = $droppable.closest("ul, ol").find(".draggable");
143       for (_i = 0, _len = dropped.length; _i < _len; _i++) {
144         d = dropped[_i];
145         if (this.draggable_equal($draggable, $(d))) {
146           return false;
147         }
148       }
149       return true;
150     };
151
152     Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
153       var $added,
154         _this = this;
155       $added = $draggable.clone();
156       $added.data("original", $draggable.get(0));
157       if (!ismultiple) {
158         $draggable.addClass('disabled').draggable('disable');
159       }
160       $placeholder.after($added);
161       if (!$placeholder.hasClass('multiple')) {
162         $placeholder.hide();
163       }
164       if ($added.is(".add-li")) {
165         $added.wrap("<li/>");
166       }
167       $added.append('<span class="remove">x</span><div class="clr"></div>');
168       return $('.remove', $added).click(function(ev) {
169         _this.retry();
170         if (!ismultiple) {
171           $($added.data('original')).removeClass('disabled').draggable('enable');
172         }
173         if ($added.is(".add-li")) {
174           $added = $added.closest('li');
175         }
176         $added.prev(".placeholder:not(.multiple)").show();
177         return $added.remove();
178       });
179     };
180
181     Exercise.prototype.dragging = function(ismultiple, issortable) {
182       var _this = this;
183       return $(".question", this.element).each(function(i, question) {
184         var draggable_opts, self;
185         draggable_opts = {
186           revert: 'invalid',
187           helper: 'clone',
188           start: _this.retry
189         };
190         $(".draggable", question).draggable(draggable_opts);
191         self = _this;
192         return $(".placeholder", question).droppable({
193           accept: function(draggable) {
194             var $draggable, is_accepted;
195             $draggable = $(draggable);
196             is_accepted = true;
197             if (!$draggable.is(".draggable")) {
198               is_accepted = false;
199             }
200             if (is_accepted) {
201               is_accepted = self.draggable_accept($draggable, $(this));
202             }
203             if (is_accepted) {
204               $(this).addClass('accepting');
205             } else {
206               $(this).removeClass('accepting');
207             }
208             return is_accepted;
209           },
210           drop: function(ev, ui) {
211             $(ev.target).removeClass('accepting dragover');
212             return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
213           },
214           over: function(ev, ui) {
215             return $(ev.target).addClass('dragover');
216           },
217           out: function(ev, ui) {
218             return $(ev.target).removeClass('dragover');
219           }
220         });
221       });
222     };
223
224     return Exercise;
225
226   })(Binding);
227
228   Wybor = (function(_super) {
229     __extends(Wybor, _super);
230
231     function Wybor(element) {
232       Wybor.__super__.constructor.call(this, element);
233       $(".question-piece input", element).change(this.retry);
234     }
235
236     Wybor.prototype.check_question = function(question) {
237       var all, bad, good, solution,
238         _this = this;
239       all = 0;
240       good = 0;
241       bad = 0;
242       solution = this.get_value_list(question, 'solution');
243       $(".question-piece", question).each(function(i, qpiece) {
244         var is_checked, piece_name, piece_no, should_be_checked;
245         piece_no = $(qpiece).attr('data-no');
246         piece_name = $(qpiece).attr('data-name');
247         if (piece_name) {
248           should_be_checked = solution.indexOf(piece_name) >= 0;
249         } else {
250           should_be_checked = solution.indexOf(piece_no) >= 0;
251         }
252         is_checked = $("input", qpiece).is(":checked");
253         if (should_be_checked) {
254           all += 1;
255         }
256         if (is_checked) {
257           if (should_be_checked) {
258             good += 1;
259             return _this.piece_correct(qpiece);
260           } else {
261             bad += 1;
262             return _this.piece_incorrect(qpiece);
263           }
264         } else {
265           return $(qpiece).removeClass("correct,incorrect");
266         }
267       });
268       return [good, bad, all];
269     };
270
271     Wybor.prototype.solve_question = function(question) {
272       var solution,
273         _this = this;
274       solution = this.get_value_list(question, 'solution');
275       return $(".question-piece", question).each(function(i, qpiece) {
276         var piece_name, piece_no, should_be_checked;
277         piece_no = $(qpiece).attr('data-no');
278         piece_name = $(qpiece).attr('data-name');
279         if (piece_name) {
280           should_be_checked = solution.indexOf(piece_name) >= 0;
281         } else {
282           should_be_checked = solution.indexOf(piece_no) >= 0;
283         }
284         console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
285         return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
286       });
287     };
288
289     return Wybor;
290
291   })(Exercise);
292
293   Uporzadkuj = (function(_super) {
294     __extends(Uporzadkuj, _super);
295
296     function Uporzadkuj(element) {
297       Uporzadkuj.__super__.constructor.call(this, element);
298       $('ol, ul', this.element).sortable({
299         items: "> li",
300         start: this.retry
301       });
302     }
303
304     Uporzadkuj.prototype.check_question = function(question) {
305       var all, bad, correct, pkt, pkts, positions, sorted, _i, _ref;
306       positions = this.get_value_list(question, 'original', true);
307       sorted = positions.sort();
308       pkts = $('.question-piece', question);
309       correct = 0;
310       bad = 0;
311       all = 0;
312       for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
313         all += 1;
314         if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
315           correct += 1;
316           this.piece_correct(pkts.eq(pkt));
317         } else {
318           bad += 1;
319           this.piece_incorrect(pkts.eq(pkt));
320         }
321       }
322       return [correct, bad, all];
323     };
324
325     Uporzadkuj.prototype.solve_question = function(question) {
326       var p, parent, pkts, positions, sorted, _i, _len, _results;
327       positions = this.get_value_list(question, 'original', true);
328       sorted = positions.sort();
329       pkts = $('.question-piece', question);
330       pkts.sort(function(a, b) {
331         var q, w;
332         q = $(a).data('pos');
333         w = $(b).data('pos');
334         if (q < w) {
335           return 1;
336         }
337         if (q > w) {
338           return -1;
339         }
340         return 0;
341       });
342       parent = pkts.eq(0).parent();
343       _results = [];
344       for (_i = 0, _len = pkts.length; _i < _len; _i++) {
345         p = pkts[_i];
346         _results.push(parent.prepend(p));
347       }
348       return _results;
349     };
350
351     return Uporzadkuj;
352
353   })(Exercise);
354
355   Luki = (function(_super) {
356     __extends(Luki, _super);
357
358     function Luki(element) {
359       Luki.__super__.constructor.call(this, element);
360       this.dragging(false, false);
361     }
362
363     Luki.prototype.check = function() {
364       var all, bad, correct,
365         _this = this;
366       all = $(".placeholder", this.element).length;
367       correct = 0;
368       bad = 0;
369       $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
370         var $placeholder;
371         $placeholder = $(qpiece).prev(".placeholder");
372         if ($placeholder.data('solution') === $(qpiece).data('no')) {
373           _this.piece_correct(qpiece);
374           return correct += 1;
375         } else {
376           bad += 1;
377           return _this.piece_incorrect(qpiece);
378         }
379       });
380       return this.show_score([correct, bad, all]);
381     };
382
383     Luki.prototype.solve_question = function(question) {
384       var _this = this;
385       return $(".placeholder", question).each(function(i, placeholder) {
386         var $qp;
387         $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
388         return _this.draggable_move($qp, $(placeholder), false);
389       });
390     };
391
392     return Luki;
393
394   })(Exercise);
395
396   Zastap = (function(_super) {
397     __extends(Zastap, _super);
398
399     function Zastap(element) {
400       var _this = this;
401       Zastap.__super__.constructor.call(this, element);
402       $(".paragraph", this.element).each(function(i, par) {
403         return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
404       });
405       this.dragging(false, false);
406     }
407
408     Zastap.prototype.check = function() {
409       var all, bad, correct,
410         _this = this;
411       all = 0;
412       correct = 0;
413       bad = 0;
414       $(".paragraph", this.element).each(function(i, par) {
415         return $(".placeholder", par).each(function(j, qpiece) {
416           var $dragged, $qp;
417           $qp = $(qpiece);
418           $dragged = $qp.next(".draggable");
419           if ($qp.data("solution")) {
420             if ($dragged && $qp.data("solution") === $dragged.data("no")) {
421               _this.piece_correct($dragged);
422               correct += 1;
423             }
424             return all += 1;
425           }
426         });
427       });
428       return this.show_score([correct, bad, all]);
429     };
430
431     Zastap.prototype.show_solutions = function() {
432       var _this = this;
433       this.reset();
434       return $(".paragraph", this.element).each(function(i, par) {
435         return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
436           var $dr, $qp;
437           $qp = $(qpiece);
438           $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
439           return _this.draggable_move($dr, $qp, false);
440         });
441       });
442     };
443
444     Zastap.prototype.wrap_words = function(element, wrapper) {
445       var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
446       ignore = /^[ \t.,:;()]+/;
447       insertWrapped = function(txt, elem) {
448         var nw;
449         nw = wrapper.clone();
450         return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
451       };
452       _results = [];
453       for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
454         chld = element.get(0).childNodes[j];
455         if (chld.nodeType === document.TEXT_NODE) {
456           len = chld.textContent.length;
457           wordb = 0;
458           i = 0;
459           while (i < len) {
460             space = ignore.exec(chld.textContent.substr(i));
461             if (space != null) {
462               if (wordb < i) {
463                 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
464               }
465               $(document.createTextNode(space[0])).insertBefore(chld);
466               i += space[0].length;
467               wordb = i;
468             } else {
469               i = i + 1;
470             }
471           }
472           if (wordb < len - 1) {
473             insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
474           }
475           _results.push($(chld).remove());
476         } else {
477           _results.push(void 0);
478         }
479       }
480       return _results;
481     };
482
483     return Zastap;
484
485   })(Exercise);
486
487   Przyporzadkuj = (function(_super) {
488     __extends(Przyporzadkuj, _super);
489
490     Przyporzadkuj.prototype.is_multiple = function() {
491       var qp, _i, _len, _ref;
492       _ref = $(".question-piece", this.element);
493       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
494         qp = _ref[_i];
495         if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
496           return true;
497         }
498       }
499       return false;
500     };
501
502     function Przyporzadkuj(element) {
503       Przyporzadkuj.__super__.constructor.call(this, element);
504       this.multiple = this.is_multiple();
505       this.dragging(this.multiple, true);
506     }
507
508     Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
509       return d1.data("no") === d2.data("no");
510     };
511
512     Przyporzadkuj.prototype.check_question = function(question) {
513       var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
514       minimum = $(question).data("minimum");
515       count = 0;
516       bad_count = 0;
517       all = 0;
518       if (!minimum) {
519         self = this;
520         $(".subject .question-piece", question).each(function(i, el) {
521           var mandatory, v;
522           v = self.get_value_optional_list(el, 'solution');
523           mandatory = v[0];
524           return all += mandatory.length;
525         });
526       }
527       _ref = $(".predicate [data-predicate]", question);
528       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
529         pred = _ref[_i];
530         pn = $(pred).attr('data-predicate');
531         _ref1 = $(".question-piece", pred);
532         for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
533           qp = _ref1[_j];
534           v = this.get_value_optional_list(qp, 'solution');
535           mandatory = v[0];
536           optional = v[1];
537           if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
538             count += 1;
539             this.piece_correct(qp);
540           } else {
541             bad_count += 1;
542             this.piece_incorrect(qp);
543           }
544         }
545       }
546       return [count, bad_count, all];
547     };
548
549     Przyporzadkuj.prototype.solve_question = function(question) {
550       var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
551       minimum = $(question).data("min");
552       _ref = $(".subject .question-piece", question);
553       _results = [];
554       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
555         qp = _ref[_i];
556         v = this.get_value_optional_list(qp, 'solution');
557         mandatory = v[0];
558         optional = v[1];
559         if (minimum) {
560           draggables = mandatory.count(optional).slice(0, minimum);
561         } else {
562           draggables = mandatory;
563         }
564         _results.push((function() {
565           var _j, _len1, _results1;
566           _results1 = [];
567           for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
568             m = draggables[_j];
569             $pr = $(".predicate [data-predicate=" + m + "]", question);
570             $ph = $pr.find(".placeholder:visible");
571             _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
572           }
573           return _results1;
574         }).call(this));
575       }
576       return _results;
577     };
578
579     return Przyporzadkuj;
580
581   })(Exercise);
582
583   PrawdaFalsz = (function(_super) {
584     __extends(PrawdaFalsz, _super);
585
586     function PrawdaFalsz(element) {
587       var qp, _i, _len, _ref,
588         _this = this;
589       PrawdaFalsz.__super__.constructor.call(this, element);
590       _ref = $(".question-piece", this.element);
591       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
592         qp = _ref[_i];
593         $(".true", qp).click(function(ev) {
594           ev.preventDefault();
595           _this.retry();
596           $(ev.target).closest(".question-piece").data("value", "true");
597           return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
598         });
599         $(".false", qp).click(function(ev) {
600           ev.preventDefault();
601           _this.retry();
602           $(ev.target).closest(".question-piece").data("value", "false");
603           return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
604         });
605       }
606     }
607
608     PrawdaFalsz.prototype.check_question = function() {
609       var all, bad, good, qp, _i, _len, _ref;
610       all = 0;
611       good = 0;
612       bad = 0;
613       _ref = $(".question-piece", this.element);
614       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
615         qp = _ref[_i];
616         if ($(qp).data("solution").toString() === $(qp).data("value")) {
617           good += 1;
618           this.piece_correct(qp);
619         } else {
620           bad += 1;
621           this.piece_incorrect(qp);
622         }
623         all += 1;
624       }
625       return [good, bad, all];
626     };
627
628     PrawdaFalsz.prototype.show_solutions = function() {
629       var qp, _i, _len, _ref, _results;
630       this.reset();
631       _ref = $(".question-piece", this.element);
632       _results = [];
633       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
634         qp = _ref[_i];
635         if ($(qp).data('solution') === true) {
636           _results.push($(".true", qp).click());
637         } else {
638           _results.push($(".false", qp).click());
639         }
640       }
641       return _results;
642     };
643
644     return PrawdaFalsz;
645
646   })(Exercise);
647
648   exercise = function(ele) {
649     var cls, es;
650     es = {
651       wybor: Wybor,
652       uporzadkuj: Uporzadkuj,
653       luki: Luki,
654       zastap: Zastap,
655       przyporzadkuj: Przyporzadkuj,
656       prawdafalsz: PrawdaFalsz
657     };
658     cls = es[$(ele).attr('data-type')];
659     return new cls(ele);
660   };
661
662   window.edumed = {
663     'EduModule': EduModule
664   };
665
666   $(document).ready(function() {
667     new EduModule($("#book-text"));
668     return $(".exercise").each(function(i, el) {
669       return exercise(this);
670     });
671   });
672
673 }).call(this);