| 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 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 (opt_element || doc.activeElement).dispatchEvent(e); | 101 (opt_element || doc.activeElement).dispatchEvent(e); |
| 102 } | 102 } |
| 103 }, | 103 }, |
| 104 | 104 |
| 105 /** | 105 /** |
| 106 * Call this when there have been changes that might change whether the | 106 * Call this when there have been changes that might change whether the |
| 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( |
| 112 opt_node || this.ownerDocument.activeElement); | 112 this, 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 key (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 * "Backspace|Meta" for Apple command backspace. | 125 * "Backspace|Meta" for Apple command backspace. |
| 126 * "a|Ctrl" for Control A | 126 * "a|Ctrl" for Control A |
| 127 * "Delete Backspace|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() { return this.shortcut_; }, |
| 133 return this.shortcut_; | |
| 134 }, | |
| 135 set shortcut(shortcut) { | 133 set shortcut(shortcut) { |
| 136 var oldShortcut = this.shortcut_; | 134 var oldShortcut = this.shortcut_; |
| 137 if (shortcut !== oldShortcut) { | 135 if (shortcut !== oldShortcut) { |
| 138 this.keyboardShortcuts_ = shortcut.split(/\s+/).map(function(shortcut) { | 136 this.keyboardShortcuts_ = shortcut.split(/\s+/).map(function(shortcut) { |
| 139 return new KeyboardShortcut(shortcut); | 137 return new KeyboardShortcut(shortcut); |
| 140 }); | 138 }); |
| 141 | 139 |
| 142 // Set this after the keyboardShortcuts_ since that might throw. | 140 // Set this after the keyboardShortcuts_ since that might throw. |
| 143 this.shortcut_ = shortcut; | 141 this.shortcut_ = shortcut; |
| 144 cr.dispatchPropertyChange(this, 'shortcut', this.shortcut_, | 142 cr.dispatchPropertyChange( |
| 145 oldShortcut); | 143 this, 'shortcut', this.shortcut_, oldShortcut); |
| 146 } | 144 } |
| 147 }, | 145 }, |
| 148 | 146 |
| 149 /** | 147 /** |
| 150 * Whether the event object matches the shortcut for this command. | 148 * Whether the event object matches the shortcut for this command. |
| 151 * @param {!Event} e The key event object. | 149 * @param {!Event} e The key event object. |
| 152 * @return {boolean} Whether it matched or not. | 150 * @return {boolean} Whether it matched or not. |
| 153 */ | 151 */ |
| 154 matchesEvent: function(e) { | 152 matchesEvent: function(e) { |
| 155 if (!this.keyboardShortcuts_) | 153 if (!this.keyboardShortcuts_) |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 */ | 303 */ |
| 306 command: null, | 304 command: null, |
| 307 | 305 |
| 308 /** | 306 /** |
| 309 * Whether the target can execute the command. Setting this also stops the | 307 * Whether the target can execute the command. Setting this also stops the |
| 310 * propagation and prevents the default. Callers can tell if an event has | 308 * propagation and prevents the default. Callers can tell if an event has |
| 311 * been handled via |this.defaultPrevented|. | 309 * been handled via |this.defaultPrevented|. |
| 312 * @type {boolean} | 310 * @type {boolean} |
| 313 */ | 311 */ |
| 314 canExecute_: false, | 312 canExecute_: false, |
| 315 get canExecute() { | 313 get canExecute() { return this.canExecute_; }, |
| 316 return this.canExecute_; | |
| 317 }, | |
| 318 set canExecute(canExecute) { | 314 set canExecute(canExecute) { |
| 319 this.canExecute_ = !!canExecute; | 315 this.canExecute_ = !!canExecute; |
| 320 this.stopPropagation(); | 316 this.stopPropagation(); |
| 321 this.preventDefault(); | 317 this.preventDefault(); |
| 322 } | 318 } |
| 323 }; | 319 }; |
| 324 | 320 |
| 325 // Export | 321 // Export |
| 326 return { | 322 return {Command: Command, CanExecuteEvent: CanExecuteEvent}; |
| 327 Command: Command, | |
| 328 CanExecuteEvent: CanExecuteEvent | |
| 329 }; | |
| 330 }); | 323 }); |
| OLD | NEW |