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 $(ev.target).next(".retry").show();
44 return $(ev.target).hide();
46 $(".retry", this.element).click(function(ev) {
47 $(".correct, .incorrect", _this.element).removeClass("correct incorrect");
48 $(ev.target).prev(".check").show();
49 return $(ev.target).hide();
51 $('.solutions', this.element).click(function() {
52 _this.show_solutions();
53 return $(".comment", _this.element).show();
55 $('.reset', this.element).click(function() {
60 Exercise.prototype.reset = function() {
61 $(this.element).html($(this.element).data('exercise-html'));
62 return exercise(this.element);
65 Exercise.prototype.piece_correct = function(qpiece) {
66 return $(qpiece).removeClass('incorrect').addClass('correct');
69 Exercise.prototype.piece_incorrect = function(qpiece) {
70 return $(qpiece).removeClass('correct').addClass('incorrect');
73 Exercise.prototype.check = function() {
77 $(".question", this.element).each(function(i, question) {
78 return scores.push(_this.check_question(question));
81 $.each(scores, function(i, s) {
83 return score[1] += s[1];
85 return this.show_score(score);
88 Exercise.prototype.show_solutions = function() {
91 return $(".question", this.element).each(function(i, question) {
92 return _this.solve_question(question);
96 Exercise.prototype.get_value_list = function(elem, data_key, numbers) {
98 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
100 vl = vl.map(function(x) {
107 Exercise.prototype.get_value_optional_list = function(elem, data_key) {
108 var mandat, opt, v, vals, _i, _len;
109 vals = this.get_value_list(elem, data_key);
112 for (_i = 0, _len = vals.length; _i < _len; _i++) {
114 if (v.slice(-1) === "?") {
115 opt.push(v.slice(0, -1));
120 return [mandat, opt];
123 Exercise.prototype.show_score = function(score) {
124 return $(".message", this.element).text("Wynik: " + score[0] + " / " + score[1]);
127 Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
131 Exercise.prototype.draggable_accept = function($draggable, $droppable) {
132 var d, dropped, _i, _len;
133 dropped = $droppable.closest("ul, ol").find(".draggable");
134 for (_i = 0, _len = dropped.length; _i < _len; _i++) {
136 if (this.draggable_equal($draggable, $(d))) {
143 Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
146 $added = $draggable.clone();
147 $added.data("original", $draggable.get(0));
149 $draggable.addClass('disabled').draggable('disable');
151 $placeholder.after($added);
152 if (!$placeholder.hasClass('multiple')) {
155 if ($added.is(".add-li")) {
156 $added.wrap("<li/>");
158 $added.append('<span class="remove">x</span>');
159 return $('.remove', $added).click(function(ev) {
161 $($added.data('original')).removeClass('disabled').draggable('enable');
163 if ($added.is(".add-li")) {
164 $added = $added.closest('li');
166 $added.prev(".placeholder:not(.multiple)").show();
167 return $added.remove();
171 Exercise.prototype.dragging = function(ismultiple, issortable) {
173 return $(".question", this.element).each(function(i, question) {
174 var draggable_opts, self;
179 $(".draggable", question).draggable(draggable_opts);
181 return $(".placeholder", question).droppable({
182 accept: function(draggable) {
183 var $draggable, is_accepted;
184 $draggable = $(draggable);
186 if (!$draggable.is(".draggable")) {
190 is_accepted = self.draggable_accept($draggable, $(this));
193 $(this).addClass('accepting');
195 $(this).removeClass('accepting');
199 drop: function(ev, ui) {
200 $(ev.target).removeClass('accepting dragover');
201 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
203 over: function(ev, ui) {
204 return $(ev.target).addClass('dragover');
206 out: function(ev, ui) {
207 return $(ev.target).removeClass('dragover');
217 Wybor = (function(_super) {
219 __extends(Wybor, _super);
221 function Wybor(element) {
222 Wybor.__super__.constructor.call(this, element);
225 Wybor.prototype.check_question = function(question) {
226 var all, good, solution,
230 solution = this.get_value_list(question, 'solution');
231 $(".question-piece", question).each(function(i, qpiece) {
232 var is_checked, piece_name, piece_no, should_be_checked;
233 piece_no = $(qpiece).attr('data-no');
234 piece_name = $(qpiece).attr('data-name');
236 should_be_checked = solution.indexOf(piece_name) >= 0;
238 should_be_checked = solution.indexOf(piece_no) >= 0;
240 is_checked = $("input", qpiece).is(":checked");
241 if (should_be_checked) {
245 if (should_be_checked) {
247 return _this.piece_correct(qpiece);
249 return _this.piece_incorrect(qpiece);
252 return $(qpiece).removeClass("correct,incorrect");
258 Wybor.prototype.solve_question = function(question) {
261 solution = this.get_value_list(question, 'solution');
262 return $(".question-piece", question).each(function(i, qpiece) {
263 var piece_name, piece_no, should_be_checked;
264 piece_no = $(qpiece).attr('data-no');
265 piece_name = $(qpiece).attr('data-name');
267 should_be_checked = solution.indexOf(piece_name) >= 0;
269 should_be_checked = solution.indexOf(piece_no) >= 0;
271 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
272 return $("input[type=checkbox]", qpiece).prop('checked', should_be_checked);
280 Uporzadkuj = (function(_super) {
282 __extends(Uporzadkuj, _super);
284 function Uporzadkuj(element) {
285 Uporzadkuj.__super__.constructor.call(this, element);
286 $('ol, ul', this.element).sortable({
291 Uporzadkuj.prototype.check_question = function(question) {
292 var all, correct, pkt, pkts, positions, sorted, _i, _ref;
293 positions = this.get_value_list(question, 'original', true);
294 sorted = positions.sort();
295 pkts = $('.question-piece', question);
298 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
300 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
302 this.piece_correct(pkts.eq(pkt));
304 this.piece_incorrect(pkts.eq(pkt));
307 return [correct, all];
310 Uporzadkuj.prototype.solve_question = function(question) {
311 var p, parent, pkts, positions, sorted, _i, _len, _results;
312 positions = this.get_value_list(question, 'original', true);
313 sorted = positions.sort();
314 pkts = $('.question-piece', question);
315 pkts.sort(function(a, b) {
317 q = $(a).data('pos');
318 w = $(b).data('pos');
327 parent = pkts.eq(0).parent();
329 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
331 _results.push(parent.prepend(p));
340 Luki = (function(_super) {
342 __extends(Luki, _super);
344 function Luki(element) {
345 Luki.__super__.constructor.call(this, element);
346 this.dragging(false, false);
349 Luki.prototype.check = function() {
354 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
356 $placeholder = $(qpiece).prev(".placeholder");
357 if ($placeholder.data('solution') === $(qpiece).data('no')) {
358 _this.piece_correct(qpiece);
361 _this.piece_incorrect(qpiece);
365 return this.show_score([correct, all]);
368 Luki.prototype.solve_question = function(question) {
370 return $(".placeholder", question).each(function(i, placeholder) {
372 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
373 return _this.draggable_move($qp, $(placeholder), false);
381 Zastap = (function(_super) {
383 __extends(Zastap, _super);
385 function Zastap(element) {
387 Zastap.__super__.constructor.call(this, element);
388 $(".paragraph", this.element).each(function(i, par) {
389 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
391 this.dragging(false, false);
394 Zastap.prototype.check = function() {
399 $(".paragraph", this.element).each(function(i, par) {
400 return $(".placeholder", par).each(function(j, qpiece) {
403 $dragged = $qp.next(".draggable");
404 if ($qp.data("solution")) {
405 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
406 _this.piece_correct($dragged);
413 return this.show_score([correct, all]);
416 Zastap.prototype.show_solutions = function() {
419 return $(".paragraph", this.element).each(function(i, par) {
420 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
423 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
424 return _this.draggable_move($dr, $qp, false);
429 Zastap.prototype.wrap_words = function(element, wrapper) {
430 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
431 ignore = /^[ \t.,:;()]+/;
432 insertWrapped = function(txt, elem) {
434 nw = wrapper.clone();
435 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
438 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
439 chld = element.get(0).childNodes[j];
440 if (chld.nodeType === document.TEXT_NODE) {
441 len = chld.textContent.length;
445 space = ignore.exec(chld.textContent.substr(i));
448 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
450 $(document.createTextNode(space[0])).insertBefore(chld);
451 i += space[0].length;
457 if (wordb < len - 1) {
458 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
460 _results.push($(chld).remove());
462 _results.push(void 0);
472 Przyporzadkuj = (function(_super) {
474 __extends(Przyporzadkuj, _super);
476 Przyporzadkuj.prototype.is_multiple = function() {
477 var qp, _i, _len, _ref;
478 _ref = $(".question-piece", this.element);
479 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
481 if ($(qp).data('solution').split(/[ ,]+/).length > 1) {
488 function Przyporzadkuj(element) {
489 Przyporzadkuj.__super__.constructor.call(this, element);
490 this.multiple = this.is_multiple();
491 this.dragging(this.multiple, true);
494 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
495 return d1.data("no") === d2.data("no");
498 Przyporzadkuj.prototype.check_question = function(question) {
499 var all, count, mandatory, minimum, optional, pn, pred, qp, v, _i, _j, _len, _len1, _ref, _ref1;
500 minimum = $(question).data("minimum");
504 all = $(".subjects .question-piece", question).length;
506 _ref = $(".predicate [data-predicate]", question);
507 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
509 pn = $(pred).attr('data-predicate');
510 if (minimum != null) {
513 _ref1 = $(".question-piece", pred);
514 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
516 v = this.get_value_optional_list(qp, 'solution');
519 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
521 this.piece_correct(qp);
523 this.piece_incorrect(qp);
530 Przyporzadkuj.prototype.solve_question = function(question) {
531 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
532 minimum = $(question).data("min");
533 _ref = $(".subject .question-piece", question);
535 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
537 v = this.get_value_optional_list(qp, 'solution');
541 draggables = mandatory.count(optional).slice(0, minimum);
543 draggables = mandatory;
545 _results.push((function() {
546 var _j, _len1, _results1;
548 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
550 $pr = $(".predicate [data-predicate=" + m + "]", question);
551 $ph = $pr.find(".placeholder:visible");
552 _results1.push(this.draggable_move($(qp), $ph, this.multiple));
560 return Przyporzadkuj;
564 PrawdaFalsz = (function(_super) {
566 __extends(PrawdaFalsz, _super);
568 function PrawdaFalsz(element) {
569 var qp, _i, _len, _ref;
570 PrawdaFalsz.__super__.constructor.call(this, element);
571 _ref = $(".question-piece", this.element);
572 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
574 $(".true", qp).click(function(ev) {
576 $(this).closest(".question-piece").data("value", "true");
577 return $(this).addClass('chosen').siblings('a').removeClass('chosen');
579 $(".false", qp).click(function(ev) {
581 $(this).closest(".question-piece").data("value", "false");
582 return $(this).addClass('chosen').siblings('a').removeClass('chosen');
587 PrawdaFalsz.prototype.check_question = function() {
588 var all, good, qp, _i, _len, _ref;
591 _ref = $(".question-piece", this.element);
592 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
594 if ($(qp).data("solution").toString() === $(qp).data("value")) {
596 this.piece_correct(qp);
598 this.piece_incorrect(qp);
605 PrawdaFalsz.prototype.show_solutions = function() {
606 var qp, _i, _len, _ref, _results;
608 _ref = $(".question-piece", this.element);
610 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
612 if ($(qp).data('solution') === true) {
613 _results.push($(".true", qp).click());
615 _results.push($(".false", qp).click());
625 exercise = function(ele) {
629 uporzadkuj: Uporzadkuj,
632 przyporzadkuj: Przyporzadkuj,
633 prawdafalsz: PrawdaFalsz
635 cls = es[$(ele).attr('data-type')];
640 'EduModule': EduModule
643 $(document).ready(function() {
644 new EduModule($("#book-text"));
645 return $(".exercise").each(function(i, el) {
646 return exercise(this);