| 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 // eslint-disable-next-line no-restricted-properties | 12 // eslint-disable-next-line no-restricted-properties |
| 13 return document.getElementById(id); | 13 return document.getElementById(id); |
| 14 }; | 14 }; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Class to manage the options page. | 17 * Class to manage the options page. |
| 18 * | 18 * |
| 19 * @constructor | 19 * @constructor |
| 20 */ | 20 */ |
| 21 function SwitchAccessOptions() { | 21 function SwitchAccessOptions() { |
| 22 let background = chrome.extension.getBackgroundPage(); | 22 let background = chrome.extension.getBackgroundPage(); |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * User preferences. | 25 * SwitchAccess reference. |
| 26 * | 26 * |
| 27 * @type {SwitchAccessPrefs} | 27 * @private {SwitchAccessInterface} |
| 28 */ | 28 */ |
| 29 this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs; | 29 this.switchAccess_ = background.switchAccess; |
| 30 | 30 |
| 31 this.init_(); | 31 this.init_(); |
| 32 document.addEventListener('change', this.handleInputChange_.bind(this)); | 32 document.addEventListener('change', this.handleInputChange_.bind(this)); |
| 33 background.document.addEventListener( | 33 background.document.addEventListener( |
| 34 'prefsUpdate', this.handlePrefsUpdate_.bind(this)); | 34 'prefsUpdate', this.handlePrefsUpdate_.bind(this)); |
| 35 } | 35 } |
| 36 | 36 |
| 37 SwitchAccessOptions.prototype = { | 37 SwitchAccessOptions.prototype = { |
| 38 /** | 38 /** |
| 39 * Initialize the options page by setting all elements representing a user | 39 * Initialize the options page by setting all elements representing a user |
| 40 * preference to show the correct value. | 40 * preference to show the correct value. |
| 41 * | 41 * |
| 42 * @private | 42 * @private |
| 43 */ | 43 */ |
| 44 init_: function() { | 44 init_: function() { |
| 45 $('enableAutoScan').checked = | 45 $('enableAutoScan').checked = |
| 46 this.switchAccessPrefs_.getBooleanPref('enableAutoScan'); | 46 this.switchAccess_.getBooleanPref('enableAutoScan'); |
| 47 $('autoScanTime').value = | 47 $('autoScanTime').value = |
| 48 this.switchAccessPrefs_.getNumberPref('autoScanTime') / 1000; | 48 this.switchAccess_.getNumberPref('autoScanTime') / 1000; |
| 49 |
| 50 for (let command of this.switchAccess_.getCommands()) { |
| 51 $(command).value = |
| 52 String.fromCharCode(this.switchAccess_.getNumberPref(command)); |
| 53 } |
| 49 }, | 54 }, |
| 50 | 55 |
| 51 /** | 56 /** |
| 52 * Handle a change by the user to an element representing a user preference. | 57 * Handle a change by the user to an element representing a user preference. |
| 53 * | 58 * |
| 54 * @param {!Event} event | 59 * @param {!Event} event |
| 55 * @private | 60 * @private |
| 56 */ | 61 */ |
| 57 handleInputChange_: function(event) { | 62 handleInputChange_: function(event) { |
| 58 let input = event.target; | 63 let input = event.target; |
| 59 switch (input.id) { | 64 switch (input.id) { |
| 60 case 'enableAutoScan': | 65 case 'enableAutoScan': |
| 61 this.switchAccessPrefs_.setPref(input.id, input.checked); | 66 this.switchAccess_.setPref(input.id, input.checked); |
| 62 break; | 67 break; |
| 63 case 'autoScanTime': | 68 case 'autoScanTime': |
| 64 let oldVal = this.switchAccessPrefs_.getNumberPref(input.id); | 69 let oldVal = this.switchAccess_.getNumberPref(input.id); |
| 65 let val = Number(input.value) * 1000; | 70 let val = Number(input.value) * 1000; |
| 66 let min = Number(input.min) * 1000; | 71 let min = Number(input.min) * 1000; |
| 67 if (this.isValidInput_(val, oldVal, min)) { | 72 if (this.isValidScanTimeInput_(val, oldVal, min)) { |
| 68 input.value = Number(input.value); | 73 input.value = Number(input.value); |
| 69 this.switchAccessPrefs_.setPref(input.id, val); | 74 this.switchAccess_.setPref(input.id, val); |
| 70 } else { | 75 } else { |
| 71 input.value = oldVal; | 76 input.value = oldVal; |
| 72 } | 77 } |
| 73 break; | 78 break; |
| 79 default: |
| 80 if (this.switchAccess_.getCommands().includes(input.id)) { |
| 81 let keyCode = input.value.toUpperCase().charCodeAt(0); |
| 82 if (this.isValidKeyCode_(keyCode)) { |
| 83 input.value = input.value.toUpperCase(); |
| 84 this.switchAccess_.setPref(input.id, keyCode); |
| 85 } else { |
| 86 let oldKeyCode = this.switchAccess_.getNumberPref(input.id); |
| 87 input.value = String.fromCharCode(oldKeyCode); |
| 88 } |
| 89 } |
| 74 } | 90 } |
| 75 }, | 91 }, |
| 76 | 92 |
| 77 /** | 93 /** |
| 94 * Return true if |keyCode| is a letter or number, and if it is not already |
| 95 * being used. |
| 96 * |
| 97 * @param {number} keyCode |
| 98 * @return {boolean} |
| 99 */ |
| 100 isValidKeyCode_: function(keyCode) { |
| 101 return ((keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0)) || |
| 102 (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0))) && |
| 103 !this.switchAccess_.keyCodeIsUsed(keyCode); |
| 104 }, |
| 105 |
| 106 /** |
| 78 * Return true if the input is a valid autoScanTime input. Otherwise, return | 107 * Return true if the input is a valid autoScanTime input. Otherwise, return |
| 79 * false. | 108 * false. |
| 80 * | 109 * |
| 81 * @param {number} value | 110 * @param {number} value |
| 82 * @param {number} oldValue | 111 * @param {number} oldValue |
| 83 * @param {number} min | 112 * @param {number} min |
| 84 * @return {boolean} | 113 * @return {boolean} |
| 85 */ | 114 */ |
| 86 isValidInput_: function(value, oldValue, min) { | 115 isValidScanTimeInput_: function(value, oldValue, min) { |
| 87 return (value !== oldValue) && (value >= min); | 116 return (value !== oldValue) && (value >= min); |
| 88 }, | 117 }, |
| 89 | 118 |
| 90 /** | 119 /** |
| 91 * Handle a change in user preferences. | 120 * Handle a change in user preferences. |
| 92 * | 121 * |
| 93 * @param {!Event} event | 122 * @param {!Event} event |
| 94 * @private | 123 * @private |
| 95 */ | 124 */ |
| 96 handlePrefsUpdate_: function(event) { | 125 handlePrefsUpdate_: function(event) { |
| 97 let updatedPrefs = event.detail; | 126 let updatedPrefs = event.detail; |
| 98 for (let key of Object.keys(updatedPrefs)) { | 127 for (let key of Object.keys(updatedPrefs)) { |
| 99 switch (key) { | 128 switch (key) { |
| 100 case 'enableAutoScan': | 129 case 'enableAutoScan': |
| 101 $(key).checked = updatedPrefs[key]; | 130 $(key).checked = updatedPrefs[key]; |
| 102 break; | 131 break; |
| 103 case 'autoScanTime': | 132 case 'autoScanTime': |
| 104 $(key).value = updatedPrefs[key] / 1000; | 133 $(key).value = updatedPrefs[key] / 1000; |
| 105 break; | 134 break; |
| 135 default: |
| 136 if (this.switchAccess_.getCommands().includes(key)) |
| 137 $(key).value = String.fromCharCode(updatedPrefs[key]); |
| 106 } | 138 } |
| 107 } | 139 } |
| 108 } | 140 } |
| 109 }; | 141 }; |
| 110 | 142 |
| 111 document.addEventListener('DOMContentLoaded', function() { | 143 document.addEventListener('DOMContentLoaded', function() { |
| 112 new SwitchAccessOptions(); | 144 new SwitchAccessOptions(); |
| 113 }); | 145 }); |
| OLD | NEW |