| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Alias for document.getElementById. |
| 7 * |
| 8 * @param {string} id |
| 9 * @return {Element} |
| 10 */ |
| 11 let $ = function(id) { |
| 12 return document.getElementById(id); |
| 13 }; |
| 14 |
| 15 /** |
| 16 * Class to manage the options page. |
| 17 * |
| 18 * @constructor |
| 19 */ |
| 20 let SwitchAccessOptions = function() { |
| 21 let background = chrome.extension.getBackgroundPage(); |
| 22 |
| 23 /** |
| 24 * User preferences. |
| 25 * |
| 26 * @type {SwitchAccessPrefs} |
| 27 */ |
| 28 this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs; |
| 29 |
| 30 this.init_(); |
| 31 document.addEventListener('change', this.handleInputChange_.bind(this)); |
| 32 background.document.addEventListener( |
| 33 'prefsUpdate', this.handlePrefsUpdate_.bind(this)); |
| 34 }; |
| 35 |
| 36 SwitchAccessOptions.prototype = { |
| 37 /** |
| 38 * Initialize the options page by setting all elements representing a user |
| 39 * preference to show the correct value. |
| 40 * |
| 41 * @private |
| 42 */ |
| 43 init_: function() { |
| 44 let prefs = this.switchAccessPrefs_.getPrefs(); |
| 45 $('enableAutoScan').checked = prefs['enableAutoScan']; |
| 46 $('autoScanTime').value = prefs['autoScanTime']; |
| 47 }, |
| 48 |
| 49 /** |
| 50 * Handle a change by the user to an element representing a user preference. |
| 51 * |
| 52 * @param {!Event} event |
| 53 * @private |
| 54 */ |
| 55 handleInputChange_: function(event) { |
| 56 switch (event.target.id) { |
| 57 case 'enableAutoScan': |
| 58 this.switchAccessPrefs_.setPref(event.target.id, event.target.checked); |
| 59 break; |
| 60 case 'autoScanTime': |
| 61 this.switchAccessPrefs_.setPref(event.target.id, event.target.value); |
| 62 break; |
| 63 } |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Handle a change in user preferences. |
| 68 * |
| 69 * @param {!Event} event |
| 70 * @private |
| 71 */ |
| 72 handlePrefsUpdate_: function(event) { |
| 73 let updatedPrefs = event.detail; |
| 74 for (let key of Object.keys(updatedPrefs)) { |
| 75 switch (key) { |
| 76 case 'enableAutoScan': |
| 77 $(key).checked = updatedPrefs[key]; |
| 78 break; |
| 79 case 'autoScanTime': |
| 80 $(key).value = updatedPrefs[key]; |
| 81 break; |
| 82 } |
| 83 } |
| 84 } |
| 85 }; |
| 86 |
| 87 document.addEventListener('DOMContentLoaded', function() { |
| 88 new SwitchAccessOptions(); |
| 89 }); |
| OLD | NEW |