This repository has been archived by the owner on Feb 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
json-inspector.js
169 lines (159 loc) · 4.96 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var React = require('react');
var createReactClass = require('create-react-class');
var PropTypes = require('prop-types');
var debounce = require('debounce');
var h = React.createElement;
var Leaf = require('./lib/leaf');
var SearchBar = require('./lib/search-bar');
var filterer = require('./lib/filterer');
var isEmpty = require('./lib/is-empty');
var lens = require('./lib/lens');
var noop = require('./lib/noop');
module.exports = createReactClass({
propTypes: {
data: PropTypes.any.isRequired,
// For now it expects a factory function, not element.
search: PropTypes.oneOfType([
PropTypes.func,
PropTypes.bool
]),
searchOptions: PropTypes.shape({
debounceTime: PropTypes.number
}),
onClick: PropTypes.func,
validateQuery: PropTypes.func,
isExpanded: PropTypes.func,
filterOptions: PropTypes.shape({
cacheResults: PropTypes.bool,
ignoreCase: PropTypes.bool
}),
query: PropTypes.string,
verboseShowOriginal: PropTypes.bool
},
getDefaultProps: function() {
return {
data: null,
search: SearchBar,
searchOptions: {
debounceTime: 0
},
className: '',
id: 'json-' + Date.now(),
onClick: noop,
filterOptions: {
cacheResults: true,
ignoreCase: false
},
validateQuery: function(query) {
return query.length >= 2;
},
/**
* 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;
},
verboseShowOriginal: false
};
},
getInitialState: function() {
return {
query: this.props.query || ''
};
},
render: function() {
var p = this.props;
var s = this.state;
var isQueryValid = (
s.query !== '' &&
p.validateQuery(s.query)
);
var data = (
isQueryValid ?
s.filterer(s.query) :
p.data
);
var isNotFound = (
isQueryValid &&
isEmpty(data)
);
return h('div', { className: 'json-inspector ' + p.className },
this.renderToolbar(),
(
isNotFound ?
h('div', { className: 'json-inspector__not-found' }, 'Nothing found') :
h(Leaf, {
data: data,
onClick: p.onClick,
id: p.id,
getOriginal: this.getOriginal,
query: (
isQueryValid ?
new RegExp(
s.query,
(p.filterOptions.ignoreCase ? 'i' : '')
) :
null
),
label: 'root',
root: true,
isExpanded: p.isExpanded,
interactiveLabel: p.interactiveLabel,
verboseShowOriginal: p.verboseShowOriginal
})
)
);
},
renderToolbar: function() {
var search = this.props.search;
if (search) {
return h('div', { className: 'json-inspector__toolbar' },
h(search, {
onChange: debounce(this.search, this.props.searchOptions.debounceTime),
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);
},
componentWillReceiveProps: function(p) {
this.createFilterer(p.data, p.filterOptions);
var isReceivingNewQuery = (
typeof p.query === 'string' &&
p.query !== this.state.query
);
if (isReceivingNewQuery) {
this.setState({
query: p.query
});
}
},
shouldComponentUpdate: function (p, s) {
return (
p.query !== this.props.query ||
s.query !== this.state.query ||
p.data !== this.props.data ||
p.onClick !== this.props.onClick
);
},
createFilterer: function(data, options) {
this.setState({
filterer: filterer(data, options)
});
},
getOriginal: function(path) {
return lens(this.props.data, path);
}
});