Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/network/JSONView.js

Issue 1899893003: [Devtools] JSONView implements Searchable interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 /** 31 /**
32 * @constructor 32 * @constructor
33 * @extends {WebInspector.VBox} 33 * @extends {WebInspector.VBox}
34 * @implements {WebInspector.Searchable}
34 * @param {!WebInspector.ParsedJSON} parsedJSON 35 * @param {!WebInspector.ParsedJSON} parsedJSON
35 */ 36 */
36 WebInspector.JSONView = function(parsedJSON) 37 WebInspector.JSONView = function(parsedJSON)
37 { 38 {
38 WebInspector.VBox.call(this); 39 WebInspector.VBox.call(this);
39 this._parsedJSON = parsedJSON; 40 this._parsedJSON = parsedJSON;
40 this.element.classList.add("json-view"); 41 this.element.classList.add("json-view");
42
43 /** @type {?WebInspector.SearchableView} */
44 this._searchableView;
45 /** @type {!WebInspector.ObjectPropertiesSection} */
46 this._treeOutline;
allada 2016/05/05 23:01:35 The name did not make sense so I renamed it to mat
47 /** @type {number} */
48 this._currentSearchFocusIndex = 0;
49 /** @type {!Array.<!TreeElement>} */
50 this._currentSearchTreeElements = [];
51 /** @type {?RegExp} */
52 this._searchRegex = null;
41 } 53 }
42 54
43 /** 55 /**
56 * @param {!WebInspector.ParsedJSON} parsedJSON
57 * @return {!WebInspector.SearchableView}
58 */
59 WebInspector.JSONView.createSearchableView = function(parsedJSON)
60 {
61 var jsonView = new WebInspector.JSONView(parsedJSON);
62 var searchableView = new WebInspector.SearchableView(jsonView);
63 searchableView.setPlaceholder(WebInspector.UIString("Find"));
64 jsonView.setSearchableView(searchableView);
65 jsonView.show(searchableView.element);
66 jsonView.element.setAttribute("tabIndex", 0);
67 return searchableView;
68 }
69
70 /**
44 * @param {?string} text 71 * @param {?string} text
45 * @return {!Promise<?WebInspector.ParsedJSON>} 72 * @return {!Promise<?WebInspector.ParsedJSON>}
46 */ 73 */
47 WebInspector.JSONView.parseJSON = function(text) 74 WebInspector.JSONView.parseJSON = function(text)
48 { 75 {
49 var returnObj = null; 76 var returnObj = null;
50 if (text) 77 if (text)
51 returnObj = WebInspector.JSONView._extractJSON(/** @type {string} */ (te xt)); 78 returnObj = WebInspector.JSONView._extractJSON(/** @type {string} */ (te xt));
52 if (!returnObj) 79 if (!returnObj)
53 return Promise.resolve(/** @type {?WebInspector.ParsedJSON} */ (null)); 80 return Promise.resolve(/** @type {?WebInspector.ParsedJSON} */ (null));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 { 132 {
106 var start = text.indexOf(open); 133 var start = text.indexOf(open);
107 var end = text.lastIndexOf(close); 134 var end = text.lastIndexOf(close);
108 var length = end - start - 1; 135 var length = end - start - 1;
109 if (start == -1 || end == -1 || end < start) 136 if (start == -1 || end == -1 || end < start)
110 length = -1; 137 length = -1;
111 return {start: start, end: end, length: length}; 138 return {start: start, end: end, length: length};
112 } 139 }
113 140
114 WebInspector.JSONView.prototype = { 141 WebInspector.JSONView.prototype = {
142 /**
143 * @param {?WebInspector.SearchableView} view
144 */
145 setSearchableView: function(view)
lushnikov 2016/05/06 18:46:25 Could this be made private?
allada 2016/05/06 19:31:50 Done. But I did it because I was following how Sou
146 {
147 this._searchableView = view;
148 },
149
115 wasShown: function() 150 wasShown: function()
116 { 151 {
117 this._initialize(); 152 this._initialize();
118 }, 153 },
119 154
120 _initialize: function() 155 _initialize: function()
121 { 156 {
122 if (this._initialized) 157 if (this._initialized)
123 return; 158 return;
124 this._initialized = true; 159 this._initialized = true;
125 160
126 var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.dat a); 161 var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.dat a);
127 var title = this._parsedJSON.prefix + obj.description + this._parsedJSON .suffix; 162 var title = this._parsedJSON.prefix + obj.description + this._parsedJSON .suffix;
128 var section = new WebInspector.ObjectPropertiesSection(obj, title); 163 this._treeOutline = new WebInspector.ObjectPropertiesSection(obj, title) ;
129 section.setEditable(false); 164 this._treeOutline.setEditable(false);
130 section.expand(); 165 this._treeOutline.expand();
131 this.element.appendChild(section.element); 166 this.element.appendChild(this._treeOutline.element);
167 },
168
169 /**
170 * @param {number} index
171 */
172 _jumpToMatch: function(index)
173 {
174 if (!this._searchRegex)
175 return;
176 var newFocusElement = this._currentSearchTreeElements[index];
177 var previousFocusElement = this._currentSearchTreeElements[this._current SearchFocusIndex];
178
179 if (!newFocusElement)
180 index = 0;
181
182 if (previousFocusElement)
183 previousFocusElement.setSearchRegex(this._searchRegex);
184
185 this._updateSearchIndex(index);
186 if (newFocusElement) {
187 newFocusElement.setSearchRegex(this._searchRegex, WebInspector.highl ightedCurrentSearchResultClassName);
188 newFocusElement.reveal();
189 }
lushnikov 2016/05/06 18:46:25 I think _jumpToMatch function could be structured
allada 2016/05/06 19:31:51 Done.
190 },
191
192 /**
193 * @param {number} count
194 */
195 _updateSearchCount: function(count)
196 {
197 if (!this._searchableView)
198 return;
199 this._searchableView.updateSearchMatchesCount(count);
200 },
201
202 /**
203 * @param {number} index
204 */
205 _updateSearchIndex: function(index)
206 {
207 this._currentSearchFocusIndex = index;
208 if (!this._searchableView)
209 return;
210 this._searchableView.updateCurrentMatchIndex(index);
211 },
212
213 /**
214 * @override
215 */
216 searchCanceled: function()
217 {
218 this._searchRegex = null;
219 this._currentSearchTreeElements = [];
220
221 for (var element = this._treeOutline.rootElement(); element; element = e lement.traverseNextTreeElement(false)) {
222 if (!(element instanceof WebInspector.ObjectPropertyTreeElement))
223 continue;
224 element.revertHighlightChanges();
225 }
226 this._updateSearchCount(0);
227 this._updateSearchIndex(0);
228 },
229
230 /**
231 * @override
232 * @param {!WebInspector.SearchableView.SearchConfig} searchConfig
233 * @param {boolean} shouldJump
234 * @param {boolean=} jumpBackwards
235 */
236 performSearch: function(searchConfig, shouldJump, jumpBackwards)
237 {
238 var newIndex = this._currentSearchFocusIndex;
239 var previousSearchFocusElement = this._currentSearchTreeElements[newInde x];
240 this.searchCanceled();
241 this._searchRegex = searchConfig.toSearchRegex(true);
242
243 for (var element = this._treeOutline.rootElement(); element; element = e lement.traverseNextTreeElement(false)) {
244 if (!(element instanceof WebInspector.ObjectPropertyTreeElement))
lushnikov 2016/05/06 18:46:25 which other type could it be?
allada 2016/05/06 19:31:51 Root node has a special type. Also the WebInspecto
245 continue;
246 var hasMatch = element.setSearchRegex(this._searchRegex);
247 if (hasMatch)
248 this._currentSearchTreeElements.push(element);
249 if (previousSearchFocusElement === element) {
250 var currentIndex = this._currentSearchTreeElements.length - 1;
251 if (hasMatch || jumpBackwards)
252 newIndex = currentIndex;
253 else
254 newIndex = currentIndex + 1;
255 }
256 }
257 this._updateSearchCount(this._currentSearchTreeElements.length);
258
259 if (!this._currentSearchTreeElements.length) {
260 this._updateSearchIndex(0);
261 return;
262 }
263 newIndex = mod(newIndex, this._currentSearchTreeElements.length);
264
265 this._jumpToMatch(newIndex);
266 },
267
268 /**
269 * @override
270 */
271 jumpToNextSearchResult: function()
272 {
273 if (!this._currentSearchTreeElements.length)
274 return;
275 var newIndex = mod(this._currentSearchFocusIndex + 1, this._currentSearc hTreeElements.length);
276 this._jumpToMatch(newIndex);
277 },
278
279 /**
280 * @override
281 */
282 jumpToPreviousSearchResult: function()
283 {
284 if (!this._currentSearchTreeElements.length)
285 return;
286 var newIndex = mod(this._currentSearchFocusIndex - 1, this._currentSearc hTreeElements.length);
287 this._jumpToMatch(newIndex);
288 },
289
290 /**
291 * @override
292 * @return {boolean}
293 */
294 supportsCaseSensitiveSearch: function()
295 {
296 return true;
297 },
298
299 /**
300 * @override
301 * @return {boolean}
302 */
303 supportsRegexSearch: function()
304 {
305 return true;
132 }, 306 },
133 307
134 __proto__: WebInspector.VBox.prototype 308 __proto__: WebInspector.VBox.prototype
135 } 309 }
136 310
137 /** 311 /**
138 * @constructor 312 * @constructor
139 * @param {*} data 313 * @param {*} data
140 * @param {string} prefix 314 * @param {string} prefix
141 * @param {string} suffix 315 * @param {string} suffix
142 */ 316 */
143 WebInspector.ParsedJSON = function(data, prefix, suffix) 317 WebInspector.ParsedJSON = function(data, prefix, suffix)
144 { 318 {
145 this.data = data; 319 this.data = data;
146 this.prefix = prefix; 320 this.prefix = prefix;
147 this.suffix = suffix; 321 this.suffix = suffix;
148 } 322 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698