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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {
Component,
createRef,
createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const { div, span } = dom;
const Actions = require("resource://devtools/client/netmonitor/src/actions/index.js");
const {
PANELS,
} = require("resource://devtools/client/netmonitor/src/constants.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const {
connect,
} = require("resource://devtools/client/shared/redux/visibility-handler-connect.js");
const TreeViewClass = require("resource://devtools/client/shared/components/tree/TreeView.js");
const TreeView = createFactory(TreeViewClass);
const LabelCell = createFactory(
require("resource://devtools/client/shared/components/tree/LabelCell.js")
);
const {
SearchProvider,
} = require("resource://devtools/client/netmonitor/src/components/search/search-provider.js");
const Toolbar = createFactory(
require("resource://devtools/client/netmonitor/src/components/search/Toolbar.js")
);
const StatusBar = createFactory(
require("resource://devtools/client/netmonitor/src/components/search/StatusBar.js")
);
const {
limitTooltipLength,
} = require("resource://devtools/client/netmonitor/src/utils/tooltips.js");
// There are two levels in the search panel tree hierarchy:
// 0: Resource - represents the source request object
// 1: Search Result - represents a match coming from the parent resource
const RESOURCE_LEVEL = 0;
const SEARCH_RESULT_LEVEL = 1;
/**
* This component is responsible for rendering all search results
* coming from the current search.
*/
class SearchPanel extends Component {
static get propTypes() {
return {
clearSearchResults: PropTypes.func.isRequired,
openSearch: PropTypes.func.isRequired,
closeSearch: PropTypes.func.isRequired,
search: PropTypes.func.isRequired,
caseSensitive: PropTypes.bool,
connector: PropTypes.object.isRequired,
addSearchQuery: PropTypes.func.isRequired,
query: PropTypes.string.isRequired,
results: PropTypes.array,
navigate: PropTypes.func.isRequired,
isDisplaying: PropTypes.bool.isRequired,
};
}
constructor(props) {
super(props);
this.searchboxRef = createRef();
this.renderValue = this.renderValue.bind(this);
this.renderLabel = this.renderLabel.bind(this);
this.onClickTreeRow = this.onClickTreeRow.bind(this);
this.provider = SearchProvider;
}
componentDidMount() {
if (this.searchboxRef) {
this.searchboxRef.current.focus();
}
}
componentDidUpdate(prevProps) {
if (this.props.isDisplaying && !prevProps.isDisplaying) {
this.searchboxRef.current.focus();
}
}
onClickTreeRow(path, event, member) {
if (member.object.parentResource) {
this.props.navigate(member.object);
}
}
/**
* Custom TreeView label rendering. The search result
* value isn't rendered in separate column, but in the
* same column as the label (to save space).
*/
renderLabel(props) {
const { member } = props;
const level = member.level || 0;
const className = level == RESOURCE_LEVEL ? "resourceCell" : "resultCell";
// Customize label rendering by adding a suffix/value
const renderSuffix = () => {
return dom.span(
{
className,
},
" ",
this.renderValue(props)
);
};
return LabelCell({
...props,
title:
member.level == 1
? limitTooltipLength(member.object.value)
: this.provider.getResourceTooltipLabel(member.object),
renderSuffix,
});
}
renderTree() {
const { results } = this.props;
return TreeView({
object: results,
provider: this.provider,
expandableStrings: false,
renderLabelCell: this.renderLabel,
columns: [],
onClickRow: this.onClickTreeRow,
});
}
/**
* Custom tree value rendering. This method is responsible for
* rendering highlighted query string within the search result
* result tree.
*/
renderValue(props) {
const { member } = props;
const { query, caseSensitive } = this.props;
// Handle only second level (zero based) that displays
// the search result. Find the query string inside the
// search result value (`props.object`) and render it
// within a span element with proper class name.
// level 0 = resource name
if (member.level === SEARCH_RESULT_LEVEL) {
const { object } = member;
// Handles multiple matches in a string
if (object.startIndex && object.startIndex.length > 1) {
let indexStart = 0;
const allMatches = object.startIndex.map((match, index) => {
if (index === 0) {
indexStart = match - 50;
}
const highlightedMatch = [
span(
{ key: "match-" + match },
object.value.substring(indexStart, match - query.length)
),
span(
{
className: "query-match",
key: "match-" + match + "-highlight",
},
object.value.substring(match - query.length, match)
),
];
indexStart = match;
return highlightedMatch;
});
return span(
{
title: limitTooltipLength(object.value),
},
allMatches
);
}
const indexStart = caseSensitive
? object.value.indexOf(query)
: object.value.toLowerCase().indexOf(query.toLowerCase());
const indexEnd = indexStart + query.length;
// Handles a match in a string
if (indexStart >= 0) {
return span(
{ title: limitTooltipLength(object.value) },
span({}, object.value.substring(0, indexStart)),
span(
{ className: "query-match" },
object.value.substring(indexStart, indexStart + query.length)
),
span({}, object.value.substring(indexEnd, object.value.length))
);
}
// Default for key:value matches where query might not
// be present in the value, but found in the key.
return span(
{ title: limitTooltipLength(object.value) },
span({}, object.value)
);
}
return this.provider.getValue(member.object);
}
render() {
const {
openSearch,
closeSearch,
clearSearchResults,
connector,
addSearchQuery,
search,
} = this.props;
return div(
{ className: "search-panel", style: { width: "100%" } },
Toolbar({
searchboxRef: this.searchboxRef,
openSearch,
closeSearch,
clearSearchResults,
addSearchQuery,
search,
connector,
}),
div(
{ className: "search-panel-content", style: { width: "100%" } },
this.renderTree()
),
StatusBar()
);
}
}
module.exports = connect(
state => ({
query: state.search.query,
caseSensitive: state.search.caseSensitive,
results: state.search.results,
ongoingSearch: state.search.ongoingSearch,
isDisplaying: state.ui.selectedActionBarTabId === PANELS.SEARCH,
status: state.search.status,
}),
dispatch => ({
closeSearch: () => dispatch(Actions.closeSearch()),
openSearch: () => dispatch(Actions.openSearch()),
search: () => dispatch(Actions.search()),
clearSearchResults: () => dispatch(Actions.clearSearchResults()),
addSearchQuery: query => dispatch(Actions.addSearchQuery(query)),
navigate: searchResult => dispatch(Actions.navigate(searchResult)),
})
)(SearchPanel);
|