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) ->
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
290 all = $(".placeholder", @element).length
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
300 @show_score [correct, all]
302 solve_question: (question) ->
303 $(".placeholder", question).each (i, placeholder) =>
304 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
305 @draggable_move $qp, $(placeholder), false
308 class Zastap extends Exercise
309 constructor: (element) ->
311 $(".paragraph", @element).each (i, par) =>
312 @wrap_words $(par), $('<span class="placeholder zastap"/>')
313 @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, 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")
398 all = $(".subject .question-piece", question).length
400 for pred in $(".predicate [data-predicate]", question)
401 pn = $(pred).attr('data-predicate')
405 for qp in $(".question-piece", pred)
406 v = @get_value_optional_list qp, 'solution'
410 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
418 solve_question: (question) ->
419 minimum = $(question).data("min")
421 for qp in $(".subject .question-piece", question)
422 v = @get_value_optional_list qp, 'solution'
427 draggables = mandatory.count(optional)[0...minimum]
429 draggables = mandatory
431 $pr = $(".predicate [data-predicate=" + m + "]", question)
432 $ph = $pr.find ".placeholder:visible"
433 @draggable_move $(qp), $ph.eq(0), @multiple
437 class PrawdaFalsz extends Exercise
438 constructor: (element) ->
441 for qp in $(".question-piece", @element)
442 $(".true", qp).click (ev) =>
445 $(ev.target).closest(".question-piece").data("value", "true")
446 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
447 $(".false", qp).click (ev) =>
450 $(ev.target).closest(".question-piece").data("value", "false")
451 $(ev.target).addClass('chosen').siblings('a').removeClass('chosen')
457 for qp in $(".question-piece", @element)
458 if $(qp).data("solution").toString() == $(qp).data("value")
470 for qp in $(".question-piece", @element)
471 if $(qp).data('solution') == true
472 $(".true", qp).click()
474 $(".false", qp).click()
482 uporzadkuj: Uporzadkuj
485 przyporzadkuj: Przyporzadkuj
486 prawdafalsz: PrawdaFalsz
489 cls = es[$(ele).attr('data-type')]
494 'EduModule': EduModule
499 $(document).ready () ->
500 new EduModule($("#book-text"))
502 $(".exercise").each (i, el) ->