-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefrag.js
103 lines (87 loc) · 3.53 KB
/
Defrag.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
var Defrag = function(container, fragments, auto, callback) {
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.defrag-blocks{font-size:0}.defrag-block{background:#00fdff;border:1px solid #000;display:inline-block;height:7px;margin:0 1px 1px 0;width:5px}.defrag-block.moved{background:#fff;border-color:#fff}.defrag-block.inprogress{background:#ff2101}.defrag-block.done{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAGElEQVQIHWNg0P//n+H///+MIILBgIEBAFNLCFkekQ5uAAAAAElFTkSuQmCC)}';
document.getElementsByTagName('head')[0].appendChild(style);
var blocks = container,
nextBlock = 0,
currentBlock = 0,
finished = false,
started = false,
smallRunToGo = 0,
smallRunDone = 0;
blocks.classList.add('defrag-blocks');
for (var i = 0; i < fragments; i++) {
var block = document.createElement('div');
block.classList.add('defrag-block');
blocks.appendChild(block);
}
function defragGo() {
var blocksToDefrag = getRandomInt(2, 40);
if((nextBlock + blocksToDefrag) > fragments) {
blocksToDefrag = fragments - nextBlock;
finished = true;
}
if(getRandomInt(0,100) > 80) {
smallRunToGo = blocksToDefrag;
smallRunDone = 0;
smallRun();
} else {
for (var i = nextBlock; i < nextBlock + blocksToDefrag; i++) {
var block = document.getElementsByClassName('defrag-block')[i];
block.classList.add('moved');
}
setTimeout(progress, getRandomInt(10, 1500));
}
function smallRun() {
if(smallRunToGo > smallRunDone) {
var block = document.getElementsByClassName('defrag-block')[nextBlock];
block.classList.add('moved');
setTimeout(function() {
block.classList.remove('moved');
block.classList.add('inprogress');
setTimeout(function() {
block.classList.remove('inprogress');
block.classList.add('done');
nextBlock++;
smallRunDone++;
smallRun();
}, getRandomInt(30, 100));
}, getRandomInt(20, 90));
} else {
defragGo();
}
}
function progress() {
for (var i = nextBlock; i < nextBlock + blocksToDefrag; i++) {
var block = document.getElementsByClassName('defrag-block')[i];
block.classList.remove('moved');
block.classList.add('inprogress');
}
setTimeout(done, getRandomInt(500, 3000));
}
function done() {
for (var i = nextBlock; i < nextBlock + blocksToDefrag; i++) {
var block = document.getElementsByClassName('defrag-block')[i];
block.classList.remove('inprogress');
block.classList.add('done');
}
if(!finished) {
nextBlock += blocksToDefrag;
defragGo();
} else {
if(typeof callback == 'function') callback();
}
}
}
if(auto) {
started = true;
setTimeout(defragGo, getRandomInt(0, 1000));
}
this.start = function() {
defragGo();
}
}