1 // Generated by CoffeeScript 1.3.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() {
11 function Binding(handler, element) {
12 this.handler = handler;
13 this.element = element;
14 $(this.element).data(this.handler, this);
21 EduModule = (function(_super) {
23 __extends(EduModule, _super);
25 function EduModule(element) {
26 EduModule.__super__.constructor.call(this, 'edumodule', element);
33 Exercise = (function(_super) {
35 __extends(Exercise, _super);
37 function Exercise(element) {
39 Exercise.__super__.constructor.call(this, 'exercise', element);
40 $(this.element).data("exercise-html", $(this.element).html());
41 $(".check", this.element).click(function(ev) {
43 $(".retry", _this.element).show();
44 return $(".check", _this.element).hide();
46 $(".retry", this.element).click(function(ev) {
49 $('.solutions', this.element).click(function() {
50 _this.show_solutions();
51 return $(".comment", _this.element).show();
53 $('.reset', this.element).click(function() {
58 Exercise.prototype.retry = function() {
59 $(".correct, .incorrect", this.element).removeClass("correct incorrect");
60 $(".check", this.element).show();
61 return $(".retry", this.element).hide();
64 Exercise.prototype.reset = function() {
65 $(this.element).html($(this.element).data('exercise-html'));
66 return exercise(this.element);
69 Exercise.prototype.piece_correct = function(qpiece) {
70 return $(qpiece).removeClass('incorrect').addClass('correct');
73 Exercise.prototype.piece_incorrect = function(qpiece) {
74 return $(qpiece).removeClass('correct').addClass('incorrect');
77 Exercise.prototype.check = function() {
81 $(".question", this.element).each(function(i, question) {
82 return scores.push(_this.check_question(question));
85 $.each(scores, function(i, s) {
88 return score[2] += s[2];
90 return this.show_score(score);
93 Exercise.prototype.show_solutions = function() {
96 return $(".question", this.element).each(function(i, question) {
97 return _this.solve_question(question);
101 Exercise.prototype.get_value_list = function(elem, data_key, numbers) {
103 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
105 vl = vl.map(function(x) {
112 Exercise.prototype.get_value_optional_list = function(elem, data_key) {
113 var mandat, opt, v, vals, _i, _len;
114 vals = this.get_value_list(elem, data_key);
117 for (_i = 0, _len = vals.length; _i < _len; _i++) {
119 if (v.slice(-1) === "?") {
120 opt.push(v.slice(0, -1));
125 return [mandat, opt];
128 Exercise.prototype.show_score = function(score) {
130 $msg = $(".message", this.element);
131 $msg.text("Wynik: " + score[0] + " / " + score[2]);
132 if (score[0] >= score[2] && score[1] === 0) {
133 return $msg.addClass("maxscore");
135 return $msg.removeClass("maxscore");
139 Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
143 Exercise.prototype.draggable_accept = function($draggable, $droppable) {
144 var d, dropped, _i, _len;
145 dropped = $droppable.closest("ul, ol").find(".draggable");
146 for (_i = 0, _len = dropped.length; _i < _len; _i++) {
148 if (this.draggable_equal($draggable, $(d))) {
155 Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
158 $added = $draggable.clone();
159 $added.data("original", $draggable.get(0));
161 $draggable.addClass('disabled').draggable('disable');
163 $placeholder.after($added);
164 if (!$placeholder.hasClass('multiple')) {
167 if ($added.is(".add-li")) {
168 $added.wrap("<li/>");
170 $added.append('<span class="remove">x</span><div class="clr"></div>');
171 return $('.remove', $added).click(function(ev) {
174 $($added.data('original')).removeClass('disabled').draggable('enable');
176 if ($added.is(".add-li")) {
177 $added = $added.closest('li');
179 $added.prev(".placeholder:not(.multiple)").show();
180 return $added.remove();
184 Exercise.prototype.dragging = function(ismultiple, issortable) {
186 return $(".question", this.element).each(function(i, question) {
187 var draggable_opts, self;
193 $(".draggable", question).draggable(draggable_opts);
195 return $(".placeholder", question).droppable({
196 accept: function(draggable) {
197 var $draggable, is_accepted;
198 $draggable = $(draggable);
200 if (!$draggable.is(".draggable")) {
204 is_accepted = self.draggable_accept($draggable, $(this));
207 $(this).addClass('accepting');
209 $(this).removeClass('accepting');
213 drop: function(ev, ui) {
214 $(ev.target).removeClass('accepting dragover');
215 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
217 over: function(ev, ui) {
218 return $(ev.target).addClass('dragover');
220 out: function(ev, ui) {
221 return $(ev.target).removeClass('dragover');
231 Wybor = (function(_super) {
233 __extends(Wybor, _super);
235 function Wybor(element) {
236 Wybor.__super__.constructor.call(this, element);
237 $(".question-piece input", element).change(this.retry);
240 Wybor.prototype.check_question = function(question) {
241 var all, bad, good, solution,
246 solution = this.get_value_list(question, 'solution');
247 $(".question-piece", question).each(function(i, qpiece) {
248 var is_checked, piece_name, piece_no, should_be_checked;
249 piece_no = $(qpiece).attr('data-no');
250 piece_name = $(qpiece).attr('data-name');
252 should_be_checked = solution.indexOf(piece_name) >= 0;
254 should_be_checked = solution.indexOf(piece_no) >= 0;
256 is_checked = $("input", qpiece).is(":checked");
257 if (should_be_checked) {
261 if (should_be_checked) {
263 return _this.piece_correct(qpiece);
266 return _this.piece_incorrect(qpiece);
269 return $(qpiece).removeClass("correct,incorrect");
272 return [good, bad, all];
275 Wybor.prototype.solve_question = function(question) {
278 solution = this.get_value_list(question, 'solution');
279 return $(".question-piece", question).each(function(i, qpiece) {
280 var piece_name, piece_no, should_be_checked;
281 piece_no = $(qpiece).attr('data-no');
282 piece_name = $(qpiece).attr('data-name');
284 should_be_checked = solution.indexOf(piece_name) >= 0;
286 should_be_checked = solution.indexOf(piece_no) >= 0;
288 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
289 return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
297 Uporzadkuj = (function(_super) {
299 __extends(Uporzadkuj, _super);
301 function Uporzadkuj(element) {
302 Uporzadkuj.__super__.constructor.call(this, element);
303 $('ol, ul', this.element).sortable({
309 Uporzadkuj.prototype.check_question = function(question) {
310 var all, bad, correct, pkt, pkts, positions, sorted, _i, _ref;
311 positions = this.get_value_list(question, 'original', true);
312 sorted = positions.sort();
313 pkts = $('.question-piece', question);
317 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
319 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
321 this.piece_correct(pkts.eq(pkt));
324 this.piece_incorrect(pkts.eq(pkt));
327 return [correct, bad, all];
330 Uporzadkuj.prototype.solve_question = function(question) {
331 var p, parent, pkts, positions, sorted, _i, _len, _results;
332 positions = this.get_value_list(question, 'original', true);
333 sorted = positions.sort();
334 pkts = $('.question-piece', question);
335 pkts.sort(function(a, b) {
337 q = $(a).data('pos');
338 w = $(b).data('pos');
347 parent = pkts.eq(0).parent();
349 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
351 _results.push(parent.prepend(p));
360 Luki = (function(_super) {
362 __extends(Luki, _super);
364 function Luki(element) {
365 Luki.__super__.constructor.call(this, element);
366 this.dragging(false, false);
369 Luki.prototype.check = function() {
370 var all, bad, correct,
372 all = $(".placeholder", this.element).length;
375 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
377 $placeholder = $(qpiece).prev(".placeholder");
378 if ($placeholder.data('solution') === $(qpiece).data('no')) {
379 _this.piece_correct(qpiece);
383 return _this.piece_incorrect(qpiece);
386 return this.show_score([correct, bad, all]);
389 Luki.prototype.solve_question = function(question) {
391 return $(".placeholder", question).each(function(i, placeholder) {
393 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
394 return _this.draggable_move($qp, $(placeholder), false);
402 Zastap = (function(_super) {
404 __extends(Zastap, _super);
406 function Zastap(element) {
408 Zastap.__super__.constructor.call(this, element);
409 $(".paragraph", this.element).each(function(i, par) {
410 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
412 this.dragging(false, false);
415 Zastap.prototype.check = function() {
416 var all, bad, correct,
421 $(".paragraph", this.element).each(function(i, par) {
422 return $(".placeholder", par).each(function(j, qpiece) {
425 $dragged = $qp.next(".draggable");
426 if ($qp.data("solution")) {
427 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
428 _this.piece_correct($dragged);
435 return this.show_score([correct, bad, all]);
438 Zastap.prototype.show_solutions = function() {
441 return $(".paragraph", this.element).each(function(i, par) {
442 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
445 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
446 return _this.draggable_move($dr, $qp, false);
451 Zastap.prototype.wrap_words = function(element, wrapper) {
452 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
453 ignore = /^[ \t.,:;()]+/;
454 insertWrapped = function(txt, elem) {
456 nw = wrapper.clone();
457 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
460 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
461 chld = element.get(0).childNodes[j];
462 if (chld.nodeType === document.TEXT_NODE) {
463 len = chld.textContent.length;
467 space = ignore.exec(chld.textContent.substr(i));
470 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
472 $(document.createTextNode(space[0])).insertBefore(chld);
473 i += space[0].length;
479 if (wordb < len - 1) {
480 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
482 _results.push($(chld).remove());
484 _results.push(void 0);
494 Przyporzadkuj = (function(_super) {
496 __extends(Przyporzadkuj, _super);
498 Przyporzadkuj.prototype.is_multiple = function() {
499 var qp, _i, _len, _ref;
500 _ref = $(".question-piece", this.element);
501 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
503 if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
510 function Przyporzadkuj(element) {
511 Przyporzadkuj.__super__.constructor.call(this, element);
512 this.multiple = this.is_multiple();
513 this.dragging(this.multiple, true);
516 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
517 return d1.data("no") === d2.data("no");
520 Przyporzadkuj.prototype.check_question = function(question) {
521 var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
522 minimum = $(question).data("minimum");
528 $(".subject .question-piece", question).each(function(i, el) {
530 v = self.get_value_optional_list(el, 'solution');
532 return all += mandatory.length;
535 _ref = $(".predicate [data-predicate]", question);
536 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
538 pn = $(pred).attr('data-predicate');
539 _ref1 = $(".question-piece", pred);
540 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
542 v = this.get_value_optional_list(qp, 'solution');
545 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
547 this.piece_correct(qp);
550 this.piece_incorrect(qp);
554 return [count, bad_count, all];
557 Przyporzadkuj.prototype.solve_question = function(question) {
558 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
559 minimum = $(question).data("min");
560 _ref = $(".subject .question-piece", question);
562 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
564 v = this.get_value_optional_list(qp, 'solution');
568 draggables = mandatory.count(optional).slice(0, minimum);
570 draggables = mandatory;
572 _results.push((function() {
573 var _j, _len1, _results1;
575 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
577 $pr = $(".predicate [data-predicate=" + m + "]", question);
578 $ph = $pr.find(".placeholder:visible");
579 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
587 return Przyporzadkuj;
591 PrawdaFalsz = (function(_super) {
593 __extends(PrawdaFalsz, _super);
595 function PrawdaFalsz(element) {
596 var qp, _i, _len, _ref,
598 PrawdaFalsz.__super__.constructor.call(this, element);
599 _ref = $(".question-piece", this.element);
600 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
602 $(".true", qp).click(function(ev) {
605 $(ev.target).closest(".question-piece").data("value", "true");
606 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
608 $(".false", qp).click(function(ev) {
611 $(ev.target).closest(".question-piece").data("value", "false");
612 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
617 PrawdaFalsz.prototype.check_question = function() {
618 var all, bad, good, qp, _i, _len, _ref;
622 _ref = $(".question-piece", this.element);
623 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
625 if ($(qp).data("solution").toString() === $(qp).data("value")) {
627 this.piece_correct(qp);
630 this.piece_incorrect(qp);
634 return [good, bad, all];
637 PrawdaFalsz.prototype.show_solutions = function() {
638 var qp, _i, _len, _ref, _results;
640 _ref = $(".question-piece", this.element);
642 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
644 if ($(qp).data('solution') === true) {
645 _results.push($(".true", qp).click());
647 _results.push($(".false", qp).click());
657 exercise = function(ele) {
661 uporzadkuj: Uporzadkuj,
664 przyporzadkuj: Przyporzadkuj,
665 prawdafalsz: PrawdaFalsz
667 cls = es[$(ele).attr('data-type')];
672 'EduModule': EduModule
675 $(document).ready(function() {
676 new EduModule($("#book-text"));
677 return $(".exercise").each(function(i, el) {
678 return exercise(this);