1// Copyright (c) 2011 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
7  var OptionsPage = options.OptionsPage;
8  var Preferences = options.Preferences;
9
10  /////////////////////////////////////////////////////////////////////////////
11  // ProxyOptions class:
12
13  /**
14   * Encapsulated handling of ChromeOS proxy options page.
15   * @constructor
16   */
17  function ProxyOptions(model) {
18    OptionsPage.call(this, 'proxy', localStrings.getString('proxyPage'),
19                     'proxyPage');
20  }
21
22  cr.addSingletonGetter(ProxyOptions);
23
24  /**
25   * UI pref change handler.
26   */
27  function handlePrefUpdate(e) {
28    ProxyOptions.getInstance().updateControls();
29  }
30
31  /**
32   * Monitor pref change of given element.
33   */
34  function observePrefsUI(el) {
35    Preferences.getInstance().addEventListener(el.pref, handlePrefUpdate);
36  }
37
38  ProxyOptions.prototype = {
39    // Inherit ProxyOptions from OptionsPage.
40    __proto__: OptionsPage.prototype,
41
42    /**
43     * Initializes ProxyOptions page.
44     */
45    initializePage: function() {
46      // Call base class implementation to starts preference initialization.
47      OptionsPage.prototype.initializePage.call(this);
48
49      // Set up ignored page.
50      options.proxyexceptions.ProxyExceptions.decorate($('ignoredHostList'));
51
52      this.addEventListener('visibleChange', this.handleVisibleChange_);
53      $('removeHost').addEventListener('click', this.handleRemoveExceptions_);
54      $('addHost').addEventListener('click', this.handleAddException_);
55      $('directProxy').addEventListener('click', this.disableManual_);
56      $('manualProxy').addEventListener('click', this.enableManual_);
57      $('autoProxy').addEventListener('click', this.disableManual_);
58      $('proxyAllProtocols').addEventListener('click', this.toggleSingle_);
59
60      observePrefsUI($('directProxy'));
61      observePrefsUI($('manualProxy'));
62      observePrefsUI($('autoProxy'));
63      observePrefsUI($('proxyAllProtocols'));
64    },
65
66    proxyListInitalized_: false,
67
68    /**
69     * Update controls state.
70     * @public
71     */
72    updateControls: function() {
73      this.toggleSingle_();
74      if ($('manualProxy').checked) {
75        this.enableManual_();
76      } else {
77        this.disableManual_();
78      }
79      if (!this.proxyListInitalized_ && this.visible) {
80        this.proxyListInitalized_ = true;
81        $('ignoredHostList').redraw();
82      }
83    },
84
85    /**
86     * Handler for OptionsPage's visible property change event.
87     * @private
88     * @param {Event} e Property change event.
89     */
90    handleVisibleChange_: function(e) {
91      this.updateControls();
92    },
93
94    /**
95     * Handler for when the user clicks on the checkbox to allow a
96     * single proxy usage.
97     * @private
98     * @param {Event} e Click Event.
99     */
100    toggleSingle_: function(e) {
101      if($('proxyAllProtocols').value) {
102        $('multiProxy').style.display = 'none';
103        $('singleProxy').style.display = 'block';
104      } else {
105        $('multiProxy').style.display = 'block';
106        $('singleProxy').style.display = 'none';
107      }
108    },
109
110    /**
111     * Handler for selecting a radio button that will disable the manual
112     * controls.
113     * @private
114     * @param {Event} e Click event.
115     */
116    disableManual_: function(e) {
117      $('proxyAllProtocols').disabled = true;
118      $('proxyHostName').disabled = true;
119      $('proxyHostPort').disabled = true;
120      $('proxyHostSingleName').disabled = true;
121      $('proxyHostSinglePort').disabled = true;
122      $('secureProxyHostName').disabled = true;
123      $('secureProxyPort').disabled = true;
124      $('ftpProxy').disabled = true;
125      $('ftpProxyPort').disabled = true;
126      $('socksHost').disabled = true;
127      $('socksPort').disabled = true;
128      $('newHost').disabled = true;
129      $('removeHost').disabled = true;
130      $('addHost').disabled = true;
131      $('advancedConfig').style.display = 'none';
132    },
133
134    /**
135     * Handler for selecting a radio button that will enable the manual
136     * controls.
137     * @private
138     * @param {Event} e Click event.
139     */
140    enableManual_: function(e) {
141      $('proxyAllProtocols').disabled = false;
142      $('proxyHostName').disabled = false;
143      $('proxyHostPort').disabled = false;
144      $('proxyHostSingleName').disabled = false;
145      $('proxyHostSinglePort').disabled = false;
146      $('secureProxyHostName').disabled = false;
147      $('secureProxyPort').disabled = false;
148      $('ftpProxy').disabled = false;
149      $('ftpProxyPort').disabled = false;
150      $('socksHost').disabled = false;
151      $('socksPort').disabled = false;
152      $('newHost').disabled = false;
153      $('removeHost').disabled = false;
154      $('addHost').disabled = false;
155      $('advancedConfig').style.display = '-webkit-box';
156      $('ignoredHostList').redraw();
157    },
158
159    /**
160     * Handler for "add" event fired from userNameEdit.
161     * @private
162     * @param {Event} e Add event fired from userNameEdit.
163     */
164    handleAddException_: function(e) {
165      var exception = $('newHost').value;
166      $('newHost').value = '';
167
168      exception = exception.trim();
169      if (exception)
170        $('ignoredHostList').addException(exception);
171    },
172
173    /**
174     * Handler for when the remove button is clicked
175     * @private
176     */
177    handleRemoveExceptions_: function(e) {
178      var selectedItems = $('ignoredHostList').selectedItems;
179      for (var x = 0; x < selectedItems.length; x++) {
180        $('ignoredHostList').removeException(selectedItems[x]);
181      }
182    }
183  };
184
185  // Export
186  return {
187    ProxyOptions: ProxyOptions
188  };
189
190});
191