Skip to content

Commit ef85c3d

Browse files
authored
feat(progress): total value of 0 was unsupported
A total value of 0 was unsupported and wrongly fetched (0 !== undefined !) Although this might be a rarely used situation, it should behave correctly. Means an explicitely given total value of 0 is always fullfilled and should behave as 100% This PR also fixes wrong handling of the limitValues setting and a wrong check in precentage calculation for the total value which might be undefined and resulted in NaN numbers otherwise.
1 parent 9f496c3 commit ef85c3d

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/definitions/modules/progress.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ $.fn.progress = function(parameters) {
169169
value : module.helper.forceArray($module.data(metadata.value))
170170
}
171171
;
172-
if(data.total) {
172+
if(data.total !== undefined) {
173173
module.debug('Total value set from metadata', data.total);
174174
module.set.total(data.total);
175175
}
@@ -272,18 +272,18 @@ $.fn.progress = function(parameters) {
272272
var
273273
index_ = index || 0,
274274
value = module.get.value(index_),
275-
total = module.total || 0,
275+
total = module.get.total(),
276276
percent = (animating)
277277
? module.get.displayPercent(index_)
278278
: module.get.percent(index_),
279-
left = (module.total > 0)
280-
? (total - value)
279+
left = (total !== false)
280+
? Math.max(0,total - value)
281281
: (100 - percent)
282282
;
283283
templateText = templateText || '';
284284
templateText = templateText
285285
.replace('{value}', value)
286-
.replace('{total}', total)
286+
.replace('{total}', total || 0)
287287
.replace('{left}', left)
288288
.replace('{percent}', percent)
289289
.replace('{bar}', settings.text.bars[index_] || '')
@@ -373,7 +373,7 @@ $.fn.progress = function(parameters) {
373373
return module.nextValue || module.value && module.value[index || 0] || 0;
374374
},
375375
total: function() {
376-
return module.total || false;
376+
return module.total !== undefined ? module.total : false;
377377
}
378378
},
379379

@@ -506,23 +506,23 @@ $.fn.progress = function(parameters) {
506506
;
507507
});
508508
var hasTotal = module.has.total();
509-
var totalPecent = module.helper.sum(percents);
510-
var isMultpleValues = percents.length > 1 && hasTotal;
509+
var totalPercent = module.helper.sum(percents);
510+
var isMultipleValues = percents.length > 1 && hasTotal;
511511
var sumTotal = module.helper.sum(module.helper.forceArray(module.value));
512-
if (isMultpleValues && sumTotal > module.total) {
512+
if (isMultipleValues && sumTotal > module.total) {
513513
// Sum values instead of pecents to avoid precision issues when summing floats
514514
module.error(error.sumExceedsTotal, sumTotal, module.total);
515-
} else if (!isMultpleValues && totalPecent > 100) {
516-
// Sum before rouding since sum of rounded may have error though sum of actual is fine
517-
module.error(error.tooHigh, totalPecent);
518-
} else if (totalPecent < 0) {
519-
module.error(error.tooLow, totalPecent);
515+
} else if (!isMultipleValues && totalPercent > 100) {
516+
// Sum before rounding since sum of rounded may have error though sum of actual is fine
517+
module.error(error.tooHigh, totalPercent);
518+
} else if (totalPercent < 0) {
519+
module.error(error.tooLow, totalPercent);
520520
} else {
521521
var autoPrecision = settings.precision > 0
522522
? settings.precision
523-
: isMultpleValues
523+
: isMultipleValues
524524
? module.helper.derivePrecision(Math.min.apply(null, module.value), module.total)
525-
: undefined;
525+
: 0;
526526

527527
// round display percentage
528528
var roundedPercents = percents.map(function (percent) {
@@ -532,7 +532,7 @@ $.fn.progress = function(parameters) {
532532
;
533533
});
534534
module.percent = roundedPercents;
535-
if (!hasTotal) {
535+
if (hasTotal) {
536536
module.value = roundedPercents.map(function (percent) {
537537
return (autoPrecision > 0)
538538
? Math.round((percent / 100) * module.total * (10 * autoPrecision)) / (10 * autoPrecision)
@@ -541,11 +541,7 @@ $.fn.progress = function(parameters) {
541541
});
542542
if (settings.limitValues) {
543543
module.value = module.value.map(function (value) {
544-
return (value > 100)
545-
? 100
546-
: (module.value < 0)
547-
? 0
548-
: module.value;
544+
return Math.max(0, Math.min(100, value));
549545
});
550546
}
551547
}
@@ -622,7 +618,7 @@ $.fn.progress = function(parameters) {
622618
if (text !== undefined) {
623619
$progress.text( module.get.text(text, index) );
624620
}
625-
else if (settings.label == 'ratio' && module.total) {
621+
else if (settings.label == 'ratio' && module.has.total()) {
626622
module.verbose('Adding ratio to bar label');
627623
$progress.text( module.get.text(settings.text.ratio, index) );
628624
}
@@ -752,7 +748,7 @@ $.fn.progress = function(parameters) {
752748
}
753749
value = module.get.normalizedValue(value);
754750
if (hasTotal) {
755-
percentComplete = (value / module.total) * 100;
751+
percentComplete = module.total > 0 ? (value / module.total) * 100 : 100;
756752
module.debug('Calculating percent complete from total', percentComplete);
757753
}
758754
else {

0 commit comments

Comments
 (0)