-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress.js
189 lines (143 loc) · 4.48 KB
/
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
progress = (typeof progress == "undefined") ? (function() {
var progress = {};
var rest = 0;
var blocks = [];
var getvalues = function(arr) {
var result = 0;
var sum = 0;
var b = (+bottomline);
for(var i=0;i<arr.length;i++) {
var offset = (+arr[i]);
if (offset < bottomline) {
// simultanious activity
result += b;
} else if( offset > topline) {
// high activity, making wider
result += offset;
} else {
// no activity
}
sum += offset;
}
var unit = sum / 100;
var line = result ? result / unit : 0;
return {
// 'p' can be bigger than 100
// because bottomline is used as replacement for "< bottom"
p : line > 100 ? 100 : line,
r : result
}
}
progress.build = function(node) {
var background = "black";
var container = typeof node == "string" ? document.getElementById( node ) : node;
var style = container.style;
style.backgroundColor = background;
style.width = "128px";
style.height = "64px";
style.margin = "1px";
style.overflow = "hidden";
style.position = "relative";
var leader = container.appendChild(document.createElement("span"));
var style = leader.style;
style.height = "100%";
style.width = "2px";
style.display = "inline-block";
style.backgroundColor = background;
var hiding = container.appendChild(document.createElement("div"));
var style = hiding.style;
style.backgroundColor = background;
style.position = "absolute";
style.width = "100%";
style.height = "1%";
style.bottom = 0;
var frag = document.createDocumentFragment(),
clone = document.createElement("span"),
style = clone.style;
clone.className = "ACM_graphUnit";
style.width = "5px";
style.height = "1%";
style.marginRight = "2px";
style.padding = 0;
style.display = "inline-block";
style.backgroundColor = "yellow";
for (var i=0; i<18; i++) {
blocks.unshift(
frag.appendChild( clone.cloneNode(true) )
);
}
container.appendChild( frag );
}
progress.do_next = function(height){
var last = blocks.pop(),
parent = last.parentNode;
blocks.unshift( last );
parent.removeChild( last );
// 1% is not visible, so, add every where +1
last.style.height = ((height) >= 100 ? 100 : ++height) + "%"; // to keep showing something;
parent.appendChild( last );
}
progress.do_test = function() {
var counter = blocks.length;
setTimeout(function scope(){
var r = ~~(Math.random() * 100);
this.do_next( r );
if (--counter) {
setTimeout( scope, 1000 );
}
},1000);
}
progress.push = function( arr, time ) {
var values = getvalues( arr );
// if result of the arr is bigger than the interval, calculate the rest:
var result = values.r + rest;
// console.debug('Progress result is',result,'perc',values.p);
if (result > time) {
// how much bigger?
var count = ~~(result / time);
for(var i=0;i<count;i++) {
this.do_next( 100 );
result = result - time;
}
rest = result;
} else {
this.do_next( values.p );
rest = 0;
}
}
progress.collect = function() {
timer.collect();
collector.start( progress );
}
progress.stop = function() {
timer.stop();
collector.stop();
}
return progress;
})() : progress;
collector = (typeof collector == "undefined" ) ? (function() {
var interval;
var time = 1000;
var collector = {};
collector.start = function(indicator) {
if (interval) {
debug('Collector already started');
return;
}
var t = time;
interval = setInterval(function() {
var arr = window.stack;
if (arr.length) {
window.stack = [];
indicator.push( arr, t );
}
}, time);
}
collector.stop = function() {
if (interval) {
clearInterval( interval );
interval = null;
}
}
return collector;
})() : collector;