-
Notifications
You must be signed in to change notification settings - Fork 3
/
NodeTree.jsx
246 lines (207 loc) · 6.32 KB
/
NodeTree.jsx
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
var _ = require('underscore');
var React = require('react');
var ReactART = require('react-art');
var Group = ReactART.Group;
var Path = ReactART.Path;
var Shape = ReactART.Shape;
var Surface = ReactART.Surface;
var Text = ReactART.Text;
var Pattern = ReactART.Pattern;
var circlePath = function(r) {
return new Path()
.moveTo(0, -r)
// Two arcs make a circle
.arcTo(0, r, r, r, true)
.arcTo(0, -r, r, r, true)
.close();
};
var linePath = function(x1, y1, x2, y2) {
return new Path()
.moveTo(x1, y1)
.lineTo(x2, y2);
};
var countObjEndpoints = function(obj) {
var children = Object.keys(obj);
var nChildren = children.length;
// Return a count of 1 if this node is an endpoint (i.e. has no children)
if (nChildren === 0) {
return 1;
}
// Go through each child and increment count by the number of endpoints
var count = 0;
for (var i = 0; i < nChildren; i++) {
count += countObjEndpoints(obj[children[i]]);
}
return count;
};
var calculateObjectHeight = function(obj) {
var children = Object.keys(obj);
var nChildren = children.length;
// Return a count of 1 if this node is an endpoint (i.e. has no children)
if (nChildren === 0) {
return 1;
}
// Go through each child and find the max height, then add 1 for this point
var max = 0;
for (var i = 0; i < nChildren; i++) {
var height = calculateObjectHeight(obj[children[i]]);
max = Math.max(height, max);
}
return max + 1;
};
var NodeLine = React.createClass({
render: function() {
var x = this.props.x;
var y = this.props.y;
var parentX = this.props.parentX;
var parentY = this.props.parentY;
return (<Group x={x} y={y}>
<Shape
stroke="#ddd"
strokeWidth="3"
d={linePath(0, 0, parentX, parentY)}/>
</Group>);
}
});
var Node = React.createClass({
defaultProps: {
nodeSize: React.PropTypes.number.isRequired,
value: React.PropTypes.number.isRequired,
x: React.PropTypes.number.isRequired,
y: React.PropTypes.isRequired
},
render: function() {
var nodeSize = this.props.nodeSize;
var fontSize = nodeSize*1.5 - 4;
var fontAlignment = -fontSize / 2;
var colorHue = this.props.colorHue.toString();
var value = this.props.value;
var x = this.props.x;
var y = this.props.y;
return (<Group x={x} y={y}>
<Shape
fill={`hsl(${colorHue}, 90%, 70%)`}
stroke={`hsl(${colorHue}, 70%, 40%)`}
strokeWidth="2"
strokeLocation="inside"
strokeJoin="round"
d={circlePath(this.props.nodeSize)}/>
<Text
fill={`hsl(${colorHue}, 70%, 30%)`}
font={`normal ${fontSize}px monospace`}
y={fontAlignment}
alignment="center">
{value}
</Text>
</Group>);
}
});
var NodeTree = React.createClass({
defaultProps: {
treeObj: React.PropTypes.object.isRequired,
nNodes: React.PropTypes.number.isRequired
},
getInitialState: function() {
return {
surfaceWidth: 0,
nodeSize: 15
};
},
handleResize: function() {
var surfaceWidth = this.refs.surfaceContainer.getDOMNode().offsetWidth;
var horizontalSpacing = surfaceWidth/this.props.nNodes;
var defaultSize = 15;
var minSpacing = 2*defaultSize + 5;
var minSize = 9;
this.setState({
surfaceWidth: surfaceWidth,
nodeSize: horizontalSpacing < minSpacing ? minSize : defaultSize
});
},
componentDidMount: function() {
this.handleResize();
window.addEventListener('resize', this.handleResize);
},
componentWillUnmount: function() {
window.removeEventListener('resize', this.handleResize);
},
getHorizontalSpacing: function() {
return this.state.surfaceWidth/this.props.nNodes;
},
getVerticalSpacing: function() {
return this.state.nodeSize * 3;
},
calculateSurfaceHeight: function(treeObj) {
var treeHeight = this.calculateTreeHeight(treeObj);
var verticalSpacing = this.getVerticalSpacing();
return (treeHeight - 1)*(verticalSpacing) + this.state.nodeSize;
},
calculateTreeHeight: function(treeObj) {
return calculateObjectHeight(treeObj);
},
_renderTreeNodes: function(nodesObj, type, x, y, parent, colorHue) {
var nodes = [];
var childX;
var childY;
var nodeSize = this.state.nodeSize;
var hasParent = !_.isEmpty(parent);
var parentX = parent.x;
var parentY = parent.y;
// If this is a top level node place it near the top of the box; otherwise
// do the full vertical spacing from the parent
var verticalSpacing = hasParent ? this.getVerticalSpacing() : 2*nodeSize;
var horizontalSpacing = this.getHorizontalSpacing();
colorHue = colorHue != null ? colorHue + 30 : 0;
_.each(nodesObj, (children, curNodeValue) => {
var numEndpoints = countObjEndpoints(children);
var hasChildren = !_.isEmpty(children);
// Place the node halfway between the leftmost and rightmost endnodes
childX = x + horizontalSpacing*numEndpoints/2;
childY = y + verticalSpacing;
var nodeProps = {
x: childX,
y: childY,
nodeSize: this.state.nodeSize,
key: "node-" + type + "-" + curNodeValue
};
if (type === "line" && hasParent) {
nodes.push(<NodeLine
{...nodeProps}
parentX={parentX - childX}
parentY={parentY - childY}/>);
} else if (type === "node") {
nodes.push(<Node
{...nodeProps}
colorHue={colorHue}
value={curNodeValue}/>);
}
if (hasChildren) {
// Draw the children of this node ...
nodes.push(
this._renderTreeNodes(children, type, x, childY,
{x: childX, y:childY}, colorHue)
);
}
x += numEndpoints * horizontalSpacing;
});
return nodes;
},
render: function() {
var x = 0;
var y = 0;
var treeObj = this.props.treeObj;
var surfaceWidth = this.state.surfaceWidth;
var surfaceHeight = this.calculateSurfaceHeight(treeObj);
return (<div ref="surfaceContainer">
<Surface
width={surfaceWidth}
height={surfaceHeight}>
<Group>
{this._renderTreeNodes(treeObj, "line", x, y, {})}
{this._renderTreeNodes(treeObj, "node", x, y, {})}
</Group>
</Surface>
</div>);
}
});
module.exports = NodeTree;