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) {
26 EduModule.__super__.constructor.call(this, 'edumodule', element);
33 Excercise = (function(_super) {
35 __extends(Excercise, _super);
37 function Excercise(element) {
39 Excercise.__super__.constructor.call(this, 'excercise', element);
40 $(this.element).data("excercise-html", $(this.element).html());
41 $(".check", this.element).click(function() {
44 $('.solutions', this.element).click(function() {
45 return _this.show_solutions();
47 $('.reset', this.element).click(function() {
52 Excercise.prototype.reset = function() {
53 $(this.element).html($(this.element).data('excercise-html'));
54 return excercise(this.element);
57 Excercise.prototype.piece_correct = function(qpiece) {
58 return $(qpiece).removeClass('incorrect').addClass('correct');
61 Excercise.prototype.piece_incorrect = function(qpiece) {
62 return $(qpiece).removeClass('correct').addClass('incorrect');
65 Excercise.prototype.check = function() {
69 $(".question", this.element).each(function(i, question) {
70 return scores.push(_this.check_question(question));
73 $.each(scores, function(i, s) {
75 return score[1] += s[1];
77 return this.show_score(score);
80 Excercise.prototype.show_solutions = function() {
83 return $(".question", this.element).each(function(i, question) {
84 return _this.solve_question(question);
88 Excercise.prototype.get_value_list = function(elem, data_key, numbers) {
90 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
92 vl = vl.map(function(x) {
99 Excercise.prototype.get_value_optional_list = function(elem, data_key) {
100 var mandat, opt, v, vals, _i, _len;
101 vals = this.get_value_list(elem, data_key);
104 for (_i = 0, _len = vals.length; _i < _len; _i++) {
106 if (v.slice(-1) === "?") {
107 opt.push(v.slice(0, -1));
112 return [mandat, opt];
115 Excercise.prototype.show_score = function(score) {
116 return $(".message", this.element).text("Wynik: " + score[0] + " / " + score[1]);
119 Excercise.prototype.draggable_equal = function($draggable1, $draggable2) {
123 Excercise.prototype.draggable_accept = function($draggable, $droppable) {
124 var d, dropped, _i, _len;
125 dropped = $droppable.closest("ul, ol").find(".draggable");
126 for (_i = 0, _len = dropped.length; _i < _len; _i++) {
128 if (this.draggable_equal($draggable, $(d))) {
135 Excercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
138 $added = $draggable.clone();
139 $added.data("original", $draggable.get(0));
141 $draggable.addClass('disabled').draggable('disable');
143 $placeholder.after($added);
144 if (!$placeholder.hasClass('multiple')) {
147 $added.append('<span class="remove">x</span>');
148 return $('.remove', $added).click(function(ev) {
149 $added.prev(".placeholder:not(.multiple)").show();
151 $($added.data('original')).removeClass('disabled').draggable('enable');
153 return $added.remove();
157 Excercise.prototype.dragging = function(ismultiple, issortable) {
159 return $(".question", this.element).each(function(i, question) {
160 var draggable_opts, self;
165 $(".draggable", question).draggable(draggable_opts);
167 return $(".placeholder", question).droppable({
168 accept: function(draggable) {
169 var $draggable, is_accepted;
170 $draggable = $(draggable);
172 if (!$draggable.is(".draggable")) {
176 is_accepted = self.draggable_accept($draggable, $(this));
179 $(this).addClass('accepting');
181 $(this).removeClass('accepting');
185 drop: function(ev, ui) {
186 $(ev.target).removeClass('accepting dragover');
187 return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
189 over: function(ev, ui) {
190 return $(ev.target).addClass('dragover');
192 out: function(ev, ui) {
193 return $(ev.target).removeClass('dragover');
203 Wybor = (function(_super) {
205 __extends(Wybor, _super);
207 function Wybor(element) {
208 Wybor.__super__.constructor.call(this, element);
211 Wybor.prototype.check_question = function(question) {
212 var all, good, solution,
216 solution = this.get_value_list(question, 'solution');
217 $(".question-piece", question).each(function(i, qpiece) {
218 var is_checked, piece_name, piece_no, should_be_checked;
219 piece_no = $(qpiece).attr('data-no');
220 piece_name = $(qpiece).attr('data-name');
222 should_be_checked = solution.indexOf(piece_name) >= 0;
224 should_be_checked = solution.indexOf(piece_no) >= 0;
226 is_checked = $("input", qpiece).is(":checked");
227 if (should_be_checked) {
231 if (should_be_checked) {
233 return _this.piece_correct(qpiece);
235 return _this.piece_incorrect(qpiece);
238 return $(qpiece).removeClass("correct,incorrect");
244 Wybor.prototype.solve_question = function(question) {
247 solution = this.get_value_list(question, 'solution');
248 return $(".question-piece", question).each(function(i, qpiece) {
249 var piece_name, piece_no, should_be_checked;
250 piece_no = $(qpiece).attr('data-no');
251 piece_name = $(qpiece).attr('data-name');
253 should_be_checked = solution.indexOf(piece_name) >= 0;
255 should_be_checked = solution.indexOf(piece_no) >= 0;
257 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
258 return $("input[type=checkbox]", qpiece).prop('checked', should_be_checked);
266 Uporzadkuj = (function(_super) {
268 __extends(Uporzadkuj, _super);
270 function Uporzadkuj(element) {
271 Uporzadkuj.__super__.constructor.call(this, element);
272 $('ol, ul', this.element).sortable({
277 Uporzadkuj.prototype.check_question = function(question) {
278 var all, correct, pkt, pkts, positions, sorted, _i, _ref;
279 positions = this.get_value_list(question, 'original', true);
280 sorted = positions.sort();
281 pkts = $('.question-piece', question);
284 for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
286 if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
288 this.piece_correct(pkts.eq(pkt));
290 this.piece_incorrect(pkts.eq(pkt));
293 return [correct, all];
296 Uporzadkuj.prototype.solve_question = function(question) {
297 var p, parent, pkts, positions, sorted, _i, _len, _results;
298 positions = this.get_value_list(question, 'original', true);
299 sorted = positions.sort();
300 pkts = $('.question-piece', question);
301 pkts.sort(function(a, b) {
303 q = $(a).data('pos');
304 w = $(b).data('pos');
313 parent = pkts.eq(0).parent();
315 for (_i = 0, _len = pkts.length; _i < _len; _i++) {
317 _results.push(parent.prepend(p));
326 Luki = (function(_super) {
328 __extends(Luki, _super);
330 function Luki(element) {
331 Luki.__super__.constructor.call(this, element);
332 this.dragging(false, false);
335 Luki.prototype.check = function() {
340 $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
342 $placeholder = $(qpiece).prev(".placeholder");
343 if ($placeholder.data('solution') === $(qpiece).data('no')) {
344 _this.piece_correct(qpiece);
347 _this.piece_incorrect(qpiece);
351 return this.show_score([correct, all]);
354 Luki.prototype.solve_question = function(question) {
356 return $(".placeholder", question).each(function(i, placeholder) {
358 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
359 return _this.draggable_move($qp, $(placeholder), false);
367 Zastap = (function(_super) {
369 __extends(Zastap, _super);
371 function Zastap(element) {
373 Zastap.__super__.constructor.call(this, element);
374 $(".paragraph", this.element).each(function(i, par) {
375 return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
377 this.dragging(false, false);
380 Zastap.prototype.check = function() {
385 $(".paragraph", this.element).each(function(i, par) {
386 return $(".placeholder", par).each(function(j, qpiece) {
389 $dragged = $qp.next(".draggable");
390 if ($qp.data("solution")) {
391 if ($dragged && $qp.data("solution") === $dragged.data("no")) {
392 _this.piece_correct($dragged);
399 return this.show_score([correct, all]);
402 Zastap.prototype.show_solutions = function() {
405 return $(".paragraph", this.element).each(function(i, par) {
406 return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
409 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
410 return _this.draggable_move($dr, $qp, false);
415 Zastap.prototype.wrap_words = function(element, wrapper) {
416 var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
417 ignore = /^[ \t.,:;()]+/;
418 insertWrapped = function(txt, elem) {
420 nw = wrapper.clone();
421 return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
424 for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
425 chld = element.get(0).childNodes[j];
426 if (chld.nodeType === document.TEXT_NODE) {
427 len = chld.textContent.length;
431 space = ignore.exec(chld.textContent.substr(i));
434 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
436 $(document.createTextNode(space[0])).insertBefore(chld);
437 i += space[0].length;
443 if (wordb < len - 1) {
444 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
446 _results.push($(chld).remove());
448 _results.push(void 0);
458 Przyporzadkuj = (function(_super) {
460 __extends(Przyporzadkuj, _super);
462 Przyporzadkuj.prototype.is_multiple = function() {
463 var qp, _i, _len, _ref;
464 _ref = $(".question-piece", this.element);
465 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
467 if ($(qp).data('solution').split(/[ ,]+/).length > 1) {
474 function Przyporzadkuj(element) {
475 Przyporzadkuj.__super__.constructor.call(this, element);
476 this.multiple = this.is_multiple();
477 this.dragging(this.multiple, true);
480 Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
481 return d1.data("no") === d2.data("no");
484 Przyporzadkuj.prototype.check_question = function(question) {
485 var all, count, mandatory, minimum, optional, pn, pred, qp, v, _i, _j, _len, _len1, _ref, _ref1;
486 minimum = $(question).data("minimum");
490 all = $(".subjects .question-piece", question).length;
492 _ref = $(".predicate [data-predicate]", question);
493 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
495 pn = $(pred).attr('data-predicate');
496 if (minimum != null) {
499 _ref1 = $(".question-piece", pred);
500 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
502 v = this.get_value_optional_list(qp, 'solution');
505 if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
507 this.piece_correct(qp);
509 this.piece_incorrect(qp);
516 Przyporzadkuj.prototype.solve_question = function(question) {
517 var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
518 minimum = $(question).data("min");
519 _ref = $(".subject .question-piece", question);
521 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
523 v = this.get_value_optional_list(qp, 'solution');
527 draggables = mandatory.count(optional).slice(0, minimum);
529 draggables = mandatory;
531 _results.push((function() {
532 var _j, _len1, _results1;
534 for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
536 $pr = $(".predicate [data-predicate=" + m + "]", question);
537 $ph = $pr.find(".placeholder:visible");
538 _results1.push(this.draggable_move($(qp), $ph, this.multiple));
546 return Przyporzadkuj;
550 PrawdaFalsz = (function(_super) {
552 __extends(PrawdaFalsz, _super);
554 function PrawdaFalsz(element) {
555 var qp, _i, _len, _ref;
556 PrawdaFalsz.__super__.constructor.call(this, element);
557 _ref = $(".question-piece", this.element);
558 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
560 $(".true", qp).click(function(ev) {
562 $(this).closest(".question-piece").data("value", "true");
563 return $(this).addClass('chosen').siblings('a').removeClass('chosen');
565 $(".false", qp).click(function(ev) {
567 $(this).closest(".question-piece").data("value", "false");
568 return $(this).addClass('chosen').siblings('a').removeClass('chosen');
573 PrawdaFalsz.prototype.check_question = function() {
574 var all, good, qp, _i, _len, _ref;
577 _ref = $(".question-piece", this.element);
578 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
580 if ($(qp).data("solution").toString() === $(qp).data("value")) {
582 this.piece_correct(qp);
584 this.piece_incorrect(qp);
591 PrawdaFalsz.prototype.show_solutions = function() {
592 var qp, _i, _len, _ref, _results;
594 _ref = $(".question-piece", this.element);
596 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
598 if ($(qp).data('solution') === 'true') {
599 _results.push($(".true", qp).click());
601 _results.push($(".false", qp).click());
611 excercise = function(ele) {
615 uporzadkuj: Uporzadkuj,
618 przyporzadkuj: Przyporzadkuj,
619 prawdafalsz: PrawdaFalsz
621 cls = es[$(ele).attr('data-type')];
626 'EduModule': EduModule
629 $(document).ready(function() {
630 new EduModule($("#book-text"));
631 return $(".excercise").each(function(i, el) {
632 return excercise(this);