OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // require: cr.js | 5 // require: cr.js |
6 | 6 |
7 cr.define('chrome.sync', function() { | 7 cr.define('chrome.sync', function() { |
8 var currSearchId = 0; | 8 var currSearchId = 0; |
9 | 9 |
10 /** | 10 /** |
11 * Runs a search with the given query. | 11 * Runs a search with the given query. |
12 * | 12 * |
13 * @param {string} query The query to do the search with. | 13 * @param {string} query The regex to do the search with. |
14 * @param {Array.<{id: string, title: string, isFolder: boolean}>} | 14 * @param {Array.<NodeSummary>} |
Andrew T Wilson (Slow)
2012/04/06 20:30:57
Put the word "callback" on this line since it fits
rlarocque
2012/04/07 00:58:04
You're right. It wasn't always that way, but the
| |
15 * callback The callback called with the search results; not | 15 * callback The callback called with the search results; not |
16 * called if doSearch() is called again while the search is | 16 * called if doSearch() is called again while the search is |
17 * running. | 17 * running. |
18 */ | 18 */ |
19 var doSearch = function(query, callback) { | 19 var doSearch = function(query, callback) { |
20 var searchId = ++currSearchId; | 20 var searchId = ++currSearchId; |
21 chrome.sync.findNodesContainingString(query, function(ids) { | 21 try { |
22 if (currSearchId != searchId) { | 22 var regex = new RegExp(query); |
23 return; | 23 chrome.sync.getAllNodes(query, function(allNodes) { |
24 } | |
25 chrome.sync.getNodeSummariesById(ids, function(nodeSummaries) { | |
26 if (currSearchId != searchId) { | 24 if (currSearchId != searchId) { |
27 return; | 25 return; |
28 } | 26 } |
29 callback(nodeSummaries); | 27 callback(allNodes.filter(function(elem) { |
28 return regex.test(JSON.stringify(elem, null, 2)); | |
Andrew T Wilson (Slow)
2012/04/06 20:30:57
One quick question - is this fast enough to not tr
rlarocque
2012/04/07 00:58:04
It depends how you define large. I think my test,
| |
29 }), null); | |
30 }); | 30 }); |
31 }); | 31 } catch (err) { |
32 // Sometimes the provided regex is invalid. This and other errors will | |
33 // be caught and handled here. | |
34 callback([], err); | |
35 } | |
32 }; | 36 }; |
33 | 37 |
34 /** | 38 /** |
35 * Decorates the various search controls. | 39 * Decorates the various search controls. |
36 * | 40 * |
37 * @param {!HTMLInputElement} queryControl The <input> object of | 41 * @param {!HTMLInputElement} queryControl The <input> object of |
38 * type=search where the user types in his query. | 42 * type=search where the user types in his query. |
39 * @param {!HTMLElement} statusControl The <span> object display the | 43 * @param {!HTMLElement} statusControl The <span> object display the |
40 * search status. | 44 * search status. |
41 * @param {!HTMLElement} listControl The <list> object which holds | 45 * @param {!HTMLElement} listControl The <list> object which holds |
42 * the list of returned results. | 46 * the list of returned results. |
43 * @param {!HTMLPreElement} detailsControl The <pre> object which | 47 * @param {!HTMLPreElement} detailsControl The <pre> object which |
44 * holds the details of the selected result. | 48 * holds the details of the selected result. |
45 */ | 49 */ |
46 function decorateSearchControls(queryControl, statusControl, | 50 function decorateSearchControls(queryControl, statusControl, |
47 resultsControl, detailsControl) { | 51 resultsControl, detailsControl) { |
48 var resultsDataModel = new cr.ui.ArrayDataModel([]); | 52 var resultsDataModel = new cr.ui.ArrayDataModel([]); |
49 | 53 |
50 // Decorate search box. | 54 // Decorate search box. |
51 queryControl.onsearch = function() { | 55 queryControl.onsearch = function() { |
52 var query = queryControl.value; | 56 var query = queryControl.value; |
53 statusControl.textContent = ''; | 57 statusControl.textContent = ''; |
54 resultsDataModel.splice(0, resultsDataModel.length); | 58 resultsDataModel.splice(0, resultsDataModel.length); |
55 if (!query) { | 59 if (!query) { |
56 return; | 60 return; |
57 } | 61 } |
58 statusControl.textContent = 'Searching for ' + query + '...'; | 62 statusControl.textContent = 'Searching for ' + query + '...'; |
63 queryControl.removeAttribute('error'); | |
59 var timer = chrome.sync.makeTimer(); | 64 var timer = chrome.sync.makeTimer(); |
60 doSearch(query, function(nodeSummaries) { | 65 doSearch(query, function(nodes, error) { |
61 statusControl.textContent = | 66 if (error) { |
62 'Found ' + nodeSummaries.length + ' nodes in ' | 67 statusControl.textContent = 'Error: ' + error; |
63 + timer.elapsedSeconds + 's'; | 68 queryControl.setAttribute('error'); |
64 // TODO(akalin): Write a nicer list display. | 69 } else { |
65 for (var i = 0; i < nodeSummaries.length; ++i) { | 70 statusControl.textContent = |
66 nodeSummaries[i].toString = function() { | 71 'Found ' + nodes.length + ' nodes in ' |
Andrew T Wilson (Slow)
2012/04/06 20:30:57
nit: move + from next line to be on this line.
rlarocque
2012/04/07 00:58:04
Done.
| |
67 return this.title; | 72 + timer.elapsedSeconds + 's'; |
68 }.bind(nodeSummaries[i]); | 73 queryControl.removeAttribute('error'); |
74 | |
75 // TODO(akalin): Write a nicer list display. | |
76 for (var i = 0; i < nodes.length; ++i) { | |
77 nodes[i].toString = function() { | |
78 return this.NON_UNIQUE_NAME; | |
79 }.bind(nodes[i]); | |
Andrew T Wilson (Slow)
2012/04/06 20:30:57
Is this bind actually necessary (is the function i
rlarocque
2012/04/07 00:58:04
Done.
I tried a more clever solution, but it fail
| |
80 } | |
81 resultsDataModel.push.apply(resultsDataModel, nodes); | |
82 // Workaround for http://crbug.com/83452 . | |
83 resultsControl.redraw(); | |
69 } | 84 } |
70 resultsDataModel.push.apply(resultsDataModel, nodeSummaries); | |
71 // Workaround for http://crbug.com/83452 . | |
72 resultsControl.redraw(); | |
73 }); | 85 }); |
74 }; | 86 }; |
75 queryControl.value = ''; | 87 queryControl.value = ''; |
76 | 88 |
77 // Decorate results list. | 89 // Decorate results list. |
78 cr.ui.List.decorate(resultsControl); | 90 cr.ui.List.decorate(resultsControl); |
79 resultsControl.dataModel = resultsDataModel; | 91 resultsControl.dataModel = resultsDataModel; |
80 resultsControl.selectionModel.addEventListener('change', function(event) { | 92 resultsControl.selectionModel.addEventListener('change', function(event) { |
81 detailsControl.textContent = ''; | 93 detailsControl.textContent = ''; |
82 var selected = resultsControl.selectedItem; | 94 var selected = resultsControl.selectedItem; |
83 if (selected) { | 95 if (selected) { |
84 chrome.sync.getNodeDetailsById([selected.id], function(nodeDetails) { | 96 detailsControl.textContent = JSON.stringify(selected, null, 2); |
85 var selectedNodeDetails = nodeDetails[0] || null; | |
86 if (selectedNodeDetails) { | |
87 detailsControl.textContent = | |
88 JSON.stringify(selectedNodeDetails, null, 2); | |
89 } | |
90 }); | |
91 } | 97 } |
92 }); | 98 }); |
93 } | 99 } |
94 | 100 |
95 return { | 101 return { |
96 decorateSearchControls: decorateSearchControls | 102 decorateSearchControls: decorateSearchControls |
97 }; | 103 }; |
98 }); | 104 }); |
OLD | NEW |