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 3// found in the LICENSE file. 4 5cr.define('options', function() { 6 /** @const */ var Page = cr.ui.pageManager.Page; 7 /** @const */ var PageManager = cr.ui.pageManager.PageManager; 8 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; 9 10 /** 11 * Encapsulated handling of search engine management page. 12 * @constructor 13 * @extends {cr.ui.pageManager.Page} 14 */ 15 function SearchEngineManager() { 16 this.activeNavTab = null; 17 Page.call(this, 'searchEngines', 18 loadTimeData.getString('searchEngineManagerPageTabTitle'), 19 'search-engine-manager-page'); 20 } 21 22 cr.addSingletonGetter(SearchEngineManager); 23 24 SearchEngineManager.prototype = { 25 __proto__: Page.prototype, 26 27 /** 28 * List for default search engine options. 29 * @private 30 */ 31 defaultsList_: null, 32 33 /** 34 * List for other search engine options. 35 * @private 36 */ 37 othersList_: null, 38 39 /** 40 * List for extension keywords. 41 * @private 42 */ 43 extensionList_: null, 44 45 /** @override */ 46 initializePage: function() { 47 Page.prototype.initializePage.call(this); 48 49 this.defaultsList_ = $('default-search-engine-list'); 50 this.setUpList_(this.defaultsList_); 51 52 this.othersList_ = $('other-search-engine-list'); 53 this.setUpList_(this.othersList_); 54 55 this.extensionList_ = $('extension-keyword-list'); 56 this.setUpList_(this.extensionList_); 57 58 $('search-engine-manager-confirm').onclick = function() { 59 PageManager.closeOverlay(); 60 }; 61 }, 62 63 /** 64 * Sets up the given list as a search engine list 65 * @param {HTMLElement} list The list to set up. 66 * @private 67 */ 68 setUpList_: function(list) { 69 options.search_engines.SearchEngineList.decorate(list); 70 list.autoExpands = true; 71 }, 72 73 /** 74 * Updates the search engine list with the given entries. 75 * @private 76 * @param {!Array} defaultEngines List of possible default search engines. 77 * @param {!Array} otherEngines List of other search engines. 78 * @param {!Array} keywords List of keywords from extensions. 79 */ 80 updateSearchEngineList_: function(defaultEngines, otherEngines, keywords) { 81 this.defaultsList_.dataModel = new ArrayDataModel(defaultEngines); 82 83 otherEngines = otherEngines.map(function(x) { 84 return [x, x.name.toLocaleLowerCase()]; 85 }).sort(function(a, b) { 86 return a[1].localeCompare(b[1]); 87 }).map(function(x) { 88 return x[0]; 89 }); 90 91 var othersModel = new ArrayDataModel(otherEngines); 92 // Add a "new engine" row. 93 othersModel.push({ 94 'modelIndex': '-1', 95 'canBeEdited': true 96 }); 97 this.othersList_.dataModel = othersModel; 98 99 if (keywords.length > 0) { 100 $('extension-keyword-div').hidden = false; 101 var extensionsModel = new ArrayDataModel(keywords); 102 this.extensionList_.dataModel = extensionsModel; 103 } else { 104 $('extension-keyword-div').hidden = true; 105 } 106 }, 107 }; 108 109 SearchEngineManager.updateSearchEngineList = function(defaultEngines, 110 otherEngines, 111 keywords) { 112 SearchEngineManager.getInstance().updateSearchEngineList_(defaultEngines, 113 otherEngines, 114 keywords); 115 }; 116 117 SearchEngineManager.validityCheckCallback = function(validity, modelIndex) { 118 // Forward to all lists; those without a matching modelIndex will ignore it. 119 SearchEngineManager.getInstance().defaultsList_.validationComplete( 120 validity, modelIndex); 121 SearchEngineManager.getInstance().othersList_.validationComplete( 122 validity, modelIndex); 123 SearchEngineManager.getInstance().extensionList_.validationComplete( 124 validity, modelIndex); 125 }; 126 127 // Export 128 return { 129 SearchEngineManager: SearchEngineManager 130 }; 131 132}); 133 134