Skip to content

Commit

Permalink
Sorted out messy updateProgress function
Browse files Browse the repository at this point in the history
  • Loading branch information
katste committed Jan 13, 2016
1 parent f399c6e commit 3a51394
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions static/js/app/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ define(['jquery', 'bootbox', 'survey.sum', 'survey.cell', 'surveys.dispatch', 'b
return $(this).val();
});
},
filledRequiredInputs: function () {
return survey.requiredEnabledInputs().filter(function () {
return $(this).val();
});
},
emptyInputs: function () {
return survey.enabledInputs().filter(function () {
return !$(this).val();
Expand All @@ -54,14 +59,12 @@ define(['jquery', 'bootbox', 'survey.sum', 'survey.cell', 'surveys.dispatch', 'b
return $(this).val();
});
},
correctInputs: function () {
return survey.enabledInputs().filter(function () {
return this.value && $(this).closest('.form-group').hasClass('has-success');
});
},
requiredInputs: function () {
return survey.enabledInputs().filter('[data-bv-notempty]');
},
requiredEnabledInputs: function() {
return survey.enabledInputs().filter('[data-bv-notempty]').not('[disabled]');
},
sumOfInputs: function () {
return survey.enabledInputs().filter('[data-sum-of]');
}
Expand Down Expand Up @@ -237,11 +240,11 @@ define(['jquery', 'bootbox', 'survey.sum', 'survey.cell', 'surveys.dispatch', 'b
};
var updateProgress = function () {
var total = survey.enabledInputs().length;
var correct = survey.correctInputs().length;
var percent = (correct / total) * 100;

var requiredPercent = Math.ceil((survey.requiredInputs().length / total) * 100);
var boostedPercent = Math.ceil(1.15 * percent);
var filled = survey.filledInputs().length;
var filledPercent = Math.ceil((filled / total) * 100);
var required = survey.requiredEnabledInputs().length;
var filledRequired = survey.filledRequiredInputs().length;
var filledRequiredPercent = Math.ceil((filledRequired / required) * 100);

var setText = function (text) {
survey.form('.answers-text').text(text);
Expand All @@ -254,14 +257,12 @@ define(['jquery', 'bootbox', 'survey.sum', 'survey.cell', 'surveys.dispatch', 'b
setPercent(percent);
};

if (correct === 0) {
if (filled === 0) {
setText('Inga fält är ifyllda');
setPercent(0);
} else if (boostedPercent <= requiredPercent) {
setPercentAndText(Math.min(boostedPercent, requiredPercent));
} else {
percent = (correct == total) ? 100 : Math.ceil(percent);
setPercentAndText(Math.max(percent, requiredPercent));
filledPercent = (filled == total) ? 100 : filledPercent;
setPercentAndText(Math.max(filledPercent, filledRequiredPercent)); // Use filledRequiredPercent if it's higher than filledPercent
}
};
var initProgress = function () {
Expand Down

0 comments on commit 3a51394

Please sign in to comment.