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>} |
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 chrome.sync.getAllNodes(query, function(allNodes) { |
22 if (currSearchId != searchId) { | 22 var regex = new RegExp(query); |
23 return; | 23 callback(allNodes.filter(function (elem) { |
24 } | 24 return JSON.stringify(elem).match(regex); |
rlarocque
2012/03/27 01:49:05
I believe JSON.stringify returns a "compact" versi
akalin
2012/03/29 18:41:43
Does searching the raw json provide us any benefit
rlarocque
2012/03/29 20:51:32
Searching the serialized version makes it easy to
| |
25 chrome.sync.getNodeSummariesById(ids, function(nodeSummaries) { | 25 })); |
26 if (currSearchId != searchId) { | |
27 return; | |
28 } | |
29 callback(nodeSummaries); | |
30 }); | |
31 }); | 26 }); |
32 }; | 27 }; |
33 | 28 |
34 /** | 29 /** |
35 * Decorates the various search controls. | 30 * Decorates the various search controls. |
36 * | 31 * |
37 * @param {!HTMLInputElement} queryControl The <input> object of | 32 * @param {!HTMLInputElement} queryControl The <input> object of |
38 * type=search where the user types in his query. | 33 * type=search where the user types in his query. |
39 * @param {!HTMLElement} statusControl The <span> object display the | 34 * @param {!HTMLElement} statusControl The <span> object display the |
40 * search status. | 35 * search status. |
(...skipping 16 matching lines...) Expand all Loading... | |
57 } | 52 } |
58 statusControl.textContent = 'Searching for ' + query + '...'; | 53 statusControl.textContent = 'Searching for ' + query + '...'; |
59 var timer = chrome.sync.makeTimer(); | 54 var timer = chrome.sync.makeTimer(); |
60 doSearch(query, function(nodeSummaries) { | 55 doSearch(query, function(nodeSummaries) { |
61 statusControl.textContent = | 56 statusControl.textContent = |
62 'Found ' + nodeSummaries.length + ' nodes in ' | 57 'Found ' + nodeSummaries.length + ' nodes in ' |
63 + timer.elapsedSeconds + 's'; | 58 + timer.elapsedSeconds + 's'; |
64 // TODO(akalin): Write a nicer list display. | 59 // TODO(akalin): Write a nicer list display. |
65 for (var i = 0; i < nodeSummaries.length; ++i) { | 60 for (var i = 0; i < nodeSummaries.length; ++i) { |
66 nodeSummaries[i].toString = function() { | 61 nodeSummaries[i].toString = function() { |
67 return this.title; | 62 return this.NON_UNIQUE_NAME; |
akalin
2012/03/29 18:41:43
any reason you changed this? is the NON_UNIQUE_NA
rlarocque
2012/03/29 20:51:32
Less readable, actually. It's a long story.
Th
rlarocque
2012/03/30 21:52:30
I talked to Nicolas about this. It turns out that
| |
68 }.bind(nodeSummaries[i]); | 63 }.bind(nodeSummaries[i]); |
69 } | 64 } |
70 resultsDataModel.push.apply(resultsDataModel, nodeSummaries); | 65 resultsDataModel.push.apply(resultsDataModel, nodeSummaries); |
71 // Workaround for http://crbug.com/83452 . | 66 // Workaround for http://crbug.com/83452 . |
72 resultsControl.redraw(); | 67 resultsControl.redraw(); |
73 }); | 68 }); |
74 }; | 69 }; |
75 queryControl.value = ''; | 70 queryControl.value = ''; |
76 | 71 |
77 // Decorate results list. | 72 // Decorate results list. |
78 cr.ui.List.decorate(resultsControl); | 73 cr.ui.List.decorate(resultsControl); |
79 resultsControl.dataModel = resultsDataModel; | 74 resultsControl.dataModel = resultsDataModel; |
80 resultsControl.selectionModel.addEventListener('change', function(event) { | 75 resultsControl.selectionModel.addEventListener('change', function(event) { |
81 detailsControl.textContent = ''; | 76 detailsControl.textContent = ''; |
82 var selected = resultsControl.selectedItem; | 77 var selected = resultsControl.selectedItem; |
83 if (selected) { | 78 if (selected) { |
84 chrome.sync.getNodeDetailsById([selected.id], function(nodeDetails) { | 79 chrome.sync.getNodeDetailsById([selected.META_HANDLE], |
akalin
2012/03/29 18:41:43
hunh. is id == META_HANDLE? any reason you chang
rlarocque
2012/03/29 20:51:32
Same as above, but id == META_HANDLE (as opposed t
| |
80 function(nodeDetails) { | |
85 var selectedNodeDetails = nodeDetails[0] || null; | 81 var selectedNodeDetails = nodeDetails[0] || null; |
86 if (selectedNodeDetails) { | 82 if (selectedNodeDetails) { |
87 detailsControl.textContent = | 83 detailsControl.textContent = |
88 JSON.stringify(selectedNodeDetails, null, 2); | 84 JSON.stringify(selectedNodeDetails, null, 2); |
89 } | 85 } |
90 }); | 86 }); |
91 } | 87 } |
92 }); | 88 }); |
93 } | 89 } |
94 | 90 |
95 return { | 91 return { |
96 decorateSearchControls: decorateSearchControls | 92 decorateSearchControls: decorateSearchControls |
97 }; | 93 }; |
98 }); | 94 }); |
OLD | NEW |