Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: chrome/browser/resources/chromeos/switch_access/options.js

Issue 2939133004: Added to options page to let users change keyboard mappings. (Closed)
Patch Set: Missing semicolon Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 function SwitchAccessOptions() { 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 * @private {SwitchAccessPrefs}
27 */ 27 */
28 this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs; 28 this.prefs_ = 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 $('enableAutoScan').checked = 44 $('enableAutoScan').checked =
45 this.switchAccessPrefs_.getBooleanPref('enableAutoScan'); 45 this.prefs_.getBooleanPref('enableAutoScan');
46 $('autoScanTime').value = 46 $('autoScanTime').value =
47 this.switchAccessPrefs_.getNumberPref('autoScanTime') / 1000; 47 this.prefs_.getNumberPref('autoScanTime') / 1000;
48
49 for (let command of this.prefs_.getCommands()) {
50 $(command).value = String.fromCharCode(
51 this.prefs_.getNumberPref(command));
52 }
48 }, 53 },
49 54
50 /** 55 /**
51 * Handle a change by the user to an element representing a user preference. 56 * Handle a change by the user to an element representing a user preference.
52 * 57 *
53 * @param {!Event} event 58 * @param {!Event} event
54 * @private 59 * @private
55 */ 60 */
56 handleInputChange_: function(event) { 61 handleInputChange_: function(event) {
57 let input = event.target; 62 let input = event.target;
58 switch (input.id) { 63 switch (input.id) {
59 case 'enableAutoScan': 64 case 'enableAutoScan':
60 this.switchAccessPrefs_.setPref(input.id, input.checked); 65 this.prefs_.setPref(input.id, input.checked);
61 break; 66 break;
62 case 'autoScanTime': 67 case 'autoScanTime':
63 let oldVal = this.switchAccessPrefs_.getNumberPref(input.id); 68 let oldVal = this.prefs_.getNumberPref(input.id);
64 let val = Number(input.value) * 1000; 69 let val = Number(input.value) * 1000;
65 let min = Number(input.min) * 1000; 70 let min = Number(input.min) * 1000;
66 if (this.isValidInput_(val, oldVal, min)) { 71 if (this.isValidScanTimeInput_(val, oldVal, min)) {
67 input.value = Number(input.value); 72 input.value = Number(input.value);
68 this.switchAccessPrefs_.setPref(input.id, val); 73 this.prefs_.setPref(input.id, val);
69 } else { 74 } else {
70 input.value = oldVal; 75 input.value = oldVal;
71 } 76 }
72 break; 77 break;
73 } 78 default:
79 if (this.prefs_.getCommands().includes(input.id)) {
80 let keyCode = input.value.toUpperCase().charCodeAt(0);
81 if (this.isValidKeyCode_(keyCode)) {
82 input.value = input.value.toUpperCase();
83 this.prefs_.setPref(input.id, keyCode);
84 } else {
85 let oldKeyCode = this.prefs_.getNumberPref(input.id);
86 input.value = String.fromCharCode(oldKeyCode);
87 }
88 }
89 }
74 }, 90 },
75 91
76 /** 92 /**
93 * Return true if |keyCode| is a letter or number, and if it is not already
94 * being used.
95 *
96 * @param {number} keyCode
97 * @return {boolean}
98 */
99 isValidKeyCode_: function(keyCode) {
100 return ((keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0))
101 || (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)))
102 && !this.prefs_.keyCodeIsUsed(keyCode);
103 },
104
105 /**
77 * Return true if the input is a valid autoScanTime input. Otherwise, return 106 * Return true if the input is a valid autoScanTime input. Otherwise, return
78 * false. 107 * false.
79 * 108 *
80 * @param {number} value 109 * @param {number} value
81 * @param {number} oldValue 110 * @param {number} oldValue
82 * @param {number} min 111 * @param {number} min
83 * @return {boolean} 112 * @return {boolean}
84 */ 113 */
85 isValidInput_: function(value, oldValue, min) { 114 isValidScanTimeInput_: function(value, oldValue, min) {
86 return (value !== oldValue) && (value >= min); 115 return (value !== oldValue) && (value >= min);
87 }, 116 },
88 117
89 /** 118 /**
90 * Handle a change in user preferences. 119 * Handle a change in user preferences.
91 * 120 *
92 * @param {!Event} event 121 * @param {!Event} event
93 * @private 122 * @private
94 */ 123 */
95 handlePrefsUpdate_: function(event) { 124 handlePrefsUpdate_: function(event) {
96 let updatedPrefs = event.detail; 125 let updatedPrefs = event.detail;
97 for (let key of Object.keys(updatedPrefs)) { 126 for (let key of Object.keys(updatedPrefs)) {
98 switch (key) { 127 switch (key) {
99 case 'enableAutoScan': 128 case 'enableAutoScan':
100 $(key).checked = updatedPrefs[key]; 129 $(key).checked = updatedPrefs[key];
101 break; 130 break;
102 case 'autoScanTime': 131 case 'autoScanTime':
103 $(key).value = updatedPrefs[key] / 1000; 132 $(key).value = updatedPrefs[key] / 1000;
104 break; 133 break;
134 default:
135 if (this.prefs_.getCommands().includes(key))
136 $(key).value = String.fromCharCode(updatedPrefs[key]);
105 } 137 }
106 } 138 }
107 } 139 }
108 }; 140 };
109 141
110 document.addEventListener('DOMContentLoaded', function() { 142 document.addEventListener('DOMContentLoaded', function() {
111 new SwitchAccessOptions(); 143 new SwitchAccessOptions();
112 }); 144 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698