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) {
87 return score[1] += s[1];
89 return this.show_score(score);
92 Exercise.prototype.show_solutions = function() {
95 return $(".question", this.element).each(function(i, question) {
96 return _this.solve_question(question);
100 Exercise.prototype.get_value_list = function(elem, data_key, numbers) {
102 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
104 vl = vl.map(function(x) {
111 Exercise.prototype.get_value_optional_list = function(elem, data_key) {
112 var mandat, opt, v, vals, _i, _len;
113 vals = this.get_value_list(elem, data_key);
116 for (_i = 0, _len = vals.length; _i < _len; _i++) {
118 if (v.slice(-1) === "?") {
119 opt.push(v.slice(0, -1));
124 return [mandat, opt];
127 Exercise.prototype.show_score = function(score) {
129 $msg = $(".message", this.element);
130 $msg.text("Wynik: " + score[0] + " / " + score[1]);
131 if (score[0] === score[1]) {
132 return $msg.addClass("maxscore");
134 return $msg.removeClass("maxscore");
138 Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
142 Exercise.prototype.draggable_accept = function($draggable, $droppable) {
143 var d, dropped, _i, _len;
144 dropped = $droppable.closest("ul, ol").find(".draggable");
145 for (_i = 0, _len = dropped.length; _i < _len; _i++) {
147 if (this.draggable_equal($draggable, $(d))) {
154 Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
157 $added = $draggable.clone();
158 $added.data("original", $draggable.get(0));
160 $draggable.addClass('disabled').draggable('disable');
162 $placeholder.after($added);
163 if (!$placeholder.hasClass('multiple')) {
166 if ($added.is(".add-li")) {
167 $added.wrap("<li/>");
169 $added.append('<span class="remove">x</span><div class="clr"></div>');
170 return $('.remove', $added).click(function(ev) {
173 $($added.data('original')).removeClass('disabled').draggable('enable');
175 if ($added.is(".add-li")) {
176 $added = $added.closest('li');
178 $added.prev(".placeholder:not(.multiple)").show();
179 return $added.remove();
183 Exercise.prototype.dragging = function(ismultiple, issortable) {
185 return $(".question", this.element).each(function(i, question) {
186 var draggable_opts, self;
192 $(".draggable", question).draggable(draggable_opts);
194 return $(".placeholder", question).droppable({
195 accept: function(draggable) {
196 var $draggable, is_accepted;
197 $draggable = $(draggable);
199 if (!$draggable.is(".draggable")) {
203 is_accepted = self.draggable_accept($draggable, $(this));
206 $(this).addClass('accepting');
208 $(this).removeClass('accepting');
212 drop: function(ev, ui) {
213 $(ev.target).removeClass('accepting dragover');
214 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
216 over: function(ev, ui) {
217 return $(ev.target).addClass('dragover');
219 out: function(ev, ui) {
220 return $(ev.target).removeClass('dragover');
230 Wybor = (function(_super) {
232 __extends(Wybor, _super);
234 function Wybor(element) {
235 Wybor.__super__.constructor.call(this, element);
236 $(".question-piece input", element).change(this.retry);
239 Wybor.prototype.check_question = function(question) {
240 var all, good, solution,
244 solution = this.get_value_list(question, 'solution');
245 $(".question-piece", question).each(function(i, qpiece) {
246 var is_checked, piece_name, piece_no, should_be_checked;
247 piece_no = $(qpiece).attr('data-no');
248 piece_name = $(qpiece).attr('data-name');
250 should_be_checked = solution.indexOf(piece_name) >= 0;
252 should_be_checked = solution.indexOf(piece_no) >= 0;
254 is_checked = $("input", qpiece).is(":checked");
255 if (should_be_checked) {
259 if (should_be_checked) {
261 return _this.piece_correct(qpiece);
263 return _this.piece_incorrect(qpiece);
266 return $(qpiece).removeClass("correct,incorrect");
272 Wybor.prototype.solve_question = function(question) {
275 solution = this.get_value_list(question, 'solution');
276 return $(".question-piece", question).each(function(i, qpiece) {
277 var piece_name, piece_no, should_be_checked;
278 piece_no = $(qpiece).attr('data-no');
279 piece_name = $(qpiece).attr('data-name');
281 should_be_checked = solution.indexOf(piece_name) >= 0;
283 should_be_checked = solution.indexOf(piece_no) >= 0;
285 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
286 return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
294 Uporzadkuj = (function(_super) {
296 __extends(Uporzadkuj, _super);
298 function Uporzadkuj(element) {
299 Uporzadkuj.__super__.constructor.call(this, element);
300 $('ol, ul', this.element).sortable({
306 Uporzadkuj.prototype.check_question = function(question) {
307 var all, correct, pkt, pkts, positions, sorted, _i, _ref;
308 positions = this.get_value_list(question, 'original', true);
309 sorted = positions.sort();
310 pkts = $('.question-piece', question);
313 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
315 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
317 this.piece_correct(pkts.eq(pkt));
319 this.piece_incorrect(pkts.eq(pkt));
322 return [correct, 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) {
357 __extends(Luki, _super);
359 function Luki(element) {
360 Luki.__super__.constructor.call(this, element);
361 this.dragging(false, false);
364 Luki.prototype.check = function() {
367 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);
376 return _this.piece_incorrect(qpiece);
379 return this.show_score([correct, all]);
382 Luki.prototype.solve_question = function(question) {
384 return $(".placeholder", question).each(function(i, placeholder) {
386 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
387 return _this.draggable_move($qp, $(placeholder), false);
395 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() {
413 $(".paragraph", this.element).each(function(i, par) {
414 return $(".placeholder", par).each(function(j, qpiece) {
417 $dragged = $qp.next(".draggable");
418 if ($qp.data("solution")) {
419 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
420 _this.piece_correct($dragged);
427 return this.show_score([correct, all]);
430 Zastap.prototype.show_solutions = function() {
433 return $(".paragraph", this.element).each(function(i, par) {
434 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
437 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
438 return _this.draggable_move($dr, $qp, false);
443 Zastap.prototype.wrap_words = function(element, wrapper) {
444 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
445 ignore = /^[ \t.,:;()]+/;
446 insertWrapped = function(txt, elem) {
448 nw = wrapper.clone();
449 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
452 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
453 chld = element.get(0).childNodes[j];
454 if (chld.nodeType === document.TEXT_NODE) {
455 len = chld.textContent.length;
459 space = ignore.exec(chld.textContent.substr(i));
462 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
464 $(document.createTextNode(space[0])).insertBefore(chld);
465 i += space[0].length;
471 if (wordb < len - 1) {
472 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
474 _results.push($(chld).remove());
476 _results.push(void 0);
486 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, count, mandatory, minimum, optional, pn, pred, qp, v, _i, _j, _len, _len1, _ref, _ref1;
514 minimum = $(question).data("minimum");
518 all = $(".subject .question-piece", question).length;
520 _ref = $(".predicate [data-predicate]", question);
521 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
523 pn = $(pred).attr('data-predicate');
524 if (minimum != null) {
527 _ref1 = $(".question-piece", pred);
528 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
530 v = this.get_value_optional_list(qp, 'solution');
533 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
535 this.piece_correct(qp);
537 this.piece_incorrect(qp);
544 Przyporzadkuj.prototype.solve_question = function(question) {
545 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
546 minimum = $(question).data("min");
547 _ref = $(".subject .question-piece", question);
549 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
551 v = this.get_value_optional_list(qp, 'solution');
555 draggables = mandatory.count(optional).slice(0, minimum);
557 draggables = mandatory;
559 _results.push((function() {
560 var _j, _len1, _results1;
562 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
564 $pr = $(".predicate [data-predicate=" + m + "]", question);
565 $ph = $pr.find(".placeholder:visible");
566 _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
574 return Przyporzadkuj;
578 PrawdaFalsz = (function(_super) {
580 __extends(PrawdaFalsz, _super);
582 function PrawdaFalsz(element) {
583 var qp, _i, _len, _ref,
585 PrawdaFalsz.__super__.constructor.call(this, element);
586 _ref = $(".question-piece", this.element);
587 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
589 $(".true", qp).click(function(ev) {
592 $(ev.target).closest(".question-piece").data("value", "true");
593 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
595 $(".false", qp).click(function(ev) {
598 $(ev.target).closest(".question-piece").data("value", "false");
599 return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
604 PrawdaFalsz.prototype.check_question = function() {
605 var all, good, qp, _i, _len, _ref;
608 _ref = $(".question-piece", this.element);
609 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
611 if ($(qp).data("solution").toString() === $(qp).data("value")) {
613 this.piece_correct(qp);
615 this.piece_incorrect(qp);
622 PrawdaFalsz.prototype.show_solutions = function() {
623 var qp, _i, _len, _ref, _results;
625 _ref = $(".question-piece", this.element);
627 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
629 if ($(qp).data('solution') === true) {
630 _results.push($(".true", qp).click());
632 _results.push($(".false", qp).click());
642 exercise = function(ele) {
646 uporzadkuj: Uporzadkuj,
649 przyporzadkuj: Przyporzadkuj,
650 prawdafalsz: PrawdaFalsz
652 cls = es[$(ele).attr('data-type')];
657 'EduModule': EduModule
660 $(document).ready(function() {
661 new EduModule($("#book-text"));
662 return $(".exercise").each(function(i, el) {
663 return exercise(this);