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