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) =>
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) ->
66 $(".question", @element).each (i, question) =>
67 @solve_question question
69 # Parses a list of values, separated by space or comma.
70 # The list is read from data attribute of elem using data_key
71 # Returns a list with elements
72 # eg.: things_i_need: "house bike tv playstation"
73 # yields ["house", "bike", "tv", "playstation"]
74 # If optional numbers argument is true, returns list of numbers
76 get_value_list: (elem, data_key, numbers) ->
77 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
79 vl = vl.map((x) -> parseInt(x))
82 # Parses a list of values, separated by space or comma.
83 # The list is read from data attribute of elem using data_key
84 # Returns a 2-element list with mandatory and optional
85 # items. optional items are marked with a question mark on the end
86 # eg.: things_i_need: "house bike tv? playstation?"
87 # yields [[ "house", "bike"], ["tv", "playstation"]]
88 get_value_optional_list: (elem, data_key) ->
89 vals = @get_value_list(elem, data_key)
94 opt.push v.slice(0, -1)
99 show_score: (score) ->
100 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
103 draggable_equal: ($draggable1, $draggable2) ->
106 draggable_accept: ($draggable, $droppable) ->
107 dropped = $droppable.closest("ul, ol").find(".draggable")
109 if @draggable_equal $draggable, $(d)
113 draggable_move: ($draggable, $placeholder, ismultiple) ->
114 $added = $draggable.clone()
115 $added.data("original", $draggable.get(0))
117 $draggable.addClass('disabled').draggable('disable')
119 $placeholder.after($added)
120 if not $placeholder.hasClass('multiple')
122 if $added.is(".add-li")
125 $added.append('<span class="remove">x</span><div class="clr"></div>')
126 $('.remove', $added).click (ev) =>
128 $($added.data('original')).removeClass('disabled').draggable('enable')
130 if $added.is(".add-li")
131 $added = $added.closest('li')
132 $added.prev(".placeholder:not(.multiple)").show()
136 ## XXX co z issortable?
137 dragging: (ismultiple, issortable) ->
138 $(".question", @element).each (i, question) =>
144 $(".draggable", question).draggable(draggable_opts)
146 $(".placeholder", question).droppable
147 accept: (draggable) ->
148 $draggable = $(draggable)
151 if not $draggable.is(".draggable")
155 is_accepted= self.draggable_accept $draggable, $(this)
158 $(this).addClass 'accepting'
160 $(this).removeClass 'accepting'
164 $(ev.target).removeClass 'accepting dragover'
166 @draggable_move $(ui.draggable), $(ev.target), ismultiple
168 # $added = $(ui.draggable).clone()
169 # $added.data("original", ui.draggable)
171 # $(ui.draggable).addClass('disabled').draggable('disable')
173 # $(ev.target).after(added)
174 # if not $(ev.target).hasClass('multiple')
175 # $(ev.target).hide()
176 # $added.append('<span class="remove">x</span>')
177 # $('.remove', added).click (ev) =>
178 # $added.prev(".placeholder:not(.multiple)").show()
180 # $added.data('original').removeClass('disabled').draggable('enable')
184 $(ev.target).addClass 'dragover'
188 $(ev.target).removeClass 'dragover'
192 class Wybor extends Exercise
193 constructor: (element) ->
195 $(".question-piece input", element).change(@retry);
198 check_question: (question) ->
201 solution = @get_value_list(question, 'solution')
202 $(".question-piece", question).each (i, qpiece) =>
203 piece_no = $(qpiece).attr 'data-no'
204 piece_name = $(qpiece).attr 'data-name'
206 should_be_checked = solution.indexOf(piece_name) >= 0
208 should_be_checked = solution.indexOf(piece_no) >= 0
209 is_checked = $("input", qpiece).is(":checked")
217 @piece_correct qpiece
219 @piece_incorrect qpiece
221 $(qpiece).removeClass("correct,incorrect")
225 solve_question: (question) ->
226 solution = @get_value_list(question, 'solution')
227 $(".question-piece", question).each (i, qpiece) =>
228 piece_no = $(qpiece).attr 'data-no'
229 piece_name = $(qpiece).attr 'data-name'
231 should_be_checked = solution.indexOf(piece_name) >= 0
233 should_be_checked = solution.indexOf(piece_no) >= 0
234 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
235 $("input[type=checkbox],input[type=radio]", qpiece).prop 'checked', should_be_checked
239 class Uporzadkuj extends Exercise
240 constructor: (element) ->
242 $('ol, ul', @element).sortable({ items: "> li", start: @retry })
244 check_question: (question) ->
245 positions = @get_value_list(question, 'original', true)
246 sorted = positions.sort()
247 pkts = $('.question-piece', question)
252 for pkt in [0...pkts.length]
254 if pkts.eq(pkt).data('pos') == sorted[pkt]
256 @piece_correct pkts.eq(pkt)
258 @piece_incorrect pkts.eq(pkt)
259 return [correct, all]
261 solve_question: (question) ->
262 positions = @get_value_list(question, 'original', true)
263 sorted = positions.sort()
264 pkts = $('.question-piece', question)
272 parent = pkts.eq(0).parent()
277 # XXX propozycje="1/0"
278 class Luki extends Exercise
279 constructor: (element) ->
281 @dragging false, false
286 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
287 $placeholder = $(qpiece).prev(".placeholder")
288 if $placeholder.data('solution') == $(qpiece).data('no')
289 @piece_correct qpiece
292 @piece_incorrect qpiece
295 @show_score [correct, all]
297 solve_question: (question) ->
298 $(".placeholder", question).each (i, placeholder) =>
299 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
300 @draggable_move $qp, $(placeholder), false
303 class Zastap extends Exercise
304 constructor: (element) ->
306 $(".paragraph", @element).each (i, par) =>
307 @wrap_words $(par), $('<span class="placeholder zastap"/>')
308 @dragging false, false
314 $(".paragraph", @element).each (i, par) =>
315 $(".placeholder", par).each (j, qpiece) =>
317 $dragged = $qp.next(".draggable")
318 if $qp.data("solution")
319 if $dragged and $qp.data("solution") == $dragged.data("no")
320 @piece_correct $dragged
322 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
326 @show_score [correct, all]
330 $(".paragraph", @element).each (i, par) =>
331 $(".placeholder[data-solution]", par).each (j, qpiece) =>
333 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
334 @draggable_move $dr, $qp, false
337 wrap_words: (element, wrapper) ->
338 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
339 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
340 ignore = /^[ \t.,:;()]+/
342 insertWrapped = (txt, elem) ->
344 $(document.createTextNode(txt))
345 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
347 for j in [element.get(0).childNodes.length-1..0]
348 chld = element.get(0).childNodes[j]
349 if chld.nodeType == document.TEXT_NODE
350 len = chld.textContent.length
354 space = ignore.exec(chld.textContent.substr(i))
357 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
359 $(document.createTextNode(space[0])).insertBefore(chld)
365 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
369 class Przyporzadkuj extends Exercise
371 for qp in $(".question-piece", @element)
372 if $(qp).data('solution').split(/[ ,]+/).length > 1
376 constructor: (element) ->
379 @multiple = @is_multiple()
381 @dragging @multiple, true
383 draggable_equal: (d1, d2) ->
384 return d1.data("no") == d2.data("no")
387 check_question: (question) ->
388 # subjects placed in predicates
389 minimum = $(question).data("minimum")
393 all = $(".subjects .question-piece", question).length
395 for pred in $(".predicate [data-predicate]", question)
396 pn = $(pred).attr('data-predicate')
400 for qp in $(".question-piece", pred)
401 v = @get_value_optional_list qp, 'solution'
405 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
413 solve_question: (question) ->
414 minimum = $(question).data("min")
416 for qp in $(".subject .question-piece", question)
417 v = @get_value_optional_list qp, 'solution'
422 draggables = mandatory.count(optional)[0...minimum]
424 draggables = mandatory
426 $pr = $(".predicate [data-predicate=" + m + "]", question)
427 $ph = $pr.find ".placeholder:visible"
428 @draggable_move $(qp), $ph, @multiple
432 class PrawdaFalsz extends Exercise
433 constructor: (element) ->
436 for qp in $(".question-piece", @element)
437 $(".true", qp).click (ev) ->
440 $(this).closest(".question-piece").data("value", "true")
441 $(this).addClass('chosen').siblings('a').removeClass('chosen')
442 $(".false", qp).click (ev) ->
445 $(this).closest(".question-piece").data("value", "false")
446 $(this).addClass('chosen').siblings('a').removeClass('chosen')
452 for qp in $(".question-piece", @element)
453 if $(qp).data("solution").toString() == $(qp).data("value")
465 for qp in $(".question-piece", @element)
466 if $(qp).data('solution') == true
467 $(".true", qp).click()
469 $(".false", qp).click()
477 uporzadkuj: Uporzadkuj
480 przyporzadkuj: Przyporzadkuj
481 prawdafalsz: PrawdaFalsz
484 cls = es[$(ele).attr('data-type')]
489 'EduModule': EduModule
494 $(document).ready () ->
495 new EduModule($("#book-text"))
497 $(".exercise").each (i, el) ->