| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * @fileoverview Provides a compatibility layer for ChromeVox Classic during the | 6 * @fileoverview Provides a compatibility layer for ChromeVox Classic during the |
| 7 * transition to ChromeVox Next. | 7 * transition to ChromeVox Next. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 goog.provide('ClassicCompatibility'); | 10 goog.provide('ClassicCompatibility'); |
| 11 | 11 |
| 12 goog.require('cvox.ExtensionBridge'); | |
| 13 goog.require('cvox.KeyMap'); | 12 goog.require('cvox.KeyMap'); |
| 14 goog.require('cvox.KeySequence'); | 13 goog.require('cvox.KeySequence'); |
| 15 goog.require('cvox.KeyUtil'); | 14 goog.require('cvox.KeyUtil'); |
| 16 goog.require('cvox.SimpleKeyEvent'); | 15 goog.require('cvox.SimpleKeyEvent'); |
| 17 | 16 |
| 18 /** | 17 /** |
| 18 * @param {boolean=} opt_active Whether compatibility is currently active. |
| 19 * @constructor | 19 * @constructor |
| 20 */ | 20 */ |
| 21 var ClassicCompatibility = function() { | 21 var ClassicCompatibility = function(opt_active) { |
| 22 /** @type {boolean} */ |
| 23 this.active = !!opt_active; |
| 24 |
| 22 /** | 25 /** |
| 23 * @type {!Array<{description: string, name: string, shortcut: string}>} | 26 * @type {!Array<{description: string, name: string, shortcut: string}>} |
| 24 * @private | 27 * @private |
| 25 */ | 28 */ |
| 26 this.commands_ = []; | 29 this.commands_ = []; |
| 27 | 30 |
| 28 /** @type {!cvox.KeyMap} */ | 31 /** @type {!cvox.KeyMap} */ |
| 29 this.keyMap = cvox.KeyMap.fromCurrentKeyMap(); | 32 this.keyMap = cvox.KeyMap.fromCurrentKeyMap(); |
| 30 | 33 |
| 31 chrome.commands.getAll(function(commands) { | 34 chrome.commands.getAll(function(commands) { |
| 32 this.commands_ = commands; | 35 this.commands_ = commands; |
| 33 }.bind(this)); | 36 }.bind(this)); |
| 34 }; | 37 }; |
| 35 | 38 |
| 36 ClassicCompatibility.prototype = { | 39 ClassicCompatibility.prototype = { |
| 37 /** | 40 /** |
| 38 * Processes a ChromeVox Next command. | 41 * Processes a ChromeVox Next command. |
| 39 * @param {string} command | 42 * @param {string} command |
| 40 * @return {boolean} Whether the command was successfully processed. | 43 * @return {boolean} Whether the command was successfully processed. |
| 41 */ | 44 */ |
| 42 onGotCommand: function(command) { | 45 onGotCommand: function(command) { |
| 43 var evt = this.buildKeyEvent_(command); | 46 if (!this.active) |
| 44 if (!evt) | |
| 45 return false; | 47 return false; |
| 46 this.simulateKeyDownNext_(evt); | |
| 47 return true; | |
| 48 }, | |
| 49 | 48 |
| 50 /** | |
| 51 * Processes a ChromeVox Next command while in CLASSIC mode. | |
| 52 * @param {string} command | |
| 53 * @return {boolean} Whether the command was successfully processed. | |
| 54 */ | |
| 55 onGotClassicCommand: function(command) { | |
| 56 var evt = this.buildKeyEvent_(command); | |
| 57 if (!evt) | |
| 58 return false; | |
| 59 this.simulateKeyDownClassic_(evt); | |
| 60 return true; | |
| 61 }, | |
| 62 | |
| 63 /** | |
| 64 * @param {string} command | |
| 65 * @return {cvox.SimpleKeyEvent?} | |
| 66 */ | |
| 67 buildKeyEvent_: function(command) { | |
| 68 var commandInfo = this.commands_.filter(function(c) { | 49 var commandInfo = this.commands_.filter(function(c) { |
| 69 return c.name == command; | 50 return c.name == command; |
| 70 }.bind(this))[0]; | 51 }.bind(this))[0]; |
| 71 if (!commandInfo) | 52 if (!commandInfo) |
| 72 return null; | 53 return false; |
| 73 var shortcut = commandInfo.shortcut; | 54 var shortcut = commandInfo.shortcut; |
| 74 return this.convertCommandShortcutToKeyEvent_(shortcut); | 55 var evt = this.convertCommandShortcutToKeyEvent_(shortcut); |
| 56 this.simulateKeyDown_(evt); |
| 57 return true; |
| 75 }, | 58 }, |
| 76 | 59 |
| 77 /** | 60 /** |
| 78 * @param {cvox.SimpleKeyEvent} evt | 61 * @param {cvox.SimpleKeyEvent} evt |
| 79 * @private | 62 * @private |
| 80 */ | 63 */ |
| 81 simulateKeyDownNext_: function(evt) { | 64 simulateKeyDown_: function(evt) { |
| 82 var keySequence = cvox.KeyUtil.keyEventToKeySequence(evt); | 65 var keySequence = cvox.KeyUtil.keyEventToKeySequence(evt); |
| 83 var classicCommand = this.keyMap.commandForKey(keySequence); | 66 var classicCommand = this.keyMap.commandForKey(keySequence); |
| 84 if (classicCommand) { | 67 if (classicCommand) { |
| 85 var nextCommand = this.getNextCommand_(classicCommand); | 68 var nextCommand = this.getNextCommand_(classicCommand); |
| 86 if (nextCommand) | 69 if (nextCommand) |
| 87 global.backgroundObj.onGotCommand(nextCommand, true); | 70 global.backgroundObj.onGotCommand(nextCommand, true); |
| 88 } | 71 } |
| 89 }, | 72 }, |
| 90 | 73 |
| 91 /** | |
| 92 * @param {cvox.SimpleKeyEvent} evt | |
| 93 * @private | |
| 94 */ | |
| 95 simulateKeyDownClassic_: function(evt) { | |
| 96 var keySequence = cvox.KeyUtil.keyEventToKeySequence(evt); | |
| 97 var classicCommand = this.keyMap.commandForKey(keySequence); | |
| 98 if (classicCommand) { | |
| 99 cvox.ExtensionBridge.send({ | |
| 100 'message': 'USER_COMMAND', | |
| 101 'command': classicCommand | |
| 102 }); | |
| 103 } | |
| 104 }, | |
| 105 | |
| 106 /** | 74 /** |
| 107 * @param {string} shortcut | 75 * @param {string} shortcut |
| 108 * @return {cvox.SimpleKeyEvent} | 76 * @return {cvox.SimpleKeyEvent} |
| 109 * @private | 77 * @private |
| 110 */ | 78 */ |
| 111 convertCommandShortcutToKeyEvent_: function(shortcut) { | 79 convertCommandShortcutToKeyEvent_: function(shortcut) { |
| 112 var evt = {}; | 80 var evt = {}; |
| 113 shortcut.split('+').forEach(function(token) { | 81 shortcut.split('+').forEach(function(token) { |
| 114 // Known tokens. | 82 // Known tokens. |
| 115 switch (token) { | 83 switch (token) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 return 'previousLine'; | 134 return 'previousLine'; |
| 167 case 'forceClickOnCurrentItem': | 135 case 'forceClickOnCurrentItem': |
| 168 return 'doDefault'; | 136 return 'doDefault'; |
| 169 case 'readFromHere': | 137 case 'readFromHere': |
| 170 return 'continuousRead'; | 138 return 'continuousRead'; |
| 171 default: | 139 default: |
| 172 return classicCommand; | 140 return classicCommand; |
| 173 } | 141 } |
| 174 } | 142 } |
| 175 }; | 143 }; |
| OLD | NEW |