OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Class to run and get details about user commands. |
| 7 * |
| 8 * @constructor |
| 9 * @param {SwitchAccessInterface} switchAccess |
| 10 */ |
| 11 function Commands(switchAccess) { |
| 12 /** |
| 13 * SwitchAccess reference. |
| 14 * |
| 15 * @private {SwitchAccessInterface} |
| 16 */ |
| 17 this.switchAccess_ = switchAccess; |
| 18 |
| 19 /** |
| 20 * A map from command name to the default key code and function binding for |
| 21 * the command. |
| 22 * |
| 23 * @private {!Object} |
| 24 */ |
| 25 this.commandMap_ = this.buildCommandMap_(); |
| 26 } |
| 27 |
| 28 Commands.prototype = { |
| 29 |
| 30 /** |
| 31 * Return a list of the names of all user commands. |
| 32 * |
| 33 * @return {!Array<string>} |
| 34 */ |
| 35 getCommands: function() { |
| 36 return Object.keys(this.commandMap_); |
| 37 }, |
| 38 |
| 39 /** |
| 40 * Return the default key code for a command. |
| 41 * |
| 42 * @param {string} command |
| 43 * @return {number} |
| 44 */ |
| 45 getDefaultKeyCodeFor: function(command) { |
| 46 return this.commandMap_[command]['defaultKeyCode']; |
| 47 }, |
| 48 |
| 49 /** |
| 50 * Run the function binding for the specified command. |
| 51 * |
| 52 * @param {string} command |
| 53 */ |
| 54 runCommand: function(command) { |
| 55 this.commandMap_[command]['binding'](); |
| 56 }, |
| 57 |
| 58 /** |
| 59 * Build the object that maps from command name to the default key code and |
| 60 * function binding for the command. |
| 61 * |
| 62 * @return {!Object} |
| 63 */ |
| 64 buildCommandMap_: function() { |
| 65 return { |
| 66 'next': { |
| 67 'defaultKeyCode': 49, /* '1' key */ |
| 68 'binding': this.switchAccess_.moveToNode.bind(this.switchAccess_, true) |
| 69 }, |
| 70 'previous': { |
| 71 'defaultKeyCode': 50, /* '2' key */ |
| 72 'binding': this.switchAccess_.moveToNode.bind(this.switchAccess_, false) |
| 73 }, |
| 74 'select': { |
| 75 'defaultKeyCode': 51, /* '3' key */ |
| 76 'binding': this.switchAccess_.selectCurrentNode.bind(this.switchAccess_) |
| 77 }, |
| 78 'options': { |
| 79 'defaultKeyCode': 52, /* '4' key */ |
| 80 'binding': this.switchAccess_.showOptionsPage.bind(this.switchAccess_) |
| 81 }, |
| 82 'debugNext': { |
| 83 'defaultKeyCode': -1, /* unused key */ |
| 84 'binding': this.switchAccess_.debugMoveToNext.bind(this.switchAccess_) |
| 85 }, |
| 86 'debugPrevious': { |
| 87 'defaultKeyCode': -1, /* unused key */ |
| 88 'binding': |
| 89 this.switchAccess_.debugMoveToPrevious.bind(this.switchAccess_) |
| 90 }, |
| 91 'debugChild': { |
| 92 'defaultKeyCode': -1, /* unused key */ |
| 93 'binding': |
| 94 this.switchAccess_.debugMoveToFirstChild.bind(this.switchAccess_) |
| 95 }, |
| 96 'debugParent': { |
| 97 'defaultKeyCode': -1, /* unused key */ |
| 98 'binding': this.switchAccess_.debugMoveToParent.bind(this.switchAccess_) |
| 99 } |
| 100 }; |
| 101 } |
| 102 }; |
OLD | NEW |