score[1] += s[1]
@show_score(score)
+ # Parses a list of values, separated by space or comma.
+ # The list is read from data attribute of elem using data_key
+ # Returns a list with elements
+ # eg.: things_i_need: "house bike tv playstation"
+ # yields ["house", "bike", "tv", "playstation"]
+ # If optional numbers argument is true, returns list of numbers
+ # instead of strings
get_value_list: (elem, data_key, numbers) ->
vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
if numbers
vl = vl.map((x) -> parseInt(x))
return vl
+ # Parses a list of values, separated by space or comma.
+ # The list is read from data attribute of elem using data_key
+ # Returns a 2-element list with mandatory and optional
+ # items. optional items are marked with a question mark on the end
+ # eg.: things_i_need: "house bike tv? playstation?"
+ # yields [[ "house", "bike"], ["tv", "playstation"]]
get_value_optional_list: (elem, data_key) ->
vals = @get_value_list(elem, data_key)
mandat = []
$(".placeholder", question).droppable
accept: (draggable) ->
$draggable = $(draggable)
+ is_accepted = true
+
if not $draggable.is(".draggable")
- return false
- return self.draggable_accept $draggable, $(this)
+ is_accepted = false
+
+ if is_accepted
+ is_accepted= self.draggable_accept $draggable, $(this)
+
+ if is_accepted
+ $(this).addClass 'accepting'
+ else
+ $(this).removeClass 'accepting'
+ return is_accepted
drop: (ev, ui) ->
+ $(ev.target).removeClass 'accepting'
+
added = $(ui.draggable).clone()
$added = added
$added.data("original", ui.draggable)
$added.data('original').removeClass('disabled').draggable('enable')
$(added).remove()
+ over: (ev, ui) ->
+ $(ev.target).addClass 'dragover'
-# $(".droppable", question).each (i, droppable) =>
-# $droppable = $(droppable)
-# $droppable.droppable
-# accept: (draggable) =>
-# return false
-# $draggable = $(draggable)
-# if not $draggable.is(".draggable")
-# console.log('not draggable?')
-# return false
-
-# for added in $droppable.children(".draggable")
-# if @draggable_equal($(added), $draggable)
-# console.log('already here:' + $draggable.text())
-# return false
-
-# return @draggable_accept $draggable
-
-# drop: (ev, ui) =>
-# added = ui.draggable.clone()
-# # added.attr('style', '')
-# $(ev.target).append(added)
-# added.draggable(draggable_opts)
-# @draggable_dropped added
-
-# if not @multiple
-# $(ui.draggable).draggable("disable")
-
-
- # if issortable
- # $(".droppable", question).sortable
- # items: "> li"
-
- # receive: (event, ui) =>
- # console.log("receive " + $(ui.item).text())
- # if not accept(ui.item, event.target)
- # console.log("REVERT")
- # $(event.target).sortable('cancel')
-
- # over: (event, ui) =>
- # if not accept(ui.item, event.target)
- # $(event.target).sortable('disable')
- # out: (event, ui) =>
- # $(event.target).sortable('enable')
+ out: (ev, ui) ->
+ $(ev.target).removeClass 'dragover'
class Wybor extends Excercise
constructor: (element) ->
super element
$(".paragraph", @element).each (i, par) =>
- @wrap_words $(par), $('<span class="zastap question-piece"/>')
- spans = $("> span", par).attr("contenteditable", "true")
- spans.click (ev) =>
- spans.filter(':not(:empty)').removeClass('editing')
- $(ev.target).addClass('editing')
-
+ @wrap_words $(par), $('<span class="placeholder zastap"/>')
+ @dragging false, false
check: ->
all = 0
correct = 0
- $(".question-piece", @element).each (i, qpiece) =>
- txt = $(qpiece).data('original')
- should_be_changed = false
- if not txt?
- txt = $(qpiece).data('solution')
- should_be_changed = true
- if not txt?
- return
-
- if should_be_changed
- all += 1
- if txt != $(qpiece).text()
- @piece_incorrect qpiece
- else
- if should_be_changed
- @piece_correct qpiece
- correct += 1
+ $(".paragraph", @element).each (i, par) =>
+ $(".placeholder", par).each (j, qpiece) =>
+ should_be_checked = false
+ $qp = $(qpiece)
+ $dragged = $qp.next(".draggable")
+ if $qp.data("solution")
+ if $dragged and $qp.data("solution") == $dragged.data("no")
+ @piece_correct $dragged
+ correct += 1
+# else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
+
+ all += 1
@show_score [correct, all]
self = _this;
return $(".placeholder", question).droppable({
accept: function(draggable) {
- var $draggable;
+ var $draggable, is_accepted;
$draggable = $(draggable);
- if (!$draggable.is(".draggable")) return false;
- return self.draggable_accept($draggable, $(this));
+ is_accepted = true;
+ if (!$draggable.is(".draggable")) is_accepted = false;
+ if (is_accepted) {
+ is_accepted = self.draggable_accept($draggable, $(this));
+ }
+ if (is_accepted) {
+ $(this).addClass('accepting');
+ } else {
+ $(this).removeClass('accepting');
+ }
+ return is_accepted;
},
drop: function(ev, ui) {
var $added, added,
_this = this;
+ $(ev.target).removeClass('accepting');
added = $(ui.draggable).clone();
$added = added;
$added.data("original", ui.draggable);
}
return $(added).remove();
});
+ },
+ over: function(ev, ui) {
+ return $(ev.target).addClass('dragover');
+ },
+ out: function(ev, ui) {
+ return $(ev.target).removeClass('dragover');
}
});
});
var _this = this;
Zastap.__super__.constructor.call(this, element);
$(".paragraph", this.element).each(function(i, par) {
- var spans;
- _this.wrap_words($(par), $('<span class="zastap question-piece"/>'));
- spans = $("> span", par).attr("contenteditable", "true");
- return spans.click(function(ev) {
- spans.filter(':not(:empty)').removeClass('editing');
- return $(ev.target).addClass('editing');
- });
+ return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
});
+ this.dragging(false, false);
}
Zastap.prototype.check = function() {
_this = this;
all = 0;
correct = 0;
- $(".question-piece", this.element).each(function(i, qpiece) {
- var should_be_changed, txt;
- txt = $(qpiece).data('original');
- should_be_changed = false;
- if (!(txt != null)) {
- txt = $(qpiece).data('solution');
- should_be_changed = true;
- }
- if (!(txt != null)) return;
- if (should_be_changed) all += 1;
- if (txt !== $(qpiece).text()) {
- return _this.piece_incorrect(qpiece);
- } else {
- if (should_be_changed) {
- _this.piece_correct(qpiece);
- return correct += 1;
+ $(".paragraph", this.element).each(function(i, par) {
+ return $(".placeholder", par).each(function(j, qpiece) {
+ var $dragged, $qp, should_be_checked;
+ should_be_checked = false;
+ $qp = $(qpiece);
+ $dragged = $qp.next(".draggable");
+ if ($qp.data("solution")) {
+ if ($dragged && $qp.data("solution") === $dragged.data("no")) {
+ _this.piece_correct($dragged);
+ correct += 1;
+ }
+ return all += 1;
}
- }
+ });
});
return this.show_score([correct, all]);
};