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 Excercise extends Binding
21 constructor: (element) ->
22 super 'excercise', element
23 # just save the html to reset the excercise
24 $(@element).data("excercise-html", $(@element).html())
26 $(".check", @element).click =>
28 $('.solutions', @element).click =>
30 $('.reset', @element).click =>
34 $(@element).html($(@element).data('excercise-html'))
37 piece_correct: (qpiece) ->
38 $(qpiece).removeClass('incorrect').addClass('correct')
40 piece_incorrect: (qpiece) ->
41 $(qpiece).removeClass('correct').addClass('incorrect')
45 $(".question", @element).each (i, question) =>
46 scores.push(@check_question question)
49 $.each scores, (i, s) ->
56 $(".question", @element).each (i, question) =>
57 @solve_question question
59 # Parses a list of values, separated by space or comma.
60 # The list is read from data attribute of elem using data_key
61 # Returns a list with elements
62 # eg.: things_i_need: "house bike tv playstation"
63 # yields ["house", "bike", "tv", "playstation"]
64 # If optional numbers argument is true, returns list of numbers
66 get_value_list: (elem, data_key, numbers) ->
67 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
69 vl = vl.map((x) -> parseInt(x))
72 # Parses a list of values, separated by space or comma.
73 # The list is read from data attribute of elem using data_key
74 # Returns a 2-element list with mandatory and optional
75 # items. optional items are marked with a question mark on the end
76 # eg.: things_i_need: "house bike tv? playstation?"
77 # yields [[ "house", "bike"], ["tv", "playstation"]]
78 get_value_optional_list: (elem, data_key) ->
79 vals = @get_value_list(elem, data_key)
84 opt.push v.slice(0, -1)
89 show_score: (score) ->
90 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
93 draggable_equal: ($draggable1, $draggable2) ->
96 draggable_accept: ($draggable, $droppable) ->
97 dropped = $droppable.closest("ul, ol").find(".draggable")
99 if @draggable_equal $draggable, $(d)
103 draggable_move: ($draggable, $placeholder, ismultiple) ->
104 $added = $draggable.clone()
105 $added.data("original", $draggable.get(0))
107 $draggable.addClass('disabled').draggable('disable')
109 $placeholder.after($added)
110 if not $placeholder.hasClass('multiple')
112 $added.append('<span class="remove">x</span>')
113 $('.remove', $added).click (ev) =>
114 $added.prev(".placeholder:not(.multiple)").show()
116 $($added.data('original')).removeClass('disabled').draggable('enable')
119 ## XXX co z issortable?
120 dragging: (ismultiple, issortable) ->
121 $(".question", @element).each (i, question) =>
126 $(".draggable", question).draggable(draggable_opts)
128 $(".placeholder", question).droppable
129 accept: (draggable) ->
130 $draggable = $(draggable)
133 if not $draggable.is(".draggable")
137 is_accepted= self.draggable_accept $draggable, $(this)
140 $(this).addClass 'accepting'
142 $(this).removeClass 'accepting'
146 $(ev.target).removeClass 'accepting dragover'
148 @draggable_move $(ui.draggable), $(ev.target), ismultiple
150 # $added = $(ui.draggable).clone()
151 # $added.data("original", ui.draggable)
153 # $(ui.draggable).addClass('disabled').draggable('disable')
155 # $(ev.target).after(added)
156 # if not $(ev.target).hasClass('multiple')
157 # $(ev.target).hide()
158 # $added.append('<span class="remove">x</span>')
159 # $('.remove', added).click (ev) =>
160 # $added.prev(".placeholder:not(.multiple)").show()
162 # $added.data('original').removeClass('disabled').draggable('enable')
166 $(ev.target).addClass 'dragover'
170 $(ev.target).removeClass 'dragover'
174 class Wybor extends Excercise
175 constructor: (element) ->
179 check_question: (question) ->
182 solution = @get_value_list(question, 'solution')
183 $(".question-piece", question).each (i, qpiece) =>
184 piece_no = $(qpiece).attr 'data-no'
185 piece_name = $(qpiece).attr 'data-name'
187 should_be_checked = solution.indexOf(piece_name) >= 0
189 should_be_checked = solution.indexOf(piece_no) >= 0
190 is_checked = $("input", qpiece).is(":checked")
198 @piece_correct qpiece
200 @piece_incorrect qpiece
202 $(qpiece).removeClass("correct,incorrect")
206 solve_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 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
216 $("input[type=checkbox]", qpiece).prop 'checked', should_be_checked
220 class Uporzadkuj extends Excercise
221 constructor: (element) ->
223 $('ol, ul', @element).sortable({ items: "> li" })
225 check_question: (question) ->
226 positions = @get_value_list(question, 'original', true)
227 sorted = positions.sort()
228 pkts = $('.question-piece', question)
233 for pkt in [0...pkts.length]
235 if pkts.eq(pkt).data('pos') == sorted[pkt]
237 @piece_correct pkts.eq(pkt)
239 @piece_incorrect pkts.eq(pkt)
240 return [correct, all]
242 solve_question: (question) ->
243 positions = @get_value_list(question, 'original', true)
244 sorted = positions.sort()
245 pkts = $('.question-piece', question)
253 parent = pkts.eq(0).parent()
258 # XXX propozycje="1/0"
259 class Luki extends Excercise
260 constructor: (element) ->
262 @dragging false, false
267 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
268 $placeholder = $(qpiece).prev(".placeholder")
269 if $placeholder.data('solution') == $(qpiece).data('no')
270 @piece_correct qpiece
273 @piece_incorrect qpiece
276 @show_score [correct, all]
278 solve_question: (question) ->
279 $(".placeholder", question).each (i, placeholder) =>
280 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
281 @draggable_move $qp, $(placeholder), false
284 class Zastap extends Excercise
285 constructor: (element) ->
287 $(".paragraph", @element).each (i, par) =>
288 @wrap_words $(par), $('<span class="placeholder zastap"/>')
289 @dragging false, false
295 $(".paragraph", @element).each (i, par) =>
296 $(".placeholder", par).each (j, qpiece) =>
298 $dragged = $qp.next(".draggable")
299 if $qp.data("solution")
300 if $dragged and $qp.data("solution") == $dragged.data("no")
301 @piece_correct $dragged
303 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
307 @show_score [correct, all]
311 $(".paragraph", @element).each (i, par) =>
312 $(".placeholder[data-solution]", par).each (j, qpiece) =>
314 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
315 @draggable_move $dr, $qp, false
318 wrap_words: (element, wrapper) ->
319 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
320 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
321 ignore = /^[ \t.,:;()]+/
323 insertWrapped = (txt, elem) ->
325 $(document.createTextNode(txt))
326 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
328 for j in [element.get(0).childNodes.length-1..0]
329 chld = element.get(0).childNodes[j]
330 if chld.nodeType == document.TEXT_NODE
331 len = chld.textContent.length
335 space = ignore.exec(chld.textContent.substr(i))
338 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
340 $(document.createTextNode(space[0])).insertBefore(chld)
346 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
350 class Przyporzadkuj extends Excercise
352 for qp in $(".question-piece", @element)
353 if $(qp).data('solution').split(/[ ,]+/).length > 1
357 constructor: (element) ->
360 @multiple = @is_multiple()
362 @dragging @multiple, true
364 draggable_equal: (d1, d2) ->
365 return d1.data("no") == d2.data("no")
368 check_question: (question) ->
369 # subjects placed in predicates
370 minimum = $(question).data("minimum")
374 all = $(".subjects .question-piece", question).length
376 for pred in $(".predicate [data-predicate]", question)
377 pn = $(pred).attr('data-predicate')
381 for qp in $(".question-piece", pred)
382 v = @get_value_optional_list qp, 'solution'
386 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
394 solve_question: (question) ->
395 minimum = $(question).data("min")
397 for qp in $(".subject .question-piece", question)
398 v = @get_value_optional_list qp, 'solution'
403 draggables = mandatory.count(optional)[0...minimum]
405 draggables = mandatory
407 $pr = $(".predicate [data-predicate=" + m + "]", question)
408 $ph = $pr.find ".placeholder:visible"
409 @draggable_move $(qp), $ph, @multiple
413 class PrawdaFalsz extends Excercise
414 constructor: (element) ->
417 for qp in $(".question-piece", @element)
418 $(".true", qp).click (ev) ->
420 $(this).closest(".question-piece").data("value", "true")
421 $(this).addClass('chosen').siblings('a').removeClass('chosen')
422 $(".false", qp).click (ev) ->
424 $(this).closest(".question-piece").data("value", "false")
425 $(this).addClass('chosen').siblings('a').removeClass('chosen')
431 for qp in $(".question-piece", @element)
432 if $(qp).data("solution").toString() == $(qp).data("value")
444 for qp in $(".question-piece", @element)
445 if $(qp).data('solution') == 'true'
446 $(".true", qp).click()
448 $(".false", qp).click()
456 uporzadkuj: Uporzadkuj
459 przyporzadkuj: Przyporzadkuj
460 prawdafalsz: PrawdaFalsz
463 cls = es[$(ele).attr('data-type')]
468 'EduModule': EduModule
473 $(document).ready () ->
474 new EduModule($("#book-text"))
476 $(".excercise").each (i, el) ->