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

Side by Side Diff: chrome/browser/resources/chromeos/keyboard_overlay.js

Issue 10356042: Fix presubmit js style nits. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <include src="keyboard_overlay_data.js"/> 5 <include src="keyboard_overlay_data.js"/>
6 <include src="keyboard_overlay_accessibility_helper.js"/> 6 <include src="keyboard_overlay_accessibility_helper.js"/>
7 7
8 var BASE_KEYBOARD = { 8 var BASE_KEYBOARD = {
9 top: 0, 9 top: 0,
10 left: 0, 10 left: 0,
(...skipping 19 matching lines...) Expand all
30 '1D': 'is-ctrl', 30 '1D': 'is-ctrl',
31 '38': 'is-alt' 31 '38': 'is-alt'
32 }; 32 };
33 33
34 var LABEL_TO_IDENTIFIER = { 34 var LABEL_TO_IDENTIFIER = {
35 'search': 'E0 5B', 35 'search': 'E0 5B',
36 'ctrl': '1D', 36 'ctrl': '1D',
37 'alt': '38', 37 'alt': '38',
38 'caps lock': '3A', 38 'caps lock': '3A',
39 'disabled': 'DISABLED' 39 'disabled': 'DISABLED'
40 } 40 };
41 41
42 var keyboardOverlayId = 'en_US'; 42 var keyboardOverlayId = 'en_US';
43 var identifierMap = {}; 43 var identifierMap = {};
44 44
45 /** 45 /**
46 * Returns layouts data. 46 * Returns layouts data.
47 * @return {Object} Keyboard layout data.
47 */ 48 */
48 function getLayouts() { 49 function getLayouts() {
49 return keyboardOverlayData['layouts']; 50 return keyboardOverlayData['layouts'];
50 } 51 }
51 52
52 /** 53 /**
53 * Returns shortcut data. 54 * Returns shortcut data.
55 * @return {Object} Keyboard shortcut data.
54 */ 56 */
55 function getShortcutData() { 57 function getShortcutData() {
56 return keyboardOverlayData['shortcut']; 58 return keyboardOverlayData['shortcut'];
57 } 59 }
58 60
59 /** 61 /**
60 * Returns the keyboard overlay ID. 62 * Returns the keyboard overlay ID.
63 * @return {string} Keyboard overlay ID.
61 */ 64 */
62 function getKeyboardOverlayId() { 65 function getKeyboardOverlayId() {
63 return keyboardOverlayId; 66 return keyboardOverlayId;
64 } 67 }
65 68
66 /** 69 /**
67 * Returns keyboard glyph data. 70 * Returns keyboard glyph data.
71 * @return {Object} Keyboard glyph data.
68 */ 72 */
69 function getKeyboardGlyphData() { 73 function getKeyboardGlyphData() {
70 return keyboardOverlayData['keyboardGlyph'][getKeyboardOverlayId()]; 74 return keyboardOverlayData['keyboardGlyph'][getKeyboardOverlayId()];
71 } 75 }
72 76
73 /** 77 /**
74 * Converts a single hex number to a character. 78 * Converts a single hex number to a character.
79 * @param {string} hex Hexadecimal string.
80 * @return {string} Unicode values of hexadecimal string.
75 */ 81 */
76 function hex2char(hex) { 82 function hex2char(hex) {
77 if (!hex) { 83 if (!hex) {
78 return ''; 84 return '';
79 } 85 }
80 var result = ''; 86 var result = '';
81 var n = parseInt(hex, 16); 87 var n = parseInt(hex, 16);
82 if (n <= 0xFFFF) { 88 if (n <= 0xFFFF) {
83 result += String.fromCharCode(n); 89 result += String.fromCharCode(n);
84 } else if (n <= 0x10FFFF) { 90 } else if (n <= 0x10FFFF) {
85 n -= 0x10000; 91 n -= 0x10000;
86 result += (String.fromCharCode(0xD800 | (n >> 10)) + 92 result += (String.fromCharCode(0xD800 | (n >> 10)) +
87 String.fromCharCode(0xDC00 | (n & 0x3FF))); 93 String.fromCharCode(0xDC00 | (n & 0x3FF)));
88 } else { 94 } else {
89 console.error('hex2Char error: Code point out of range :' + hex); 95 console.error('hex2Char error: Code point out of range :' + hex);
90 } 96 }
91 return result; 97 return result;
92 } 98 }
93 99
94 /** 100 /**
95 * Returns a list of modifiers from the key event. 101 * Returns a list of modifiers from the key event.
102 * @param {Event} e The key event.
103 * @return {Array} List of modifiers based on key event.
96 */ 104 */
97 function getModifiers(e) { 105 function getModifiers(e) {
98 if (!e) { 106 if (!e)
99 return []; 107 return [];
100 } 108
101 var isKeyDown = (e.type == 'keydown'); 109 var isKeyDown = (e.type == 'keydown');
102 var keyCodeToModifier = { 110 var keyCodeToModifier = {
103 16: 'SHIFT', 111 16: 'SHIFT',
104 17: 'CTRL', 112 17: 'CTRL',
105 18: 'ALT', 113 18: 'ALT',
106 91: 'ALT', // left ALT pressed with SHIFT 114 91: 'ALT', // left ALT pressed with SHIFT
107 92: 'ALT', // right ALT pressed with SHIFT 115 92: 'ALT', // right ALT pressed with SHIFT
108 }; 116 };
109 var modifierWithKeyCode = keyCodeToModifier[e.keyCode]; 117 var modifierWithKeyCode = keyCodeToModifier[e.keyCode];
110 var isPressed = {'SHIFT': e.shiftKey, 'CTRL': e.ctrlKey, 'ALT': e.altKey}; 118 var isPressed = {'SHIFT': e.shiftKey, 'CTRL': e.ctrlKey, 'ALT': e.altKey};
111 // if e.keyCode is one of Shift, Ctrl and Alt, isPressed should 119 // if e.keyCode is one of Shift, Ctrl and Alt, isPressed should
112 // be changed because the key currently pressed 120 // be changed because the key currently pressed
113 // does not affect the values of e.shiftKey, e.ctrlKey and e.altKey 121 // does not affect the values of e.shiftKey, e.ctrlKey and e.altKey
114 if(modifierWithKeyCode){ 122 if (modifierWithKeyCode)
115 isPressed[modifierWithKeyCode] = isKeyDown; 123 isPressed[modifierWithKeyCode] = isKeyDown;
116 } 124
117 // make the result array 125 // make the result array
118 return ['SHIFT', 'CTRL', 'ALT'].filter( 126 return ['SHIFT', 'CTRL', 'ALT'].filter(
119 function(modifier) { 127 function(modifier) {
120 return isPressed[modifier]; 128 return isPressed[modifier];
121 }).sort(); 129 }).sort();
122 } 130 }
123 131
124 /** 132 /**
125 * Returns an ID of the key. 133 * Returns an ID of the key.
134 * @param {string} identifier Key identifier.
135 * @param {number} i Key number.
136 * @return {string} Key ID.
126 */ 137 */
127 function keyId(identifier, i) { 138 function keyId(identifier, i) {
128 return identifier + '-key-' + i; 139 return identifier + '-key-' + i;
129 } 140 }
130 141
131 /** 142 /**
132 * Returns an ID of the text on the key. 143 * Returns an ID of the text on the key.
144 * @param {string} identifier Key identifier.
145 * @param {number} i Key number.
146 * @return {string} Key text ID.
133 */ 147 */
134 function keyTextId(identifier, i) { 148 function keyTextId(identifier, i) {
135 return identifier + '-key-text-' + i; 149 return identifier + '-key-text-' + i;
136 } 150 }
137 151
138 /** 152 /**
139 * Returns an ID of the shortcut text. 153 * Returns an ID of the shortcut text.
154 * @param {string} identifier Key identifier.
155 * @param {number} i Key number.
156 * @return {string} Key shortcut text ID.
140 */ 157 */
141 function shortcutTextId(identifier, i) { 158 function shortcutTextId(identifier, i) {
142 return identifier + '-shortcut-text-' + i; 159 return identifier + '-shortcut-text-' + i;
143 } 160 }
144 161
145 /** 162 /**
146 * Returns true if |list| contains |e|. 163 * Returns true if |list| contains |e|.
164 * @param {Array} list Container list.
165 * @param {string} e Element string.
166 * @return {boolean} Returns true if the list contains the element.
147 */ 167 */
148 function contains(list, e) { 168 function contains(list, e) {
149 return list.indexOf(e) != -1; 169 return list.indexOf(e) != -1;
150 } 170 }
151 171
152 /** 172 /**
153 * Returns a list of the class names corresponding to the identifier and 173 * Returns a list of the class names corresponding to the identifier and
154 * modifiers. 174 * modifiers.
175 * @param {string} identifier Key identifier.
176 * @param {Array} modifiers List of key modifiers.
177 * @return {Array} List of class names corresponding to specified params.
155 */ 178 */
156 function getKeyClasses(identifier, modifiers) { 179 function getKeyClasses(identifier, modifiers) {
157 var classes = ['keyboard-overlay-key']; 180 var classes = ['keyboard-overlay-key'];
158 for (var i = 0; i < modifiers.length; ++i) { 181 for (var i = 0; i < modifiers.length; ++i) {
159 classes.push(MODIFIER_TO_CLASS[modifiers[i]]); 182 classes.push(MODIFIER_TO_CLASS[modifiers[i]]);
160 } 183 }
161 184
162 if ((identifier == '2A' && contains(modifiers, 'SHIFT')) || 185 if ((identifier == '2A' && contains(modifiers, 'SHIFT')) ||
163 (identifier == '1D' && contains(modifiers, 'CTRL')) || 186 (identifier == '1D' && contains(modifiers, 'CTRL')) ||
164 (identifier == '38' && contains(modifiers, 'ALT'))) { 187 (identifier == '38' && contains(modifiers, 'ALT'))) {
165 classes.push('pressed'); 188 classes.push('pressed');
166 classes.push(IDENTIFIER_TO_CLASS[identifier]); 189 classes.push(IDENTIFIER_TO_CLASS[identifier]);
167 } 190 }
168 return classes; 191 return classes;
169 } 192 }
170 193
171 /** 194 /**
172 * Returns true if a character is a ASCII character. 195 * Returns true if a character is a ASCII character.
196 * @param {string} c A character to be checked.
197 * @return {boolean} True if the character is an ASCII character.
173 */ 198 */
174 function isAscii(c) { 199 function isAscii(c) {
175 var charCode = c.charCodeAt(0); 200 var charCode = c.charCodeAt(0);
176 return 0x00 <= charCode && charCode <= 0x7F; 201 return 0x00 <= charCode && charCode <= 0x7F;
177 } 202 }
178 203
179 /** 204 /**
180 * Returns a remapped identiifer based on the preference. 205 * Returns a remapped identiifer based on the preference.
206 * @param {string} identifier Key identifier.
207 * @return {string} Remapped identifier.
181 */ 208 */
182 function remapIdentifier(identifier) { 209 function remapIdentifier(identifier) {
183 return identifierMap[identifier] || identifier; 210 return identifierMap[identifier] || identifier;
184 } 211 }
185 212
186 /** 213 /**
187 * Returns a label of the key. 214 * Returns a label of the key.
215 * @param {string} keyData Key glyph data.
216 * @param {Array} modifiers Key Modifier list.
217 * @return {string} Label of the key.
188 */ 218 */
189 function getKeyLabel(keyData, modifiers) { 219 function getKeyLabel(keyData, modifiers) {
190 if (!keyData) { 220 if (!keyData) {
191 return ''; 221 return '';
192 } 222 }
193 if (keyData.label) { 223 if (keyData.label) {
194 return keyData.label; 224 return keyData.label;
195 } 225 }
196 var keyLabel = ''; 226 var keyLabel = '';
197 for (var j = 1; j <= 9; j++) { 227 for (var j = 1; j <= 9; j++) {
198 var pos = keyData['p' + j]; 228 var pos = keyData['p' + j];
199 if (!pos) { 229 if (!pos) {
200 continue; 230 continue;
201 } 231 }
202 keyLabel = hex2char(pos); 232 keyLabel = hex2char(pos);
203 if (!keyLabel) { 233 if (!keyLabel) {
204 continue; 234 continue;
205 } 235 }
206 if (isAscii(keyLabel) && 236 if (isAscii(keyLabel) &&
207 getShortcutData()[getAction(keyLabel, modifiers)]) { 237 getShortcutData()[getAction(keyLabel, modifiers)]) {
208 break; 238 break;
209 } 239 }
210 } 240 }
211 return keyLabel; 241 return keyLabel;
212 } 242 }
213 243
214 /** 244 /**
215 * Returns a normalized string used for a key of shortcutData. 245 * Returns a normalized string used for a key of shortcutData.
216 * 246 *
217 * Examples: 247 * Examples:
218 * keycode: 'd', modifiers: ['CTRL', 'SHIFT'] => 'd<>CTRL<>SHIFT' 248 * keyCode: 'd', modifiers: ['CTRL', 'SHIFT'] => 'd<>CTRL<>SHIFT'
219 * keycode: 'alt', modifiers: ['ALT', 'SHIFT'] => 'ALT<>SHIFT' 249 * keyCode: 'alt', modifiers: ['ALT', 'SHIFT'] => 'ALT<>SHIFT'
250 *
251 * @param {string} keyCode Key code.
252 * @param {Array} modifiers Key Modifier list.
253 * @return {string} Normalized key shortcut data string.
220 */ 254 */
221 function getAction(keycode, modifiers) { 255 function getAction(keyCode, modifiers) {
222 const SEPARATOR = '<>'; 256 /** @const */ var separatorStr = '<>';
223 if (keycode.toUpperCase() in MODIFIER_TO_CLASS) { 257 if (keyCode.toUpperCase() in MODIFIER_TO_CLASS) {
224 keycode = keycode.toUpperCase(); 258 keyCode = keyCode.toUpperCase();
225 if (keycode in modifiers) { 259 if (keyCode in modifiers) {
226 return modifiers.join(SEPARATOR); 260 return modifiers.join(separatorStr);
227 } else { 261 } else {
228 var action = [keycode].concat(modifiers) 262 var action = [keyCode].concat(modifiers);
229 action.sort(); 263 action.sort();
230 return action.join(SEPARATOR); 264 return action.join(separatorStr);
231 } 265 }
232 } 266 }
233 return [keycode].concat(modifiers).join(SEPARATOR); 267 return [keyCode].concat(modifiers).join(separatorStr);
234 } 268 }
235 269
236 /** 270 /**
237 * Returns a text which displayed on a key. 271 * Returns a text which displayed on a key.
272 * @param {string} keyData Key glyph data.
273 * @return {string} Key text value.
238 */ 274 */
239 function getKeyTextValue(keyData) { 275 function getKeyTextValue(keyData) {
240 if (keyData.label) { 276 if (keyData.label) {
241 // Do not show text on the space key. 277 // Do not show text on the space key.
242 if (keyData.label == 'space') { 278 if (keyData.label == 'space') {
243 return ''; 279 return '';
244 } 280 }
245 return keyData.label; 281 return keyData.label;
246 } 282 }
247 283
248 var chars = []; 284 var chars = [];
249 for (var j = 1; j <= 9; ++j) { 285 for (var j = 1; j <= 9; ++j) {
250 var pos = keyData['p' + j]; 286 var pos = keyData['p' + j];
251 if (pos && pos.length > 0) { 287 if (pos && pos.length > 0) {
252 chars.push(hex2char(pos)); 288 chars.push(hex2char(pos));
253 } 289 }
254 } 290 }
255 return chars.join(' '); 291 return chars.join(' ');
256 } 292 }
257 293
258 /** 294 /**
259 * Updates the whole keyboard. 295 * Updates the whole keyboard.
296 * @param {Array} modifiers Key Modifier list.
260 */ 297 */
261 function update(modifiers) { 298 function update(modifiers) {
262 var instructions = document.getElementById('instructions'); 299 var instructions = document.getElementById('instructions');
263 if (modifiers.length == 0) { 300 if (modifiers.length == 0) {
264 instructions.style.visibility = 'visible'; 301 instructions.style.visibility = 'visible';
265 } else { 302 } else {
266 instructions.style.visibility = 'hidden'; 303 instructions.style.visibility = 'hidden';
267 } 304 }
268 305
269 var keyboardGlyphData = getKeyboardGlyphData(); 306 var keyboardGlyphData = getKeyboardGlyphData();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 if (format == 'left' || format == 'right') { 355 if (format == 'left' || format == 'right') {
319 shortcutText.style.textAlign = format; 356 shortcutText.style.textAlign = format;
320 keyText.style.textAlign = format; 357 keyText.style.textAlign = format;
321 } 358 }
322 } 359 }
323 } 360 }
324 } 361 }
325 362
326 /** 363 /**
327 * A callback function for onkeydown and onkeyup events. 364 * A callback function for onkeydown and onkeyup events.
365 * @param {Event} e Key event.
328 */ 366 */
329 function handleKeyEvent(e){ 367 function handleKeyEvent(e) {
330 var modifiers = getModifiers(e); 368 var modifiers = getModifiers(e);
331 if (!getKeyboardOverlayId()) { 369 if (!getKeyboardOverlayId()) {
332 return; 370 return;
333 } 371 }
334 update(modifiers); 372 update(modifiers);
335 KeyboardOverlayAccessibilityHelper.maybeSpeakAllShortcuts(modifiers); 373 KeyboardOverlayAccessibilityHelper.maybeSpeakAllShortcuts(modifiers);
336 } 374 }
337 375
338 /** 376 /**
339 * Initializes the layout of the keys. 377 * Initializes the layout of the keys.
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 function init() { 475 function init() {
438 document.addEventListener('keydown', handleKeyEvent); 476 document.addEventListener('keydown', handleKeyEvent);
439 document.addEventListener('keyup', handleKeyEvent); 477 document.addEventListener('keyup', handleKeyEvent);
440 chrome.send('getLabelMap'); 478 chrome.send('getLabelMap');
441 } 479 }
442 480
443 /** 481 /**
444 * Initializes the global map for remapping identifiers of modifier keys based 482 * Initializes the global map for remapping identifiers of modifier keys based
445 * on the preference. 483 * on the preference.
446 * Called after sending the 'getLabelMap' message. 484 * Called after sending the 'getLabelMap' message.
485 * @param {Object} remap Identifier map.
447 */ 486 */
448 function initIdentifierMap(remap) { 487 function initIdentifierMap(remap) {
449 for (var key in remap) { 488 for (var key in remap) {
450 var val = remap[key]; 489 var val = remap[key];
451 if ((key in LABEL_TO_IDENTIFIER) && 490 if ((key in LABEL_TO_IDENTIFIER) &&
452 (val in LABEL_TO_IDENTIFIER)) { 491 (val in LABEL_TO_IDENTIFIER)) {
453 identifierMap[LABEL_TO_IDENTIFIER[key]] = 492 identifierMap[LABEL_TO_IDENTIFIER[key]] =
454 LABEL_TO_IDENTIFIER[val]; 493 LABEL_TO_IDENTIFIER[val];
455 } else { 494 } else {
456 console.error('Invalid label map element: ' + key + ', ' + val); 495 console.error('Invalid label map element: ' + key + ', ' + val);
457 } 496 }
458 } 497 }
459 chrome.send('getInputMethodId'); 498 chrome.send('getInputMethodId');
460 } 499 }
461 500
462 /** 501 /**
463 * Initializes the global keyboad overlay ID and the layout of keys. 502 * Initializes the global keyboad overlay ID and the layout of keys.
464 * Called after sending the 'getInputMethodId' message. 503 * Called after sending the 'getInputMethodId' message.
504 * @param {inputMethodId} inputMethodId Input Method Identifier.
465 */ 505 */
466 function initKeyboardOverlayId(inputMethodId) { 506 function initKeyboardOverlayId(inputMethodId) {
467 // Libcros returns an empty string when it cannot find the keyboard overlay ID 507 // Libcros returns an empty string when it cannot find the keyboard overlay ID
468 // corresponding to the current input method. 508 // corresponding to the current input method.
469 // In such a case, fallback to the default ID (en_US). 509 // In such a case, fallback to the default ID (en_US).
470 var inputMethodIdToOverlayId = keyboardOverlayData['inputMethodIdToOverlayId'] 510 var inputMethodIdToOverlayId =
511 keyboardOverlayData['inputMethodIdToOverlayId'];
471 if (inputMethodId) { 512 if (inputMethodId) {
472 keyboardOverlayId = inputMethodIdToOverlayId[inputMethodId]; 513 keyboardOverlayId = inputMethodIdToOverlayId[inputMethodId];
473 } 514 }
474 if (!keyboardOverlayId) { 515 if (!keyboardOverlayId) {
475 console.error('No keyboard overlay ID for ' + inputMethodId); 516 console.error('No keyboard overlay ID for ' + inputMethodId);
476 keyboardOverlayId = 'en_US'; 517 keyboardOverlayId = 'en_US';
477 } 518 }
478 while(document.body.firstChild) { 519 while (document.body.firstChild) {
479 document.body.removeChild(document.body.firstChild); 520 document.body.removeChild(document.body.firstChild);
480 } 521 }
481 initLayout(); 522 initLayout();
482 update([]); 523 update([]);
483 } 524 }
484 525
485 /** 526 /**
486 * Handles click events of the learn more link. 527 * Handles click events of the learn more link.
528 * @param {Event} e Mouse click event.
487 */ 529 */
488 function learnMoreClicked(e) { 530 function learnMoreClicked(e) {
489 chrome.send('openLearnMorePage'); 531 chrome.send('openLearnMorePage');
490 chrome.send('DialogClose'); 532 chrome.send('DialogClose');
491 e.preventDefault(); 533 e.preventDefault();
492 } 534 }
493 535
494 document.addEventListener('DOMContentLoaded', init); 536 document.addEventListener('DOMContentLoaded', init);
OLDNEW
« no previous file with comments | « chrome/browser/resources/chromeos/image_burner.js ('k') | chrome/browser/resources/chromeos/mobile_setup.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698