| 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 * Alias for document.getElementById. | 6 * Alias for document.getElementById. |
| 7 * | 7 * |
| 8 * @param {string} id | 8 * @param {string} id |
| 9 * @return {Element} | 9 * @return {Element} |
| 10 */ | 10 */ |
| 11 let $ = function(id) { | 11 let $ = function(id) { |
| 12 return document.getElementById(id); | 12 return document.getElementById(id); |
| 13 }; | 13 }; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Class to manage the options page. | 16 * Class to manage the options page. |
| 17 * | 17 * |
| 18 * @constructor | 18 * @constructor |
| 19 */ | 19 */ |
| 20 let SwitchAccessOptions = function() { | 20 function SwitchAccessOptions() { |
| 21 let background = chrome.extension.getBackgroundPage(); | 21 let background = chrome.extension.getBackgroundPage(); |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * User preferences. | 24 * User preferences. |
| 25 * | 25 * |
| 26 * @type {SwitchAccessPrefs} | 26 * @type {SwitchAccessPrefs} |
| 27 */ | 27 */ |
| 28 this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs; | 28 this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs; |
| 29 | 29 |
| 30 this.init_(); | 30 this.init_(); |
| 31 document.addEventListener('change', this.handleInputChange_.bind(this)); | 31 document.addEventListener('change', this.handleInputChange_.bind(this)); |
| 32 background.document.addEventListener( | 32 background.document.addEventListener( |
| 33 'prefsUpdate', this.handlePrefsUpdate_.bind(this)); | 33 'prefsUpdate', this.handlePrefsUpdate_.bind(this)); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 SwitchAccessOptions.prototype = { | 36 SwitchAccessOptions.prototype = { |
| 37 /** | 37 /** |
| 38 * Initialize the options page by setting all elements representing a user | 38 * Initialize the options page by setting all elements representing a user |
| 39 * preference to show the correct value. | 39 * preference to show the correct value. |
| 40 * | 40 * |
| 41 * @private | 41 * @private |
| 42 */ | 42 */ |
| 43 init_: function() { | 43 init_: function() { |
| 44 let prefs = this.switchAccessPrefs_.getPrefs(); | 44 $('enableAutoScan').checked = |
| 45 $('enableAutoScan').checked = prefs['enableAutoScan']; | 45 this.switchAccessPrefs_.getBooleanPref('enableAutoScan'); |
| 46 $('autoScanTime').value = prefs['autoScanTime']; | 46 $('autoScanTime').value = |
| 47 this.switchAccessPrefs_.getNumberPref('autoScanTime') / 1000; |
| 47 }, | 48 }, |
| 48 | 49 |
| 49 /** | 50 /** |
| 50 * Handle a change by the user to an element representing a user preference. | 51 * Handle a change by the user to an element representing a user preference. |
| 51 * | 52 * |
| 52 * @param {!Event} event | 53 * @param {!Event} event |
| 53 * @private | 54 * @private |
| 54 */ | 55 */ |
| 55 handleInputChange_: function(event) { | 56 handleInputChange_: function(event) { |
| 56 switch (event.target.id) { | 57 let input = event.target; |
| 58 switch (input.id) { |
| 57 case 'enableAutoScan': | 59 case 'enableAutoScan': |
| 58 this.switchAccessPrefs_.setPref(event.target.id, event.target.checked); | 60 this.switchAccessPrefs_.setPref(input.id, input.checked); |
| 59 break; | 61 break; |
| 60 case 'autoScanTime': | 62 case 'autoScanTime': |
| 61 this.switchAccessPrefs_.setPref(event.target.id, event.target.value); | 63 let oldVal = this.switchAccessPrefs_.getNumberPref(input.id); |
| 64 let val = Number(input.value) * 1000; |
| 65 let min = Number(input.min) * 1000; |
| 66 if (this.isValidInput_(val, oldVal, min)) { |
| 67 input.value = Number(input.value); |
| 68 this.switchAccessPrefs_.setPref(input.id, val); |
| 69 } else { |
| 70 input.value = oldVal; |
| 71 } |
| 62 break; | 72 break; |
| 63 } | 73 } |
| 64 }, | 74 }, |
| 65 | 75 |
| 66 /** | 76 /** |
| 77 * Return true if the input is a valid autoScanTime input. Otherwise, return |
| 78 * false. |
| 79 * |
| 80 * @param {number} value |
| 81 * @param {number} oldValue |
| 82 * @param {number} min |
| 83 * @return {boolean} |
| 84 */ |
| 85 isValidInput_: function(value, oldValue, min) { |
| 86 return (value !== oldValue) && (value >= min); |
| 87 }, |
| 88 |
| 89 /** |
| 67 * Handle a change in user preferences. | 90 * Handle a change in user preferences. |
| 68 * | 91 * |
| 69 * @param {!Event} event | 92 * @param {!Event} event |
| 70 * @private | 93 * @private |
| 71 */ | 94 */ |
| 72 handlePrefsUpdate_: function(event) { | 95 handlePrefsUpdate_: function(event) { |
| 73 let updatedPrefs = event.detail; | 96 let updatedPrefs = event.detail; |
| 74 for (let key of Object.keys(updatedPrefs)) { | 97 for (let key of Object.keys(updatedPrefs)) { |
| 75 switch (key) { | 98 switch (key) { |
| 76 case 'enableAutoScan': | 99 case 'enableAutoScan': |
| 77 $(key).checked = updatedPrefs[key]; | 100 $(key).checked = updatedPrefs[key]; |
| 78 break; | 101 break; |
| 79 case 'autoScanTime': | 102 case 'autoScanTime': |
| 80 $(key).value = updatedPrefs[key]; | 103 $(key).value = updatedPrefs[key] / 1000; |
| 81 break; | 104 break; |
| 82 } | 105 } |
| 83 } | 106 } |
| 84 } | 107 } |
| 85 }; | 108 }; |
| 86 | 109 |
| 87 document.addEventListener('DOMContentLoaded', function() { | 110 document.addEventListener('DOMContentLoaded', function() { |
| 88 new SwitchAccessOptions(); | 111 new SwitchAccessOptions(); |
| 89 }); | 112 }); |
| OLD | NEW |