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