// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // require: cr.js cr.define('chrome.sync', function() { var currSearchId = 0; var setQueryString = function(queryControl, query) { queryControl.value = query; }; var createDoQueryFunction = function(queryControl, submitControl, query) { return function() { setQueryString(queryControl, query); submitControl.click(); }; }; /** * Decorates the quick search controls * * @param {Array of DOM elements} quickLinkArray The object which * will be given a link to a quick filter option. * @param {!HTMLInputElement} queryControl The object of * type=search where the user types in his query. */ var decorateQuickQueryControls = function(quickLinkArray, submitControl, queryControl) { for (var index = 0; index < allLinks.length; ++index) { var quickQuery = allLinks[index].getAttribute('data-query'); var quickQueryFunction = createDoQueryFunction(queryControl, submitControl, quickQuery); allLinks[index].addEventListener('click', quickQueryFunction); } }; /** * Runs a search with the given query. * * @param {string} query The regex to do the search with. * @param {function} callback The callback called with the search results; * not called if doSearch() is called again while the search is running. */ var doSearch = function(query, callback) { var searchId = ++currSearchId; try { var regex = new RegExp(query); chrome.sync.getAllNodes(function(node_map) { // Put all nodes into one big list that ignores the type. var nodes = node_map. map(function(x) { return x.nodes; }). reduce(function(a, b) { return a.concat(b); }); if (currSearchId != searchId) { return; } callback(nodes.filter(function(elem) { return regex.test(JSON.stringify(elem, null, 2)); }), null); }); } catch (err) { // Sometimes the provided regex is invalid. This and other errors will // be caught and handled here. callback([], err); } }; /** * Decorates the various search controls. * * @param {!HTMLInputElement} queryControl The object of * type=search where the user types in his query. * @param {!HTMLButtonElement} submitControl The