• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OptionsPage = options.OptionsPage;
7  /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
8
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  }
19
20  cr.addSingletonGetter(SearchEngineManager);
21
22  SearchEngineManager.prototype = {
23    __proto__: OptionsPage.prototype,
24
25    /**
26     * List for default search engine options.
27     * @private
28     */
29    defaultsList_: null,
30
31    /**
32     * List for other search engine options.
33     * @private
34     */
35    othersList_: null,
36
37    /**
38     * List for extension keywords.
39     * @private
40     */
41    extensionList_: null,
42
43    /** inheritDoc */
44    initializePage: function() {
45      OptionsPage.prototype.initializePage.call(this);
46
47      this.defaultsList_ = $('default-search-engine-list');
48      this.setUpList_(this.defaultsList_);
49
50      this.othersList_ = $('other-search-engine-list');
51      this.setUpList_(this.othersList_);
52
53      this.extensionList_ = $('extension-keyword-list');
54      this.setUpList_(this.extensionList_);
55
56      $('search-engine-manager-confirm').onclick = function() {
57        OptionsPage.closeOverlay();
58      };
59    },
60
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    },
70
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);
80
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      });
88
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;
96
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  };
106
107  SearchEngineManager.updateSearchEngineList = function(defaultEngines,
108                                                        otherEngines,
109                                                        keywords) {
110    SearchEngineManager.getInstance().updateSearchEngineList_(defaultEngines,
111                                                              otherEngines,
112                                                              keywords);
113  };
114
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  };
124
125  // Export
126  return {
127    SearchEngineManager: SearchEngineManager
128  };
129
130});
131
132