forked from gaearon/react-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepGraph.js
More file actions
213 lines (199 loc) · 5.17 KB
/
DepGraph.js
File metadata and controls
213 lines (199 loc) · 5.17 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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @ xx flow unused at the moment
*/
'use strict';
var React = require('react');
var decorate = require('../../frontend/decorate');
var crawlChildren = require('./crawlChildren');
var dagre = require('dagre');
class DepGraph extends React.Component {
constructor(props: Object) {
super(props);
this.state = {renderCount: 0};
}
render() {
if (this.state.renderCount > 0) {
return (
<DepWrapper
renderCount={this.state.renderCount}
onClose={() => this.setState({renderCount: 0})}
onReload={() => this.setState({renderCount: this.state.renderCount + 1})}
/>
);
}
return <button onClick={() => this.setState({renderCount: 1})}>Calculate DepGraph</button>;
}
}
class DisplayDeps extends React.Component {
props: Object;
componentWillReceiveProps(nextProps) {
if (nextProps.selected !== this.props.selected) {
this.props.onClose();
}
}
render() {
return (
<div style={styles.container}>
<div style={styles.scrollParent}>
<SvgGraph
onHover={this.props.onHover}
onClick={this.props.onClick}
graph={this.props.graph}
/>
</div>
<div style={styles.buttons}>
<button onClick={this.props.onReload}>Reload</button>
<button onClick={this.props.onClose}>×</button>
</div>
</div>
);
}
}
class SvgGraph extends React.Component {
props: Object;
render() {
var graph = this.props.graph;
if (!graph) {
return <em>No graph to display. Select something else</em>;
}
var transform = 'translate(10, 10)';
return (
<svg style={styles.svg} width={graph.graph().width + 20} height={graph.graph().height + 20}>
<g transform={transform}>
{graph.edges().map(n => {
var edge = graph.edge(n);
return (
<polyline
points={edge.points.map(p => p.x + ',' + p.y).join(' ')}
fill="none"
stroke="orange"
strokeWidth="2"
/>
);
})}
</g>
<g transform={transform}>
{graph.nodes().map(n => {
var node = graph.node(n);
return (
<rect
onMouseOver={this.props.onHover.bind(null, node.label)}
onMouseOut={this.props.onHover.bind(null, null)}
onClick={this.props.onClick.bind(null, node.label)}
height={node.height}
width={node.width}
x={node.x - node.width / 2}
y={node.y - node.height / 2}
style={styles.rect}
fill="white"
stroke="black"
strokeWidth="1"
/>
);
})}
</g>
<g transform={transform}>
{graph.nodes().map(n => {
var node = graph.node(n);
return (
<text
style={{pointerEvents: 'none'}}
x={node.x}
y={node.y + node.height / 4}
textAnchor="middle"
fontSize="10"
fontFamily="sans-serif"
>{node.label + ' ' + node.count}</text>
);
})}
</g>
</svg>
);
}
}
var styles = {
container: {
border: '1px solid red',
position: 'relative',
minWidth: 0,
minHeight: 0,
flex: 1,
},
scrollParent: {
overflow: 'auto',
top: 0,
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
textAlign: 'center',
},
rect: {
cursor: 'pointer',
},
svg: {
flexShrink: 0,
},
buttons: {
position: 'absolute',
bottom: 3,
right: 3,
},
};
function dagrize(graph) {
var g = new dagre.graphlib.Graph();
g.setGraph({
nodesep: 20,
ranksep: 50,
});
g.setDefaultEdgeLabel(() => ({}));
var hasNodes = false;
for (var nodeName in graph.nodes) {
hasNodes = true;
g.setNode(nodeName, {
label: nodeName,
count: graph.nodes[nodeName],
width: nodeName.length * 7 + 20,
height: 20,
});
}
if (!hasNodes) {
return false;
}
for (var edgeName in graph.edges) {
var parts = edgeName.split('\x1f');
if (parts[0] === '$root') {
continue;
}
g.setEdge(parts[0], parts[1], {label: graph[edgeName]});
}
dagre.layout(g);
return g;
}
var DepWrapper = decorate({
listeners: () => ['selected'],
shouldUpdate(nextProps, props) {
return nextProps.renderCount !== props.renderCount;
},
props(store) {
var graph = {
edges: {},
nodes: {},
};
crawlChildren('$root', [store.selected], store._nodes, 0, graph);
return {
selected: store.selected,
graph: dagrize(graph),
onHover: name => store.hoverClass(name),
onClick: name => store.selectFirstOfClass(name),
};
},
}, DisplayDeps);
module.exports = DepGraph;