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 = 0, bad = 0, good = 0, _this = this;
242 var single = $(question).closest('.exercise').attr('data-subtype') === 'single';
244 $(".question-piece", question).each(function(i, qpiece) {
245 var is_checked, should_be_checked;
246 should_be_checked = $(qpiece).attr('data-sol') === 'prawda';
247 is_checked = $("input", qpiece).is(":checked");
248 if (!single || should_be_checked)
252 if (should_be_checked) {
254 return _this.piece_correct(qpiece);
257 return _this.piece_incorrect(qpiece);
260 return $(qpiece).removeClass("correct,incorrect");
263 if (is_checked !== should_be_checked) {
265 return _this.piece_incorrect(qpiece);
268 return _this.piece_correct(qpiece);
272 return [good, bad, all];
275 Wybor.prototype.solve_question = function(question) {
278 return $(".question-piece", question).each(function(i, qpiece) {
279 var should_be_checked;
280 should_be_checked = $(qpiece).attr('data-sol') === 'prawda';
281 return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
289 Uporzadkuj = (function(_super) {
291 __extends(Uporzadkuj, _super);
293 function Uporzadkuj(element) {
294 Uporzadkuj.__super__.constructor.call(this, element);
295 $('ol, ul', this.element).sortable({
301 Uporzadkuj.prototype.check_question = function(question) {
302 var all, bad, correct, pkt, pkts, positions, sorted, _i, _ref;
303 positions = this.get_value_list(question, 'original', true);
304 sorted = positions.sort(function(a, b) {
307 pkts = $('.question-piece', question);
311 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
313 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
315 this.piece_correct(pkts.eq(pkt));
318 this.piece_incorrect(pkts.eq(pkt));
321 return [correct, bad, all];
324 Uporzadkuj.prototype.solve_question = function(question) {
325 var p, parent, pkts, _i, _len, _results;
326 pkts = $('.question-piece', question);
327 pkts.sort(function(a, b) {
328 return $(a).data('pos') - $(b).data('pos');
330 parent = pkts.eq(0).parent();
332 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
334 _results.push(parent.append(p));
343 Luki = (function(_super) {
345 __extends(Luki, _super);
347 function Luki(element) {
348 Luki.__super__.constructor.call(this, element);
349 this.dragging(false, false);
352 Luki.prototype.check = function() {
353 var all, bad, correct,
355 all = $(".placeholder", this.element).length;
358 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
360 $placeholder = $(qpiece).prev(".placeholder");
361 if ($placeholder.data('solution') === $(qpiece).data('no')) {
362 _this.piece_correct(qpiece);
366 return _this.piece_incorrect(qpiece);
369 return this.show_score([correct, bad, all]);
372 Luki.prototype.solve_question = function(question) {
374 return $(".placeholder", question).each(function(i, placeholder) {
376 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
377 return _this.draggable_move($qp, $(placeholder), false);
385 Zastap = (function(_super) {
387 __extends(Zastap, _super);
389 function Zastap(element) {
391 Zastap.__super__.constructor.call(this, element);
392 $(".paragraph", this.element).each(function(i, par) {
393 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
395 this.dragging(false, false);
398 Zastap.prototype.check = function() {
399 var all, bad, correct,
404 $(".paragraph", this.element).each(function(i, par) {
405 return $(".placeholder", par).each(function(j, qpiece) {
408 $dragged = $qp.next(".draggable");
409 if ($qp.data("solution")) {
410 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
411 _this.piece_correct($dragged);
418 return this.show_score([correct, bad, all]);
421 Zastap.prototype.show_solutions = function() {
424 return $(".paragraph", this.element).each(function(i, par) {
425 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
428 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
429 return _this.draggable_move($dr, $qp, false);
434 Zastap.prototype.wrap_words = function(element, wrapper) {
435 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
436 ignore = /^[ \t.,:;()]+/;
437 insertWrapped = function(txt, elem) {
439 nw = wrapper.clone();
440 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
443 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
444 chld = element.get(0).childNodes[j];
445 if (chld.nodeType === document.TEXT_NODE) {
446 len = chld.textContent.length;
450 space = ignore.exec(chld.textContent.substr(i));
453 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
455 $(document.createTextNode(space[0])).insertBefore(chld);
456 i += space[0].length;
462 if (wordb < len - 1) {
463 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
465 _results.push($(chld).remove());
467 _results.push(void 0);
477 Przyporzadkuj = (function(_super) {
479 __extends(Przyporzadkuj, _super);
481 Przyporzadkuj.prototype.is_multiple = function() {
482 var qp, _i, _len, _ref;
483 _ref = $(".question-piece", this.element);
484 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
486 if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
493 function Przyporzadkuj(element) {
494 Przyporzadkuj.__super__.constructor.call(this, element);
495 this.multiple = this.is_multiple();
496 this.dragging(this.multiple, true);
499 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
500 return d1.data("no") === d2.data("no");
503 Przyporzadkuj.prototype.check_question = function(question) {
504 var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
505 minimum = $(question).data("minimum");
511 $(".subject .question-piece", question).each(function(i, el) {
513 v = self.get_value_optional_list(el, 'solution');
515 return all += mandatory.length;
518 _ref = $(".predicate [data-predicate]", question);
519 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
521 pn = $(pred).attr('data-predicate');
522 _ref1 = $(".question-piece", pred);
523 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
525 v = this.get_value_optional_list(qp, 'solution');
528 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
530 this.piece_correct(qp);
533 this.piece_incorrect(qp);
537 return [count, bad_count, all];
540 Przyporzadkuj.prototype.solve_question = function(question) {
541 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
542 minimum = $(question).data("min");
543 _ref = $(".subject .question-piece", question);
545 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
547 v = this.get_value_optional_list(qp, 'solution');
551 draggables = mandatory.count(optional).slice(0, minimum);
553 draggables = mandatory;
555 _results.push((function() {
556 var _j, _len1, _results1;
558 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
560 $pr = $(".predicate [data-predicate=" + m + "]", question);
561 $ph = $pr.find(".placeholder:visible");
562 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
570 return Przyporzadkuj;
574 PrawdaFalsz = (function(_super) {
576 __extends(PrawdaFalsz, _super);
578 function PrawdaFalsz(element) {
579 var qp, _i, _len, _ref,
581 PrawdaFalsz.__super__.constructor.call(this, element);
582 _ref = $(".question-piece", this.element);
583 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
585 $(".true", qp).click(function(ev) {
588 $(ev.target).closest(".question-piece").data("value", "true");
589 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
591 $(".false", qp).click(function(ev) {
594 $(ev.target).closest(".question-piece").data("value", "false");
595 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
600 PrawdaFalsz.prototype.check_question = function() {
601 var all, bad, good, qp, _i, _len, _ref;
605 _ref = $(".question-piece", this.element);
606 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
608 if ($(qp).data("solution").toString() === $(qp).data("value")) {
610 this.piece_correct(qp);
613 this.piece_incorrect(qp);
617 return [good, bad, all];
620 PrawdaFalsz.prototype.show_solutions = function() {
621 var qp, _i, _len, _ref, _results;
623 _ref = $(".question-piece", this.element);
625 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
627 if ($(qp).data('solution') === true) {
628 _results.push($(".true", qp).click());
630 _results.push($(".false", qp).click());
640 exercise = function(ele) {
644 uporzadkuj: Uporzadkuj,
647 przyporzadkuj: Przyporzadkuj,
648 prawdafalsz: PrawdaFalsz
650 cls = es[$(ele).attr('data-type')];
655 'EduModule': EduModule
658 $(document).ready(function() {
659 new EduModule($("#book-text"));
660 return $(".exercise").each(function(i, el) {
661 return exercise(this);