| OLD | NEW |
| 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 /** | 5 /** |
| 6 * @fileoverview A command is an abstraction of an action a user can do in the | 6 * @fileoverview A command is an abstraction of an action a user can do in the |
| 7 * UI. | 7 * UI. |
| 8 * | 8 * |
| 9 * When the focus changes in the document for each command a canExecute event | 9 * When the focus changes in the document for each command a canExecute event |
| 10 * is dispatched on the active element. By listening to this event you can | 10 * is dispatched on the active element. By listening to this event you can |
| 11 * enable and disable the command by setting the event.canExecute property. | 11 * enable and disable the command by setting the event.canExecute property. |
| 12 * | 12 * |
| 13 * When a command is executed a command event is dispatched on the active | 13 * When a command is executed a command event is dispatched on the active |
| 14 * element. Note that you should stop the propagation after you have handled the | 14 * element. Note that you should stop the propagation after you have handled the |
| 15 * command if there might be other command listeners higher up in the DOM tree. | 15 * command if there might be other command listeners higher up in the DOM tree. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 cr.define('cr.ui', function() { | 18 cr.define('cr.ui', function() { |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * This is used to identify keyboard shortcuts. | 21 * This is used to identify keyboard shortcuts. |
| 22 * @param {string} shortcut The text used to describe the keys for this | 22 * @param {string} shortcut The text used to describe the keys for this |
| 23 * keyboard shortcut. | 23 * keyboard shortcut. |
| 24 * @constructor | 24 * @constructor |
| 25 */ | 25 */ |
| 26 function KeyboardShortcut(shortcut) { | 26 function KeyboardShortcut(shortcut) { |
| 27 var mods = {}; | 27 var mods = {}; |
| 28 var ident = ''; | 28 var ident = ''; |
| 29 shortcut.split('-').forEach(function(part) { | 29 shortcut.split('|').forEach(function(part) { |
| 30 var partLc = part.toLowerCase(); | 30 var partLc = part.toLowerCase(); |
| 31 switch (partLc) { | 31 switch (partLc) { |
| 32 case 'alt': | 32 case 'alt': |
| 33 case 'ctrl': | 33 case 'ctrl': |
| 34 case 'meta': | 34 case 'meta': |
| 35 case 'shift': | 35 case 'shift': |
| 36 mods[partLc + 'Key'] = true; | 36 mods[partLc + 'Key'] = true; |
| 37 break; | 37 break; |
| 38 default: | 38 default: |
| 39 if (ident) | 39 if (ident) |
| 40 throw Error('Invalid shortcut'); | 40 throw Error('Invalid shortcut'); |
| 41 ident = part; | 41 ident = part; |
| 42 } | 42 } |
| 43 }); | 43 }); |
| 44 | 44 |
| 45 this.ident_ = ident; | 45 this.ident_ = ident; |
| 46 this.mods_ = mods; | 46 this.mods_ = mods; |
| 47 } | 47 } |
| 48 | 48 |
| 49 KeyboardShortcut.prototype = { | 49 KeyboardShortcut.prototype = { |
| 50 /** | 50 /** |
| 51 * Whether the keyboard shortcut object matches a keyboard event. | 51 * Whether the keyboard shortcut object matches a keyboard event. |
| 52 * @param {!Event} e The keyboard event object. | 52 * @param {!Event} e The keyboard event object. |
| 53 * @return {boolean} Whether we found a match or not. | 53 * @return {boolean} Whether we found a match or not. |
| 54 */ | 54 */ |
| 55 matchesEvent: function(e) { | 55 matchesEvent: function(e) { |
| 56 if (e.keyIdentifier == this.ident_) { | 56 if (e.key == this.ident_) { |
| 57 // All keyboard modifiers needs to match. | 57 // All keyboard modifiers needs to match. |
| 58 var mods = this.mods_; | 58 var mods = this.mods_; |
| 59 return ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].every(function(k) { | 59 return ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].every(function(k) { |
| 60 return e[k] == !!mods[k]; | 60 return e[k] == !!mods[k]; |
| 61 }); | 61 }); |
| 62 } | 62 } |
| 63 return false; | 63 return false; |
| 64 } | 64 } |
| 65 }; | 65 }; |
| 66 | 66 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 * command can be executed or not. | 107 * command can be executed or not. |
| 108 * @param {Node=} opt_node Node for which to actuate command state. | 108 * @param {Node=} opt_node Node for which to actuate command state. |
| 109 */ | 109 */ |
| 110 canExecuteChange: function(opt_node) { | 110 canExecuteChange: function(opt_node) { |
| 111 dispatchCanExecuteEvent(this, | 111 dispatchCanExecuteEvent(this, |
| 112 opt_node || this.ownerDocument.activeElement); | 112 opt_node || this.ownerDocument.activeElement); |
| 113 }, | 113 }, |
| 114 | 114 |
| 115 /** | 115 /** |
| 116 * The keyboard shortcut that triggers the command. This is a string | 116 * The keyboard shortcut that triggers the command. This is a string |
| 117 * consisting of a keyIdentifier (as reported by WebKit in keydown) as | 117 * consisting of a key (as reported by WebKit in keydown) as |
| 118 * well as optional key modifiers joinded with a '-'. | 118 * well as optional key modifiers joinded with a '|'. |
| 119 * | 119 * |
| 120 * Multiple keyboard shortcuts can be provided by separating them by | 120 * Multiple keyboard shortcuts can be provided by separating them by |
| 121 * whitespace. | 121 * whitespace. |
| 122 * | 122 * |
| 123 * For example: | 123 * For example: |
| 124 * "F1" | 124 * "F1" |
| 125 * "U+0008-Meta" for Apple command backspace. | 125 * "Backspace|Meta" for Apple command backspace. |
| 126 * "U+0041-Ctrl" for Control A | 126 * "a|Ctrl" for Control A |
| 127 * "U+007F U+0008-Meta" for Delete and Command Backspace | 127 * "Delete Backspace|Meta" for Delete and Command Backspace |
| 128 * | 128 * |
| 129 * @type {string} | 129 * @type {string} |
| 130 */ | 130 */ |
| 131 shortcut_: '', | 131 shortcut_: '', |
| 132 get shortcut() { | 132 get shortcut() { |
| 133 return this.shortcut_; | 133 return this.shortcut_; |
| 134 }, | 134 }, |
| 135 set shortcut(shortcut) { | 135 set shortcut(shortcut) { |
| 136 var oldShortcut = this.shortcut_; | 136 var oldShortcut = this.shortcut_; |
| 137 if (shortcut !== oldShortcut) { | 137 if (shortcut !== oldShortcut) { |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 this.preventDefault(); | 321 this.preventDefault(); |
| 322 } | 322 } |
| 323 }; | 323 }; |
| 324 | 324 |
| 325 // Export | 325 // Export |
| 326 return { | 326 return { |
| 327 Command: Command, | 327 Command: Command, |
| 328 CanExecuteEvent: CanExecuteEvent | 328 CanExecuteEvent: CanExecuteEvent |
| 329 }; | 329 }; |
| 330 }); | 330 }); |
| OLD | NEW |