Skip to content
Snippets Groups Projects
Commit 5de09367 authored by srosse's avatar srosse
Browse files

OO-3266: fix javascript method defined in global contexts and some formatting

parent a323da8a
No related branches found
No related tags found
No related merge requests found
......@@ -133,11 +133,11 @@
}
Drawing.prototype.getCoords = function(spot) {
if(spot.hasClass('o_draw_circle')) {
return calculateCircleCoords(spot);
} else if(spot.hasClass('o_draw_rectangle')) {
return calculateRectangleCoords(spot);
}
if(spot.hasClass('o_draw_circle')) {
return calculateCircleCoords(spot);
} else if(spot.hasClass('o_draw_rectangle')) {
return calculateRectangleCoords(spot);
}
}
Drawing.prototype.generateInputsAndId = function(prefix, shape, coords) {
......@@ -150,7 +150,7 @@
return newId;
};
calculateCircleCoords = function(spot) {
function calculateCircleCoords(spot) {
var id = jQuery(spot).attr('id');
var position = jQuery(spot).position();
var radius = parseInt(jQuery(spot).width(), 10) / 2;
......@@ -160,7 +160,7 @@
return coords;
};
calculateRectangleCoords = function(spot) {
function calculateRectangleCoords(spot) {
var id = jQuery(spot).attr('id');
var position = jQuery(spot).position();
var width = parseInt(jQuery(spot).width(), 10);
......@@ -170,5 +170,4 @@
jQuery("#" + id + "_coords").val(coords);
return coords;
};
}(jQuery));
......@@ -21,147 +21,145 @@
return this;
};
drawAssociations = function($obj, settings) {
var containerId = $obj.attr('id');
var associationPairs = settings.responseValue.split(',');
var associationEls = jQuery('#' + containerId + '_panel .association');
for(var i=0; i<associationPairs.length; i++) {
var associationPair = associationPairs[i].split(' ');
var associationEl = jQuery(associationEls.get(i));
if(associationEl.size() == 0) {
var boxId = newAssociationBox(containerId, settings);
associationEl = jQuery('#' + boxId);
}
var association1 = jQuery("#" + containerId + "_items div[data-qti-id='" + associationPair[0] + "']");
if(needToBeAvailable(association1, containerId)) {
association1 = jQuery(association1).clone();
}
var association2 = jQuery("#" + containerId + "_items div[data-qti-id='" + associationPair[1] + "']");
if(needToBeAvailable(association2, containerId)) {
association2 = jQuery(association2).clone();
}
jQuery(association1).addClass('oo-choosed');
jQuery(association2).addClass('oo-choosed');
jQuery(associationEl.find('.association_box.left'))
.addClass('oo-filled')
.append(association1);
jQuery(associationEl.find('.association_box.right'))
.addClass('oo-filled')
.append(association2);
}
recalculate(containerId, settings);
if(settings.unrestricted && settings.opened) {
function drawAssociations($obj, settings) {
var containerId = $obj.attr('id');
var associationPairs = settings.responseValue.split(',');
var associationEls = jQuery('#' + containerId + '_panel .association');
for (var i = 0; i < associationPairs.length; i++) {
var associationPair = associationPairs[i].split(' ');
var associationEl = jQuery(associationEls.get(i));
if (associationEl.size() == 0) {
var boxId = newAssociationBox(containerId, settings);
associationEl = jQuery('#' + boxId);
}
var association1 = jQuery("#" + containerId + "_items div[data-qti-id='" + associationPair[0] + "']");
if (needToBeAvailable(association1, containerId)) {
association1 = jQuery(association1).clone();
}
var association2 = jQuery("#" + containerId + "_items div[data-qti-id='" + associationPair[1] + "']");
if (needToBeAvailable(association2, containerId)) {
association2 = jQuery(association2).clone();
}
jQuery(association1).addClass('oo-choosed');
jQuery(association2).addClass('oo-choosed');
jQuery(associationEl.find('.association_box.left')).addClass('oo-filled').append(association1);
jQuery(associationEl.find('.association_box.right')).addClass('oo-filled').append(association2);
}
recalculate(containerId, settings);
if (settings.unrestricted && settings.opened) {
addNewAssociationBoxAndEvents(containerId, settings);
}
};
}
};
associate = function($obj, settings) {
var containerId = $obj.attr('id');
var items = jQuery("#" + containerId + " .o_associate_item");
initializeGapEvents(items, containerId, settings);
var associationBox = jQuery("#" + containerId + "_panel .association_box");
initializeAssociationBoxEvents(associationBox, containerId, settings);
function associate($obj, settings) {
var containerId = $obj.attr('id');
var items = jQuery("#" + containerId + " .o_associate_item");
initializeGapEvents(items, containerId, settings);
var associationBox = jQuery("#" + containerId + "_panel .association_box");
initializeAssociationBoxEvents(associationBox, containerId, settings);
};
initializeGapEvents = function(jElements, containerId, settings) {
jElements.on('click', function(e, el) {
var itemEl = jQuery(this);
if(!itemEl.hasClass('oo-choosed') && !itemEl.hasClass('oo-selected')) {
itemEl.addClass('oo-selected');
}
}).draggable({
containment: "#" + containerId,
scroll: false,
revert: "invalid",
stop: function(event, ui) {
jQuery(this).css({'left': '0px', 'top': '0px' });
jQuery(ui.helper).removeClass('oo-drag');
},
helper: function() {
var choiceEl = jQuery(this);
var boxed = choiceEl.parent('.association_box').size() > 0;
if(!boxed && needToBeAvailable(this, containerId)) {
choiceEl.removeClass('oo-selected');
var cloned = choiceEl.clone();// need some click / drag listeners
jQuery(cloned)
.attr('id', 'n' + guid())
.data('qti-cloned','true')
.addClass('oo-drag');
return cloned;
function initializeGapEvents(jElements, containerId, settings) {
jElements.on('click', function(e, el) {
var itemEl = jQuery(this);
if(!itemEl.hasClass('oo-choosed') && !itemEl.hasClass('oo-selected')) {
itemEl.addClass('oo-selected');
}
choiceEl.addClass('oo-drag');
return choiceEl;
}
}).on('click', {formId: settings.formDispatchFieldId}, setFlexiFormDirtyByListener);
}).draggable({
containment: "#" + containerId,
scroll: false,
revert: "invalid",
stop: function(event, ui) {
jQuery(this).css({'left': '0px', 'top': '0px' });
jQuery(ui.helper).removeClass('oo-drag');
},
helper: function() {
var choiceEl = jQuery(this);
var boxed = choiceEl.parent('.association_box').size() > 0;
if(!boxed && needToBeAvailable(this, containerId)) {
choiceEl.removeClass('oo-selected');
var cloned = choiceEl.clone();// need some click / drag listeners
jQuery(cloned)
.attr('id', 'n' + guid())
.data('qti-cloned','true')
.addClass('oo-drag');
return cloned;
}
choiceEl.addClass('oo-drag');
return choiceEl;
}
}).on('click', {formId: settings.formDispatchFieldId}, setFlexiFormDirtyByListener);
}
needToBeAvailable = function(selectedEl, containerId) {
var choiceEl = jQuery(selectedEl);
var matchMax = choiceEl.data("qti-match-max");
var gapId = choiceEl.data("qti-id");
var currentUsedGap = jQuery("#" + containerId + "_panel div[data-qti-id='" + gapId + "']").size();
return (matchMax == 0 || currentUsedGap + 1 < matchMax);
}
initializeAssociationBoxEvents = function(jElements, containerId, settings) {
jElements.on('click', function(e, el) {
var box = jQuery(this);
function needToBeAvailable(selectedEl, containerId) {
var choiceEl = jQuery(selectedEl);
var matchMax = choiceEl.data("qti-match-max");
var gapId = choiceEl.data("qti-id");
var currentUsedGap = jQuery(
"#" + containerId + "_panel div[data-qti-id='" + gapId + "']")
.size();
return (matchMax == 0 || currentUsedGap + 1 < matchMax);
}
function initializeAssociationBoxEvents(jElements, containerId, settings) {
jElements.on('click', function(e, el) {
var box = jQuery(this);
var hasItems = jQuery(".o_associate_item", this).size();
if(hasItems == 1) {
jQuery(".o_associate_item", this).each(function(index, selectedEl) {
removeGap(selectedEl, box, containerId);
});
} else {
jQuery("#" + containerId + "_items .o_associate_item.oo-selected").each(function(index, selectedEl) {
var choiceEl = jQuery(selectedEl);
if(needToBeAvailable(selectedEl, containerId)) {
choiceEl.removeClass('oo-selected');
moveGap(choiceEl.clone(), box, containerId);
} else {
moveGap(choiceEl, box, containerId);
}
});
}
var hasItems = jQuery(".o_associate_item", this).size();
if(hasItems == 1) {
jQuery(".o_associate_item", this).each(function(index, selectedEl) {
removeGap(selectedEl, box, containerId);
});
} else {
jQuery("#" + containerId + "_items .o_associate_item.oo-selected").each(function(index, selectedEl) {
var choiceEl = jQuery(selectedEl);
if(needToBeAvailable(selectedEl, containerId)) {
choiceEl.removeClass('oo-selected');
moveGap(choiceEl.clone(), box, containerId);
} else {
moveGap(choiceEl, box, containerId);
}
});
}
recalculate(containerId, settings);
setFlexiFormDirty(settings.formDispatchFieldId, false);
}).droppable({
drop: function(event, ui) {
var box = jQuery(this);
var hasItems = jQuery(".o_associate_item", this).size();
if(hasItems > 0) {
jQuery(".o_associate_item", this).each(function(index, selectedEl) {
removeGap(selectedEl, box, containerId);
});
}
}).droppable({
drop: function(event, ui) {
var box = jQuery(this);
var hasItems = jQuery(".o_associate_item", this).size();
if(hasItems > 0) {
jQuery(".o_associate_item", this).each(function(index, selectedEl) {
removeGap(selectedEl, box, containerId);
});
}
var choiceEl;
if(ui.helper != null && jQuery(ui.helper).data('qti-cloned') == 'true') {
var choiceEl = jQuery(ui.draggable);
choiceEl
.removeClass('oo-selected')
.removeClass('oo-drag');
choiceEl = choiceEl.clone()
initializeGapEvents(choiceEl, containerId, settings);
moveGap(choiceEl, box, containerId);
} else {
var choiceEl = jQuery(ui.draggable)
choiceEl
.removeClass('oo-selected')
.removeClass('oo-drag');
moveGap(choiceEl, box, containerId);
}
var choiceEl;
if(ui.helper != null && jQuery(ui.helper).data('qti-cloned') == 'true') {
var choiceEl = jQuery(ui.draggable);
choiceEl
.removeClass('oo-selected')
.removeClass('oo-drag');
choiceEl = choiceEl.clone();
initializeGapEvents(choiceEl, containerId, settings);
moveGap(choiceEl, box, containerId);
} else {
var choiceEl = jQuery(ui.draggable);
choiceEl
.removeClass('oo-selected')
.removeClass('oo-drag');
moveGap(choiceEl, box, containerId);
}
recalculate(containerId, settings);
setFlexiFormDirty(settings.formDispatchFieldId, false);
}
}).on('click', {formId: settings.formDispatchFieldId}, setFlexiFormDirtyByListener);
recalculate(containerId, settings);
setFlexiFormDirty(settings.formDispatchFieldId, false);
}
}).on('click', {formId: settings.formDispatchFieldId}, setFlexiFormDirtyByListener);
};
moveGap = function(choiceEl, box, containerId) {
function moveGap(choiceEl, box, containerId) {
choiceEl
.removeClass('oo-selected')
.css({'left': '0px', 'top': '0px' })
......@@ -170,80 +168,81 @@
box.addClass('oo-filled');
};
removeGap = function(selectedEl, box, containerId) {
box.removeClass('oo-filled');
var jSelectedEl = jQuery(selectedEl);
jSelectedEl.removeClass('oo-choosed');
function removeGap(selectedEl, box, containerId) {
box.removeClass('oo-filled');
var jSelectedEl = jQuery(selectedEl);
jSelectedEl.removeClass('oo-choosed');
var gapId = jSelectedEl.data('qti-id');
var availableGaps = jQuery("#" + containerId + "_items div[data-qti-id='" + gapId + "']").size();
if(availableGaps == 0) {
jSelectedEl.appendTo(jQuery('#' + containerId +'_items'));
} else {
jSelectedEl.remove();
}
var gapId = jSelectedEl.data('qti-id');
var availableGaps = jQuery("#" + containerId + "_items div[data-qti-id='" + gapId + "']").size();
if(availableGaps == 0) {
jSelectedEl.appendTo(jQuery('#' + containerId +'_items'));
} else {
jSelectedEl.remove();
}
};
guid = function() {
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + s4() + s4() + s4() + s4() + s4();
};
recalculate = function (containerId, settings) {
var divContainer = jQuery('#' + containerId + '_panel');
divContainer.find("input[type='hidden']").remove();
jQuery("#" + containerId + "_panel .association").each(function(index, associationEl) {
var associations = jQuery(associationEl).find('.o_associate_item');
if(associations.length == 2) {
var id1 = jQuery(associations.get(0)).data('qti-id');
var id2 = jQuery(associations.get(1)).data('qti-id');
var inputElement = jQuery('<input type="hidden"/>')
.attr('name', 'qtiworks_response_' + settings.responseIdentifier)
.attr('value', id1 + " " + id2);
divContainer.prepend(inputElement);
} else {
if(associations.length == 0 && settings.unrestricted) {
associationEl.remove();//remove empty slots
} else {
jQuery(associationEl).find('.association_box').each(function(index, associationBoxEl) {
var numOfItems = jQuery(associationBoxEl).find('.o_associate_item').size();
if(numOfItems == 0 && jQuery(associationBoxEl).hasClass('oo-filled')) {
jQuery(associationBoxEl).removeClass('oo-filled');
}
});
}
}
});
function recalculate(containerId, settings) {
var divContainer = jQuery('#' + containerId + '_panel');
divContainer.find("input[type='hidden']").remove();
jQuery("#" + containerId + "_panel .association").each(function(index, associationEl) {
var associations = jQuery(associationEl).find('.o_associate_item');
if (associations.length == 2) {
var id1 = jQuery(associations.get(0)).data('qti-id');
var id2 = jQuery(associations.get(1)).data('qti-id');
var inputElement = jQuery('<input type="hidden"/>')
.attr('name', 'qtiworks_response_' + settings.responseIdentifier)
.attr('value', id1 + " " + id2);
divContainer.prepend(inputElement);
} else {
if (associations.length == 0 && settings.unrestricted) {
associationEl.remove();// remove empty slots
} else {
jQuery(associationEl).find('.association_box').each(function(index, associationBoxEl) {
var numOfItems = jQuery(associationBoxEl).find('.o_associate_item').size();
if (numOfItems == 0 && jQuery(associationBoxEl).hasClass('oo-filled')) {
jQuery(associationBoxEl).removeClass('oo-filled');
}
});
}
}
});
addNewAssociationBoxAndEvents(containerId, settings);
};
};
addNewAssociationBoxAndEvents = function (containerId, settings) {
if(!settings.unrestricted || !settings.opened) return;
// all slots are full -> add a new one
var divContainer = jQuery('#' + containerId + '_panel');
var full = true;
jQuery("#" + containerId + "_panel .association").each(function(index, associationEl) {
var associations = jQuery(associationEl).find('.o_associate_item');
if(associations.length != 2) {
full = false;
}
});
if(full) {
function addNewAssociationBoxAndEvents(containerId, settings) {
if (!settings.unrestricted || !settings.opened)
return;
// all slots are full -> add a new one
var divContainer = jQuery('#' + containerId + '_panel');
var full = true;
jQuery("#" + containerId + "_panel .association").each(function(index, associationEl) {
var associations = jQuery(associationEl).find('.o_associate_item');
if (associations.length != 2) {
full = false;
}
});
if (full) {
var boxId = newAssociationBox(containerId, settings);
var lastAssociationBox = jQuery("#" +boxId + " .association_box");
initializeAssociationBoxEvents(lastAssociationBox, containerId, settings);
}
};
var lastAssociationBox = jQuery("#" + boxId + " .association_box");
initializeAssociationBoxEvents(lastAssociationBox, containerId, settings);
}
};
newAssociationBox = function (containerId, settings) {
var boxId = guid();
var slot = '<div id="' + boxId + '" class="association" style="">\n'
function newAssociationBox(containerId, settings) {
var boxId = guid();
var slot = '<div id="' + boxId + '" class="association" style="">\n'
+ ' <div class="association_box left" style="width: 100px; height:50px; float:left;"></div>\n'
+ ' <div class="association_box right" style="width: 100px; height:50px; float:right;"></div>\n'
+ ' <div style="clear:both; "></div>\n'
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment