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 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) { |
akalin
2012/04/03 00:36:08
remove space after 'function'
rlarocque
2012/04/03 19:01:42
Done.
| |
28 return JSON.stringify(elem, null, 2).search(regex) != -1; | |
akalin
2012/04/03 00:36:08
use regex.test(str) instead?
rlarocque
2012/04/03 19:01:42
Done.
| |
29 }), null); | |
30 }); | 30 }); |
31 }); | 31 } catch (err) { |
32 // Sometimes the provided regex is invalid. | |
akalin
2012/04/03 00:36:08
update comment to indicate that this can also catc
rlarocque
2012/04/03 19:01:42
Done.
| |
33 callback([], err); | |
34 } | |
32 }; | 35 }; |
33 | 36 |
34 /** | 37 /** |
35 * Decorates the various search controls. | 38 * Decorates the various search controls. |
36 * | 39 * |
37 * @param {!HTMLInputElement} queryControl The <input> object of | 40 * @param {!HTMLInputElement} queryControl The <input> object of |
38 * type=search where the user types in his query. | 41 * type=search where the user types in his query. |
39 * @param {!HTMLElement} statusControl The <span> object display the | 42 * @param {!HTMLElement} statusControl The <span> object display the |
40 * search status. | 43 * search status. |
41 * @param {!HTMLElement} listControl The <list> object which holds | 44 * @param {!HTMLElement} listControl The <list> object which holds |
42 * the list of returned results. | 45 * the list of returned results. |
43 * @param {!HTMLPreElement} detailsControl The <pre> object which | 46 * @param {!HTMLPreElement} detailsControl The <pre> object which |
44 * holds the details of the selected result. | 47 * holds the details of the selected result. |
45 */ | 48 */ |
46 function decorateSearchControls(queryControl, statusControl, | 49 function decorateSearchControls(queryControl, statusControl, |
47 resultsControl, detailsControl) { | 50 resultsControl, detailsControl) { |
48 var resultsDataModel = new cr.ui.ArrayDataModel([]); | 51 var resultsDataModel = new cr.ui.ArrayDataModel([]); |
49 | 52 |
50 // Decorate search box. | 53 // Decorate search box. |
51 queryControl.onsearch = function() { | 54 queryControl.onsearch = function() { |
52 var query = queryControl.value; | 55 var query = queryControl.value; |
53 statusControl.textContent = ''; | 56 statusControl.textContent = ''; |
54 resultsDataModel.splice(0, resultsDataModel.length); | 57 resultsDataModel.splice(0, resultsDataModel.length); |
55 if (!query) { | 58 if (!query) { |
56 return; | 59 return; |
57 } | 60 } |
58 statusControl.textContent = 'Searching for ' + query + '...'; | 61 statusControl.textContent = 'Searching for ' + query + '...'; |
62 queryControl.removeAttribute('error'); | |
59 var timer = chrome.sync.makeTimer(); | 63 var timer = chrome.sync.makeTimer(); |
60 doSearch(query, function(nodeSummaries) { | 64 doSearch(query, function(nodes, error) { |
61 statusControl.textContent = | 65 if (error) { |
62 'Found ' + nodeSummaries.length + ' nodes in ' | 66 statusControl.textContent = 'Error: ' + error; |
63 + timer.elapsedSeconds + 's'; | 67 queryControl.setAttribute('error'); |
64 // TODO(akalin): Write a nicer list display. | 68 } else { |
65 for (var i = 0; i < nodeSummaries.length; ++i) { | 69 statusControl.textContent = |
66 nodeSummaries[i].toString = function() { | 70 'Found ' + nodes.length + ' nodes in ' |
67 return this.title; | 71 + timer.elapsedSeconds + 's'; |
68 }.bind(nodeSummaries[i]); | 72 queryControl.removeAttribute('error'); |
73 | |
74 // TODO(akalin): Write a nicer list display. | |
75 for (var i = 0; i < nodes.length; ++i) { | |
76 nodes[i].toString = function() { | |
77 return this.NON_UNIQUE_NAME; | |
78 }.bind(nodes[i]); | |
79 } | |
80 resultsDataModel.push.apply(resultsDataModel, nodes); | |
81 // Workaround for http://crbug.com/83452 . | |
82 resultsControl.redraw(); | |
69 } | 83 } |
70 resultsDataModel.push.apply(resultsDataModel, nodeSummaries); | |
71 // Workaround for http://crbug.com/83452 . | |
72 resultsControl.redraw(); | |
73 }); | 84 }); |
74 }; | 85 }; |
75 queryControl.value = ''; | 86 queryControl.value = ''; |
76 | 87 |
77 // Decorate results list. | 88 // Decorate results list. |
78 cr.ui.List.decorate(resultsControl); | 89 cr.ui.List.decorate(resultsControl); |
79 resultsControl.dataModel = resultsDataModel; | 90 resultsControl.dataModel = resultsDataModel; |
80 resultsControl.selectionModel.addEventListener('change', function(event) { | 91 resultsControl.selectionModel.addEventListener('change', function(event) { |
81 detailsControl.textContent = ''; | 92 detailsControl.textContent = ''; |
82 var selected = resultsControl.selectedItem; | 93 var selected = resultsControl.selectedItem; |
83 if (selected) { | 94 if (selected) { |
84 chrome.sync.getNodeDetailsById([selected.id], function(nodeDetails) { | 95 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 } | 96 } |
92 }); | 97 }); |
93 } | 98 } |
94 | 99 |
95 return { | 100 return { |
96 decorateSearchControls: decorateSearchControls | 101 decorateSearchControls: decorateSearchControls |
97 }; | 102 }; |
98 }); | 103 }); |
OLD | NEW |