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) =>
129 $($added.data('original')).removeClass('disabled').draggable('enable')
131 if $added.is(".add-li")
132 $added = $added.closest('li')
133 $added.prev(".placeholder:not(.multiple)").show()
137 ## XXX co z issortable?
138 dragging: (ismultiple, issortable) ->
139 $(".question", @element).each (i, question) =>
145 $(".draggable", question).draggable(draggable_opts)
147 $(".placeholder", question).droppable
148 accept: (draggable) ->
149 $draggable = $(draggable)
152 if not $draggable.is(".draggable")
156 is_accepted= self.draggable_accept $draggable, $(this)
159 $(this).addClass 'accepting'
161 $(this).removeClass 'accepting'
165 $(ev.target).removeClass 'accepting dragover'
167 @draggable_move $(ui.draggable), $(ev.target), ismultiple
169 # $added = $(ui.draggable).clone()
170 # $added.data("original", ui.draggable)
172 # $(ui.draggable).addClass('disabled').draggable('disable')
174 # $(ev.target).after(added)
175 # if not $(ev.target).hasClass('multiple')
176 # $(ev.target).hide()
177 # $added.append('<span class="remove">x</span>')
178 # $('.remove', added).click (ev) =>
179 # $added.prev(".placeholder:not(.multiple)").show()
181 # $added.data('original').removeClass('disabled').draggable('enable')
185 $(ev.target).addClass 'dragover'
189 $(ev.target).removeClass 'dragover'
193 class Wybor extends Exercise
194 constructor: (element) ->
196 $(".question-piece input", element).change(@retry);
199 check_question: (question) ->
202 solution = @get_value_list(question, 'solution')
203 $(".question-piece", question).each (i, qpiece) =>
204 piece_no = $(qpiece).attr 'data-no'
205 piece_name = $(qpiece).attr 'data-name'
207 should_be_checked = solution.indexOf(piece_name) >= 0
209 should_be_checked = solution.indexOf(piece_no) >= 0
210 is_checked = $("input", qpiece).is(":checked")
218 @piece_correct qpiece
220 @piece_incorrect qpiece
222 $(qpiece).removeClass("correct,incorrect")
226 solve_question: (question) ->
227 solution = @get_value_list(question, 'solution')
228 $(".question-piece", question).each (i, qpiece) =>
229 piece_no = $(qpiece).attr 'data-no'
230 piece_name = $(qpiece).attr 'data-name'
232 should_be_checked = solution.indexOf(piece_name) >= 0
234 should_be_checked = solution.indexOf(piece_no) >= 0
235 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
236 $("input[type=checkbox],input[type=radio]", qpiece).prop 'checked', should_be_checked
240 class Uporzadkuj extends Exercise
241 constructor: (element) ->
243 $('ol, ul', @element).sortable({ items: "> li", start: @retry })
245 check_question: (question) ->
246 positions = @get_value_list(question, 'original', true)
247 sorted = positions.sort()
248 pkts = $('.question-piece', question)
253 for pkt in [0...pkts.length]
255 if pkts.eq(pkt).data('pos') == sorted[pkt]
257 @piece_correct pkts.eq(pkt)
259 @piece_incorrect pkts.eq(pkt)
260 return [correct, all]
262 solve_question: (question) ->
263 positions = @get_value_list(question, 'original', true)
264 sorted = positions.sort()
265 pkts = $('.question-piece', question)
273 parent = pkts.eq(0).parent()
278 # XXX propozycje="1/0"
279 class Luki extends Exercise
280 constructor: (element) ->
282 @dragging false, false
287 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
288 $placeholder = $(qpiece).prev(".placeholder")
289 if $placeholder.data('solution') == $(qpiece).data('no')
290 @piece_correct qpiece
293 @piece_incorrect qpiece
296 @show_score [correct, all]
298 solve_question: (question) ->
299 $(".placeholder", question).each (i, placeholder) =>
300 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
301 @draggable_move $qp, $(placeholder), false
304 class Zastap extends Exercise
305 constructor: (element) ->
307 $(".paragraph", @element).each (i, par) =>
308 @wrap_words $(par), $('<span class="placeholder zastap"/>')
309 @dragging false, false
315 $(".paragraph", @element).each (i, par) =>
316 $(".placeholder", par).each (j, qpiece) =>
318 $dragged = $qp.next(".draggable")
319 if $qp.data("solution")
320 if $dragged and $qp.data("solution") == $dragged.data("no")
321 @piece_correct $dragged
323 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
327 @show_score [correct, all]
331 $(".paragraph", @element).each (i, par) =>
332 $(".placeholder[data-solution]", par).each (j, qpiece) =>
334 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
335 @draggable_move $dr, $qp, false
338 wrap_words: (element, wrapper) ->
339 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
340 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
341 ignore = /^[ \t.,:;()]+/
343 insertWrapped = (txt, elem) ->
345 $(document.createTextNode(txt))
346 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
348 for j in [element.get(0).childNodes.length-1..0]
349 chld = element.get(0).childNodes[j]
350 if chld.nodeType == document.TEXT_NODE
351 len = chld.textContent.length
355 space = ignore.exec(chld.textContent.substr(i))
358 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
360 $(document.createTextNode(space[0])).insertBefore(chld)
366 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
370 class Przyporzadkuj extends Exercise
372 for qp in $(".question-piece", @element)
373 if $(qp).data('solution').split(/[ ,]+/).length > 1
377 constructor: (element) ->
380 @multiple = @is_multiple()
382 @dragging @multiple, true
384 draggable_equal: (d1, d2) ->
385 return d1.data("no") == d2.data("no")
388 check_question: (question) ->
389 # subjects placed in predicates
390 minimum = $(question).data("minimum")
394 all = $(".subjects .question-piece", question).length
396 for pred in $(".predicate [data-predicate]", question)
397 pn = $(pred).attr('data-predicate')
401 for qp in $(".question-piece", pred)
402 v = @get_value_optional_list qp, 'solution'
406 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
414 solve_question: (question) ->
415 minimum = $(question).data("min")
417 for qp in $(".subject .question-piece", question)
418 v = @get_value_optional_list qp, 'solution'
423 draggables = mandatory.count(optional)[0...minimum]
425 draggables = mandatory
427 $pr = $(".predicate [data-predicate=" + m + "]", question)
428 $ph = $pr.find ".placeholder:visible"
429 @draggable_move $(qp), $ph, @multiple
433 class PrawdaFalsz extends Exercise
434 constructor: (element) ->
437 for qp in $(".question-piece", @element)
438 $(".true", qp).click (ev) ->
441 $(this).closest(".question-piece").data("value", "true")
442 $(this).addClass('chosen').siblings('a').removeClass('chosen')
443 $(".false", qp).click (ev) ->
446 $(this).closest(".question-piece").data("value", "false")
447 $(this).addClass('chosen').siblings('a').removeClass('chosen')
453 for qp in $(".question-piece", @element)
454 if $(qp).data("solution").toString() == $(qp).data("value")
466 for qp in $(".question-piece", @element)
467 if $(qp).data('solution') == true
468 $(".true", qp).click()
470 $(".false", qp).click()
478 uporzadkuj: Uporzadkuj
481 przyporzadkuj: Przyporzadkuj
482 prawdafalsz: PrawdaFalsz
485 cls = es[$(ele).attr('data-type')]
490 'EduModule': EduModule
495 $(document).ready () ->
496 new EduModule($("#book-text"))
498 $(".exercise").each (i, el) ->