forked from Lapple/react-json-inspector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-inspector.js
153 lines (144 loc) · 3.94 KB
/
json-inspector.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
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
var React = require('react');
var PropTypes = require('prop-types');
var createClass = require('create-react-class');
var Leaf = require('./lib/leaf');
var leaf = React.createFactory(Leaf);
var SearchBar = require('./lib/search-bar');
var searchBar = React.createFactory(SearchBar);
var filterer = require('./lib/filterer');
var isEmpty = require('./lib/is-empty');
var lens = require('./lib/lens');
var noop = require('./lib/noop');
module.exports = createClass({
propTypes: {
data: PropTypes.any.isRequired,
// For now it expects a factory function, not element.
search: PropTypes.oneOfType([
PropTypes.func,
PropTypes.bool
]),
onClick: PropTypes.func,
validateQuery: PropTypes.func,
isExpanded: PropTypes.func,
isRootCollapsed: PropTypes.bool,
filterOptions: PropTypes.object,
query: PropTypes.string,
filterFunc: PropTypes.func,
shouldHighlight: PropTypes.func,
highlightRenderer: PropTypes.func
},
getDefaultProps: function() {
return {
data: null,
search: searchBar,
className: '',
id: 'json-' + Date.now(),
onClick: noop,
filterOptions: {},
validateQuery: function(query) {
return query !== null && query !== undefined;
},
/**
* Decide whether the leaf node at given `keypath` should be
* expanded initially.
* @param {String} keypath
* @param {Any} value
* @return {Boolean}
*/
isExpanded: function(keypath, value) {
return false;
},
highlightRenderer: null
};
},
render: function() {
var p = this.props;
var s = this.state;
var isQueryValid = (
p.query !== '' &&
p.validateQuery(p.query)
);
var data = (
isQueryValid ?
s.filterer(p.query) :
p.data
);
var isNotFound = (
isQueryValid &&
isEmpty(data)
);
return React.createElement('div', { className: 'json-inspector ' + p.className },
this.renderToolbar(),
(
isNotFound ?
React.createElement('div', { className: 'json-inspector__not-found' }, 'Nothing found') :
leaf({
data: data,
onClick: p.onClick,
id: p.id,
getOriginal: this.getOriginal,
query: (
isQueryValid ?
p.query :
null
),
label: 'root',
root: true,
isRootCollapsed: p.isRootCollapsed,
isExpanded: p.isExpanded,
interactiveLabel: p.interactiveLabel,
shouldHighlight: p.shouldHighlight,
highlightRenderer: p.highlightRenderer,
})
)
);
},
renderToolbar: function() {
var search = this.props.search;
if (search) {
return React.createElement('div', { className: 'json-inspector__toolbar' },
search({
onChange: this.search,
data: this.props.data,
query: this.state.query
})
);
}
},
search: function(query) {
this.setState({
query: query
});
},
componentWillMount: function() {
this.createFilterer(this.props.data, this.props.filterOptions, this.props.filterFunc);
},
componentWillReceiveProps: function(p) {
this.createFilterer(p.data, p.filterOptions, p.filterFunc);
this.setState({query: p.query});
},
shouldComponentUpdate: function (p, s) {
return (
p.filterFunc !== this.props.filterFunc ||
p.query !== this.props.query ||
p.data !== this.props.data ||
p.onClick !== this.props.onClick
);
},
getFilterer: function(pFilterFunc) {
var filterFunc = pFilterFunc;
if (!filterFunc) {
filterFunc = filterer
}
return filterFunc;
},
createFilterer: function(data, options, filterFunc) {
var f = this.getFilterer(filterFunc);
this.setState({
filterer: f(data, options)
});
},
getOriginal: function(path) {
return lens(this.props.data, path);
}
});