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