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 $(ev.target).next(".retry").show()
30 $(".retry", @element).click (ev) =>
31 $(".correct, .incorrect", @element).removeClass("correct incorrect")
32 $(ev.target).prev(".check").show()
34 $('.solutions', @element).click =>
36 $(".comment", @element).show()
37 $('.reset', @element).click =>
41 $(@element).html($(@element).data('exercise-html'))
44 piece_correct: (qpiece) ->
45 $(qpiece).removeClass('incorrect').addClass('correct')
47 piece_incorrect: (qpiece) ->
48 $(qpiece).removeClass('correct').addClass('incorrect')
52 $(".question", @element).each (i, question) =>
53 scores.push(@check_question question)
56 $.each scores, (i, s) ->
63 $(".question", @element).each (i, question) =>
64 @solve_question question
66 # Parses a list of values, separated by space or comma.
67 # The list is read from data attribute of elem using data_key
68 # Returns a list with elements
69 # eg.: things_i_need: "house bike tv playstation"
70 # yields ["house", "bike", "tv", "playstation"]
71 # If optional numbers argument is true, returns list of numbers
73 get_value_list: (elem, data_key, numbers) ->
74 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
76 vl = vl.map((x) -> parseInt(x))
79 # Parses a list of values, separated by space or comma.
80 # The list is read from data attribute of elem using data_key
81 # Returns a 2-element list with mandatory and optional
82 # items. optional items are marked with a question mark on the end
83 # eg.: things_i_need: "house bike tv? playstation?"
84 # yields [[ "house", "bike"], ["tv", "playstation"]]
85 get_value_optional_list: (elem, data_key) ->
86 vals = @get_value_list(elem, data_key)
91 opt.push v.slice(0, -1)
96 show_score: (score) ->
97 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
100 draggable_equal: ($draggable1, $draggable2) ->
103 draggable_accept: ($draggable, $droppable) ->
104 dropped = $droppable.closest("ul, ol").find(".draggable")
106 if @draggable_equal $draggable, $(d)
110 draggable_move: ($draggable, $placeholder, ismultiple) ->
111 $added = $draggable.clone()
112 $added.data("original", $draggable.get(0))
114 $draggable.addClass('disabled').draggable('disable')
116 $placeholder.after($added)
117 if not $placeholder.hasClass('multiple')
119 if $added.is(".add-li")
122 $added.append('<span class="remove">x</span>')
123 $('.remove', $added).click (ev) =>
125 $($added.data('original')).removeClass('disabled').draggable('enable')
127 if $added.is(".add-li")
128 $added = $added.closest('li')
129 $added.prev(".placeholder:not(.multiple)").show()
133 ## XXX co z issortable?
134 dragging: (ismultiple, issortable) ->
135 $(".question", @element).each (i, question) =>
140 $(".draggable", question).draggable(draggable_opts)
142 $(".placeholder", question).droppable
143 accept: (draggable) ->
144 $draggable = $(draggable)
147 if not $draggable.is(".draggable")
151 is_accepted= self.draggable_accept $draggable, $(this)
154 $(this).addClass 'accepting'
156 $(this).removeClass 'accepting'
160 $(ev.target).removeClass 'accepting dragover'
162 @draggable_move $(ui.draggable), $(ev.target), ismultiple
164 # $added = $(ui.draggable).clone()
165 # $added.data("original", ui.draggable)
167 # $(ui.draggable).addClass('disabled').draggable('disable')
169 # $(ev.target).after(added)
170 # if not $(ev.target).hasClass('multiple')
171 # $(ev.target).hide()
172 # $added.append('<span class="remove">x</span>')
173 # $('.remove', added).click (ev) =>
174 # $added.prev(".placeholder:not(.multiple)").show()
176 # $added.data('original').removeClass('disabled').draggable('enable')
180 $(ev.target).addClass 'dragover'
184 $(ev.target).removeClass 'dragover'
188 class Wybor extends Exercise
189 constructor: (element) ->
193 check_question: (question) ->
196 solution = @get_value_list(question, 'solution')
197 $(".question-piece", question).each (i, qpiece) =>
198 piece_no = $(qpiece).attr 'data-no'
199 piece_name = $(qpiece).attr 'data-name'
201 should_be_checked = solution.indexOf(piece_name) >= 0
203 should_be_checked = solution.indexOf(piece_no) >= 0
204 is_checked = $("input", qpiece).is(":checked")
212 @piece_correct qpiece
214 @piece_incorrect qpiece
216 $(qpiece).removeClass("correct,incorrect")
220 solve_question: (question) ->
221 solution = @get_value_list(question, 'solution')
222 $(".question-piece", question).each (i, qpiece) =>
223 piece_no = $(qpiece).attr 'data-no'
224 piece_name = $(qpiece).attr 'data-name'
226 should_be_checked = solution.indexOf(piece_name) >= 0
228 should_be_checked = solution.indexOf(piece_no) >= 0
229 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
230 $("input[type=checkbox]", qpiece).prop 'checked', should_be_checked
234 class Uporzadkuj extends Exercise
235 constructor: (element) ->
237 $('ol, ul', @element).sortable({ items: "> li" })
239 check_question: (question) ->
240 positions = @get_value_list(question, 'original', true)
241 sorted = positions.sort()
242 pkts = $('.question-piece', question)
247 for pkt in [0...pkts.length]
249 if pkts.eq(pkt).data('pos') == sorted[pkt]
251 @piece_correct pkts.eq(pkt)
253 @piece_incorrect pkts.eq(pkt)
254 return [correct, all]
256 solve_question: (question) ->
257 positions = @get_value_list(question, 'original', true)
258 sorted = positions.sort()
259 pkts = $('.question-piece', question)
267 parent = pkts.eq(0).parent()
272 # XXX propozycje="1/0"
273 class Luki extends Exercise
274 constructor: (element) ->
276 @dragging false, false
281 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
282 $placeholder = $(qpiece).prev(".placeholder")
283 if $placeholder.data('solution') == $(qpiece).data('no')
284 @piece_correct qpiece
287 @piece_incorrect qpiece
290 @show_score [correct, all]
292 solve_question: (question) ->
293 $(".placeholder", question).each (i, placeholder) =>
294 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
295 @draggable_move $qp, $(placeholder), false
298 class Zastap extends Exercise
299 constructor: (element) ->
301 $(".paragraph", @element).each (i, par) =>
302 @wrap_words $(par), $('<span class="placeholder zastap"/>')
303 @dragging false, false
309 $(".paragraph", @element).each (i, par) =>
310 $(".placeholder", par).each (j, qpiece) =>
312 $dragged = $qp.next(".draggable")
313 if $qp.data("solution")
314 if $dragged and $qp.data("solution") == $dragged.data("no")
315 @piece_correct $dragged
317 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
321 @show_score [correct, all]
325 $(".paragraph", @element).each (i, par) =>
326 $(".placeholder[data-solution]", par).each (j, qpiece) =>
328 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
329 @draggable_move $dr, $qp, false
332 wrap_words: (element, wrapper) ->
333 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
334 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
335 ignore = /^[ \t.,:;()]+/
337 insertWrapped = (txt, elem) ->
339 $(document.createTextNode(txt))
340 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
342 for j in [element.get(0).childNodes.length-1..0]
343 chld = element.get(0).childNodes[j]
344 if chld.nodeType == document.TEXT_NODE
345 len = chld.textContent.length
349 space = ignore.exec(chld.textContent.substr(i))
352 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
354 $(document.createTextNode(space[0])).insertBefore(chld)
360 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
364 class Przyporzadkuj extends Exercise
366 for qp in $(".question-piece", @element)
367 if $(qp).data('solution').split(/[ ,]+/).length > 1
371 constructor: (element) ->
374 @multiple = @is_multiple()
376 @dragging @multiple, true
378 draggable_equal: (d1, d2) ->
379 return d1.data("no") == d2.data("no")
382 check_question: (question) ->
383 # subjects placed in predicates
384 minimum = $(question).data("minimum")
388 all = $(".subjects .question-piece", question).length
390 for pred in $(".predicate [data-predicate]", question)
391 pn = $(pred).attr('data-predicate')
395 for qp in $(".question-piece", pred)
396 v = @get_value_optional_list qp, 'solution'
400 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
408 solve_question: (question) ->
409 minimum = $(question).data("min")
411 for qp in $(".subject .question-piece", question)
412 v = @get_value_optional_list qp, 'solution'
417 draggables = mandatory.count(optional)[0...minimum]
419 draggables = mandatory
421 $pr = $(".predicate [data-predicate=" + m + "]", question)
422 $ph = $pr.find ".placeholder:visible"
423 @draggable_move $(qp), $ph, @multiple
427 class PrawdaFalsz extends Exercise
428 constructor: (element) ->
431 for qp in $(".question-piece", @element)
432 $(".true", qp).click (ev) ->
434 $(this).closest(".question-piece").data("value", "true")
435 $(this).addClass('chosen').siblings('a').removeClass('chosen')
436 $(".false", qp).click (ev) ->
438 $(this).closest(".question-piece").data("value", "false")
439 $(this).addClass('chosen').siblings('a').removeClass('chosen')
445 for qp in $(".question-piece", @element)
446 if $(qp).data("solution").toString() == $(qp).data("value")
458 for qp in $(".question-piece", @element)
459 if $(qp).data('solution') == 'true'
460 $(".true", qp).click()
462 $(".false", qp).click()
470 uporzadkuj: Uporzadkuj
473 przyporzadkuj: Przyporzadkuj
474 prawdafalsz: PrawdaFalsz
477 cls = es[$(ele).attr('data-type')]
482 'EduModule': EduModule
487 $(document).ready () ->
488 new EduModule($("#book-text"))
490 $(".exercise").each (i, el) ->