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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
67 /** | 67 /** |
| 68 * A list of keyboard shortcuts which all perform one command. |
| 69 * @param {string} shortcuts Text-based representation of one or more keyboard |
| 70 * shortcuts, separated by spaces. |
| 71 * @constructor |
| 72 */ |
| 73 function KeyboardShortcutList(shortcuts) { |
| 74 this.shortcuts_ = shortcuts.split(/\s+/).map(function(shortcut) { |
| 75 return new KeyboardShortcut(shortcut); |
| 76 }); |
| 77 } |
| 78 |
| 79 KeyboardShortcutList.prototype = { |
| 80 /** |
| 81 * Returns true if any of the keyboard shortcuts in the list matches a |
| 82 * keyboard event. |
| 83 * @param {!Event} e |
| 84 * @return {boolean} |
| 85 */ |
| 86 matchesEvent: function(e) { |
| 87 return this.shortcuts_.some(function(keyboardShortcut) { |
| 88 return keyboardShortcut.matchesEvent(e); |
| 89 }); |
| 90 }, |
| 91 }; |
| 92 |
| 93 /** |
68 * Creates a new command element. | 94 * Creates a new command element. |
69 * @constructor | 95 * @constructor |
70 * @extends {HTMLElement} | 96 * @extends {HTMLElement} |
71 */ | 97 */ |
72 var Command = cr.ui.define('command'); | 98 var Command = cr.ui.define('command'); |
73 | 99 |
74 Command.prototype = { | 100 Command.prototype = { |
75 __proto__: HTMLElement.prototype, | 101 __proto__: HTMLElement.prototype, |
76 | 102 |
77 /** | 103 /** |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 * | 154 * |
129 * @type {string} | 155 * @type {string} |
130 */ | 156 */ |
131 shortcut_: '', | 157 shortcut_: '', |
132 get shortcut() { | 158 get shortcut() { |
133 return this.shortcut_; | 159 return this.shortcut_; |
134 }, | 160 }, |
135 set shortcut(shortcut) { | 161 set shortcut(shortcut) { |
136 var oldShortcut = this.shortcut_; | 162 var oldShortcut = this.shortcut_; |
137 if (shortcut !== oldShortcut) { | 163 if (shortcut !== oldShortcut) { |
138 this.keyboardShortcuts_ = shortcut.split(/\s+/).map(function(shortcut) { | 164 this.keyboardShortcuts_ = new KeyboardShortcutList(shortcut); |
139 return new KeyboardShortcut(shortcut); | |
140 }); | |
141 | 165 |
142 // Set this after the keyboardShortcuts_ since that might throw. | 166 // Set this after the keyboardShortcuts_ since that might throw. |
143 this.shortcut_ = shortcut; | 167 this.shortcut_ = shortcut; |
144 cr.dispatchPropertyChange( | 168 cr.dispatchPropertyChange( |
145 this, 'shortcut', this.shortcut_, oldShortcut); | 169 this, 'shortcut', this.shortcut_, oldShortcut); |
146 } | 170 } |
147 }, | 171 }, |
148 | 172 |
149 /** | 173 /** |
150 * Whether the event object matches the shortcut for this command. | 174 * Whether the event object matches the shortcut for this command. |
151 * @param {!Event} e The key event object. | 175 * @param {!Event} e The key event object. |
152 * @return {boolean} Whether it matched or not. | 176 * @return {boolean} Whether it matched or not. |
153 */ | 177 */ |
154 matchesEvent: function(e) { | 178 matchesEvent: function(e) { |
155 if (!this.keyboardShortcuts_) | 179 if (!this.keyboardShortcuts_) |
156 return false; | 180 return false; |
157 | 181 return this.keyboardShortcuts_.matchesEvent(e); |
158 return this.keyboardShortcuts_.some(function(keyboardShortcut) { | |
159 return keyboardShortcut.matchesEvent(e); | |
160 }); | |
161 }, | 182 }, |
162 }; | 183 }; |
163 | 184 |
164 /** | 185 /** |
165 * The label of the command. | 186 * The label of the command. |
166 */ | 187 */ |
167 cr.defineProperty(Command, 'label', cr.PropertyKind.ATTR); | 188 cr.defineProperty(Command, 'label', cr.PropertyKind.ATTR); |
168 | 189 |
169 /** | 190 /** |
170 * Whether the command is disabled or not. | 191 * Whether the command is disabled or not. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 return this.canExecute_; | 337 return this.canExecute_; |
317 }, | 338 }, |
318 set canExecute(canExecute) { | 339 set canExecute(canExecute) { |
319 this.canExecute_ = !!canExecute; | 340 this.canExecute_ = !!canExecute; |
320 this.stopPropagation(); | 341 this.stopPropagation(); |
321 this.preventDefault(); | 342 this.preventDefault(); |
322 } | 343 } |
323 }; | 344 }; |
324 | 345 |
325 // Export | 346 // Export |
326 return {Command: Command, CanExecuteEvent: CanExecuteEvent}; | 347 return { |
| 348 Command: Command, |
| 349 CanExecuteEvent: CanExecuteEvent, |
| 350 KeyboardShortcutList: KeyboardShortcutList, |
| 351 }; |
327 }); | 352 }); |
OLD | NEW |