OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 {function} callback The callback called with the search results; |
15 * callback The callback called with the search results; not | 15 * not called if doSearch() is called again while the search is running. |
16 * called if doSearch() is called again while the search is | |
17 * running. | |
18 */ | 16 */ |
19 var doSearch = function(query, callback) { | 17 var doSearch = function(query, callback) { |
20 var searchId = ++currSearchId; | 18 var searchId = ++currSearchId; |
21 chrome.sync.findNodesContainingString(query, function(ids) { | 19 try { |
22 if (currSearchId != searchId) { | 20 var regex = new RegExp(query); |
23 return; | 21 chrome.sync.getAllNodes(query, function(allNodes) { |
24 } | |
25 chrome.sync.getNodeSummariesById(ids, function(nodeSummaries) { | |
26 if (currSearchId != searchId) { | 22 if (currSearchId != searchId) { |
27 return; | 23 return; |
28 } | 24 } |
29 callback(nodeSummaries); | 25 callback(allNodes.filter(function(elem) { |
| 26 return regex.test(JSON.stringify(elem, null, 2)); |
| 27 }), null); |
30 }); | 28 }); |
31 }); | 29 } catch (err) { |
| 30 // Sometimes the provided regex is invalid. This and other errors will |
| 31 // be caught and handled here. |
| 32 callback([], err); |
| 33 } |
32 }; | 34 }; |
33 | 35 |
34 /** | 36 /** |
35 * Decorates the various search controls. | 37 * Decorates the various search controls. |
36 * | 38 * |
37 * @param {!HTMLInputElement} queryControl The <input> object of | 39 * @param {!HTMLInputElement} queryControl The <input> object of |
38 * type=search where the user types in his query. | 40 * type=search where the user types in his query. |
39 * @param {!HTMLElement} statusControl The <span> object display the | 41 * @param {!HTMLElement} statusControl The <span> object display the |
40 * search status. | 42 * search status. |
41 * @param {!HTMLElement} listControl The <list> object which holds | 43 * @param {!HTMLElement} listControl The <list> object which holds |
42 * the list of returned results. | 44 * the list of returned results. |
43 * @param {!HTMLPreElement} detailsControl The <pre> object which | 45 * @param {!HTMLPreElement} detailsControl The <pre> object which |
44 * holds the details of the selected result. | 46 * holds the details of the selected result. |
45 */ | 47 */ |
46 function decorateSearchControls(queryControl, statusControl, | 48 function decorateSearchControls(queryControl, statusControl, |
47 resultsControl, detailsControl) { | 49 resultsControl, detailsControl) { |
48 var resultsDataModel = new cr.ui.ArrayDataModel([]); | 50 var resultsDataModel = new cr.ui.ArrayDataModel([]); |
49 | 51 |
50 // Decorate search box. | 52 // Decorate search box. |
51 queryControl.onsearch = function() { | 53 queryControl.onsearch = function() { |
52 var query = queryControl.value; | 54 var query = queryControl.value; |
53 statusControl.textContent = ''; | 55 statusControl.textContent = ''; |
54 resultsDataModel.splice(0, resultsDataModel.length); | 56 resultsDataModel.splice(0, resultsDataModel.length); |
55 if (!query) { | 57 if (!query) { |
56 return; | 58 return; |
57 } | 59 } |
58 statusControl.textContent = 'Searching for ' + query + '...'; | 60 statusControl.textContent = 'Searching for ' + query + '...'; |
| 61 queryControl.removeAttribute('error'); |
59 var timer = chrome.sync.makeTimer(); | 62 var timer = chrome.sync.makeTimer(); |
60 doSearch(query, function(nodeSummaries) { | 63 doSearch(query, function(nodes, error) { |
61 statusControl.textContent = | 64 if (error) { |
62 'Found ' + nodeSummaries.length + ' nodes in ' | 65 statusControl.textContent = 'Error: ' + error; |
63 + timer.elapsedSeconds + 's'; | 66 queryControl.setAttribute('error'); |
64 // TODO(akalin): Write a nicer list display. | 67 } else { |
65 for (var i = 0; i < nodeSummaries.length; ++i) { | 68 statusControl.textContent = |
66 nodeSummaries[i].toString = function() { | 69 'Found ' + nodes.length + ' nodes in ' + |
67 return this.title; | 70 timer.elapsedSeconds + 's'; |
68 }.bind(nodeSummaries[i]); | 71 queryControl.removeAttribute('error'); |
| 72 |
| 73 // TODO(akalin): Write a nicer list display. |
| 74 for (var i = 0; i < nodes.length; ++i) { |
| 75 nodes[i].toString = function() { |
| 76 return this.NON_UNIQUE_NAME; |
| 77 }; |
| 78 } |
| 79 resultsDataModel.push.apply(resultsDataModel, nodes); |
| 80 // Workaround for http://crbug.com/83452 . |
| 81 resultsControl.redraw(); |
69 } | 82 } |
70 resultsDataModel.push.apply(resultsDataModel, nodeSummaries); | |
71 // Workaround for http://crbug.com/83452 . | |
72 resultsControl.redraw(); | |
73 }); | 83 }); |
74 }; | 84 }; |
75 queryControl.value = ''; | 85 queryControl.value = ''; |
76 | 86 |
77 // Decorate results list. | 87 // Decorate results list. |
78 cr.ui.List.decorate(resultsControl); | 88 cr.ui.List.decorate(resultsControl); |
79 resultsControl.dataModel = resultsDataModel; | 89 resultsControl.dataModel = resultsDataModel; |
80 resultsControl.selectionModel.addEventListener('change', function(event) { | 90 resultsControl.selectionModel.addEventListener('change', function(event) { |
81 detailsControl.textContent = ''; | 91 detailsControl.textContent = ''; |
82 var selected = resultsControl.selectedItem; | 92 var selected = resultsControl.selectedItem; |
83 if (selected) { | 93 if (selected) { |
84 chrome.sync.getNodeDetailsById([selected.id], function(nodeDetails) { | 94 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 } | 95 } |
92 }); | 96 }); |
93 } | 97 } |
94 | 98 |
95 return { | 99 return { |
96 decorateSearchControls: decorateSearchControls | 100 decorateSearchControls: decorateSearchControls |
97 }; | 101 }; |
98 }); | 102 }); |
OLD | NEW |