Commit ab07bc5f by Xavier Antoviaque

Merge pull request #42 from mtyaka/clear-results

Clear results when answer is changed.
parents f11d3b17 34732646
...@@ -3,7 +3,7 @@ function AnswerBlock(runtime, element) { ...@@ -3,7 +3,7 @@ function AnswerBlock(runtime, element) {
init: function(options) { init: function(options) {
// register the child validator // register the child validator
$(':input', element).on('keyup', _.debounce(options.onChange, 1000)); $(':input', element).on('keyup', options.onChange);
var checkmark = $('.answer-checkmark', element); var checkmark = $('.answer-checkmark', element);
var completed = $('.xblock-answer', element).data('completed'); var completed = $('.xblock-answer', element).data('completed');
...@@ -20,9 +20,8 @@ function AnswerBlock(runtime, element) { ...@@ -20,9 +20,8 @@ function AnswerBlock(runtime, element) {
var checkmark = $('.answer-checkmark', element); var checkmark = $('.answer-checkmark', element);
$(element).find('.message').text((result || {}).error || ''); $(element).find('.message').text((result || {}).error || '');
checkmark.removeClass( this.clearResult();
'checkmark-incorrect icon-exclamation fa-exclamation checkmark-correct icon-ok fa-check'
);
if (result.completed) { if (result.completed) {
checkmark.addClass('checkmark-correct icon-ok fa-check'); checkmark.addClass('checkmark-correct icon-ok fa-check');
} }
...@@ -31,6 +30,13 @@ function AnswerBlock(runtime, element) { ...@@ -31,6 +30,13 @@ function AnswerBlock(runtime, element) {
} }
}, },
clearResult: function() {
var checkmark = $('.answer-checkmark', element);
checkmark.removeClass(
'checkmark-incorrect icon-exclamation fa-exclamation checkmark-correct icon-ok fa-check'
);
},
// Returns `true` if the child is valid, else `false` // Returns `true` if the child is valid, else `false`
validate: function() { validate: function() {
......
...@@ -26,8 +26,7 @@ function MentoringBlock(runtime, element) { ...@@ -26,8 +26,7 @@ function MentoringBlock(runtime, element) {
} }
function handleSubmitResults(results) { function handleSubmitResults(results) {
var messages_dom = $('.messages', element); messagesDOM.empty().hide();
messages_dom.empty().hide();
$.each(results.submitResults || [], function(index, submitResult) { $.each(results.submitResults || [], function(index, submitResult) {
var input = submitResult[0], var input = submitResult[0],
...@@ -45,13 +44,13 @@ function MentoringBlock(runtime, element) { ...@@ -45,13 +44,13 @@ function MentoringBlock(runtime, element) {
renderAttempts(); renderAttempts();
// Messages should only be displayed upon hitting 'submit', not on page reload // Messages should only be displayed upon hitting 'submit', not on page reload
messages_dom.append(results.message); messagesDOM.append(results.message);
if (messages_dom.html().trim()) { if (messagesDOM.html().trim()) {
messages_dom.prepend('<div class="title1">Feedback</div>'); messagesDOM.prepend('<div class="title1">Feedback</div>');
messages_dom.show(); messagesDOM.show();
} }
validateXBlock(); submitDOM.attr('disabled', 'disabled');
} }
function getChildren(element) { function getChildren(element) {
...@@ -101,24 +100,24 @@ function MentoringBlock(runtime, element) { ...@@ -101,24 +100,24 @@ function MentoringBlock(runtime, element) {
submitXHR = $.post(handlerUrl, JSON.stringify(data)).success(handleSubmitResults); submitXHR = $.post(handlerUrl, JSON.stringify(data)).success(handleSubmitResults);
} }
function resubmit() { function clearResults() {
// Don't autosubmit if number of attempts is limited. messagesDOM.empty().hide();
var max_attempts = $('.attempts', element).data('max_attempts');
// Only autosubmit if messages are already shown. var children = getChildren(element);
var messages_dom = $('.messages', element); for (var i = 0; i < children.length; i++) {
if (messages_dom.html().trim() && !max_attempts) { callIfExists(children[i], 'clearResult');
submit();
} }
} }
function onChange() { function onChange() {
clearResults();
validateXBlock(); validateXBlock();
resubmit();
} }
function initXBlock() { function initXBlock() {
$(element).find('.submit .input-main').bind('click', submit); messagesDOM = $(element).find('.messages');
submitDOM = $(element).find('.submit .input-main');
submitDOM.bind('click', submit);
// init children (especially mrq blocks) // init children (especially mrq blocks)
var children = getChildren(element); var children = getChildren(element);
...@@ -148,7 +147,6 @@ function MentoringBlock(runtime, element) { ...@@ -148,7 +147,6 @@ function MentoringBlock(runtime, element) {
// validate all children // validate all children
function validateXBlock() { function validateXBlock() {
var submit_dom = $(element).find('.submit .input-main');
var is_valid = true; var is_valid = true;
var data = $('.attempts', element).data(); var data = $('.attempts', element).data();
var children = getChildren(element); var children = getChildren(element);
...@@ -169,10 +167,10 @@ function MentoringBlock(runtime, element) { ...@@ -169,10 +167,10 @@ function MentoringBlock(runtime, element) {
} }
if (!is_valid) { if (!is_valid) {
submit_dom.attr('disabled','disabled'); submitDOM.attr('disabled','disabled');
} }
else { else {
submit_dom.removeAttr("disabled"); submitDOM.removeAttr("disabled");
} }
} }
......
...@@ -3,6 +3,7 @@ function MessageView(element) { ...@@ -3,6 +3,7 @@ function MessageView(element) {
return { return {
messageDOM: $('.feedback', element), messageDOM: $('.feedback', element),
allPopupsDOM: $('.choice-tips, .feedback', element), allPopupsDOM: $('.choice-tips, .feedback', element),
allResultsDOM: $('.choice-result', element),
clearPopupEvents: function() { clearPopupEvents: function() {
this.allPopupsDOM.hide(); this.allPopupsDOM.hide();
$('.close', this.allPopupsDOM).off('click'); $('.close', this.allPopupsDOM).off('click');
...@@ -39,6 +40,12 @@ function MessageView(element) { ...@@ -39,6 +40,12 @@ function MessageView(element) {
else { else {
this.showPopup(message); // already a DOM this.showPopup(message); // already a DOM
} }
},
clearResult: function() {
this.allPopupsDOM.hide();
this.allResultsDOM.removeClass(
'checkmark-incorrect icon-exclamation fa-exclamation checkmark-correct icon-ok fa-check'
);
} }
} }
} }
...@@ -61,6 +68,8 @@ function MCQBlock(runtime, element) { ...@@ -61,6 +68,8 @@ function MCQBlock(runtime, element) {
handleSubmit: function(result) { handleSubmit: function(result) {
var messageView = MessageView(element); var messageView = MessageView(element);
messageView.clearResult();
var choiceInputs = $('.choice input', element); var choiceInputs = $('.choice input', element);
$.each(choiceInputs, function(index, choiceInput) { $.each(choiceInputs, function(index, choiceInput) {
var choiceInputDOM = $(choiceInput), var choiceInputDOM = $(choiceInput),
...@@ -69,9 +78,6 @@ function MCQBlock(runtime, element) { ...@@ -69,9 +78,6 @@ function MCQBlock(runtime, element) {
choiceTipsDOM = $('.choice-tips', choiceDOM), choiceTipsDOM = $('.choice-tips', choiceDOM),
choiceTipsCloseDOM; choiceTipsCloseDOM;
choiceResultDOM.removeClass(
'checkmark-incorrect icon-exclamation fa-exclamation checkmark-correct icon-ok fa-check'
);
if (result.completed && choiceInputDOM.val() === result.submission) { if (result.completed && choiceInputDOM.val() === result.submission) {
choiceResultDOM.addClass('checkmark-correct icon-ok fa-check'); choiceResultDOM.addClass('checkmark-correct icon-ok fa-check');
} }
...@@ -110,6 +116,10 @@ function MCQBlock(runtime, element) { ...@@ -110,6 +116,10 @@ function MCQBlock(runtime, element) {
} }
}, },
clearResult: function() {
MessageView(element).clearResult();
},
validate: function(){ validate: function(){
var checked = $('input[type=radio]:checked', element); var checked = $('input[type=radio]:checked', element);
if (checked.length) { if (checked.length) {
...@@ -155,10 +165,6 @@ function MRQBlock(runtime, element) { ...@@ -155,10 +165,6 @@ function MRQBlock(runtime, element) {
choiceTipsDOM = $('.choice-tips', choiceDOM), choiceTipsDOM = $('.choice-tips', choiceDOM),
choiceTipsCloseDOM; choiceTipsCloseDOM;
choiceResultDOM.removeClass(
'checkmark-incorrect icon-exclamation fa-exclamation checkmark-correct icon-ok fa-check'
);
/* show hint if checked or max_attempts is disabled */ /* show hint if checked or max_attempts is disabled */
if (!hide_results && if (!hide_results &&
(result.completed || choiceInputDOM.prop('checked') || options.max_attempts <= 0)) { (result.completed || choiceInputDOM.prop('checked') || options.max_attempts <= 0)) {
...@@ -179,6 +185,10 @@ function MRQBlock(runtime, element) { ...@@ -179,6 +185,10 @@ function MRQBlock(runtime, element) {
}); });
}, },
clearResult: function() {
MessageView(element).clearResult();
},
validate: function(){ validate: function(){
var checked = $('input[type=checkbox]:checked', element); var checked = $('input[type=checkbox]:checked', element);
if (checked.length) { if (checked.length) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment