forked from tim-kos/Transloadit-Uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transloadit_progress.js
114 lines (91 loc) · 3.06 KB
/
transloadit_progress.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(function($) {
var tlProgressTimeout = 3000;
var steps = 30;
var timeout = Math.floor(tlProgressTimeout / steps);
$.fn.initProgress = function() {
return this.each(function() {
this.progbar = $('<div class="progbar"></div>').appendTo(this);
this.percent = $('<div class="prog-percent"></div>').appendTo(this);
this.remaining = $('<div class="prog-remain"></div>').appendTo(this);
this.cycle = 0;
this.bytesReceivedLastCycle = 0;
this.progressTimeLastCycle = null;
this.lastPercent = 0;
});
};
$.fn.updateProgress = function(bytesReceived, bytesTotal) {
var progressTime = +new Date();
var remainingMiliSeconds = 0;
var uploadRate = 0;
var $this = this[0];
if ($this.cycle > 0) {
this.show();
if (typeof $this.bytesReceivedLastCycle == 'undefined') {
$this.bytesReceivedLastCycle = bytesReceived;
}
uploadRate = (bytesReceived - $this.bytesReceivedLastCycle) / (progressTime - $this.progressTimeLastCycle);
if (uploadRate > 0) {
remainingMiliSeconds = (bytesTotal - bytesReceived) / uploadRate;
}
// convert bytes/milliseconds into kb/s
uploadRate = (uploadRate * 1000 / 1024).toFixed(1);
var txt = uploadRate + 'kb/s, ' + Math.round(remainingMiliSeconds / 1000) + 's';
$this.remaining.text(txt + ' remaining');
$this.bytesReceivedLastCycle = bytesReceived;
var percent = Math.floor(100 * bytesReceived / bytesTotal);
var lastPercent = $this.lastPercent;
var diff = percent - lastPercent;
var percentPerStep = diff / steps;
if ($this.progressTimout) {
clearTimeout($this.progressTimout);
}
function advanceProgress() {
$this.progressTimout = setTimeout(function() {
var newPercent = lastPercent + percentPerStep;
if (newPercent > percent) {
newPercent = percent;
}
var toInsert = Math.floor(newPercent);
if (isNaN(toInsert)) {
toInsert = '0';
}
$this.percent.text(toInsert + '%');
lastPercent = newPercent;
if (newPercent != percent) {
advanceProgress();
}
}, timeout);
}
advanceProgress();
$this.lastPercent = percent;
} else {
$this.percent.text('0%');
$this.remaining.text('...');
}
$this.progressTimeLastCycle = progressTime;
if (bytesReceived == bytesTotal) {
this.finishProgress(false);
}
$this.cycle++;
return this;
};
$.fn.resetProgress = function() {
var $this = this[0];
$this.cycle = 0,
$this.bytesReceivedLastCycle = 0,
$this.progressTimeLastCycle = null;
$this.progbar.show().css({display:'inline-block'});
$this.remaining.show().text('');
return this;
};
$.fn.finishProgress = function(hide) {
hide = hide || false;
var self = this;
$this = this[0];
if ($this.progressTimout) {
clearTimeout($this.progressTimout);
}
$this.percent.text('100%');
return this;
};
})(jQuery);