OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 Polymer({ | 5 Polymer({ |
6 is: 'bookmarks-command-manager', | 6 is: 'bookmarks-command-manager', |
7 | 7 |
8 behaviors: [ | 8 behaviors: [ |
9 bookmarks.StoreClient, | 9 bookmarks.StoreClient, |
10 ], | 10 ], |
11 | 11 |
12 properties: { | 12 properties: { |
13 /** @type {Set<string>} */ | 13 /** @type {Set<string>} */ |
14 menuIds_: Object, | 14 menuIds_: Object, |
15 }, | 15 }, |
16 | 16 |
17 attached: function() { | 17 attached: function() { |
18 /** @private {function(!Event)} */ | 18 /** @private {function(!Event)} */ |
19 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); | 19 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); |
20 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); | 20 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
| 21 |
| 22 /** @private {function(!Event)} */ |
| 23 this.boundOnKeydown_ = this.onKeydown_.bind(this); |
| 24 document.addEventListener('keydown', this.boundOnKeydown_); |
| 25 |
| 26 /** @private {Object<Command, string>} */ |
| 27 this.shortcuts_ = {}; |
| 28 this.shortcuts_[Command.EDIT] = cr.isMac ? 'enter' : 'f2'; |
| 29 this.shortcuts_[Command.COPY] = cr.isMac ? 'meta+c' : 'ctrl+c'; |
| 30 this.shortcuts_[Command.DELETE] = cr.isMac ? 'delete backspace' : 'delete'; |
21 }, | 31 }, |
22 | 32 |
23 detached: function() { | 33 detached: function() { |
24 document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); | 34 document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
| 35 document.removeEventListener('keydown', this.boundOnKeydown_); |
25 }, | 36 }, |
26 | 37 |
27 /** | 38 /** |
28 * Display the command context menu at (|x|, |y|) in window co-ordinates. | 39 * Display the command context menu at (|x|, |y|) in window co-ordinates. |
29 * Commands will execute on the currently selected items. | 40 * Commands will execute on the currently selected items. |
30 * @param {number} x | 41 * @param {number} x |
31 * @param {number} y | 42 * @param {number} y |
32 */ | 43 */ |
33 openCommandMenuAtPosition: function(x, y) { | 44 openCommandMenuAtPosition: function(x, y) { |
34 this.menuIds_ = this.getState().selection.items; | 45 this.menuIds_ = this.getState().selection.items; |
(...skipping 26 matching lines...) Expand all Loading... |
61 canExecute: function(command, itemIds) { | 72 canExecute: function(command, itemIds) { |
62 switch (command) { | 73 switch (command) { |
63 case Command.EDIT: | 74 case Command.EDIT: |
64 return itemIds.size == 1; | 75 return itemIds.size == 1; |
65 case Command.COPY: | 76 case Command.COPY: |
66 return itemIds.size == 1 && | 77 return itemIds.size == 1 && |
67 this.containsMatchingNode_(itemIds, function(node) { | 78 this.containsMatchingNode_(itemIds, function(node) { |
68 return !!node.url; | 79 return !!node.url; |
69 }); | 80 }); |
70 case Command.DELETE: | 81 case Command.DELETE: |
71 return true; | 82 return itemIds.size > 0; |
72 default: | 83 default: |
73 return false; | 84 return false; |
74 } | 85 } |
75 }, | 86 }, |
76 | 87 |
77 /** | 88 /** |
78 * @param {Command} command | 89 * @param {Command} command |
79 * @param {!Set<string>} itemIds | 90 * @param {!Set<string>} itemIds |
80 */ | 91 */ |
81 handle: function(command, itemIds) { | 92 handle: function(command, itemIds) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 /** | 146 /** |
136 * @param {Event} e | 147 * @param {Event} e |
137 * @private | 148 * @private |
138 */ | 149 */ |
139 onCommandClick_: function(e) { | 150 onCommandClick_: function(e) { |
140 this.closeCommandMenu(); | 151 this.closeCommandMenu(); |
141 this.handle(e.target.getAttribute('command'), assert(this.menuIds_)); | 152 this.handle(e.target.getAttribute('command'), assert(this.menuIds_)); |
142 }, | 153 }, |
143 | 154 |
144 /** | 155 /** |
| 156 * @param {!Event} e |
| 157 * @private |
| 158 */ |
| 159 onKeydown_: function(e) { |
| 160 var selection = this.getState().selection.items; |
| 161 // TODO(tsergeant): Prevent keyboard shortcuts when a dialog is open or text |
| 162 // field is focused. |
| 163 for (var commandName in this.shortcuts_) { |
| 164 var shortcut = this.shortcuts_[commandName]; |
| 165 if (Polymer.IronA11yKeysBehavior.keyboardEventMatchesKeys(e, shortcut) && |
| 166 this.canExecute(commandName, selection)) { |
| 167 this.handle(commandName, selection); |
| 168 |
| 169 e.stopPropagation(); |
| 170 e.preventDefault(); |
| 171 return; |
| 172 } |
| 173 } |
| 174 }, |
| 175 |
| 176 /** |
145 * Close the menu on mousedown so clicks can propagate to the underlying UI. | 177 * Close the menu on mousedown so clicks can propagate to the underlying UI. |
146 * This allows the user to right click the list while a context menu is | 178 * This allows the user to right click the list while a context menu is |
147 * showing and get another context menu. | 179 * showing and get another context menu. |
148 * @param {Event} e | 180 * @param {Event} e |
149 * @private | 181 * @private |
150 */ | 182 */ |
151 onMenuMousedown_: function(e) { | 183 onMenuMousedown_: function(e) { |
152 if (e.path[0] != this.$.dropdown) | 184 if (e.path[0] != this.$.dropdown) |
153 return; | 185 return; |
154 | 186 |
155 this.$.dropdown.close(); | 187 this.$.dropdown.close(); |
156 }, | 188 }, |
157 | 189 |
158 /** @private */ | 190 /** @private */ |
159 getEditActionLabel_: function() { | 191 getEditActionLabel_: function() { |
160 if (this.menuIds_.size > 1) | 192 if (this.menuIds_.size > 1) |
161 return; | 193 return; |
162 | 194 |
163 var id = Array.from(this.menuIds_)[0]; | 195 var id = Array.from(this.menuIds_)[0]; |
164 var itemUrl = this.getState().nodes[id].url; | 196 var itemUrl = this.getState().nodes[id].url; |
165 var label = itemUrl ? 'menuEdit' : 'menuRename'; | 197 var label = itemUrl ? 'menuEdit' : 'menuRename'; |
166 return loadTimeData.getString(label); | 198 return loadTimeData.getString(label); |
167 }, | 199 }, |
168 }); | 200 }); |
OLD | NEW |