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
24 $(".check", @element).click =>
26 $('.solutions', @element).click =>
29 piece_correct: (qpiece) ->
30 $(qpiece).removeClass('incorrect').addClass('correct')
32 piece_incorrect: (qpiece) ->
33 $(qpiece).removeClass('correct').addClass('incorrect')
37 $(".question", @element).each (i, question) =>
38 scores.push(@check_question question)
41 $.each scores, (i, s) ->
46 # Parses a list of values, separated by space or comma.
47 # The list is read from data attribute of elem using data_key
48 # Returns a list with elements
49 # eg.: things_i_need: "house bike tv playstation"
50 # yields ["house", "bike", "tv", "playstation"]
51 # If optional numbers argument is true, returns list of numbers
53 get_value_list: (elem, data_key, numbers) ->
54 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
56 vl = vl.map((x) -> parseInt(x))
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 2-element list with mandatory and optional
62 # items. optional items are marked with a question mark on the end
63 # eg.: things_i_need: "house bike tv? playstation?"
64 # yields [[ "house", "bike"], ["tv", "playstation"]]
65 get_value_optional_list: (elem, data_key) ->
66 vals = @get_value_list(elem, data_key)
71 opt.push v.slice(0, -1)
76 show_score: (score) ->
77 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
80 draggable_equal: ($draggable1, $draggable2) ->
83 draggable_accept: ($draggable, $droppable) ->
84 dropped = $droppable.closest("ul, ol").find(".draggable")
86 if @draggable_equal $draggable, $(d)
90 draggable_dropped: ($draggable) ->
91 $draggable.append('<span class="close">x</span>')
93 dragging: (ismultiple, issortable) ->
94 $(".question", @element).each (i, question) =>
99 $(".draggable", question).draggable(draggable_opts)
101 $(".placeholder", question).droppable
102 accept: (draggable) ->
103 $draggable = $(draggable)
106 if not $draggable.is(".draggable")
110 is_accepted= self.draggable_accept $draggable, $(this)
113 $(this).addClass 'accepting'
115 $(this).removeClass 'accepting'
119 $(ev.target).removeClass 'accepting'
121 added = $(ui.draggable).clone()
123 $added.data("original", ui.draggable)
125 $(ui.draggable).addClass('disabled').draggable('disable')
127 $(ev.target).after(added)
128 if not $(ev.target).hasClass('multiple')
130 $added.append('<span class="remove">x</span>')
131 $('.remove', added).click (ev) =>
132 $added.prev(".placeholder:not(.multiple)").show()
134 $added.data('original').removeClass('disabled').draggable('enable')
138 $(ev.target).addClass 'dragover'
142 $(ev.target).removeClass 'dragover'
145 class Wybor extends Excercise
146 constructor: (element) ->
150 check_question: (question) ->
153 solution = @get_value_list(question, 'solution')
154 $(".question-piece", question).each (i, qpiece) =>
155 piece_no = $(qpiece).attr 'data-no'
156 piece_name = $(qpiece).attr 'data-name'
158 should_be_checked = solution.indexOf(piece_name) >= 0
160 should_be_checked = solution.indexOf(piece_no) >= 0
161 is_checked = $("input", qpiece).is(":checked")
169 @piece_correct qpiece
171 @piece_incorrect qpiece
173 $(qpiece).removeClass("correct,incorrect")
180 class Uporzadkuj extends Excercise
181 constructor: (element) ->
183 $('ol, ul', @element).sortable({ items: "> li" })
185 check_question: (question) ->
186 positions = @get_value_list(question, 'original', true)
187 sorted = positions.sort()
188 pkts = $('.question-piece', question)
193 for pkt in [0...pkts.length]
195 if pkts.eq(pkt).data('pos') == sorted[pkt]
197 @piece_correct pkts.eq(pkt)
199 @piece_incorrect pkts.eq(pkt)
200 return [correct, all]
203 # XXX propozycje="1/0"
204 class Luki extends Excercise
205 constructor: (element) ->
207 @dragging false, false
212 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
213 $placeholder = $(qpiece).prev(".placeholder")
214 if $placeholder.data('solution') == $(qpiece).data('no')
215 @piece_correct qpiece
218 @piece_incorrect qpiece
221 @show_score [correct, all]
224 class Zastap extends Excercise
225 constructor: (element) ->
227 $(".paragraph", @element).each (i, par) =>
228 @wrap_words $(par), $('<span class="placeholder zastap"/>')
229 @dragging false, false
235 $(".paragraph", @element).each (i, par) =>
236 $(".placeholder", par).each (j, qpiece) =>
237 should_be_checked = false
239 $dragged = $qp.next(".draggable")
240 if $qp.data("solution")
241 if $dragged and $qp.data("solution") == $dragged.data("no")
242 @piece_correct $dragged
244 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
248 @show_score [correct, all]
250 wrap_words: (element, wrapper) ->
251 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
252 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
253 ignore = /^[ \t.,:;()]+/
255 insertWrapped = (txt, elem) ->
257 $(document.createTextNode(txt))
258 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
260 for j in [element.get(0).childNodes.length-1..0]
261 chld = element.get(0).childNodes[j]
262 if chld.nodeType == document.TEXT_NODE
263 len = chld.textContent.length
267 space = ignore.exec(chld.textContent.substr(i))
270 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
272 $(document.createTextNode(space[0])).insertBefore(chld)
278 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
282 class Przyporzadkuj extends Excercise
284 for qp in $(".question-piece", @element)
285 if $(qp).data('solution').split(/[ ,]+/).length > 1
289 constructor: (element) ->
292 @multiple = @is_multiple()
294 @dragging @multiple, true
296 draggable_equal: (d1, d2) ->
297 return d1.data("no") == d2.data("no")
300 check_question: (question) ->
301 # subjects placed in predicates
305 for qp in $(".predicate .question-piece", question)
306 pred = $(qp).closest("[data-predicate]")
307 v = @get_value_optional_list qp, 'solution'
310 all_multiple += mandatory.length + optional.length
311 pn = pred.data('predicate')
312 if mandatory.indexOf(pn) >= 0 or optional.indexOf(pn) >= 0
320 for qp in $(".subject .question-piece", question)
321 v = @get_value_optional_list qp, 'solution'
324 all_multiple += mandatory.length + optional.length
325 return [count, all_multiple]
330 class PrawdaFalsz extends Excercise
331 constructor: (element) ->
334 for qp in $(".question-piece", @element)
335 $(".true", qp).click (ev) ->
337 $(this).closest(".question-piece").data("value", "true")
338 $(this).addClass('chosen').siblings('a').removeClass('chosen')
339 $(".false", qp).click (ev) ->
341 $(this).closest(".question-piece").data("value", "false")
342 $(this).addClass('chosen').siblings('a').removeClass('chosen')
348 for qp in $(".question-piece", @element)
349 if $(qp).data("solution").toString() == $(qp).data("value")
364 uporzadkuj: Uporzadkuj
367 przyporzadkuj: Przyporzadkuj
368 prawdafalsz: PrawdaFalsz
371 cls = es[$(ele).attr('data-type')]
376 'EduModule': EduModule
381 $(document).ready () ->
382 new EduModule($("#book-text"))
384 $(".excercise").each (i, el) ->