• 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
7  /**
8   * Auto-repeat delays (in ms) for the corresponding slider values, from
9   * long to short. The values were chosen to provide a large range while giving
10   * several options near the defaults.
11   * @type {!Array.<number>}
12   * @const
13   */
14  var AUTO_REPEAT_DELAYS =
15      [2000, 1500, 1000, 500, 300, 200, 150];
16
17  /**
18   * Auto-repeat intervals (in ms) for the corresponding slider values, from
19   * long to short. The slider itself is labeled "rate", the inverse of
20   * interval, and goes from slow (long interval) to fast (short interval).
21   * @type {!Array.<number>}
22   * @const
23   */
24  var AUTO_REPEAT_INTERVALS =
25      [2000, 1000, 500, 300, 200, 100, 50, 30, 20];
26
27  /**
28   * Encapsulated handling of the keyboard overlay.
29   * @constructor
30   * @extends {options.SettingsDialog}
31   */
32  function KeyboardOverlay() {
33    options.SettingsDialog.call(this, 'keyboard-overlay',
34        loadTimeData.getString('keyboardOverlayTabTitle'),
35        'keyboard-overlay',
36        assertInstanceof($('keyboard-confirm'), HTMLButtonElement),
37        assertInstanceof($('keyboard-cancel'), HTMLButtonElement));
38  }
39
40  cr.addSingletonGetter(KeyboardOverlay);
41
42  KeyboardOverlay.prototype = {
43    __proto__: options.SettingsDialog.prototype,
44
45    /** @override */
46    initializePage: function() {
47      options.SettingsDialog.prototype.initializePage.call(this);
48
49      $('enable-auto-repeat').customPrefChangeHandler =
50          this.handleAutoRepeatEnabledPrefChange_.bind(this);
51
52      var autoRepeatDelayRange = $('auto-repeat-delay-range');
53      autoRepeatDelayRange.valueMap = AUTO_REPEAT_DELAYS;
54      autoRepeatDelayRange.max = AUTO_REPEAT_DELAYS.length - 1;
55      autoRepeatDelayRange.customPrefChangeHandler =
56          this.handleAutoRepeatDelayPrefChange_.bind(this);
57
58      var autoRepeatIntervalRange = $('auto-repeat-interval-range');
59      autoRepeatIntervalRange.valueMap = AUTO_REPEAT_INTERVALS;
60      autoRepeatIntervalRange.max = AUTO_REPEAT_INTERVALS.length - 1;
61      autoRepeatIntervalRange.customPrefChangeHandler =
62          this.handleAutoRepeatIntervalPrefChange_.bind(this);
63
64      $('languages-and-input-settings').onclick = function(e) {
65        PageManager.showPageByName('languages');
66        chrome.send('coreOptionsUserMetricsAction',
67                    ['Options_KeyboardShowLanguageSettings']);
68      };
69
70      $('keyboard-shortcuts').onclick = function(e) {
71        chrome.send('showKeyboardShortcuts');
72        chrome.send('coreOptionsUserMetricsAction',
73                    ['Options_KeyboardShowKeyboardShortcuts']);
74      };
75    },
76
77    /**
78     * Handles auto-repeat enabled pref change and allows the event to continue
79     * propagating.
80     * @param {Event} e Change event.
81     * @return {boolean} Whether the event has finished being handled.
82     * @private
83     */
84    handleAutoRepeatEnabledPrefChange_: function(e) {
85      $('auto-repeat-settings-section').classList.toggle('disabled',
86                                                         !e.value.value);
87      $('auto-repeat-delay-range').disabled =
88          $('auto-repeat-interval-range').disabled = !e.value.value;
89      return false;
90    },
91
92    /**
93     * Handles auto-repeat delay pref change and stops the event from
94     * propagating.
95     * @param {Event} e Change event.
96     * @return {boolean} Whether the event has finished being handled.
97     * @private
98     */
99    handleAutoRepeatDelayPrefChange_: function(e) {
100      this.updateSliderFromValue_('auto-repeat-delay-range',
101                                  e.value.value,
102                                  AUTO_REPEAT_DELAYS);
103      return true;
104    },
105
106    /**
107     * Handles auto-repeat interval pref change and stops the event from
108     * propagating.
109     * @param {Event} e Change event.
110     * @return {boolean} Whether the event has finished being handled.
111     * @private
112     */
113    handleAutoRepeatIntervalPrefChange_: function(e) {
114      this.updateSliderFromValue_('auto-repeat-interval-range',
115                                  e.value.value,
116                                  AUTO_REPEAT_INTERVALS);
117      return true;
118    },
119
120    /**
121     * Show/hide the caps lock remapping section.
122     * @private
123     */
124    showCapsLockOptions_: function(show) {
125      $('caps-lock-remapping-section').hidden = !show;
126    },
127
128    /**
129     * Show/hide the diamond key remapping section.
130     * @private
131     */
132    showDiamondKeyOptions_: function(show) {
133      $('diamond-key-remapping-section').hidden = !show;
134    },
135
136    /**
137     * Sets the slider's value to the number in |values| that is closest to
138     * |value|.
139     * @param {string} id The slider's ID.
140     * @param {number} value The value to find.
141     * @param {!Array.<number>} values The array to search.
142     * @private
143     */
144    updateSliderFromValue_: function(id, value, values) {
145      var index = values.indexOf(value);
146      if (index == -1) {
147        var closestValue = Infinity;
148        for (var i = 0; i < values.length; i++) {
149          if (Math.abs(values[i] - value) <
150              Math.abs(closestValue - value)) {
151            closestValue = values[i];
152            index = i;
153          }
154        }
155
156        assert(index != -1,
157               'Failed to update ' + id + ' from pref with value ' + value);
158      }
159
160      $(id).value = index;
161    },
162  };
163
164  // Forward public APIs to private implementations.
165  cr.makePublic(KeyboardOverlay, [
166    'showCapsLockOptions',
167    'showDiamondKeyOptions',
168  ]);
169
170  // Export
171  return {
172    KeyboardOverlay: KeyboardOverlay
173  };
174});
175