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 (a, b) -> a - b
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 pkts = $('.question-piece', question)
274 pkts.sort (a, b) -> return $(a).data('pos') - $(b).data('pos')
275 parent = pkts.eq(0).parent()
280 # XXX propozycje="1/0"
281 class Luki extends Exercise
282 constructor: (element) ->
284 @dragging false, false
287 all = $(".placeholder", @element).length
290 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
291 $placeholder = $(qpiece).prev(".placeholder")
292 if $placeholder.data('solution') == $(qpiece).data('no')
293 @piece_correct qpiece
297 @piece_incorrect qpiece
299 @show_score [correct, bad, all]
301 solve_question: (question) ->
302 $(".placeholder", question).each (i, placeholder) =>
303 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
304 @draggable_move $qp, $(placeholder), false
307 class Zastap extends Exercise
308 constructor: (element) ->
310 $(".paragraph", @element).each (i, par) =>
311 @wrap_words $(par), $('<span class="placeholder zastap"/>')
312 @dragging false, false
319 $(".paragraph", @element).each (i, par) =>
320 $(".placeholder", par).each (j, qpiece) =>
322 $dragged = $qp.next(".draggable")
323 if $qp.data("solution")
324 if $dragged and $qp.data("solution") == $dragged.data("no")
325 @piece_correct $dragged
327 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
331 @show_score [correct, bad, all]
335 $(".paragraph", @element).each (i, par) =>
336 $(".placeholder[data-solution]", par).each (j, qpiece) =>
338 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
339 @draggable_move $dr, $qp, false
342 wrap_words: (element, wrapper) ->
343 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
344 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
345 ignore = /^[ \t.,:;()]+/
347 insertWrapped = (txt, elem) ->
349 $(document.createTextNode(txt))
350 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
352 for j in [element.get(0).childNodes.length-1..0]
353 chld = element.get(0).childNodes[j]
354 if chld.nodeType == document.TEXT_NODE
355 len = chld.textContent.length
359 space = ignore.exec(chld.textContent.substr(i))
362 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
364 $(document.createTextNode(space[0])).insertBefore(chld)
370 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
374 class Przyporzadkuj extends Exercise
376 for qp in $(".question-piece", @element)
377 if $(qp).attr('data-solution').split(/[ ,]+/).length > 1
381 constructor: (element) ->
384 @multiple = @is_multiple()
386 @dragging @multiple, true
388 draggable_equal: (d1, d2) ->
389 return d1.data("no") == d2.data("no")
392 check_question: (question) ->
393 # subjects placed in predicates
394 minimum = $(question).data("minimum")
400 $(".subject .question-piece", question).each (i, el) ->
401 v = self.get_value_optional_list el, 'solution'
403 all += mandatory.length
405 for pred in $(".predicate [data-predicate]", question)
406 pn = $(pred).attr('data-predicate')
410 for qp in $(".question-piece", pred)
411 v = @get_value_optional_list qp, 'solution'
415 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
422 return [count, bad_count, all]
424 solve_question: (question) ->
425 minimum = $(question).data("min")
427 for qp in $(".subject .question-piece", question)
428 v = @get_value_optional_list qp, 'solution'
433 draggables = mandatory.count(optional)[0...minimum]
435 draggables = mandatory
437 $pr = $(".predicate [data-predicate=" + m + "]", question)
438 $ph = $pr.find ".placeholder:visible"
439 @draggable_move $(qp), $ph.eq(0), @multiple
443 class PrawdaFalsz extends Exercise
444 constructor: (element) ->
447 for qp in $(".question-piece", @element)
448 $(".true", qp).click (ev) =>
451 $(ev.target).closest(".question-piece").data("value", "true")
452 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
453 $(".false", qp).click (ev) =>
456 $(ev.target).closest(".question-piece").data("value", "false")
457 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
464 for qp in $(".question-piece", @element)
465 if $(qp).data("solution").toString() == $(qp).data("value")
474 return [good, bad, all]
478 for qp in $(".question-piece", @element)
479 if $(qp).data('solution') == true
480 $(".true", qp).click()
482 $(".false", qp).click()
490 uporzadkuj: Uporzadkuj
493 przyporzadkuj: Przyporzadkuj
494 prawdafalsz: PrawdaFalsz
497 cls = es[$(ele).attr('data-type')]
502 'EduModule': EduModule
507 $(document).ready () ->
508 new EduModule($("#book-text"))
510 $(".exercise").each (i, el) ->