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 get_value_list: (elem, data_key, numbers) ->
47 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
49 vl = vl.map((x) -> parseInt(x))
52 get_value_optional_list: (elem, data_key) ->
53 vals = @get_value_list(elem, data_key)
58 opt.push v.slice(0, -1)
63 show_score: (score) ->
64 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
67 draggable_equal: ($draggable1, $draggable2) ->
70 draggable_accept: ($draggable, $droppable) ->
71 dropped = $droppable.closest("ul, ol").find(".draggable")
73 if @draggable_equal $draggable, $(d)
77 draggable_dropped: ($draggable) ->
78 $draggable.append('<span class="close">x</span>')
80 dragging: (ismultiple, issortable) ->
81 $(".question", @element).each (i, question) =>
86 $(".draggable", question).draggable(draggable_opts)
88 $(".placeholder", question).droppable
89 accept: (draggable) ->
90 $draggable = $(draggable)
91 if not $draggable.is(".draggable")
93 return self.draggable_accept $draggable, $(this)
96 added = $(ui.draggable).clone()
98 $added.data("original", ui.draggable)
100 $(ui.draggable).addClass('disabled').draggable('disable')
102 $(ev.target).after(added)
103 if not $(ev.target).hasClass('multiple')
105 $added.append('<span class="remove">x</span>')
106 $('.remove', added).click (ev) =>
107 $added.prev(".placeholder:not(.multiple)").show()
109 $added.data('original').removeClass('disabled').draggable('enable')
114 # $(".droppable", question).each (i, droppable) =>
115 # $droppable = $(droppable)
116 # $droppable.droppable
117 # accept: (draggable) =>
119 # $draggable = $(draggable)
120 # if not $draggable.is(".draggable")
121 # console.log('not draggable?')
124 # for added in $droppable.children(".draggable")
125 # if @draggable_equal($(added), $draggable)
126 # console.log('already here:' + $draggable.text())
129 # return @draggable_accept $draggable
132 # added = ui.draggable.clone()
133 # # added.attr('style', '')
134 # $(ev.target).append(added)
135 # added.draggable(draggable_opts)
136 # @draggable_dropped added
139 # $(ui.draggable).draggable("disable")
143 # $(".droppable", question).sortable
146 # receive: (event, ui) =>
147 # console.log("receive " + $(ui.item).text())
148 # if not accept(ui.item, event.target)
149 # console.log("REVERT")
150 # $(event.target).sortable('cancel')
152 # over: (event, ui) =>
153 # if not accept(ui.item, event.target)
154 # $(event.target).sortable('disable')
155 # out: (event, ui) =>
156 # $(event.target).sortable('enable')
159 class Wybor extends Excercise
160 constructor: (element) ->
164 check_question: (question) ->
167 solution = @get_value_list(question, 'solution')
168 $(".question-piece", question).each (i, qpiece) =>
169 piece_no = $(qpiece).attr 'data-no'
170 piece_name = $(qpiece).attr 'data-name'
172 should_be_checked = solution.indexOf(piece_name) >= 0
174 should_be_checked = solution.indexOf(piece_no) >= 0
175 is_checked = $("input", qpiece).is(":checked")
183 @piece_correct qpiece
185 @piece_incorrect qpiece
187 $(qpiece).removeClass("correct,incorrect")
194 class Uporzadkuj extends Excercise
195 constructor: (element) ->
197 $('ol, ul', @element).sortable({ items: "> li" })
199 check_question: (question) ->
200 positions = @get_value_list(question, 'original', true)
201 sorted = positions.sort()
202 pkts = $('.question-piece', question)
207 for pkt in [0...pkts.length]
209 if pkts.eq(pkt).data('pos') == sorted[pkt]
211 @piece_correct pkts.eq(pkt)
213 @piece_incorrect pkts.eq(pkt)
214 return [correct, all]
217 # XXX propozycje="1/0"
218 class Luki extends Excercise
219 constructor: (element) ->
221 @dragging false, false
226 $(".placeholder + .question-piece", @element).each (i, qpiece) =>
227 $placeholder = $(qpiece).prev(".placeholder")
228 if $placeholder.data('solution') == $(qpiece).data('no')
229 @piece_correct qpiece
232 @piece_incorrect qpiece
235 @show_score [correct, all]
238 class Zastap extends Excercise
239 constructor: (element) ->
241 $(".paragraph", @element).each (i, par) =>
242 @wrap_words $(par), $('<span class="zastap question-piece"/>')
243 spans = $("> span", par).attr("contenteditable", "true")
245 spans.filter(':not(:empty)').removeClass('editing')
246 $(ev.target).addClass('editing')
252 $(".question-piece", @element).each (i, qpiece) =>
253 txt = $(qpiece).data('original')
254 should_be_changed = false
256 txt = $(qpiece).data('solution')
257 should_be_changed = true
264 if txt != $(qpiece).text()
265 @piece_incorrect qpiece
268 @piece_correct qpiece
271 @show_score [correct, all]
273 wrap_words: (element, wrapper) ->
274 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
275 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
276 ignore = /^[ \t.,:;()]+/
278 insertWrapped = (txt, elem) ->
280 $(document.createTextNode(txt))
281 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
283 for j in [element.get(0).childNodes.length-1..0]
284 chld = element.get(0).childNodes[j]
285 if chld.nodeType == document.TEXT_NODE
286 len = chld.textContent.length
290 space = ignore.exec(chld.textContent.substr(i))
293 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
295 $(document.createTextNode(space[0])).insertBefore(chld)
301 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
305 class Przyporzadkuj extends Excercise
307 for qp in $(".question-piece", @element)
308 if $(qp).data('solution').split(/[ ,]+/).length > 1
312 constructor: (element) ->
315 @multiple = @is_multiple()
317 @dragging @multiple, true
319 draggable_equal: (d1, d2) ->
320 return d1.data("no") == d2.data("no")
323 check_question: (question) ->
324 # subjects placed in predicates
328 for qp in $(".predicate .question-piece", question)
329 pred = $(qp).closest("[data-predicate]")
330 v = @get_value_optional_list qp, 'solution'
333 all_multiple += mandatory.length + optional.length
334 pn = pred.data('predicate')
335 if mandatory.indexOf(pn) >= 0 or optional.indexOf(pn) >= 0
343 for qp in $(".subject .question-piece", question)
344 v = @get_value_optional_list qp, 'solution'
347 all_multiple += mandatory.length + optional.length
348 return [count, all_multiple]
353 class PrawdaFalsz extends Excercise
354 constructor: (element) ->
357 for qp in $(".question-piece", @element)
358 $(".true", qp).click (ev) ->
360 $(this).closest(".question-piece").data("value", "true")
361 $(this).addClass('chosen').siblings('a').removeClass('chosen')
362 $(".false", qp).click (ev) ->
364 $(this).closest(".question-piece").data("value", "false")
365 $(this).addClass('chosen').siblings('a').removeClass('chosen')
371 for qp in $(".question-piece", @element)
372 if $(qp).data("solution").toString() == $(qp).data("value")
387 uporzadkuj: Uporzadkuj
390 przyporzadkuj: Przyporzadkuj
391 prawdafalsz: PrawdaFalsz
394 cls = es[$(ele).attr('data-type')]
399 'EduModule': EduModule
404 $(document).ready () ->
405 new EduModule($("#book-text"))
407 $(".excercise").each (i, el) ->