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_value_list = function(elem, data_key, numbers) {
100 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
102 vl = vl.map(function(x) {
109 Exercise.prototype.get_value_optional_list = function(elem, data_key) {
110 var mandat, opt, v, vals, _i, _len;
111 vals = this.get_value_list(elem, data_key);
114 for (_i = 0, _len = vals.length; _i < _len; _i++) {
116 if (v.slice(-1) === "?") {
117 opt.push(v.slice(0, -1));
122 return [mandat, opt];
125 Exercise.prototype.show_score = function(score) {
127 $msg = $(".message", this.element);
128 $msg.text("Wynik: " + score[0] + " / " + score[2]);
129 if (score[0] >= score[2] && score[1] === 0) {
130 return $msg.addClass("maxscore");
132 return $msg.removeClass("maxscore");
136 Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
140 Exercise.prototype.draggable_accept = function($draggable, $droppable) {
141 var d, dropped, _i, _len;
142 dropped = $droppable.closest("ul, ol").find(".draggable");
143 for (_i = 0, _len = dropped.length; _i < _len; _i++) {
145 if (this.draggable_equal($draggable, $(d))) {
152 Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
155 $added = $draggable.clone();
156 $added.data("original", $draggable.get(0));
158 $draggable.addClass('disabled').draggable('disable');
160 $placeholder.after($added);
161 if (!$placeholder.hasClass('multiple')) {
164 if ($added.is(".add-li")) {
165 $added.wrap("<li/>");
167 $added.append('<span class="remove">x</span><div class="clr"></div>');
168 return $('.remove', $added).click(function(ev) {
171 $($added.data('original')).removeClass('disabled').draggable('enable');
173 if ($added.is(".add-li")) {
174 $added = $added.closest('li');
176 $added.prev(".placeholder:not(.multiple)").show();
177 return $added.remove();
181 Exercise.prototype.dragging = function(ismultiple, issortable) {
183 return $(".question", this.element).each(function(i, question) {
184 var draggable_opts, self;
190 $(".draggable", question).draggable(draggable_opts);
192 return $(".placeholder", question).droppable({
193 accept: function(draggable) {
194 var $draggable, is_accepted;
195 $draggable = $(draggable);
197 if (!$draggable.is(".draggable")) {
201 is_accepted = self.draggable_accept($draggable, $(this));
204 $(this).addClass('accepting');
206 $(this).removeClass('accepting');
210 drop: function(ev, ui) {
211 $(ev.target).removeClass('accepting dragover');
212 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
214 over: function(ev, ui) {
215 return $(ev.target).addClass('dragover');
217 out: function(ev, ui) {
218 return $(ev.target).removeClass('dragover');
228 Wybor = (function(_super) {
229 __extends(Wybor, _super);
231 function Wybor(element) {
232 Wybor.__super__.constructor.call(this, element);
233 $(".question-piece input", element).change(this.retry);
236 Wybor.prototype.check_question = function(question) {
237 var all, bad, good, solution,
242 solution = this.get_value_list(question, 'solution');
243 $(".question-piece", question).each(function(i, qpiece) {
244 var is_checked, piece_name, piece_no, should_be_checked;
245 piece_no = $(qpiece).attr('data-no');
246 piece_name = $(qpiece).attr('data-name');
248 should_be_checked = solution.indexOf(piece_name) >= 0;
250 should_be_checked = solution.indexOf(piece_no) >= 0;
252 is_checked = $("input", qpiece).is(":checked");
253 if (should_be_checked) {
257 if (should_be_checked) {
259 return _this.piece_correct(qpiece);
262 return _this.piece_incorrect(qpiece);
265 return $(qpiece).removeClass("correct,incorrect");
268 return [good, bad, all];
271 Wybor.prototype.solve_question = function(question) {
274 solution = this.get_value_list(question, 'solution');
275 return $(".question-piece", question).each(function(i, qpiece) {
276 var piece_name, piece_no, should_be_checked;
277 piece_no = $(qpiece).attr('data-no');
278 piece_name = $(qpiece).attr('data-name');
280 should_be_checked = solution.indexOf(piece_name) >= 0;
282 should_be_checked = solution.indexOf(piece_no) >= 0;
284 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
285 return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
293 Uporzadkuj = (function(_super) {
294 __extends(Uporzadkuj, _super);
296 function Uporzadkuj(element) {
297 Uporzadkuj.__super__.constructor.call(this, element);
298 $('ol, ul', this.element).sortable({
304 Uporzadkuj.prototype.check_question = function(question) {
305 var all, bad, correct, pkt, pkts, positions, sorted, _i, _ref;
306 positions = this.get_value_list(question, 'original', true);
307 sorted = positions.sort();
308 pkts = $('.question-piece', question);
312 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
314 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
316 this.piece_correct(pkts.eq(pkt));
319 this.piece_incorrect(pkts.eq(pkt));
322 return [correct, bad, all];
325 Uporzadkuj.prototype.solve_question = function(question) {
326 var p, parent, pkts, positions, sorted, _i, _len, _results;
327 positions = this.get_value_list(question, 'original', true);
328 sorted = positions.sort();
329 pkts = $('.question-piece', question);
330 pkts.sort(function(a, b) {
332 q = $(a).data('pos');
333 w = $(b).data('pos');
342 parent = pkts.eq(0).parent();
344 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
346 _results.push(parent.prepend(p));
355 Luki = (function(_super) {
356 __extends(Luki, _super);
358 function Luki(element) {
359 Luki.__super__.constructor.call(this, element);
360 this.dragging(false, false);
363 Luki.prototype.check = function() {
364 var all, bad, correct,
366 all = $(".placeholder", this.element).length;
369 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
371 $placeholder = $(qpiece).prev(".placeholder");
372 if ($placeholder.data('solution') === $(qpiece).data('no')) {
373 _this.piece_correct(qpiece);
377 return _this.piece_incorrect(qpiece);
380 return this.show_score([correct, bad, all]);
383 Luki.prototype.solve_question = function(question) {
385 return $(".placeholder", question).each(function(i, placeholder) {
387 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
388 return _this.draggable_move($qp, $(placeholder), false);
396 Zastap = (function(_super) {
397 __extends(Zastap, _super);
399 function Zastap(element) {
401 Zastap.__super__.constructor.call(this, element);
402 $(".paragraph", this.element).each(function(i, par) {
403 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
405 this.dragging(false, false);
408 Zastap.prototype.check = function() {
409 var all, bad, correct,
414 $(".paragraph", this.element).each(function(i, par) {
415 return $(".placeholder", par).each(function(j, qpiece) {
418 $dragged = $qp.next(".draggable");
419 if ($qp.data("solution")) {
420 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
421 _this.piece_correct($dragged);
428 return this.show_score([correct, bad, all]);
431 Zastap.prototype.show_solutions = function() {
434 return $(".paragraph", this.element).each(function(i, par) {
435 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
438 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
439 return _this.draggable_move($dr, $qp, false);
444 Zastap.prototype.wrap_words = function(element, wrapper) {
445 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
446 ignore = /^[ \t.,:;()]+/;
447 insertWrapped = function(txt, elem) {
449 nw = wrapper.clone();
450 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
453 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
454 chld = element.get(0).childNodes[j];
455 if (chld.nodeType === document.TEXT_NODE) {
456 len = chld.textContent.length;
460 space = ignore.exec(chld.textContent.substr(i));
463 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
465 $(document.createTextNode(space[0])).insertBefore(chld);
466 i += space[0].length;
472 if (wordb < len - 1) {
473 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
475 _results.push($(chld).remove());
477 _results.push(void 0);
487 Przyporzadkuj = (function(_super) {
488 __extends(Przyporzadkuj, _super);
490 Przyporzadkuj.prototype.is_multiple = function() {
491 var qp, _i, _len, _ref;
492 _ref = $(".question-piece", this.element);
493 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
495 if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
502 function Przyporzadkuj(element) {
503 Przyporzadkuj.__super__.constructor.call(this, element);
504 this.multiple = this.is_multiple();
505 this.dragging(this.multiple, true);
508 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
509 return d1.data("no") === d2.data("no");
512 Przyporzadkuj.prototype.check_question = function(question) {
513 var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
514 minimum = $(question).data("minimum");
520 $(".subject .question-piece", question).each(function(i, el) {
522 v = self.get_value_optional_list(el, 'solution');
524 return all += mandatory.length;
527 _ref = $(".predicate [data-predicate]", question);
528 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
530 pn = $(pred).attr('data-predicate');
531 _ref1 = $(".question-piece", pred);
532 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
534 v = this.get_value_optional_list(qp, 'solution');
537 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
539 this.piece_correct(qp);
542 this.piece_incorrect(qp);
546 return [count, bad_count, all];
549 Przyporzadkuj.prototype.solve_question = function(question) {
550 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
551 minimum = $(question).data("min");
552 _ref = $(".subject .question-piece", question);
554 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
556 v = this.get_value_optional_list(qp, 'solution');
560 draggables = mandatory.count(optional).slice(0, minimum);
562 draggables = mandatory;
564 _results.push((function() {
565 var _j, _len1, _results1;
567 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
569 $pr = $(".predicate [data-predicate=" + m + "]", question);
570 $ph = $pr.find(".placeholder:visible");
571 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
579 return Przyporzadkuj;
583 PrawdaFalsz = (function(_super) {
584 __extends(PrawdaFalsz, _super);
586 function PrawdaFalsz(element) {
587 var qp, _i, _len, _ref,
589 PrawdaFalsz.__super__.constructor.call(this, element);
590 _ref = $(".question-piece", this.element);
591 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
593 $(".true", qp).click(function(ev) {
596 $(ev.target).closest(".question-piece").data("value", "true");
597 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
599 $(".false", qp).click(function(ev) {
602 $(ev.target).closest(".question-piece").data("value", "false");
603 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
608 PrawdaFalsz.prototype.check_question = function() {
609 var all, bad, good, qp, _i, _len, _ref;
613 _ref = $(".question-piece", this.element);
614 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
616 if ($(qp).data("solution").toString() === $(qp).data("value")) {
618 this.piece_correct(qp);
621 this.piece_incorrect(qp);
625 return [good, bad, all];
628 PrawdaFalsz.prototype.show_solutions = function() {
629 var qp, _i, _len, _ref, _results;
631 _ref = $(".question-piece", this.element);
633 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
635 if ($(qp).data('solution') === true) {
636 _results.push($(".true", qp).click());
638 _results.push($(".false", qp).click());
648 exercise = function(ele) {
652 uporzadkuj: Uporzadkuj,
655 przyporzadkuj: Przyporzadkuj,
656 prawdafalsz: PrawdaFalsz
658 cls = es[$(ele).attr('data-type')];
663 'EduModule': EduModule
666 $(document).ready(function() {
667 new EduModule($("#book-text"));
668 return $(".exercise").each(function(i, el) {
669 return exercise(this);