summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options/edit_search_engine_overlay.js
blob: b6f9fcc6e8df60504035864cc839f02deeaec17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2010 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.

cr.define('options', function() {
  const OptionsPage = options.OptionsPage;

  /**
   * EditSearchEngineOverlay class
   * Encapsulated handling of the 'Edit Search Engine' overlay page.
   * @class
   * @constructor
   */
  function EditSearchEngineOverlay() {
    OptionsPage.call(this, 'editSearchEngineOverlay',
                     templateData.editSearchEngineTitle,
                     'editSearchEngineOverlay');
  }

  cr.addSingletonGetter(EditSearchEngineOverlay);

  EditSearchEngineOverlay.prototype = {
    __proto__: OptionsPage.prototype,

    /**
     * Initializes the page.
     */
    initializePage: function() {
      OptionsPage.prototype.initializePage.call(this);

      var self = this;
      var editForm = $('editSearchEngineForm');
      editForm.onreset = function(e) {
        chrome.send('searchEngineEditCancelled');
        self.dismissOverlay_();
      };
      editForm.onsubmit = function(e) {
        chrome.send('searchEngineEditCompleted', self.getInputFieldValues_());
        self.dismissOverlay_();
        return false;
      };
      var fieldIDs = ['editSearchEngineName',
                      'editSearchEngineKeyword',
                      'editSearchEngineURL'];
      for (var i = 0; i < fieldIDs.length; i++) {
        var field = $(fieldIDs[i]);
        field.oninput = this.validateFields_.bind(this);
        field.onkeydown = function(e) {
          if (e.keyCode == 27)  // Esc
            editForm.reset();
        };
      }
    },

    /**
     * Clears any uncommited input, and dismisses the overlay.
     * @private
     */
    dismissOverlay_: function() {
      this.setEditDetails_();
      OptionsPage.clearOverlays();
    },

    /**
     * Fills the text fields from the given search engine.
     * @private
     */
    setEditDetails_: function(engineDetails) {
      if (engineDetails) {
        $('editSearchEngineName').value = engineDetails['name'];
        $('editSearchEngineKeyword').value = engineDetails['keyword'];
        $('editSearchEngineURL').value = engineDetails['url'];
        this.validateFields_();
      } else {
        $('editSearchEngineName').value = '';
        $('editSearchEngineKeyword').value = '';
        $('editSearchEngineURL').value = '';
        var invalid = { name: false, keyword: false, url: false };
        this.updateValidityWithResults_(invalid);
      }
    },

    /**
     * Starts the process of asynchronously validating the user input. Results
     * will be reported to updateValidityWithResults_.
     * @private
     */
    validateFields_: function() {
      chrome.send('checkSearchEngineInfoValidity', this.getInputFieldValues_());
    },

    /**
     * Sets the validation images and the enabled state of the Add button based
     * on the current values of the text fields.
     * @private
     * @param {Object} The dictionary of validity states.
     */
    updateValidityWithResults_: function(validity) {
      this.setBadgeValidity_($('editSearchEngineNameValidity'),
                             validity['name'],
                             'editSearchEngineInvalidTitleToolTip');
      this.setBadgeValidity_($('editSearchEngineKeywordValidity'),
                             validity['keyword'],
                             'editSearchEngineInvalidKeywordToolTip');
      this.setBadgeValidity_($('editSearchEngineURLValidity'),
                             validity['url'],
                             'editSearchEngineInvalidURLToolTip');
      $('editSearchEngineOkayButton').disabled =
          !(validity['name'] && validity['keyword'] && validity['url']);
    },

    /**
     * Updates the state of the given validity indicator badge.
     * @private
     * @param {HTMLElement} The badge element to adjust.
     * @param {boolean} Whether or not the badge should be set to the valid
     *     state.
     * @param {string} The tooltip string id for the invalid state.
     */
    setBadgeValidity_: function(element, isValid, tooltip_id) {
      if (isValid) {
        element.className = 'valid-badge';
        element.title = '';
      } else {
        element.className = 'alert-badge';
        element.title = localStrings.getString(tooltip_id);
      }
    },

    /**
     * Returns the input field values as an array suitable for passing to
     * chrome.send. The order of the array is important.
     * @private
     * @return {array} The current input field values.
     */
    getInputFieldValues_: function() {
      return [ $('editSearchEngineName').value,
               $('editSearchEngineKeyword').value,
               $('editSearchEngineURL').value ];
    }
  };

  EditSearchEngineOverlay.setEditDetails = function(engineDetails) {
    EditSearchEngineOverlay.getInstance().setEditDetails_(engineDetails);
  };

  EditSearchEngineOverlay.validityCheckCallback = function(validity) {
    EditSearchEngineOverlay.getInstance().updateValidityWithResults_(validity);
  };

  // Export
  return {
    EditSearchEngineOverlay: EditSearchEngineOverlay
  };

});