1 // Generated by CoffeeScript 1.4.0
3 var $, Binding, EduModule, Excercise, Luki, PrawdaFalsz, Przyporzadkuj, Uporzadkuj, Wybor, Zastap, excercise,
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) {
27 EduModule.__super__.constructor.call(this, 'edumodule', element);
28 $("[name=teacher-toggle]").change(function(ev) {
29 if ($(ev.target).is(":checked")) {
30 return $(".teacher", _this.element).addClass("show");
32 return $(".teacher", _this.element).removeClass("show");
41 Excercise = (function(_super) {
43 __extends(Excercise, _super);
45 function Excercise(element) {
47 Excercise.__super__.constructor.call(this, 'excercise', element);
48 $(this.element).data("excercise-html", $(this.element).html());
49 $(".check", this.element).click(function() {
52 $('.solutions', this.element).click(function() {
53 return _this.show_solutions();
55 $('.reset', this.element).click(function() {
60 Excercise.prototype.reset = function() {
61 $(this.element).html($(this.element).data('excercise-html'));
62 return excercise(this.element);
65 Excercise.prototype.piece_correct = function(qpiece) {
66 return $(qpiece).removeClass('incorrect').addClass('correct');
69 Excercise.prototype.piece_incorrect = function(qpiece) {
70 return $(qpiece).removeClass('correct').addClass('incorrect');
73 Excercise.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 Excercise.prototype.show_solutions = function() {
91 return $(".question", this.element).each(function(i, question) {
92 return _this.solve_question(question);
96 Excercise.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 Excercise.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 Excercise.prototype.show_score = function(score) {
124 return $(".message", this.element).text("Wynik: " + score[0] + " / " + score[1]);
127 Excercise.prototype.draggable_equal = function($draggable1, $draggable2) {
131 Excercise.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 Excercise.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 $added.append('<span class="remove">x</span>');
156 return $('.remove', $added).click(function(ev) {
157 $added.prev(".placeholder:not(.multiple)").show();
159 $($added.data('original')).removeClass('disabled').draggable('enable');
161 return $added.remove();
165 Excercise.prototype.dragging = function(ismultiple, issortable) {
167 return $(".question", this.element).each(function(i, question) {
168 var draggable_opts, self;
173 $(".draggable", question).draggable(draggable_opts);
175 return $(".placeholder", question).droppable({
176 accept: function(draggable) {
177 var $draggable, is_accepted;
178 $draggable = $(draggable);
180 if (!$draggable.is(".draggable")) {
184 is_accepted = self.draggable_accept($draggable, $(this));
187 $(this).addClass('accepting');
189 $(this).removeClass('accepting');
193 drop: function(ev, ui) {
194 $(ev.target).removeClass('accepting dragover');
195 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
197 over: function(ev, ui) {
198 return $(ev.target).addClass('dragover');
200 out: function(ev, ui) {
201 return $(ev.target).removeClass('dragover');
211 Wybor = (function(_super) {
213 __extends(Wybor, _super);
215 function Wybor(element) {
216 Wybor.__super__.constructor.call(this, element);
219 Wybor.prototype.check_question = function(question) {
220 var all, good, solution,
224 solution = this.get_value_list(question, 'solution');
225 $(".question-piece", question).each(function(i, qpiece) {
226 var is_checked, piece_name, piece_no, should_be_checked;
227 piece_no = $(qpiece).attr('data-no');
228 piece_name = $(qpiece).attr('data-name');
230 should_be_checked = solution.indexOf(piece_name) >= 0;
232 should_be_checked = solution.indexOf(piece_no) >= 0;
234 is_checked = $("input", qpiece).is(":checked");
235 if (should_be_checked) {
239 if (should_be_checked) {
241 return _this.piece_correct(qpiece);
243 return _this.piece_incorrect(qpiece);
246 return $(qpiece).removeClass("correct,incorrect");
252 Wybor.prototype.solve_question = function(question) {
255 solution = this.get_value_list(question, 'solution');
256 return $(".question-piece", question).each(function(i, qpiece) {
257 var piece_name, piece_no, should_be_checked;
258 piece_no = $(qpiece).attr('data-no');
259 piece_name = $(qpiece).attr('data-name');
261 should_be_checked = solution.indexOf(piece_name) >= 0;
263 should_be_checked = solution.indexOf(piece_no) >= 0;
265 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
266 return $("input[type=checkbox]", qpiece).prop('checked', should_be_checked);
274 Uporzadkuj = (function(_super) {
276 __extends(Uporzadkuj, _super);
278 function Uporzadkuj(element) {
279 Uporzadkuj.__super__.constructor.call(this, element);
280 $('ol, ul', this.element).sortable({
285 Uporzadkuj.prototype.check_question = function(question) {
286 var all, correct, pkt, pkts, positions, sorted, _i, _ref;
287 positions = this.get_value_list(question, 'original', true);
288 sorted = positions.sort();
289 pkts = $('.question-piece', question);
292 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
294 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
296 this.piece_correct(pkts.eq(pkt));
298 this.piece_incorrect(pkts.eq(pkt));
301 return [correct, all];
304 Uporzadkuj.prototype.solve_question = function(question) {
305 var p, parent, pkts, positions, sorted, _i, _len, _results;
306 positions = this.get_value_list(question, 'original', true);
307 sorted = positions.sort();
308 pkts = $('.question-piece', question);
309 pkts.sort(function(a, b) {
311 q = $(a).data('pos');
312 w = $(b).data('pos');
321 parent = pkts.eq(0).parent();
323 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
325 _results.push(parent.prepend(p));
334 Luki = (function(_super) {
336 __extends(Luki, _super);
338 function Luki(element) {
339 Luki.__super__.constructor.call(this, element);
340 this.dragging(false, false);
343 Luki.prototype.check = function() {
348 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
350 $placeholder = $(qpiece).prev(".placeholder");
351 if ($placeholder.data('solution') === $(qpiece).data('no')) {
352 _this.piece_correct(qpiece);
355 _this.piece_incorrect(qpiece);
359 return this.show_score([correct, all]);
362 Luki.prototype.solve_question = function(question) {
364 return $(".placeholder", question).each(function(i, placeholder) {
366 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
367 return _this.draggable_move($qp, $(placeholder), false);
375 Zastap = (function(_super) {
377 __extends(Zastap, _super);
379 function Zastap(element) {
381 Zastap.__super__.constructor.call(this, element);
382 $(".paragraph", this.element).each(function(i, par) {
383 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
385 this.dragging(false, false);
388 Zastap.prototype.check = function() {
393 $(".paragraph", this.element).each(function(i, par) {
394 return $(".placeholder", par).each(function(j, qpiece) {
397 $dragged = $qp.next(".draggable");
398 if ($qp.data("solution")) {
399 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
400 _this.piece_correct($dragged);
407 return this.show_score([correct, all]);
410 Zastap.prototype.show_solutions = function() {
413 return $(".paragraph", this.element).each(function(i, par) {
414 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
417 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
418 return _this.draggable_move($dr, $qp, false);
423 Zastap.prototype.wrap_words = function(element, wrapper) {
424 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
425 ignore = /^[ \t.,:;()]+/;
426 insertWrapped = function(txt, elem) {
428 nw = wrapper.clone();
429 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
432 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
433 chld = element.get(0).childNodes[j];
434 if (chld.nodeType === document.TEXT_NODE) {
435 len = chld.textContent.length;
439 space = ignore.exec(chld.textContent.substr(i));
442 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
444 $(document.createTextNode(space[0])).insertBefore(chld);
445 i += space[0].length;
451 if (wordb < len - 1) {
452 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
454 _results.push($(chld).remove());
456 _results.push(void 0);
466 Przyporzadkuj = (function(_super) {
468 __extends(Przyporzadkuj, _super);
470 Przyporzadkuj.prototype.is_multiple = function() {
471 var qp, _i, _len, _ref;
472 _ref = $(".question-piece", this.element);
473 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
475 if ($(qp).data('solution').split(/[ ,]+/).length > 1) {
482 function Przyporzadkuj(element) {
483 Przyporzadkuj.__super__.constructor.call(this, element);
484 this.multiple = this.is_multiple();
485 this.dragging(this.multiple, true);
488 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
489 return d1.data("no") === d2.data("no");
492 Przyporzadkuj.prototype.check_question = function(question) {
493 var all, all_multiple, count, mandatory, optional, pn, pred, qp, v, _i, _j, _len, _len1, _ref, _ref1;
497 _ref = $(".predicate .question-piece", question);
498 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
500 pred = $(qp).closest("[data-predicate]");
501 v = this.get_value_optional_list(qp, 'solution');
504 all_multiple += mandatory.length + optional.length;
505 pn = pred.attr('data-predicate');
506 if (mandatory.indexOf(pn) >= 0 || optional.indexOf(pn) >= 0) {
508 this.piece_correct(qp);
510 this.piece_incorrect(qp);
515 _ref1 = $(".subject .question-piece", question);
516 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
518 v = this.get_value_optional_list(qp, 'solution');
521 all_multiple += mandatory.length + optional.length;
523 return [count, all_multiple];
529 Przyporzadkuj.prototype.solve_question = function(question) {
530 var $ph, $pr, m, mandatory, optional, qp, v, _i, _len, _ref, _results;
531 _ref = $(".subject .question-piece", question);
533 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
535 v = this.get_value_optional_list(qp, 'solution');
538 _results.push((function() {
539 var _j, _len1, _ref1, _results1;
540 _ref1 = mandatory.concat(optional);
542 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
544 $pr = $(".predicate [data-predicate=" + m + "]", question);
545 $ph = $pr.find(".placeholder");
546 _results1.push(this.draggable_move($(qp), $ph, this.multiple));
554 return Przyporzadkuj;
558 PrawdaFalsz = (function(_super) {
560 __extends(PrawdaFalsz, _super);
562 function PrawdaFalsz(element) {
563 var qp, _i, _len, _ref;
564 PrawdaFalsz.__super__.constructor.call(this, element);
565 _ref = $(".question-piece", this.element);
566 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
568 $(".true", qp).click(function(ev) {
570 $(this).closest(".question-piece").data("value", "true");
571 return $(this).addClass('chosen').siblings('a').removeClass('chosen');
573 $(".false", qp).click(function(ev) {
575 $(this).closest(".question-piece").data("value", "false");
576 return $(this).addClass('chosen').siblings('a').removeClass('chosen');
581 PrawdaFalsz.prototype.check_question = function() {
582 var all, good, qp, _i, _len, _ref;
585 _ref = $(".question-piece", this.element);
586 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
588 if ($(qp).data("solution").toString() === $(qp).data("value")) {
590 this.piece_correct(qp);
592 this.piece_incorrect(qp);
599 PrawdaFalsz.prototype.show_solutions = function() {
600 var qp, _i, _len, _ref, _results;
602 _ref = $(".question-piece", this.element);
604 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
606 if ($(qp).data('solution') === 'true') {
607 _results.push($(".true", qp).click());
609 _results.push($(".false", qp).click());
619 excercise = function(ele) {
623 uporzadkuj: Uporzadkuj,
626 przyporzadkuj: Przyporzadkuj,
627 prawdafalsz: PrawdaFalsz
629 cls = es[$(ele).attr('data-type')];
634 'EduModule': EduModule
637 $(document).ready(function() {
638 new EduModule($("#book-text"));
639 return $(".excercise").each(function(i, el) {
640 return excercise(this);