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 $msg = $(".message", @element)
101 $msg.text("Wynik: #{score[0]} / #{score[1]}")
102 if score[0] == score[1]
103 $msg.addClass("maxscore")
105 $msg.removeClass("maxscore")
108 draggable_equal: ($draggable1, $draggable2) ->
111 draggable_accept: ($draggable, $droppable) ->
112 dropped = $droppable.closest("ul, ol").find(".draggable")
114 if @draggable_equal $draggable, $(d)
118 draggable_move: ($draggable, $placeholder, ismultiple) ->
119 $added = $draggable.clone()
120 $added.data("original", $draggable.get(0))
122 $draggable.addClass('disabled').draggable('disable')
124 $placeholder.after($added)
125 if not $placeholder.hasClass('multiple')
127 if $added.is(".add-li")
130 $added.append('<span class="remove">x</span><div class="clr"></div>')
131 $('.remove', $added).click (ev) =>
134 $($added.data('original')).removeClass('disabled').draggable('enable')
136 if $added.is(".add-li")
137 $added = $added.closest('li')
138 $added.prev(".placeholder:not(.multiple)").show()
142 ## XXX co z issortable?
143 dragging: (ismultiple, issortable) ->
144 $(".question", @element).each (i, question) =>
150 $(".draggable", question).draggable(draggable_opts)
152 $(".placeholder", question).droppable
153 accept: (draggable) ->
154 $draggable = $(draggable)
157 if not $draggable.is(".draggable")
161 is_accepted= self.draggable_accept $draggable, $(this)
164 $(this).addClass 'accepting'
166 $(this).removeClass 'accepting'
170 $(ev.target).removeClass 'accepting dragover'
172 @draggable_move $(ui.draggable), $(ev.target), ismultiple
174 # $added = $(ui.draggable).clone()
175 # $added.data("original", ui.draggable)
177 # $(ui.draggable).addClass('disabled').draggable('disable')
179 # $(ev.target).after(added)
180 # if not $(ev.target).hasClass('multiple')
181 # $(ev.target).hide()
182 # $added.append('<span class="remove">x</span>')
183 # $('.remove', added).click (ev) =>
184 # $added.prev(".placeholder:not(.multiple)").show()
186 # $added.data('original').removeClass('disabled').draggable('enable')
190 $(ev.target).addClass 'dragover'
194 $(ev.target).removeClass 'dragover'
198 class Wybor extends Exercise
199 constructor: (element) ->
201 $(".question-piece input", element).change(@retry);
204 check_question: (question) ->
207 solution = @get_value_list(question, 'solution')
208 $(".question-piece", question).each (i, qpiece) =>
209 piece_no = $(qpiece).attr 'data-no'
210 piece_name = $(qpiece).attr 'data-name'
212 should_be_checked = solution.indexOf(piece_name) >= 0
214 should_be_checked = solution.indexOf(piece_no) >= 0
215 is_checked = $("input", qpiece).is(":checked")
223 @piece_correct qpiece
225 @piece_incorrect qpiece
227 $(qpiece).removeClass("correct,incorrect")
231 solve_question: (question) ->
232 solution = @get_value_list(question, 'solution')
233 $(".question-piece", question).each (i, qpiece) =>
234 piece_no = $(qpiece).attr 'data-no'
235 piece_name = $(qpiece).attr 'data-name'
237 should_be_checked = solution.indexOf(piece_name) >= 0
239 should_be_checked = solution.indexOf(piece_no) >= 0
240 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
241 $("input[type=checkbox],input[type=radio]", qpiece).prop 'checked', should_be_checked
245 class Uporzadkuj extends Exercise
246 constructor: (element) ->
248 $('ol, ul', @element).sortable({ items: "> li", start: @retry })
250 check_question: (question) ->
251 positions = @get_value_list(question, 'original', true)
252 sorted = positions.sort()
253 pkts = $('.question-piece', question)
258 for pkt in [0...pkts.length]
260 if pkts.eq(pkt).data('pos') == sorted[pkt]
262 @piece_correct pkts.eq(pkt)
264 @piece_incorrect pkts.eq(pkt)
265 return [correct, all]
267 solve_question: (question) ->
268 positions = @get_value_list(question, 'original', true)
269 sorted = positions.sort()
270 pkts = $('.question-piece', question)
278 parent = pkts.eq(0).parent()
283 # XXX propozycje="1/0"
284 class Luki extends Exercise
285 constructor: (element) ->
287 @dragging false, false
292 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
293 $placeholder = $(qpiece).prev(".placeholder")
294 if $placeholder.data('solution') == $(qpiece).data('no')
295 @piece_correct qpiece
298 @piece_incorrect qpiece
301 @show_score [correct, all]
303 solve_question: (question) ->
304 $(".placeholder", question).each (i, placeholder) =>
305 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
306 @draggable_move $qp, $(placeholder), false
309 class Zastap extends Exercise
310 constructor: (element) ->
312 $(".paragraph", @element).each (i, par) =>
313 @wrap_words $(par), $('<span class="placeholder zastap"/>')
314 @dragging false, false
320 $(".paragraph", @element).each (i, par) =>
321 $(".placeholder", par).each (j, qpiece) =>
323 $dragged = $qp.next(".draggable")
324 if $qp.data("solution")
325 if $dragged and $qp.data("solution") == $dragged.data("no")
326 @piece_correct $dragged
328 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
332 @show_score [correct, all]
336 $(".paragraph", @element).each (i, par) =>
337 $(".placeholder[data-solution]", par).each (j, qpiece) =>
339 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
340 @draggable_move $dr, $qp, false
343 wrap_words: (element, wrapper) ->
344 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
345 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
346 ignore = /^[ \t.,:;()]+/
348 insertWrapped = (txt, elem) ->
350 $(document.createTextNode(txt))
351 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
353 for j in [element.get(0).childNodes.length-1..0]
354 chld = element.get(0).childNodes[j]
355 if chld.nodeType == document.TEXT_NODE
356 len = chld.textContent.length
360 space = ignore.exec(chld.textContent.substr(i))
363 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
365 $(document.createTextNode(space[0])).insertBefore(chld)
371 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
375 class Przyporzadkuj extends Exercise
377 for qp in $(".question-piece", @element)
378 if $(qp).data('solution').split(/[ ,]+/).length > 1
382 constructor: (element) ->
385 @multiple = @is_multiple()
387 @dragging @multiple, true
389 draggable_equal: (d1, d2) ->
390 return d1.data("no") == d2.data("no")
393 check_question: (question) ->
394 # subjects placed in predicates
395 minimum = $(question).data("minimum")
399 all = $(".subjects .question-piece", question).length
401 for pred in $(".predicate [data-predicate]", question)
402 pn = $(pred).attr('data-predicate')
406 for qp in $(".question-piece", pred)
407 v = @get_value_optional_list qp, 'solution'
411 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
419 solve_question: (question) ->
420 minimum = $(question).data("min")
422 for qp in $(".subject .question-piece", question)
423 v = @get_value_optional_list qp, 'solution'
428 draggables = mandatory.count(optional)[0...minimum]
430 draggables = mandatory
432 $pr = $(".predicate [data-predicate=" + m + "]", question)
433 $ph = $pr.find ".placeholder:visible"
434 @draggable_move $(qp), $ph.eq(0), @multiple
438 class PrawdaFalsz extends Exercise
439 constructor: (element) ->
442 for qp in $(".question-piece", @element)
443 $(".true", qp).click (ev) =>
446 $(ev.target).closest(".question-piece").data("value", "true")
447 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
448 $(".false", qp).click (ev) =>
451 $(ev.target).closest(".question-piece").data("value", "false")
452 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
458 for qp in $(".question-piece", @element)
459 if $(qp).data("solution").toString() == $(qp).data("value")
471 for qp in $(".question-piece", @element)
472 if $(qp).data('solution') == true
473 $(".true", qp).click()
475 $(".false", qp).click()
483 uporzadkuj: Uporzadkuj
486 przyporzadkuj: Przyporzadkuj
487 prawdafalsz: PrawdaFalsz
490 cls = es[$(ele).attr('data-type')]
495 'EduModule': EduModule
500 $(document).ready () ->
501 new EduModule($("#book-text"))
503 $(".exercise").each (i, el) ->