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.draggable_accept = function($draggable, $droppable) {
548 dropped = $droppable.closest("ul, ol").find(".draggable");
549 return (Przyporzadkuj.__super__.draggable_accept.call(this, $draggable, $droppable)) && dropped.length === 0;
552 Przyporzadkuj.prototype.check_question = function(question) {
553 var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
554 minimum = $(question).data("minimum");
560 $(".subject .question-piece", question).each(function(i, el) {
562 v = self.get_value_optional_list(el, 'solution');
564 return all += mandatory.length;
567 _ref = $(".predicate [data-predicate]", question);
568 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
570 pn = $(pred).attr('data-predicate');
571 _ref1 = $(".question-piece", pred);
572 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
574 v = this.get_value_optional_list(qp, 'solution');
577 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
579 this.piece_correct(qp);
582 this.piece_incorrect(qp);
586 return [count, bad_count, all];
589 Przyporzadkuj.prototype.solve_question = function(question) {
590 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
591 minimum = $(question).data("min");
592 _ref = $(".subject .question-piece", question);
594 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
596 v = this.get_value_optional_list(qp, 'solution');
600 draggables = mandatory.count(optional).slice(0, minimum);
602 draggables = mandatory;
604 _results.push((function() {
605 var _j, _len1, _results1;
607 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
609 $pr = $(".predicate [data-predicate=" + m + "]", question);
610 $ph = $pr.find(".placeholder:visible");
611 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
619 Przyporzadkuj.prototype.get_answer = function(question) {
623 $(".predicate [data-predicate]", question).each(function(i, subjects) {
625 predicate = $(subjects).attr('data-predicate');
626 answer[predicate] = [];
627 return $('.question-piece', subjects).each(function(i, qpiece) {
630 return answer[predicate].push($qpiece.attr('data-id'));
636 return Przyporzadkuj;
640 PrawdaFalsz = (function(_super) {
641 __extends(PrawdaFalsz, _super);
643 function PrawdaFalsz(element) {
644 var qp, _i, _len, _ref,
646 PrawdaFalsz.__super__.constructor.call(this, element);
647 _ref = $(".question-piece", this.element);
648 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
650 $(".true", qp).click(function(ev) {
653 $(ev.target).closest(".question-piece").data("value", "true");
654 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
656 $(".false", qp).click(function(ev) {
659 $(ev.target).closest(".question-piece").data("value", "false");
660 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
665 PrawdaFalsz.prototype.check_question = function() {
666 var all, bad, good, qp, _i, _len, _ref;
670 _ref = $(".question-piece", this.element);
671 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
673 if ($(qp).data("solution").toString() === $(qp).data("value")) {
675 this.piece_correct(qp);
678 this.piece_incorrect(qp);
682 return [good, bad, all];
685 PrawdaFalsz.prototype.show_solutions = function() {
686 var qp, _i, _len, _ref, _results;
688 _ref = $(".question-piece", this.element);
690 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
692 if ($(qp).data('solution') === true) {
693 _results.push($(".true", qp).click());
695 _results.push($(".false", qp).click());
701 PrawdaFalsz.prototype.get_answer = function(question) {
705 $(".question-piece", this.element).each(function(i, qpiece) {
706 return answer.push($(qpiece).data('value') || '-');
715 exercise = function(ele) {
719 uporzadkuj: Uporzadkuj,
722 przyporzadkuj: Przyporzadkuj,
723 prawdafalsz: PrawdaFalsz
725 cls = es[$(ele).attr('data-type')];
730 'EduModule': EduModule
733 $(document).ready(function() {
734 new EduModule($("#book-text"));
735 return $(".exercise").each(function(i, el) {
736 return exercise(this);