summaryrefslogtreecommitdiff
path: root/js/util/progress.js
blob: 060e6dcf28b8e4d0f26fede78cbee4b232ba798d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function progress(root) {
  var _progress = {};
  var container = document.getElementById(root);
  var _percent = '0%';
  var bar = document.querySelector('#' + root + ' .percent');
  _progress.label = function() {
    return _percent;
  }
  /* Updates the progress bar to display a specific percentage. No range checking performed. */
  _progress.setPercent = function(percent) {
    _percent = percent;
    bar.style.width = _percent;
    bar.textContent = _progress.label();
  }
  /* Updates the progress bar to display a percentage based on the current proportion of items done. */
  _progress.update = function(current, total) {
    var percentLoaded = Math.min(100, Math.round((current / total) * 100));
    _progress.setPercent(percentLoaded + '%');
  };
  /* Resets the progress bar to display nothing done. */
  _progress.reset = function() {
    _progress.setPercent('0%');
  }
  /* Resets the progress bar to display everything done. */
  _progress.complete = function() {
    _progress.setPercent('100%');
  }
  /* Shows the progress bar. */
  _progress.show = function() {
    container.className += ' loading';
  }
  /* Hides the progress bar */
  _progress.hide = function() {
    container.className = container.className.replace(/\bloading\b/, '');
  }
  return _progress;
}