forked from kidaa30/js-visual-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.Block.js
83 lines (65 loc) · 1.79 KB
/
App.Block.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
var App = App || new Object();
(function($) {
function Item(height) {
this.height = height;
this.div = document.createElement('DIV');
this.div.style.height = height + 'em';
this.i = -1;
this.j = -1;
this.setHeight = function(height) { this.height = height; this.div.style.height = height + 'em'; };
}
$.Block = function(count) {
this.div = document.createElement('DIV');
this.div.className = 'sortBlock';
this.items = new Array();
for (var i = 1; i <= count; i++) {
this.addItem(new Item(5 * i / count));
}
this.render();
}
$.Block.prototype.addItem = function(item) {
this.items.push(item);
};
$.Block.prototype.render = function() {
this.div.innerHTML = '';
var maintainHeight = document.createElement("div");
maintainHeight.className = "maintainHeight";
this.div.appendChild(maintainHeight);
for (var i = 0; i < this.items.length; i++) {
if (this.i == i || this.j == i) {
this.items[i].div.className = 'active';
} else {
this.items[i].div.className = '';
}
this.div.appendChild(this.items[i].div);
}
};
$.Block.prototype.getCount = function() {
return this.items.length;
};
// test whether element at j is less than element at i
$.Block.prototype.less = function(i, j) {
var x = this.items[i].height;
var y = this.items[j].height;
if (x > y) {
return true;
} else {
return false;
}
};
$.Block.prototype.exch = function(i, j) {
var item = this.items[i];
this.items[i] = this.items[j];
this.items[j] = item;
};
$.Block.prototype.reverse = function() {
this.items.reverse();
};
$.Block.prototype.randomize = function() {
var items = this.items;
for (var i = 0; i < items.length; i++) {
var j = i + Math.floor(Math.random() * (items.length - i));
this.exch(i, j);
}
};
})(App);