Repository navigation
Expand file tree
/
Copy pathjquery.btree.js
More file actions
223 lines (214 loc) · 7.79 KB
/
Copy pathjquery.btree.js
File metadata and controls
223 lines (214 loc) · 7.79 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
Script for displaying binary trees
@author: FM L'Heureux
@date: 2010-01-24
@version: 1.1.0
@see: http://frank-mich.com/jQuery
*/
jQuery.fn.btree = function(options){
var result = new Array();
$(this).each(function(){
var btree = new Btree($(this), options);
btree.displayTree();
result.push(btree);
});
return result;
};
function Btree(caller, options){
var jg = new jsGraphics(document.getElementById(caller.attr("id")));
caller.css("position", "relative");
this.caller = caller;
this.size = null;
this.settings = jQuery.extend({
hSpace: 10,
vSpace: 10,
borderWidth: 1,
branchColor: "#000000",
branchStroke: 2,
jsGraphics: jg,
horizontal: true,
flip: false
}, options);
};
Btree.prototype.displayTree = function(){
//display settings
var hSpace = parseInt(this.settings.hSpace, 10);
var vSpace = parseInt(this.settings.vSpace, 10);
var borderWidth = parseInt(this.settings.borderWidth, 10);
var size = this.size;//avoids interference with the jsGraphics lib
var branchStroke = parseInt(this.settings.branchStroke, 10);
var branchColor = this.settings.branchColor;
var flip = this.settings.flip;
var i = 0;
//compute the max width and max height and apply it
var spanWidth = 0;
var spanHeight = 0;
var jg = this.settings.jsGraphics;
jg.clear();
var callerId = this.caller.attr("id");
this.caller.children("div").children("span").each(function(){
$(this).css("white-space", "nowrap");
$(this).css("display", "inline-block");
if(this.size == null || i <= this.size){
spanWidth = Math.max(spanWidth, $(this).width());
spanHeight = Math.max(spanHeight, $(this).height());
i ++;
}
});
if(this.size == null){
this.size = i;
size = i;
}
i = 0;
if(vSpace < 0){
vSpace = 0;
}
if(hSpace < 0){
hSpace = 0;
}
var maxPos = btreeBoxDistance((size - 1)/2);
if(this.settings.horizontal){
//var usingVSpace = vSpace/2 - spanHeight / 2 - borderWidth;
var usingVSpace = vSpace/2 + spanHeight / 2 + borderWidth;
var usingHSpace = hSpace - spanWidth / 2 - borderWidth;
var cssHorizontal = this.settings.flip ? "right" : "left";
//position the boxes and set size
this.caller.children("div").each(function(){
var leftPos = btreeBoxDistance(i);
if(i < size){
$(this).css("position", "absolute");
$(this).css("display", "inline-block");
$(this).css("left", "");
$(this).css(cssHorizontal, (leftPos * (spanWidth + borderWidth * 2 + hSpace) + "px"));
$(this).css("top", ((i * usingVSpace) + "px"));
$(this).css("border-width", borderWidth + "px");
$(this).css("width", spanWidth);
$(this).css("height", spanHeight);
i ++
}
});
//once everything is in place, refresh jg
//alert(usingVSpace);
this.caller.height((size + 1) * usingVSpace - vSpace);
this.caller.width((hSpace + spanWidth + borderWidth*2) * (maxPos + 1) - hSpace);
var callerOffset = this.caller.offset();
var callerLeftBorder = this.caller.css("borderLeftWidth");
callerLeftBorder = parseInt(callerLeftBorder.substr(0, callerLeftBorder.length - 2), 10);
if(isNaN(callerLeftBorder)){
//IE fix - medium ~= 3
callerLeftBorder = 3;
}
var callerTopBorder = this.caller.css("borderTopWidth");
callerTopBorder = parseInt(callerTopBorder.substr(0, callerTopBorder.length - 2), 10);
if(isNaN(callerTopBorder)){
//IE fix - medium ~= 3
callerTopBorder = 3;
}
if(branchStroke > 0){
refreshJg(jg, callerId, branchStroke, branchColor);
i = 0;
this.caller.children("div").each(function(){
var leftPos = btreeBoxDistance(i);
if(i < size){
//on the left, draw straight horizontal and vertical lines
var offset = $(this).offset();
offset.top -= callerOffset.top + callerTopBorder;
offset.left -= callerOffset.left + callerLeftBorder;
offset.right = offset.left + spanWidth + borderWidth;
if(leftPos > 0){
var x = (flip ? offset.right + hSpace / 2 + borderWidth - branchStroke / 2 : offset.left - hSpace / 2 - branchStroke / 2);
var y = offset.top + (($(this).height() + borderWidth * 2) / 2);
//horizontal line
jg.drawLine(x, y - (branchStroke / 2), flip ? offset.right + borderWidth : offset.left - (branchStroke / 2), y - (branchStroke / 2));
//var quarterLength = ((Math.pow(2, leftPos) * (spanHeight + usingVSpace + borderWidth * 2)) - 1) / 4;
var halfLength = ((Math.pow(2, leftPos) * (spanHeight/2 + vSpace/2 + borderWidth))) / 2;
//vertical line
jg.drawLine(x, y - halfLength - (branchStroke / 2), x, y + halfLength - 1);
//2 extremities
jg.drawLine(flip ? x + hSpace / 2 : x - hSpace / 2 + branchStroke / 2, y - halfLength - (branchStroke / 2), x, y - halfLength - (branchStroke / 2));
jg.drawLine(flip ? x + hSpace / 2 : x - hSpace / 2 + branchStroke / 2, y + halfLength - (branchStroke / 2), x, y + halfLength - (branchStroke / 2));
}
i ++;
}
});
}
}else{
var usingHSpace = hSpace/2 + spanWidth / 2 + borderWidth;
var last = null;
var cssVertical = this.settings.flip ? "bottom" : "top";
this.caller.children("div").each(function(){
if(i < size){
//position the boxes and set size
var topPos = btreeBoxDistance(i);
$(this).css("position", "absolute");
$(this).css("top", "");
$(this).css(cssVertical, (topPos * (spanHeight + borderWidth * 2 + vSpace) + "px"));
$(this).css("left", ((i * usingHSpace) + "px"));
$(this).css("border-width", borderWidth + "px");
$(this).css("width", spanWidth);
$(this).css("height", spanHeight);
}
i ++;
});
this.caller.height((spanHeight + borderWidth * 2) * (maxPos + 1) + maxPos * vSpace);
this.caller.width((size + 1) / 2 * (spanWidth + borderWidth * 2 + hSpace) - hSpace);
var callerOffset = this.caller.offset();
var callerLeftBorder = this.caller.css("borderLeftWidth");
callerLeftBorder = parseInt(callerLeftBorder.substr(0, callerLeftBorder.length - 2), 10);
if(isNaN(callerLeftBorder)){
//IE fix
callerLeftBorder = 0;
}
var callerTopBorder = this.caller.css("borderTopWidth");
callerTopBorder = parseInt(callerTopBorder.substr(0, callerTopBorder.length - 2), 10);
if(isNaN(callerTopBorder)){
//IE fix
callerTopBorder = 0;
}
//once everything is in place, refresh jg
refreshJg(jg, callerId, branchStroke, branchColor);
i = 0;
this.caller.children("div").each(function(){
if(i < size){
var topPos = btreeBoxDistance(i);
//on the top, draw straight horizontal and vertical lines
var offset = $(this).offset();
offset.top -= callerOffset.top + callerTopBorder;
offset.left -= callerOffset.left + callerLeftBorder;
offset.bottom = offset.top + spanHeight + borderWidth * 2;
if(topPos > 0){
var x = offset.left + ($(this).width() + borderWidth * 2) / 2;
var y = flip ? offset.bottom + vSpace / 2 - branchStroke / 2 : offset.top - vSpace / 2 - branchStroke / 2;
//middle
jg.drawLine(x - (branchStroke / 2), y, x - (branchStroke / 2), flip ? offset.bottom : offset.top - (branchStroke / 2));
var halfLength = Math.pow(2, topPos) * (usingHSpace) / 2 - 1;
//horizontal line
jg.drawLine(x - halfLength - (branchStroke / 2), y, x + halfLength, y);
//2 extremities
jg.drawLine(x - halfLength - (branchStroke / 2), flip ? y + vSpace / 2 : y - vSpace/2 + branchStroke / 2, x - halfLength - (branchStroke / 2), y);
jg.drawLine(x + halfLength - (branchStroke - 2) / 2, flip ? y + vSpace / 2: y - vSpace/2 + branchStroke / 2, x + halfLength - (branchStroke - 2) / 2, y);
}
i ++;
last = $(this);
}
});
}
jg.paint();
this.settings.jsGraphics = jg;
};
Btree.prototype.clear = function(){
this.settings.jsGraphics.clear();
};
btreeBoxDistance = function(i){
var counter = 0;
while(i % 2 == 1){
counter ++;
i = (i - 1) / 2;
}
return counter;
};
refreshJg = function(jg, callerId, branchStroke, branchColor){
jg.clear();
jg.setStroke(branchStroke);
jg.setColor(branchColor);
};