Chromium Code Reviews| Index: chrome/browser/resources/chromeos/switch_access/options.js |
| diff --git a/chrome/browser/resources/chromeos/switch_access/options.js b/chrome/browser/resources/chromeos/switch_access/options.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b38d0011ca68225791cf5c9f86479fa865e977b |
| --- /dev/null |
| +++ b/chrome/browser/resources/chromeos/switch_access/options.js |
| @@ -0,0 +1,90 @@ |
| +// Copyright 2017 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. |
| + |
| +/** |
| + * Alias for document.getElementById. |
| + * |
| + * @param {string} id |
| + * @return {Element} |
| + */ |
| +let $ = function(id) { |
| + let el = document.getElementById(id); |
| + return el; |
| +} |
| + |
| +/** |
| + * Class to manage the options page. |
| + * |
| + * @constructor |
| + */ |
| +let SwitchAccessOptions = function() { |
| + let background = chrome.extension.getBackgroundPage(); |
| + |
| + /** |
| + * User preferences. |
| + * |
| + * @private |
| + */ |
| + this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs; |
| + |
| + document.addEventListener('DOMContentLoaded', this.init_.bind(this)); |
|
dmazzoni
2017/03/22 21:53:53
Does 'load' work?
Another thing I usually do is j
elichtenberg
2017/03/22 23:02:29
'load' would work too, but 'DOMContentLoaded' is a
|
| + document.addEventListener('change', this.handleInputChange_.bind(this)); |
| + background.document.addEventListener('prefsUpdate', this.handlePrefsUpdate_); |
| +}; |
| + |
| +SwitchAccessOptions.prototype = { |
| + /** |
| + * Initialize the options page by setting all elements representing a user |
| + * preference to show the correct value. |
| + * |
| + * @private |
| + */ |
| + init_: function() { |
| + let prefs = this.switchAccessPrefs_.getPrefs(); |
| + $('enableAutoScan').checked = prefs['enableAutoScan']; |
| + $('autoScanTime').value = prefs['autoScanTime']; |
| + }, |
| + |
| + /** |
| + * Handle a change by the user to an element representing a user preference. |
| + * |
| + * @param {!Event} event |
| + * @private |
| + */ |
| + handleInputChange_: function(event) { |
| + switch (event.target.id) { |
| + case 'enableAutoScan': |
| + this.switchAccessPrefs_.setPref(event.target.id, event.target.checked); |
| + break; |
| + case 'autoScanTime': |
| + this.switchAccessPrefs_.setPref(event.target.id, event.target.value); |
| + break; |
| + } |
| + }, |
| + |
| + /** |
| + * Handle a change in user preferences. |
| + * |
| + * @param {!Event} event |
| + * @private |
| + */ |
| + handlePrefsUpdate_: function(event) { |
| + if (document.readyState === 'interactive' || |
|
dmazzoni
2017/03/22 21:53:54
rather than checking document.readyState, how abou
elichtenberg
2017/03/22 23:02:29
Done. I agree, that does look better.
|
| + document.readyState === 'complete') { |
| + let updatedPrefs = event.detail; |
| + for (let key of Object.keys(updatedPrefs)) { |
| + switch (key) { |
| + case 'enableAutoScan': |
| + $(key).checked = updatedPrefs[key]; |
| + break; |
| + case 'autoScanTime': |
| + $(key).value = updatedPrefs[key]; |
| + break; |
| + } |
| + } |
| + } |
| + } |
| +}; |
| + |
| +new SwitchAccessOptions(); |