OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Class to manage user preferences. | 6 * Class to manage user preferences. |
7 * | 7 * |
8 * @constructor | 8 * @constructor |
| 9 * @param {SwitchAccessInterface} switchAccess |
9 */ | 10 */ |
10 function SwitchAccessPrefs() { | 11 function SwitchAccessPrefs(switchAccess) { |
| 12 /** |
| 13 * SwitchAccess reference. |
| 14 * |
| 15 * @private {SwitchAccessInterface} |
| 16 */ |
| 17 this.switchAccess_ = switchAccess; |
| 18 |
11 /** | 19 /** |
12 * User preferences, initially set to the default preference values. | 20 * User preferences, initially set to the default preference values. |
13 * | 21 * |
14 * @private | 22 * @private |
15 */ | 23 */ |
16 this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); | 24 this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); |
| 25 for (let command of this.switchAccess_.getCommands()) |
| 26 this.prefs_[command] = this.switchAccess_.getDefaultKeyCodeFor(command); |
17 | 27 |
18 this.loadPrefs_(); | 28 this.loadPrefs_(); |
19 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); | 29 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); |
20 } | 30 } |
21 | 31 |
22 SwitchAccessPrefs.prototype = { | 32 SwitchAccessPrefs.prototype = { |
23 /** | 33 /** |
24 * Asynchronously load the current preferences from chrome.storage.sync and | 34 * Asynchronously load the current preferences from chrome.storage.sync and |
25 * store them in this.prefs_, if this.prefs_ is not already set to that value. | 35 * store them in this.prefs_, if this.prefs_ is not already set to that value. |
26 * If this.prefs_ changes, fire a prefsUpdate event. | 36 * If this.prefs_ changes, fire a prefsUpdate event. |
27 * | 37 * |
28 * @private | 38 * @private |
29 */ | 39 */ |
30 loadPrefs_: function() { | 40 loadPrefs_: function() { |
31 let defaultKeys = Object.keys(this.DEFAULT_PREFS); | 41 let defaultKeys = Object.keys(this.prefs_); |
32 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { | 42 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { |
33 let updatedPrefs = {}; | 43 let updatedPrefs = {}; |
34 for (let key of Object.keys(loadedPrefs)) { | 44 for (let key of Object.keys(loadedPrefs)) { |
35 if (this.prefs_[key] !== loadedPrefs[key]) { | 45 if (this.prefs_[key] !== loadedPrefs[key]) { |
36 this.prefs_[key] = loadedPrefs[key]; | 46 this.prefs_[key] = loadedPrefs[key]; |
37 updatedPrefs[key] = loadedPrefs[key]; | 47 updatedPrefs[key] = loadedPrefs[key]; |
38 } | 48 } |
39 } | 49 } |
40 if (Object.keys(updatedPrefs).length > 0) { | 50 if (Object.keys(updatedPrefs).length > 0) { |
41 let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); | 51 let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 */ | 129 */ |
120 getStringPref: function(key) { | 130 getStringPref: function(key) { |
121 let value = this.prefs_[key]; | 131 let value = this.prefs_[key]; |
122 if (typeof value === 'string') | 132 if (typeof value === 'string') |
123 return value; | 133 return value; |
124 else | 134 else |
125 throw new TypeError('No value of string type for key \'' + key + '\''); | 135 throw new TypeError('No value of string type for key \'' + key + '\''); |
126 }, | 136 }, |
127 | 137 |
128 /** | 138 /** |
129 * The default value of all preferences. All preferences should be primitives | 139 * Returns true if |keyCode| is already used to run a command from the |
130 * to prevent changes to default values. | 140 * keyboard. |
| 141 * |
| 142 * @param {number} keyCode |
| 143 * @return {boolean} |
| 144 */ |
| 145 keyCodeIsUsed: function(keyCode) { |
| 146 for (let command of this.switchAccess_.getCommands()) { |
| 147 if (keyCode === this.prefs_[command]) |
| 148 return true; |
| 149 } |
| 150 return false; |
| 151 }, |
| 152 |
| 153 /** |
| 154 * The default value of all preferences besides command keyboard bindings. |
| 155 * All preferences should be primitives to prevent changes to default values. |
131 * | 156 * |
132 * @const | 157 * @const |
133 */ | 158 */ |
134 DEFAULT_PREFS: {'enableAutoScan': false, 'autoScanTime': 800} | 159 DEFAULT_PREFS: {'enableAutoScan': false, 'autoScanTime': 800} |
135 }; | 160 }; |
OLD | NEW |