1 // Generated by CoffeeScript 1.6.3
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; };
9 Binding = (function() {
10 function Binding(handler, element) {
11 this.handler = handler;
12 this.element = element;
13 $(this.element).data(this.handler, this);
20 EduModule = (function(_super) {
21 __extends(EduModule, _super);
23 function EduModule(element) {
24 EduModule.__super__.constructor.call(this, 'edumodule', element);
31 Exercise = (function(_super) {
32 __extends(Exercise, _super);
34 function Exercise(element) {
36 Exercise.__super__.constructor.call(this, 'exercise', element);
37 $(this.element).data("exercise-html", $(this.element).html());
38 $(".check", this.element).click(function(ev) {
40 $(".retry", _this.element).show();
41 return $(".check", _this.element).hide();
43 $(".retry", this.element).click(function(ev) {
46 $('.solutions', this.element).click(function() {
47 _this.show_solutions();
48 return $(".comment", _this.element).show();
50 $('.reset', this.element).click(function() {
55 Exercise.prototype.retry = function() {
56 $(".correct, .incorrect", this.element).removeClass("correct incorrect");
57 $(".check", this.element).show();
58 return $(".retry", this.element).hide();
61 Exercise.prototype.reset = function() {
62 $(this.element).html($(this.element).data('exercise-html'));
63 return exercise(this.element);
66 Exercise.prototype.piece_correct = function(qpiece) {
67 return $(qpiece).removeClass('incorrect').addClass('correct');
70 Exercise.prototype.piece_incorrect = function(qpiece) {
71 return $(qpiece).removeClass('correct').addClass('incorrect');
74 Exercise.prototype.check = function() {
78 $(".question", this.element).each(function(i, question) {
79 return scores.push(_this.check_question(question));
82 $.each(scores, function(i, s) {
85 return score[2] += s[2];
87 return this.show_score(score);
90 Exercise.prototype.show_solutions = function() {
93 return $(".question", this.element).each(function(i, question) {
94 return _this.solve_question(question);
98 Exercise.prototype.get_answers = function() {
102 $('.question', this.element).each(function(i, question) {
103 return answers.push(_this.get_answer(question));
108 Exercise.prototype.get_value_list = function(elem, data_key, numbers) {
110 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
112 vl = vl.map(function(x) {
119 Exercise.prototype.get_value_optional_list = function(elem, data_key) {
120 var mandat, opt, v, vals, _i, _len;
121 vals = this.get_value_list(elem, data_key);
124 for (_i = 0, _len = vals.length; _i < _len; _i++) {
126 if (v.slice(-1) === "?") {
127 opt.push(v.slice(0, -1));
132 return [mandat, opt];
135 Exercise.prototype.show_score = function(score) {
137 $msg = $(".message", this.element);
138 $msg.text("Wynik: " + score[0] + " / " + score[2]);
139 if (score[0] >= score[2] && score[1] === 0) {
140 return $msg.addClass("maxscore");
142 return $msg.removeClass("maxscore");
146 Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
150 Exercise.prototype.draggable_accept = function($draggable, $droppable) {
151 var d, dropped, _i, _len;
152 dropped = $droppable.closest("ul, ol").find(".draggable");
153 for (_i = 0, _len = dropped.length; _i < _len; _i++) {
155 if (this.draggable_equal($draggable, $(d))) {
162 Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
165 $added = $draggable.clone();
166 $added.data("original", $draggable.get(0));
168 $draggable.addClass('disabled').draggable('disable');
170 $placeholder.after($added);
171 if (!$placeholder.hasClass('multiple')) {
174 if ($added.is(".add-li")) {
175 $added.wrap("<li/>");
177 $added.append('<span class="remove">x</span><div class="clr"></div>');
178 return $('.remove', $added).click(function(ev) {
181 $($added.data('original')).removeClass('disabled').draggable('enable');
183 if ($added.is(".add-li")) {
184 $added = $added.closest('li');
186 $added.prev(".placeholder:not(.multiple)").show();
187 return $added.remove();
191 Exercise.prototype.dragging = function(ismultiple, issortable) {
193 return $(".question", this.element).each(function(i, question) {
194 var draggable_opts, self;
200 $(".draggable", question).draggable(draggable_opts);
202 return $(".placeholder", question).droppable({
203 accept: function(draggable) {
204 var $draggable, is_accepted;
205 $draggable = $(draggable);
207 if (!$draggable.is(".draggable")) {
211 is_accepted = self.draggable_accept($draggable, $(this));
214 $(this).addClass('accepting');
216 $(this).removeClass('accepting');
220 drop: function(ev, ui) {
221 $(ev.target).removeClass('accepting dragover');
222 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
224 over: function(ev, ui) {
225 return $(ev.target).addClass('dragover');
227 out: function(ev, ui) {
228 return $(ev.target).removeClass('dragover');
238 Wybor = (function(_super) {
239 __extends(Wybor, _super);
241 function Wybor(element) {
242 Wybor.__super__.constructor.call(this, element);
243 $(".question-piece input", element).change(this.retry);
246 Wybor.prototype.check_question = function(question) {
247 var all, bad, good, solution,
252 solution = this.get_value_list(question, 'solution');
253 $(".question-piece", question).each(function(i, qpiece) {
254 var is_checked, piece_name, piece_no, should_be_checked;
255 piece_no = $(qpiece).attr('data-no');
256 piece_name = $(qpiece).attr('data-name');
258 should_be_checked = solution.indexOf(piece_name) >= 0;
260 should_be_checked = solution.indexOf(piece_no) >= 0;
262 is_checked = $("input", qpiece).is(":checked");
263 if (should_be_checked) {
267 if (should_be_checked) {
269 return _this.piece_correct(qpiece);
272 return _this.piece_incorrect(qpiece);
275 return $(qpiece).removeClass("correct,incorrect");
278 return [good, bad, all];
281 Wybor.prototype.solve_question = function(question) {
284 solution = this.get_value_list(question, 'solution');
285 return $(".question-piece", question).each(function(i, qpiece) {
286 var piece_name, piece_no, should_be_checked;
287 piece_no = $(qpiece).attr('data-no');
288 piece_name = $(qpiece).attr('data-name');
290 should_be_checked = solution.indexOf(piece_name) >= 0;
292 should_be_checked = solution.indexOf(piece_no) >= 0;
294 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
295 return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
299 Wybor.prototype.get_answer = function(question) {
303 $('.question-piece', question).each(function(i, qpiece) {
306 if ($("input[type=checkbox],input[type=radio]", qpiece).is(':checked')) {
307 return answer.push($qpiece.attr('data-name'));
317 Uporzadkuj = (function(_super) {
318 __extends(Uporzadkuj, _super);
320 function Uporzadkuj(element) {
321 Uporzadkuj.__super__.constructor.call(this, element);
322 $('ol, ul', this.element).sortable({
328 Uporzadkuj.prototype.check_question = function(question) {
329 var all, bad, correct, pkt, pkts, positions, sorted, _i, _ref;
330 positions = this.get_value_list(question, 'original', true);
331 sorted = positions.sort();
332 pkts = $('.question-piece', question);
336 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
338 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
340 this.piece_correct(pkts.eq(pkt));
343 this.piece_incorrect(pkts.eq(pkt));
346 return [correct, bad, all];
349 Uporzadkuj.prototype.solve_question = function(question) {
350 var p, parent, pkts, positions, sorted, _i, _len, _results;
351 positions = this.get_value_list(question, 'original', true);
352 sorted = positions.sort();
353 pkts = $('.question-piece', question);
354 pkts.sort(function(a, b) {
356 q = $(a).data('pos');
357 w = $(b).data('pos');
366 parent = pkts.eq(0).parent();
368 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
370 _results.push(parent.prepend(p));
375 Uporzadkuj.prototype.get_answer = function(question) {
379 $(".question-piece", this.element).each(function(i, qpiece) {
380 return answer.push($(qpiece).attr('data-pos'));
389 Luki = (function(_super) {
390 __extends(Luki, _super);
392 function Luki(element) {
393 Luki.__super__.constructor.call(this, element);
394 this.dragging(false, false);
397 Luki.prototype.check = function() {
398 var all, bad, correct,
400 all = $(".placeholder", this.element).length;
403 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
405 $placeholder = $(qpiece).prev(".placeholder");
406 if ($placeholder.data('solution') === $(qpiece).data('no')) {
407 _this.piece_correct(qpiece);
411 return _this.piece_incorrect(qpiece);
414 return this.show_score([correct, bad, all]);
417 Luki.prototype.solve_question = function(question) {
419 return $(".placeholder", question).each(function(i, placeholder) {
421 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
422 return _this.draggable_move($qp, $(placeholder), false);
430 Zastap = (function(_super) {
431 __extends(Zastap, _super);
433 function Zastap(element) {
435 Zastap.__super__.constructor.call(this, element);
436 $(".paragraph", this.element).each(function(i, par) {
437 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
439 this.dragging(false, false);
442 Zastap.prototype.check = function() {
443 var all, bad, correct,
448 $(".paragraph", this.element).each(function(i, par) {
449 return $(".placeholder", par).each(function(j, qpiece) {
452 $dragged = $qp.next(".draggable");
453 if ($qp.data("solution")) {
454 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
455 _this.piece_correct($dragged);
462 return this.show_score([correct, bad, all]);
465 Zastap.prototype.show_solutions = function() {
468 return $(".paragraph", this.element).each(function(i, par) {
469 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
472 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
473 return _this.draggable_move($dr, $qp, false);
478 Zastap.prototype.wrap_words = function(element, wrapper) {
479 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
480 ignore = /^[ \t.,:;()]+/;
481 insertWrapped = function(txt, elem) {
483 nw = wrapper.clone();
484 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
487 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
488 chld = element.get(0).childNodes[j];
489 if (chld.nodeType === document.TEXT_NODE) {
490 len = chld.textContent.length;
494 space = ignore.exec(chld.textContent.substr(i));
497 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
499 $(document.createTextNode(space[0])).insertBefore(chld);
500 i += space[0].length;
506 if (wordb < len - 1) {
507 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
509 _results.push($(chld).remove());
511 _results.push(void 0);
521 Przyporzadkuj = (function(_super) {
522 __extends(Przyporzadkuj, _super);
524 Przyporzadkuj.prototype.is_multiple = function() {
525 var qp, _i, _len, _ref;
526 _ref = $(".question-piece", this.element);
527 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
529 if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
536 function Przyporzadkuj(element) {
537 Przyporzadkuj.__super__.constructor.call(this, element);
538 this.multiple = this.is_multiple();
539 this.dragging(this.multiple, true);
542 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
543 return d1.data("no") === d2.data("no");
546 Przyporzadkuj.prototype.check_question = function(question) {
547 var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
548 minimum = $(question).data("minimum");
554 $(".subject .question-piece", question).each(function(i, el) {
556 v = self.get_value_optional_list(el, 'solution');
558 return all += mandatory.length;
561 _ref = $(".predicate [data-predicate]", question);
562 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
564 pn = $(pred).attr('data-predicate');
565 _ref1 = $(".question-piece", pred);
566 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
568 v = this.get_value_optional_list(qp, 'solution');
571 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
573 this.piece_correct(qp);
576 this.piece_incorrect(qp);
580 return [count, bad_count, all];
583 Przyporzadkuj.prototype.solve_question = function(question) {
584 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
585 minimum = $(question).data("min");
586 _ref = $(".subject .question-piece", question);
588 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
590 v = this.get_value_optional_list(qp, 'solution');
594 draggables = mandatory.count(optional).slice(0, minimum);
596 draggables = mandatory;
598 _results.push((function() {
599 var _j, _len1, _results1;
601 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
603 $pr = $(".predicate [data-predicate=" + m + "]", question);
604 $ph = $pr.find(".placeholder:visible");
605 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
613 Przyporzadkuj.prototype.get_answer = function(question) {
617 $(".predicate [data-predicate]", question).each(function(i, subjects) {
619 predicate = $(subjects).attr('data-predicate');
620 answer[predicate] = [];
621 return $('.question-piece', subjects).each(function(i, qpiece) {
624 return answer[predicate].push($qpiece.attr('data-id'));
630 return Przyporzadkuj;
634 PrawdaFalsz = (function(_super) {
635 __extends(PrawdaFalsz, _super);
637 function PrawdaFalsz(element) {
638 var qp, _i, _len, _ref,
640 PrawdaFalsz.__super__.constructor.call(this, element);
641 _ref = $(".question-piece", this.element);
642 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
644 $(".true", qp).click(function(ev) {
647 $(ev.target).closest(".question-piece").data("value", "true");
648 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
650 $(".false", qp).click(function(ev) {
653 $(ev.target).closest(".question-piece").data("value", "false");
654 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
659 PrawdaFalsz.prototype.check_question = function() {
660 var all, bad, good, qp, _i, _len, _ref;
664 _ref = $(".question-piece", this.element);
665 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
667 if ($(qp).data("solution").toString() === $(qp).data("value")) {
669 this.piece_correct(qp);
672 this.piece_incorrect(qp);
676 return [good, bad, all];
679 PrawdaFalsz.prototype.show_solutions = function() {
680 var qp, _i, _len, _ref, _results;
682 _ref = $(".question-piece", this.element);
684 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
686 if ($(qp).data('solution') === true) {
687 _results.push($(".true", qp).click());
689 _results.push($(".false", qp).click());
695 PrawdaFalsz.prototype.get_answer = function(question) {
699 $(".question-piece", this.element).each(function(i, qpiece) {
700 return answer.push($(qpiece).data('value') || '-');
709 exercise = function(ele) {
713 uporzadkuj: Uporzadkuj,
716 przyporzadkuj: Przyporzadkuj,
717 prawdafalsz: PrawdaFalsz
719 cls = es[$(ele).attr('data-type')];
724 'EduModule': EduModule
727 $(document).ready(function() {
728 new EduModule($("#book-text"));
729 return $(".exercise").each(function(i, el) {
730 return exercise(this);