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