-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.js
100 lines (85 loc) · 2.5 KB
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Represents a fakeProgress
* @constructor
* @param {object} options - options of the contructor
* @param {object} [options.timeConstant=1000] - the timeConstant in milliseconds (see https://en.wikipedia.org/wiki/Time_constant)
* @param {object} [options.autoStart=false] - if true then the progress auto start
*/
const FakeProgress = function (opts) {
if (!opts) {
opts = {};
}
this.timeConstant = opts.timeConstant || 1000;
this.progress = 0;
this._running = false;
this._intervalFrequency = 100;
this.autoStart = opts.autoStart || false;
this.parent = opts.parent;
this.parentStart = opts.parentStart;
this.parentEnd = opts.parentEnd;
if (this.autoStart) {
this.start();
}
};
/**
* Start fakeProgress instance
* @method
*/
FakeProgress.prototype.start = function () {
this._time = 0;
this._intervalId = setInterval(this._onInterval.bind(this), this._intervalFrequency);
};
FakeProgress.prototype._onInterval = function () {
this._time += this._intervalFrequency;
this.setProgress(1 - Math.exp(-1 * this._time / this.timeConstant));
};
/**
* Stop fakeProgress instance and set progress to 1
* @method
*/
FakeProgress.prototype.end = function () {
this.stop();
this.setProgress(1);
};
/**
* Stop fakeProgress instance
* @method
*/
FakeProgress.prototype.stop = function () {
clearInterval(this._intervalId);
this._intervalId = null;
};
/**
* Create a sub progress bar under the first progres
* @method
* @param {object} options - options of the FakeProgress contructor
* @param {object} [options.end=1] - the progress in the parent that correspond of 100% of the child
* @param {object} [options.start=fakeprogress.progress] - the progress in the parent that correspond of 0% of the child
*/
FakeProgress.prototype.createSubProgress = function (opts) {
const parentStart = opts.start || this.progress;
const parentEnd = opts.end || 1;
const options = Object.assign({}, opts, {
parent: this,
parentStart: parentStart,
parentEnd: parentEnd,
start: null,
end: null
});
const subProgress = new FakeProgress(options);
return subProgress;
};
/**
* SetProgress of the fakeProgress instance and updtae the parent
* @method
* @param {number} progress - the progress
*/
FakeProgress.prototype.setProgress = function (progress) {
this.progress = progress;
if (this.parent) {
this.parent.setProgress(((this.parentEnd - this.parentStart) * this.progress) + this.parentStart);
}
};
if (typeof exports === 'object' && typeof module === 'object') {
module.exports = FakeProgress;
}