-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
118 lines (98 loc) · 2.83 KB
/
scripts.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
$().ready( function() {
DisplayNodes.setup( $("div.smartNodeZone") );
});
var DisplayNodes = {
WIDTH: 800,
HEIGHT: 800,
DIM: 800,
setup: function( $smartZone ) {
$smartZone.css( "width", DisplayNodes.WIDTH + "px" )
$smartZone.css( "height", DisplayNodes.HEIGHT + "px" )
var $rawNodes = $smartZone.find( ".smartNode" );
var root = null;
$rawNodes.each( function() {
var $this = $(this);
var node = TreeNode.fromID( $this.attr("data-id"), root );
if ( root == null ) {
root = node;
}
node.displayObject = $this;
});
DisplayNodes.assignZones( root );
},
assignZones: function( node ) {
if ( node.root() ) {
node.zone = new NodeZone( 0, 0, DisplayNodes.WIDTH, DisplayNodes.HEIGHT )
}
DisplayNodes.updateLoc( node );
var isVert = ( node.depth()%2 ) == 0;
var nChildren = node.children.length;
var zones = node.zone.divide( nChildren, isVert );
for ( var i=0; i<nChildren; i++ ) {
var child = node.children[i];
child.zone = zones[i];
DisplayNodes.assignZones( node.children[i] );
}
},
updateLoc: function( node ) {
DisplayNodes.moveTo( node.displayObject, node.zone.x, node.zone.y )
node.displayObject.css( "width", node.zone.width + "px" );
node.displayObject.css( "height", node.zone.height + "px" );
if ( ( node.depth()%2 ) == 0 ) {
node.displayObject.addClass( "vert" );
} else {
node.displayObject.addClass( "horiz" );
}
},
moveTo: function( $node, x, y ) {
x = Math.min( x, DisplayNodes.WIDTH );
x = Math.max( x, 0 );
y = Math.min( y, DisplayNodes.HEIGHT );
y = Math.max( y, 0 );
x = Math.round( x );
y = Math.round( y );
$node.css( "left", x + "px");
$node.css( "top", y + "px");
}
};
function NodeZone( x, y, width, height ) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.divide = function( n, vSplit ) {
if ( n%2 == 1 ) { n++; }
var halfN = n/2;
var zones = [];
/*
var newW = vSplit ? this.width/2 : this.width / halfN;
var newh = vSplit ? this.height/ halfN : this.height/2;
for ( var i=0; i<halfN; i++ ) {
var incX = vSplit ? 0 : i*newH;
var incY = vSplit ? i*newW : 0;
}
*/
if ( vSplit ) {
var newW = this.width / 2;
var newH = this.height / halfN;
for ( var i=0; i<halfN; i++ ) {
var inc = i*newH;
var z1 = new NodeZone( x, y+inc, newW, newH );
var z2 = new NodeZone( x+newW, y+inc, newW, newH );
zones.push( z1 )
zones.push( z2 )
}
} else {
var newW = this.width / halfN;
var newH = this.height / 2;
for ( var i=0; i<halfN; i++ ) {
var inc = i*newH;
var z1 = new NodeZone( x+inc, y, newW, newH );
var z2 = new NodeZone( x+inc, y+newH, newW, newH );
zones.push( z1 )
zones.push( z2 )
}
}
return zones;
}
}