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));
 
 379   Luki = (function(_super) {
 
 380     __extends(Luki, _super);
 
 382     function Luki(element) {
 
 383       Luki.__super__.constructor.call(this, element);
 
 384       this.dragging(false, false);
 
 387     Luki.prototype.check = function() {
 
 388       var all, bad, correct,
 
 390       all = $(".placeholder", this.element).length;
 
 393       $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
 
 395         $placeholder = $(qpiece).prev(".placeholder");
 
 396         if ($placeholder.data('solution') === $(qpiece).data('no')) {
 
 397           _this.piece_correct(qpiece);
 
 401           return _this.piece_incorrect(qpiece);
 
 404       return this.show_score([correct, bad, all]);
 
 407     Luki.prototype.solve_question = function(question) {
 
 409       return $(".placeholder", question).each(function(i, placeholder) {
 
 411         $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
 
 412         return _this.draggable_move($qp, $(placeholder), false);
 
 420   Zastap = (function(_super) {
 
 421     __extends(Zastap, _super);
 
 423     function Zastap(element) {
 
 425       Zastap.__super__.constructor.call(this, element);
 
 426       $(".paragraph", this.element).each(function(i, par) {
 
 427         return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
 
 429       this.dragging(false, false);
 
 432     Zastap.prototype.check = function() {
 
 433       var all, bad, correct,
 
 438       $(".paragraph", this.element).each(function(i, par) {
 
 439         return $(".placeholder", par).each(function(j, qpiece) {
 
 442           $dragged = $qp.next(".draggable");
 
 443           if ($qp.data("solution")) {
 
 444             if ($dragged && $qp.data("solution") === $dragged.data("no")) {
 
 445               _this.piece_correct($dragged);
 
 452       return this.show_score([correct, bad, all]);
 
 455     Zastap.prototype.show_solutions = function() {
 
 458       return $(".paragraph", this.element).each(function(i, par) {
 
 459         return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
 
 462           $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
 
 463           return _this.draggable_move($dr, $qp, false);
 
 468     Zastap.prototype.wrap_words = function(element, wrapper) {
 
 469       var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
 
 470       ignore = /^[ \t.,:;()]+/;
 
 471       insertWrapped = function(txt, elem) {
 
 473         nw = wrapper.clone();
 
 474         return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
 
 477       for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
 
 478         chld = element.get(0).childNodes[j];
 
 479         if (chld.nodeType === document.TEXT_NODE) {
 
 480           len = chld.textContent.length;
 
 484             space = ignore.exec(chld.textContent.substr(i));
 
 487                 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
 
 489               $(document.createTextNode(space[0])).insertBefore(chld);
 
 490               i += space[0].length;
 
 496           if (wordb < len - 1) {
 
 497             insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
 
 499           _results.push($(chld).remove());
 
 501           _results.push(void 0);
 
 511   Przyporzadkuj = (function(_super) {
 
 512     __extends(Przyporzadkuj, _super);
 
 514     Przyporzadkuj.prototype.is_multiple = function() {
 
 515       var qp, _i, _len, _ref;
 
 516       _ref = $(".question-piece", this.element);
 
 517       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 519         if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
 
 526     function Przyporzadkuj(element) {
 
 527       Przyporzadkuj.__super__.constructor.call(this, element);
 
 528       this.multiple = this.is_multiple();
 
 529       this.dragging(this.multiple, true);
 
 532     Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
 
 533       return d1.data("no") === d2.data("no");
 
 536     Przyporzadkuj.prototype.check_question = function(question) {
 
 537       var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
 
 538       minimum = $(question).data("minimum");
 
 544         $(".subject .question-piece", question).each(function(i, el) {
 
 546           v = self.get_value_optional_list(el, 'solution');
 
 548           return all += mandatory.length;
 
 551       _ref = $(".predicate [data-predicate]", question);
 
 552       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 554         pn = $(pred).attr('data-predicate');
 
 555         _ref1 = $(".question-piece", pred);
 
 556         for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
 
 558           v = this.get_value_optional_list(qp, 'solution');
 
 561           if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
 
 563             this.piece_correct(qp);
 
 566             this.piece_incorrect(qp);
 
 570       return [count, bad_count, all];
 
 573     Przyporzadkuj.prototype.solve_question = function(question) {
 
 574       var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
 
 575       minimum = $(question).data("min");
 
 576       _ref = $(".subject .question-piece", question);
 
 578       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 580         v = this.get_value_optional_list(qp, 'solution');
 
 584           draggables = mandatory.count(optional).slice(0, minimum);
 
 586           draggables = mandatory;
 
 588         _results.push((function() {
 
 589           var _j, _len1, _results1;
 
 591           for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
 
 593             $pr = $(".predicate [data-predicate=" + m + "]", question);
 
 594             $ph = $pr.find(".placeholder:visible");
 
 595             _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
 
 603     Przyporzadkuj.prototype.get_answer = function(question) {
 
 607       $(".predicate [data-predicate]", question).each(function(i, subjects) {
 
 609         predicate = $(subjects).attr('data-predicate');
 
 610         answer[predicate] = [];
 
 611         return $('.question-piece', subjects).each(function(i, qpiece) {
 
 614           return answer[predicate].push($qpiece.attr('data-id'));
 
 620     return Przyporzadkuj;
 
 624   PrawdaFalsz = (function(_super) {
 
 625     __extends(PrawdaFalsz, _super);
 
 627     function PrawdaFalsz(element) {
 
 628       var qp, _i, _len, _ref,
 
 630       PrawdaFalsz.__super__.constructor.call(this, element);
 
 631       _ref = $(".question-piece", this.element);
 
 632       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 634         $(".true", qp).click(function(ev) {
 
 637           $(ev.target).closest(".question-piece").data("value", "true");
 
 638           return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
 
 640         $(".false", qp).click(function(ev) {
 
 643           $(ev.target).closest(".question-piece").data("value", "false");
 
 644           return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
 
 649     PrawdaFalsz.prototype.check_question = function() {
 
 650       var all, bad, good, qp, _i, _len, _ref;
 
 654       _ref = $(".question-piece", this.element);
 
 655       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 657         if ($(qp).data("solution").toString() === $(qp).data("value")) {
 
 659           this.piece_correct(qp);
 
 662           this.piece_incorrect(qp);
 
 666       return [good, bad, all];
 
 669     PrawdaFalsz.prototype.show_solutions = function() {
 
 670       var qp, _i, _len, _ref, _results;
 
 672       _ref = $(".question-piece", this.element);
 
 674       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 676         if ($(qp).data('solution') === true) {
 
 677           _results.push($(".true", qp).click());
 
 679           _results.push($(".false", qp).click());
 
 685     PrawdaFalsz.prototype.get_answer = function(question) {
 
 689       $(".question-piece", this.element).each(function(i, qpiece) {
 
 690         return answer.push($(qpiece).data('value') || '-');
 
 699   exercise = function(ele) {
 
 703       uporzadkuj: Uporzadkuj,
 
 706       przyporzadkuj: Przyporzadkuj,
 
 707       prawdafalsz: PrawdaFalsz
 
 709     cls = es[$(ele).attr('data-type')];
 
 714     'EduModule': EduModule
 
 717   $(document).ready(function() {
 
 718     new EduModule($("#book-text"));
 
 719     return $(".exercise").each(function(i, el) {
 
 720       return exercise(this);