forked from railsware/upterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONTree.tsx
336 lines (309 loc) · 10 KB
/
JSONTree.tsx
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import * as React from "react";
import {colors} from "../views/css/colors";
/*
=========================
* React JSONTree
* http://eskimospy.com/stuff/react/json/
* Copyright 2014, David Vedder
* MIT Licence
=========================
*/
const listStyle = {
padding: "2px 0",
listStyleType: "none",
};
const labelStyle = {
display: "inline-block",
marginRight: "0.5em",
color: colors.magenta,
};
const childrenCount = {
WebkitUserSelect: "none",
userSelect: "none",
cursor: "default",
};
type JSONProps = {
data: any,
keyName?: any,
key?: any,
initialExpanded?: boolean,
};
type JSONState = {
expanded?: any,
createdChildNodes?: any,
};
type JSONValueProps = {
value: any,
keyName: any,
key: any,
};
/**
* Returns the type of an object as a string.
*
* @param obj Object The object you want to inspect
* @return String The object's type
*/
let objType = function (obj: any) {
return Object.prototype.toString.call(obj).slice(8, -1);
};
/**
* Creates a React JSON Viewer component for a key and it's associated data
*
* @param key String The JSON key (property name) for the node
* @param value Mixed The associated data for the JSON key
* @return Component The React Component for that node
*/
let grabNode = function (key: any, value: any): any {
let nodeType = objType(value);
let aKey = key + Date.now();
if (nodeType === "Object") {
return <JSONObjectNode data={value} keyName={key} key={aKey} />;
} else if (nodeType === "Array") {
return <JSONArrayNode data={value} keyName={key} key={aKey} />;
} else if (nodeType === "String") {
return <JSONStringNode keyName={key} value={value} key={aKey} />;
} else if (nodeType === "Number") {
return <JSONNumberNode keyName={key} value={value} key={aKey} />;
} else if (nodeType === "Boolean") {
return <JSONBooleanNode keyName={key} value={value} key={aKey} />;
} else if (nodeType === "Null") {
return <JSONNullNode keyName={key} value={value} key={aKey} />;
} else {
return <div>How did this happen? {nodeType}</div>;
}
};
/**
* Mixin for stopping events from propagating and collapsing our tree all
* willy nilly.
*/
let squashClick = function (e: any) {
e.stopPropagation();
};
/**
* Array node class. If you have an array, this is what you should use to
* display it.
*/
class JSONArrayNode extends React.Component<JSONProps, JSONState> {
private itemString: boolean | string;
private needsChildNodes: boolean | Array<any>;
private renderedChildren: Array<any>;
constructor(props: JSONProps) {
super(props);
this.renderedChildren = [];
this.needsChildNodes = [];
this.itemString = false;
this.state = {
expanded: props.initialExpanded,
createdChildNodes: false,
};
}
getDefaultProps() {
return { data: [], initialExpanded: false };
}
componentWillReceiveProps() {
// resets our caches and flags we need to build child nodes again
this.renderedChildren = [];
this.itemString = false;
this.needsChildNodes = true;
}
/**
* Returns the child nodes for each element in the array. If we have
* generated them previously, we return from cache, otherwise we create
* them.
*/
getChildNodes() {
let childNodes: Array<any> = [];
if (this.state.expanded && this.needsChildNodes) {
for (let i = 0; i < this.props.data.length; i += 1) {
childNodes.push(grabNode(i, this.props.data[i]));
}
this.needsChildNodes = false;
this.renderedChildren = childNodes;
}
return this.renderedChildren;
}
/**
* Returns the "n Items" string for this node, generating and
* caching it if it hasn't been created yet.
*/
getItemString() {
if (!this.itemString) {
let lenWord = (this.props.data.length === 1) ? " Item" : " Items";
this.itemString = this.props.data.length + lenWord;
}
return this.itemString;
}
render(): JSX.Element {
let childNodes = this.getChildNodes();
let childListStyle = {
display: (this.state.expanded) ? "block" : "none",
};
let cls = "array jsonTreeParentNode";
cls += (this.state.expanded) ? " expanded" : "";
return <li style={listStyle} className={cls} onClick={e => {
e.stopPropagation();
this.setState({expanded: !this.state.expanded});
}}>
<label style={labelStyle}>{this.props.keyName}: </label>{"[...] "}
<span style={childrenCount}>{this.getItemString()}</span>
<ol style={childListStyle}>{childNodes}</ol>
</li>;
}
}
/**
* Object node class. If you have an object, this is what you should use to
* display it.
*/
class JSONObjectNode extends React.Component<JSONProps, any> {
private itemString: boolean | string;
private needsChildNodes: boolean | Array<any>;
private renderedChildren: Array<any>;
constructor(props: JSONProps) {
super(props);
this.renderedChildren = [];
this.needsChildNodes = [];
this.itemString = false;
this.state = {
expanded: props.initialExpanded,
createdChildNodes: false,
};
}
getDefaultProps() {
return { data: [], initialExpanded: false };
}
componentWillReceiveProps() {
// resets our caches and flags we need to build child nodes again
this.renderedChildren = [];
this.itemString = false;
this.needsChildNodes = true;
}
/**
* Returns the child nodes for each element in the object. If we have
* generated them previously, we return from cache, otherwise we create
* them.
*/
getChildNodes() {
if (this.state.expanded && this.needsChildNodes) {
let obj = this.props.data;
let childNodes: Array<any> = [];
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
childNodes.push( grabNode(k, obj[k]));
}
}
this.needsChildNodes = false;
this.renderedChildren = childNodes;
}
return this.renderedChildren;
}
/**
* Returns the "n Items" string for this node, generating and
* caching it if it hasn't been created yet.
*/
getItemString() {
if (!this.itemString) {
let obj = this.props.data;
let len = 0;
let lenWord = " Items";
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
len += 1;
}
}
if (len === 1) {
lenWord = " Item";
}
this.itemString = len + lenWord;
}
return this.itemString;
}
render(): JSX.Element {
let childListStyle = {
display: (this.state.expanded) ? "block" : "none",
};
let cls = "object jsonTreeParentNode";
cls += (this.state.expanded) ? " expanded" : "";
return (
<li style={listStyle} className={cls} onClick={e => {
e.stopPropagation();
this.setState({expanded: !this.state.expanded});
}}>
<label style={labelStyle}>{this.props.keyName}: </label>{"{...} "}
<span style={childrenCount}>{this.getItemString()}</span>
<ul style={childListStyle}>{this.getChildNodes()}</ul>
</li>
);
}
}
/**
* String node component
*/
class JSONStringNode extends React.Component<JSONValueProps, any> {
render(): JSX.Element {
return (
<li style={listStyle} className="string" onClick={squashClick}>
<label style={labelStyle}>{this.props.keyName}: </label>
<span style={{color: colors.green}}>"{this.props.value}"</span>
</li>
);
}
}
/**
* Number node component
*/
class JSONNumberNode extends React.Component<JSONValueProps, any> {
render(): JSX.Element {
return (
<li style={listStyle} className="" onClick={squashClick}>
<label style={labelStyle}>{this.props.keyName}: </label>
<span style={{color: colors.blue}}>{this.props.value}</span>
</li>
);
}
}
/**
* Null node component
*/
class JSONNullNode extends React.Component<JSONValueProps, any> {
render(): JSX.Element {
return (
<li style={listStyle} className="" onClick={squashClick}>
<label style={labelStyle}>{this.props.keyName}: </label>
<span style={{color: colors.red}}>null</span>
</li>
);
}
}
/**
* Boolean node component
*/
class JSONBooleanNode extends React.Component<JSONValueProps, any> {
render(): JSX.Element {
let truthString = (this.props.value) ? "true" : "false";
return <li style={listStyle} className={"boolean " + truthString} onClick={squashClick}>
<label style={labelStyle}>{this.props.keyName}: </label>
<span style={{color: colors.cyan}}>{truthString}</span>
</li>;
}
}
/**
* JSONTree component. This is the 'viewer' base. Pass it a `data` prop and it
* will render that data, or pass it a `source` URL prop and it will make
* an XMLHttpRequest for said URL and render that when it loads the data.
*
* The first node it draws will be expanded by default.
*/
export class JSONTree extends React.Component<JSONProps, any> {
render(): JSX.Element {
let nodeType = objType(this.props.data);
let rootNode: JSX.Element;
if (nodeType === "Object") {
rootNode = <JSONObjectNode data={this.props.data} keyName="(root)" initialExpanded={true} />;
} else if (nodeType === "Array") {
rootNode = <JSONArrayNode data={this.props.data} initialExpanded={true} keyName="(root)" />;
} else {
return <span>How did you manage that?</span>;
}
return <ul>{rootNode}</ul>;
}
}