5 constructor: (@handler, @element) ->
6 $(@element).data(@handler, this)
9 class EduModule extends Binding
10 constructor: (element) ->
11 super 'edumodule', element
13 # $("[name=teacher-toggle]").change (ev) =>
14 # if $(ev.target).is(":checked")
15 # $(".teacher", @element).addClass "show"
17 # $(".teacher", @element).removeClass "show"
20 class Exercise extends Binding
21 constructor: (element) ->
22 super 'exercise', element
23 # just save the html to reset the exercise
24 $(@element).data("exercise-html", $(@element).html())
26 $(".check", @element).click (ev) =>
28 $(".retry", @element).show()
29 $(".check", @element).hide()
30 $(".retry", @element).click (ev) =>
32 $('.solutions', @element).click =>
34 $(".comment", @element).show()
35 $('.reset', @element).click =>
39 $(".correct, .incorrect", @element).removeClass("correct incorrect")
40 $(".check", @element).show()
41 $(".retry", @element).hide()
44 $(@element).html($(@element).data('exercise-html'))
47 piece_correct: (qpiece) ->
48 $(qpiece).removeClass('incorrect').addClass('correct')
50 piece_incorrect: (qpiece) ->
51 $(qpiece).removeClass('correct').addClass('incorrect')
55 $(".question", @element).each (i, question) =>
56 scores.push(@check_question question)
59 $.each scores, (i, s) ->
67 $(".question", @element).each (i, question) =>
68 @solve_question question
70 # Parses a list of values, separated by space or comma.
71 # The list is read from data attribute of elem using data_key
72 # Returns a list with elements
73 # eg.: things_i_need: "house bike tv playstation"
74 # yields ["house", "bike", "tv", "playstation"]
75 # If optional numbers argument is true, returns list of numbers
77 get_value_list: (elem, data_key, numbers) ->
78 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
80 vl = vl.map((x) -> parseInt(x))
83 # Parses a list of values, separated by space or comma.
84 # The list is read from data attribute of elem using data_key
85 # Returns a 2-element list with mandatory and optional
86 # items. optional items are marked with a question mark on the end
87 # eg.: things_i_need: "house bike tv? playstation?"
88 # yields [[ "house", "bike"], ["tv", "playstation"]]
89 get_value_optional_list: (elem, data_key) ->
90 vals = @get_value_list(elem, data_key)
95 opt.push v.slice(0, -1)
100 show_score: (score) ->
101 $msg = $(".message", @element)
102 $msg.text("Wynik: #{score[0]} / #{score[2]}")
103 if score[0] >= score[2] and score[1] == 0
104 $msg.addClass("maxscore")
106 $msg.removeClass("maxscore")
109 draggable_equal: ($draggable1, $draggable2) ->
112 draggable_accept: ($draggable, $droppable) ->
113 dropped = $droppable.closest("ul, ol").find(".draggable")
115 if @draggable_equal $draggable, $(d)
119 draggable_move: ($draggable, $placeholder, ismultiple) ->
120 $added = $draggable.clone()
121 $added.data("original", $draggable.get(0))
123 $draggable.addClass('disabled').draggable('disable')
125 $placeholder.after($added)
126 if not $placeholder.hasClass('multiple')
128 if $added.is(".add-li")
131 $added.append('<span class="remove">x</span><div class="clr"></div>')
132 $('.remove', $added).click (ev) =>
135 $($added.data('original')).removeClass('disabled').draggable('enable')
137 if $added.is(".add-li")
138 $added = $added.closest('li')
139 $added.prev(".placeholder:not(.multiple)").show()
143 ## XXX co z issortable?
144 dragging: (ismultiple, issortable) ->
145 $(".question", @element).each (i, question) =>
151 $(".draggable", question).draggable(draggable_opts)
153 $(".placeholder", question).droppable
154 accept: (draggable) ->
155 $draggable = $(draggable)
158 if not $draggable.is(".draggable")
162 is_accepted= self.draggable_accept $draggable, $(this)
165 $(this).addClass 'accepting'
167 $(this).removeClass 'accepting'
171 $(ev.target).removeClass 'accepting dragover'
173 @draggable_move $(ui.draggable), $(ev.target), ismultiple
175 # $added = $(ui.draggable).clone()
176 # $added.data("original", ui.draggable)
178 # $(ui.draggable).addClass('disabled').draggable('disable')
180 # $(ev.target).after(added)
181 # if not $(ev.target).hasClass('multiple')
182 # $(ev.target).hide()
183 # $added.append('<span class="remove">x</span>')
184 # $('.remove', added).click (ev) =>
185 # $added.prev(".placeholder:not(.multiple)").show()
187 # $added.data('original').removeClass('disabled').draggable('enable')
191 $(ev.target).addClass 'dragover'
195 $(ev.target).removeClass 'dragover'
199 class Wybor extends Exercise
200 constructor: (element) ->
202 $(".question-piece input", element).change(@retry);
205 check_question: (question) ->
209 solution = @get_value_list(question, 'solution')
210 $(".question-piece", question).each (i, qpiece) =>
211 piece_no = $(qpiece).attr 'data-no'
212 piece_name = $(qpiece).attr 'data-name'
214 should_be_checked = solution.indexOf(piece_name) >= 0
216 should_be_checked = solution.indexOf(piece_no) >= 0
217 is_checked = $("input", qpiece).is(":checked")
225 @piece_correct qpiece
228 @piece_incorrect qpiece
230 $(qpiece).removeClass("correct,incorrect")
232 return [good, bad, all]
234 solve_question: (question) ->
235 solution = @get_value_list(question, 'solution')
236 $(".question-piece", question).each (i, qpiece) =>
237 piece_no = $(qpiece).attr 'data-no'
238 piece_name = $(qpiece).attr 'data-name'
240 should_be_checked = solution.indexOf(piece_name) >= 0
242 should_be_checked = solution.indexOf(piece_no) >= 0
243 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
244 $("input[type=checkbox],input[type=radio]", qpiece).prop 'checked', should_be_checked
248 class Uporzadkuj extends Exercise
249 constructor: (element) ->
251 $('ol, ul', @element).sortable({ items: "> li", start: @retry })
253 check_question: (question) ->
254 positions = @get_value_list(question, 'original', true)
255 sorted = positions.sort()
256 pkts = $('.question-piece', question)
262 for pkt in [0...pkts.length]
264 if pkts.eq(pkt).data('pos') == sorted[pkt]
266 @piece_correct pkts.eq(pkt)
269 @piece_incorrect pkts.eq(pkt)
270 return [correct, bad, all]
272 solve_question: (question) ->
273 positions = @get_value_list(question, 'original', true)
274 sorted = positions.sort()
275 pkts = $('.question-piece', question)
283 parent = pkts.eq(0).parent()
288 # XXX propozycje="1/0"
289 class Luki extends Exercise
290 constructor: (element) ->
292 @dragging false, false
295 all = $(".placeholder", @element).length
298 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
299 $placeholder = $(qpiece).prev(".placeholder")
300 if $placeholder.data('solution') == $(qpiece).data('no')
301 @piece_correct qpiece
305 @piece_incorrect qpiece
307 @show_score [correct, bad, all]
309 solve_question: (question) ->
310 $(".placeholder", question).each (i, placeholder) =>
311 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
312 @draggable_move $qp, $(placeholder), false
315 class Zastap extends Exercise
316 constructor: (element) ->
318 $(".paragraph", @element).each (i, par) =>
319 @wrap_words $(par), $('<span class="placeholder zastap"/>')
320 @dragging false, false
327 $(".paragraph", @element).each (i, par) =>
328 $(".placeholder", par).each (j, qpiece) =>
330 $dragged = $qp.next(".draggable")
331 if $qp.data("solution")
332 if $dragged and $qp.data("solution") == $dragged.data("no")
333 @piece_correct $dragged
335 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
339 @show_score [correct, bad, all]
343 $(".paragraph", @element).each (i, par) =>
344 $(".placeholder[data-solution]", par).each (j, qpiece) =>
346 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
347 @draggable_move $dr, $qp, false
350 wrap_words: (element, wrapper) ->
351 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
352 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
353 ignore = /^[ \t.,:;()]+/
355 insertWrapped = (txt, elem) ->
357 $(document.createTextNode(txt))
358 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
360 for j in [element.get(0).childNodes.length-1..0]
361 chld = element.get(0).childNodes[j]
362 if chld.nodeType == document.TEXT_NODE
363 len = chld.textContent.length
367 space = ignore.exec(chld.textContent.substr(i))
370 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
372 $(document.createTextNode(space[0])).insertBefore(chld)
378 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
382 class Przyporzadkuj extends Exercise
384 for qp in $(".question-piece", @element)
385 if $(qp).attr('data-solution').split(/[ ,]+/).length > 1
389 constructor: (element) ->
392 @multiple = @is_multiple()
394 @dragging @multiple, true
396 draggable_equal: (d1, d2) ->
397 return d1.data("no") == d2.data("no")
400 check_question: (question) ->
401 # subjects placed in predicates
402 minimum = $(question).data("minimum")
408 $(".subject .question-piece", question).each (i, el) ->
409 v = self.get_value_optional_list el, 'solution'
411 all += mandatory.length
413 for pred in $(".predicate [data-predicate]", question)
414 pn = $(pred).attr('data-predicate')
418 for qp in $(".question-piece", pred)
419 v = @get_value_optional_list qp, 'solution'
423 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
430 return [count, bad_count, all]
432 solve_question: (question) ->
433 minimum = $(question).data("min")
435 for qp in $(".subject .question-piece", question)
436 v = @get_value_optional_list qp, 'solution'
441 draggables = mandatory.count(optional)[0...minimum]
443 draggables = mandatory
445 $pr = $(".predicate [data-predicate=" + m + "]", question)
446 $ph = $pr.find ".placeholder:visible"
447 @draggable_move $(qp), $ph.eq(0), @multiple
451 class PrawdaFalsz extends Exercise
452 constructor: (element) ->
455 for qp in $(".question-piece", @element)
456 $(".true", qp).click (ev) =>
459 $(ev.target).closest(".question-piece").data("value", "true")
460 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
461 $(".false", qp).click (ev) =>
464 $(ev.target).closest(".question-piece").data("value", "false")
465 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
472 for qp in $(".question-piece", @element)
473 if $(qp).data("solution").toString() == $(qp).data("value")
482 return [good, bad, all]
486 for qp in $(".question-piece", @element)
487 if $(qp).data('solution') == true
488 $(".true", qp).click()
490 $(".false", qp).click()
498 uporzadkuj: Uporzadkuj
501 przyporzadkuj: Przyporzadkuj
502 prawdafalsz: PrawdaFalsz
505 cls = es[$(ele).attr('data-type')]
510 'EduModule': EduModule
515 $(document).ready () ->
516 new EduModule($("#book-text"))
518 $(".exercise").each (i, el) ->