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").each (i, question) =>
38 scores.push(@check_question question)
41 $.each scores, (i, s) ->
46 get_value_list: (elem, data_key) ->
47 $(elem).data(data_key).split(',').map($.trim).map((x) -> parseInt(x))
50 show_score: (score) ->
51 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
54 class Wybor extends Excercise
55 constructor: (element) ->
59 check_question: (question) ->
62 solution = @get_value_list(question, 'solution')
63 $(".question-piece", question).each (i, qpiece) =>
64 piece_no = parseInt $(qpiece).attr 'data-no'
65 should_be_checked = solution.indexOf(piece_no) >= 0
66 is_checked = $("input", qpiece).is(":checked")
76 @piece_incorrect qpiece
78 $(qpiece).removeClass("correct,incorrect")
85 class Uporzadkuj extends Excercise
86 constructor: (element) ->
88 $('ol, ul', @element).sortable({ items: "> li" })
90 check_question: (question) ->
91 positions = @get_value_list(question, 'original')
92 sorted = positions.sort()
93 pkts = $('.question-piece', question)
98 for pkt in [0...pkts.length]
100 if pkts.eq(pkt).data('pos') == sorted[pkt]
102 @piece_correct pkts.eq(pkt)
104 @piece_incorrect pkts.eq(pkt)
105 return [correct, all]
108 class Luki extends Excercise
109 constructor: (element) ->
115 $(".question-piece", @element).each (i, qpiece) =>
116 if $(qpiece).data('solution') == $(qpiece).val()
117 @piece_correct qpiece
120 @piece_incorrect qpiece
123 @show_score [correct, all]
126 class Zastap extends Excercise
127 constructor: (element) ->
129 $(".paragraph", @element).each (i, par) =>
130 @wrap_words $(par), $('<span class="zastap question-piece"/>')
131 spans = $("> span", par).attr("contenteditable", "true")
133 spans.filter(':not(:empty)').removeClass('editing')
134 $(ev.target).addClass('editing')
140 $(".question-piece", @element).each (i, qpiece) =>
141 txt = $(qpiece).data('original')
142 should_be_changed = false
144 txt = $(qpiece).data('solution')
145 should_be_changed = true
152 if txt != $(qpiece).text()
153 @piece_incorrect qpiece
156 @piece_correct qpiece
159 @show_score [correct, all]
161 wrap_words: (element, wrapper) ->
162 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
163 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
164 ignore = /^[ \t.,:;()]+/
166 insertWrapped = (txt, elem) ->
168 $(document.createTextNode(txt))
169 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
171 for j in [element.get(0).childNodes.length-1..0]
172 chld = element.get(0).childNodes[j]
173 if chld.nodeType == document.TEXT_NODE
174 len = chld.textContent.length
178 space = ignore.exec(chld.textContent.substr(i))
181 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
183 $(document.createTextNode(space[0])).insertBefore(chld)
189 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
193 class Przyporzadkuj extends Excercise
194 constructor: (element) ->
197 if @element.attr('multiple')?
202 $(".question", @element).each (i, question) =>
205 helper: if @multiple then "clone" else null
207 $(".draggable", question).draggable(draggable_opts)
212 $(".predicate .droppable", question).droppable
215 is_multiple = ui.draggable.is(".multiple")
217 added = ui.draggable.clone()
219 added.attr('style', '')
220 $(this).append(added)
221 added.draggable(draggable_opts)
224 ui.draggable.remove()
226 $(".subject", question).droppable
229 is_multiple = ui.draggable.is(".multiple")
231 added = ui.draggable.clone()
233 added.attr('style', '')
235 $(this).append(added)
236 added.draggable(draggable_opts)
238 ui.draggable.remove()
248 uporzadkuj: Uporzadkuj
251 przyporzadkuj: Przyporzadkuj
254 cls = es[$(ele).attr('data-type')]
259 'EduModule': EduModule
264 $(document).ready () ->
265 new EduModule($("#book-text"))
267 $(".excercise").each (i, el) ->