| 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 query to do the search with. |
| 14 * item from. | 14 * @param {Array.<{id: string, title: string, isFolder: boolean}>} |
| 15 * @param {function} doneCallback The callback called when the | 15 * callback The callback called with the search results; not |
| 16 * search is finished; not called if doSearch() is called again | 16 * called if doSearch() is called again while the search is |
| 17 * while the search is 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.findNodesContainingString(query, function(ids) { |
| 22 if (currSearchId != searchId) { | 22 if (currSearchId != searchId) { |
| 23 return; | 23 return; |
| 24 } | 24 } |
| 25 chrome.sync.getNodesById(ids, function(nodeInfos) { | 25 chrome.sync.getNodeSummariesById(ids, function(nodeSummaries) { |
| 26 if (currSearchId != searchId) { | 26 if (currSearchId != searchId) { |
| 27 return; | 27 return; |
| 28 } | 28 } |
| 29 callback(nodeInfos); | 29 callback(nodeSummaries); |
| 30 }); | 30 }); |
| 31 }); | 31 }); |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Decorates the various search controls. | 35 * Decorates the various search controls. |
| 36 * | 36 * |
| 37 * @param {object} queryControl The <input> object of type=search | 37 * @param {!HTMLInputElement} queryControl The <input> object of |
| 38 * where the user types in his query. | 38 * type=search where the user types in his query. |
| 39 * @param {object} statusControl The <span> object display the | 39 * @param {!HTMLElement} statusControl The <span> object display the |
| 40 * search status. | 40 * search status. |
| 41 * @param {object} listControl The <list> object which holds the | 41 * @param {!HTMLElement} listControl The <list> object which holds |
| 42 * list of returned results. | 42 * the list of returned results. |
| 43 * @param {object} detailsControl The <pre> object which holds the | 43 * @param {!HTMLPreElement} detailsControl The <pre> object which |
| 44 * details of the selected result. | 44 * holds the details of the selected result. |
| 45 */ | 45 */ |
| 46 function decorateSearchControls(queryControl, statusControl, | 46 function decorateSearchControls(queryControl, statusControl, |
| 47 resultsControl, detailsControl) { | 47 resultsControl, detailsControl) { |
| 48 var resultsDataModel = new cr.ui.ArrayDataModel([]); | 48 var resultsDataModel = new cr.ui.ArrayDataModel([]); |
| 49 | 49 |
| 50 // Decorate search box. | 50 // Decorate search box. |
| 51 queryControl.onsearch = function() { | 51 queryControl.onsearch = function() { |
| 52 var query = queryControl.value; | 52 var query = queryControl.value; |
| 53 statusControl.innerText = ''; | 53 statusControl.innerText = ''; |
| 54 resultsDataModel.splice(0, resultsDataModel.length); | 54 resultsDataModel.splice(0, resultsDataModel.length); |
| 55 if (!query) { | 55 if (!query) { |
| 56 return; | 56 return; |
| 57 } | 57 } |
| 58 statusControl.innerText = 'Searching for ' + query + '...'; | 58 statusControl.innerText = 'Searching for ' + query + '...'; |
| 59 var timer = chrome.sync.makeTimer(); | 59 var timer = chrome.sync.makeTimer(); |
| 60 doSearch(query, function(nodeInfos) { | 60 doSearch(query, function(nodeSummaries) { |
| 61 statusControl.innerText = | 61 statusControl.innerText = |
| 62 'Found ' + nodeInfos.length + ' nodes in ' | 62 'Found ' + nodeSummaries.length + ' nodes in ' |
| 63 + timer.elapsedSeconds + 's'; | 63 + timer.elapsedSeconds + 's'; |
| 64 // TODO(akalin): Write a nicer list display. | 64 // TODO(akalin): Write a nicer list display. |
| 65 for (var i = 0; i < nodeInfos.length; ++i) { | 65 for (var i = 0; i < nodeSummaries.length; ++i) { |
| 66 nodeInfos[i].toString = function() { | 66 nodeSummaries[i].toString = function() { |
| 67 return this.title; | 67 return this.title; |
| 68 }.bind(nodeInfos[i]); | 68 }.bind(nodeSummaries[i]); |
| 69 } | 69 } |
| 70 resultsDataModel.push.apply(resultsDataModel, nodeInfos); | 70 resultsDataModel.push.apply(resultsDataModel, nodeSummaries); |
| 71 // Workaround for http://crbug.com/83452 . | 71 // Workaround for http://crbug.com/83452 . |
| 72 resultsControl.redraw(); | 72 resultsControl.redraw(); |
| 73 }); | 73 }); |
| 74 }; | 74 }; |
| 75 queryControl.value = ''; | 75 queryControl.value = ''; |
| 76 | 76 |
| 77 // Decorate results list. | 77 // Decorate results list. |
| 78 cr.ui.List.decorate(resultsControl); | 78 cr.ui.List.decorate(resultsControl); |
| 79 resultsControl.dataModel = resultsDataModel; | 79 resultsControl.dataModel = resultsDataModel; |
| 80 resultsControl.selectionModel.addEventListener('change', function(event) { | 80 resultsControl.selectionModel.addEventListener('change', function(event) { |
| 81 detailsControl.innerText = ''; | 81 detailsControl.innerText = ''; |
| 82 var selected = resultsControl.selectedItem; | 82 var selected = resultsControl.selectedItem; |
| 83 if (selected) { | 83 if (selected) { |
| 84 detailsControl.innerText = JSON.stringify(selected, null, 2); | 84 chrome.sync.getNodeDetailsById([selected.id], function(nodeDetails) { |
| 85 var selectedNodeDetails = nodeDetails[0] || null; |
| 86 if (selectedNodeDetails) { |
| 87 detailsControl.innerText = |
| 88 JSON.stringify(selectedNodeDetails, null, 2); |
| 89 } |
| 90 }); |
| 85 } | 91 } |
| 86 }); | 92 }); |
| 87 } | 93 } |
| 88 | 94 |
| 89 return { | 95 return { |
| 90 decorateSearchControls: decorateSearchControls | 96 decorateSearchControls: decorateSearchControls |
| 91 }; | 97 }; |
| 92 }); | 98 }); |
| OLD | NEW |