1 // Generated by CoffeeScript 1.4.0
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(function(a, b) {
315 pkts = $('.question-piece', question);
319 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
321 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
323 this.piece_correct(pkts.eq(pkt));
326 this.piece_incorrect(pkts.eq(pkt));
329 return [correct, bad, all];
332 Uporzadkuj.prototype.solve_question = function(question) {
333 var p, parent, pkts, _i, _len, _results;
334 pkts = $('.question-piece', question);
335 pkts.sort(function(a, b) {
336 return $(a).data('pos') - $(b).data('pos');
338 parent = pkts.eq(0).parent();
340 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
342 _results.push(parent.append(p));
351 Luki = (function(_super) {
353 __extends(Luki, _super);
355 function Luki(element) {
356 Luki.__super__.constructor.call(this, element);
357 this.dragging(false, false);
360 Luki.prototype.check = function() {
361 var all, bad, correct,
363 all = $(".placeholder", this.element).length;
366 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
368 $placeholder = $(qpiece).prev(".placeholder");
369 if ($placeholder.data('solution') === $(qpiece).data('no')) {
370 _this.piece_correct(qpiece);
374 return _this.piece_incorrect(qpiece);
377 return this.show_score([correct, bad, all]);
380 Luki.prototype.solve_question = function(question) {
382 return $(".placeholder", question).each(function(i, placeholder) {
384 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
385 return _this.draggable_move($qp, $(placeholder), false);
393 Zastap = (function(_super) {
395 __extends(Zastap, _super);
397 function Zastap(element) {
399 Zastap.__super__.constructor.call(this, element);
400 $(".paragraph", this.element).each(function(i, par) {
401 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
403 this.dragging(false, false);
406 Zastap.prototype.check = function() {
407 var all, bad, correct,
412 $(".paragraph", this.element).each(function(i, par) {
413 return $(".placeholder", par).each(function(j, qpiece) {
416 $dragged = $qp.next(".draggable");
417 if ($qp.data("solution")) {
418 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
419 _this.piece_correct($dragged);
426 return this.show_score([correct, bad, all]);
429 Zastap.prototype.show_solutions = function() {
432 return $(".paragraph", this.element).each(function(i, par) {
433 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
436 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
437 return _this.draggable_move($dr, $qp, false);
442 Zastap.prototype.wrap_words = function(element, wrapper) {
443 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
444 ignore = /^[ \t.,:;()]+/;
445 insertWrapped = function(txt, elem) {
447 nw = wrapper.clone();
448 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
451 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
452 chld = element.get(0).childNodes[j];
453 if (chld.nodeType === document.TEXT_NODE) {
454 len = chld.textContent.length;
458 space = ignore.exec(chld.textContent.substr(i));
461 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
463 $(document.createTextNode(space[0])).insertBefore(chld);
464 i += space[0].length;
470 if (wordb < len - 1) {
471 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
473 _results.push($(chld).remove());
475 _results.push(void 0);
485 Przyporzadkuj = (function(_super) {
487 __extends(Przyporzadkuj, _super);
489 Przyporzadkuj.prototype.is_multiple = function() {
490 var qp, _i, _len, _ref;
491 _ref = $(".question-piece", this.element);
492 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
494 if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
501 function Przyporzadkuj(element) {
502 Przyporzadkuj.__super__.constructor.call(this, element);
503 this.multiple = this.is_multiple();
504 this.dragging(this.multiple, true);
507 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
508 return d1.data("no") === d2.data("no");
511 Przyporzadkuj.prototype.check_question = function(question) {
512 var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
513 minimum = $(question).data("minimum");
519 $(".subject .question-piece", question).each(function(i, el) {
521 v = self.get_value_optional_list(el, 'solution');
523 return all += mandatory.length;
526 _ref = $(".predicate [data-predicate]", question);
527 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
529 pn = $(pred).attr('data-predicate');
530 _ref1 = $(".question-piece", pred);
531 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
533 v = this.get_value_optional_list(qp, 'solution');
536 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
538 this.piece_correct(qp);
541 this.piece_incorrect(qp);
545 return [count, bad_count, all];
548 Przyporzadkuj.prototype.solve_question = function(question) {
549 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
550 minimum = $(question).data("min");
551 _ref = $(".subject .question-piece", question);
553 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
555 v = this.get_value_optional_list(qp, 'solution');
559 draggables = mandatory.count(optional).slice(0, minimum);
561 draggables = mandatory;
563 _results.push((function() {
564 var _j, _len1, _results1;
566 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
568 $pr = $(".predicate [data-predicate=" + m + "]", question);
569 $ph = $pr.find(".placeholder:visible");
570 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
578 return Przyporzadkuj;
582 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);