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