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 (ev) =>
28 $(ev.target).next(".retry").show()
30 $(".retry", @element).click (ev) =>
31 $(".correct, .incorrect", @element).removeClass("correct incorrect")
32 $(ev.target).prev(".check").show()
34 $('.solutions', @element).click =>
36 $(".comment", @element).show()
37 $('.reset', @element).click =>
41 $(@element).html($(@element).data('excercise-html'))
44 piece_correct: (qpiece) ->
45 $(qpiece).removeClass('incorrect').addClass('correct')
47 piece_incorrect: (qpiece) ->
48 $(qpiece).removeClass('correct').addClass('incorrect')
52 $(".question", @element).each (i, question) =>
53 scores.push(@check_question question)
56 $.each scores, (i, s) ->
63 $(".question", @element).each (i, question) =>
64 @solve_question question
66 # Parses a list of values, separated by space or comma.
67 # The list is read from data attribute of elem using data_key
68 # Returns a list with elements
69 # eg.: things_i_need: "house bike tv playstation"
70 # yields ["house", "bike", "tv", "playstation"]
71 # If optional numbers argument is true, returns list of numbers
73 get_value_list: (elem, data_key, numbers) ->
74 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
76 vl = vl.map((x) -> parseInt(x))
79 # Parses a list of values, separated by space or comma.
80 # The list is read from data attribute of elem using data_key
81 # Returns a 2-element list with mandatory and optional
82 # items. optional items are marked with a question mark on the end
83 # eg.: things_i_need: "house bike tv? playstation?"
84 # yields [[ "house", "bike"], ["tv", "playstation"]]
85 get_value_optional_list: (elem, data_key) ->
86 vals = @get_value_list(elem, data_key)
91 opt.push v.slice(0, -1)
96 show_score: (score) ->
97 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
100 draggable_equal: ($draggable1, $draggable2) ->
103 draggable_accept: ($draggable, $droppable) ->
104 dropped = $droppable.closest("ul, ol").find(".draggable")
106 if @draggable_equal $draggable, $(d)
110 draggable_move: ($draggable, $placeholder, ismultiple) ->
111 $added = $draggable.clone()
112 $added.data("original", $draggable.get(0))
114 $draggable.addClass('disabled').draggable('disable')
116 $placeholder.after($added)
117 if not $placeholder.hasClass('multiple')
119 $added.append('<span class="remove">x</span>')
120 $('.remove', $added).click (ev) =>
121 $added.prev(".placeholder:not(.multiple)").show()
123 $($added.data('original')).removeClass('disabled').draggable('enable')
126 ## XXX co z issortable?
127 dragging: (ismultiple, issortable) ->
128 $(".question", @element).each (i, question) =>
133 $(".draggable", question).draggable(draggable_opts)
135 $(".placeholder", question).droppable
136 accept: (draggable) ->
137 $draggable = $(draggable)
140 if not $draggable.is(".draggable")
144 is_accepted= self.draggable_accept $draggable, $(this)
147 $(this).addClass 'accepting'
149 $(this).removeClass 'accepting'
153 $(ev.target).removeClass 'accepting dragover'
155 @draggable_move $(ui.draggable), $(ev.target), ismultiple
157 # $added = $(ui.draggable).clone()
158 # $added.data("original", ui.draggable)
160 # $(ui.draggable).addClass('disabled').draggable('disable')
162 # $(ev.target).after(added)
163 # if not $(ev.target).hasClass('multiple')
164 # $(ev.target).hide()
165 # $added.append('<span class="remove">x</span>')
166 # $('.remove', added).click (ev) =>
167 # $added.prev(".placeholder:not(.multiple)").show()
169 # $added.data('original').removeClass('disabled').draggable('enable')
173 $(ev.target).addClass 'dragover'
177 $(ev.target).removeClass 'dragover'
181 class Wybor extends Excercise
182 constructor: (element) ->
186 check_question: (question) ->
189 solution = @get_value_list(question, 'solution')
190 $(".question-piece", question).each (i, qpiece) =>
191 piece_no = $(qpiece).attr 'data-no'
192 piece_name = $(qpiece).attr 'data-name'
194 should_be_checked = solution.indexOf(piece_name) >= 0
196 should_be_checked = solution.indexOf(piece_no) >= 0
197 is_checked = $("input", qpiece).is(":checked")
205 @piece_correct qpiece
207 @piece_incorrect qpiece
209 $(qpiece).removeClass("correct,incorrect")
213 solve_question: (question) ->
214 solution = @get_value_list(question, 'solution')
215 $(".question-piece", question).each (i, qpiece) =>
216 piece_no = $(qpiece).attr 'data-no'
217 piece_name = $(qpiece).attr 'data-name'
219 should_be_checked = solution.indexOf(piece_name) >= 0
221 should_be_checked = solution.indexOf(piece_no) >= 0
222 console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
223 $("input[type=checkbox]", qpiece).prop 'checked', should_be_checked
227 class Uporzadkuj extends Excercise
228 constructor: (element) ->
230 $('ol, ul', @element).sortable({ items: "> li" })
232 check_question: (question) ->
233 positions = @get_value_list(question, 'original', true)
234 sorted = positions.sort()
235 pkts = $('.question-piece', question)
240 for pkt in [0...pkts.length]
242 if pkts.eq(pkt).data('pos') == sorted[pkt]
244 @piece_correct pkts.eq(pkt)
246 @piece_incorrect pkts.eq(pkt)
247 return [correct, all]
249 solve_question: (question) ->
250 positions = @get_value_list(question, 'original', true)
251 sorted = positions.sort()
252 pkts = $('.question-piece', question)
260 parent = pkts.eq(0).parent()
265 # XXX propozycje="1/0"
266 class Luki extends Excercise
267 constructor: (element) ->
269 @dragging false, false
274 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
275 $placeholder = $(qpiece).prev(".placeholder")
276 if $placeholder.data('solution') == $(qpiece).data('no')
277 @piece_correct qpiece
280 @piece_incorrect qpiece
283 @show_score [correct, all]
285 solve_question: (question) ->
286 $(".placeholder", question).each (i, placeholder) =>
287 $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
288 @draggable_move $qp, $(placeholder), false
291 class Zastap extends Excercise
292 constructor: (element) ->
294 $(".paragraph", @element).each (i, par) =>
295 @wrap_words $(par), $('<span class="placeholder zastap"/>')
296 @dragging false, false
302 $(".paragraph", @element).each (i, par) =>
303 $(".placeholder", par).each (j, qpiece) =>
305 $dragged = $qp.next(".draggable")
306 if $qp.data("solution")
307 if $dragged and $qp.data("solution") == $dragged.data("no")
308 @piece_correct $dragged
310 # else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
314 @show_score [correct, all]
318 $(".paragraph", @element).each (i, par) =>
319 $(".placeholder[data-solution]", par).each (j, qpiece) =>
321 $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
322 @draggable_move $dr, $qp, false
325 wrap_words: (element, wrapper) ->
326 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
327 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
328 ignore = /^[ \t.,:;()]+/
330 insertWrapped = (txt, elem) ->
332 $(document.createTextNode(txt))
333 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
335 for j in [element.get(0).childNodes.length-1..0]
336 chld = element.get(0).childNodes[j]
337 if chld.nodeType == document.TEXT_NODE
338 len = chld.textContent.length
342 space = ignore.exec(chld.textContent.substr(i))
345 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
347 $(document.createTextNode(space[0])).insertBefore(chld)
353 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
357 class Przyporzadkuj extends Excercise
359 for qp in $(".question-piece", @element)
360 if $(qp).data('solution').split(/[ ,]+/).length > 1
364 constructor: (element) ->
367 @multiple = @is_multiple()
369 @dragging @multiple, true
371 draggable_equal: (d1, d2) ->
372 return d1.data("no") == d2.data("no")
375 check_question: (question) ->
376 # subjects placed in predicates
377 minimum = $(question).data("minimum")
381 all = $(".subjects .question-piece", question).length
383 for pred in $(".predicate [data-predicate]", question)
384 pn = $(pred).attr('data-predicate')
388 for qp in $(".question-piece", pred)
389 v = @get_value_optional_list qp, 'solution'
393 if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
401 solve_question: (question) ->
402 minimum = $(question).data("min")
404 for qp in $(".subject .question-piece", question)
405 v = @get_value_optional_list qp, 'solution'
410 draggables = mandatory.count(optional)[0...minimum]
412 draggables = mandatory
414 $pr = $(".predicate [data-predicate=" + m + "]", question)
415 $ph = $pr.find ".placeholder:visible"
416 @draggable_move $(qp), $ph, @multiple
420 class PrawdaFalsz extends Excercise
421 constructor: (element) ->
424 for qp in $(".question-piece", @element)
425 $(".true", qp).click (ev) ->
427 $(this).closest(".question-piece").data("value", "true")
428 $(this).addClass('chosen').siblings('a').removeClass('chosen')
429 $(".false", qp).click (ev) ->
431 $(this).closest(".question-piece").data("value", "false")
432 $(this).addClass('chosen').siblings('a').removeClass('chosen')
438 for qp in $(".question-piece", @element)
439 if $(qp).data("solution").toString() == $(qp).data("value")
451 for qp in $(".question-piece", @element)
452 if $(qp).data('solution') == 'true'
453 $(".true", qp).click()
455 $(".false", qp).click()
463 uporzadkuj: Uporzadkuj
466 przyporzadkuj: Przyporzadkuj
467 prawdafalsz: PrawdaFalsz
470 cls = es[$(ele).attr('data-type')]
475 'EduModule': EduModule
480 $(document).ready () ->
481 new EduModule($("#book-text"))
483 $(".excercise").each (i, el) ->