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 Polymer({ | |
6 is: 'bookmarks-command-manager', | |
7 | |
8 behaviors: [ | |
9 bookmarks.StoreClient, | |
10 ], | |
11 | |
12 properties: { | |
13 /** @type {Set<string>} */ | |
14 menuIds_: Object, | |
15 }, | |
16 | |
17 attached: function() { | |
18 /** @private {function(!Event)} */ | |
19 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); | |
20 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); | |
21 | |
22 // TODO(tsergeant): Expand this to include keyboard shortcuts. | |
23 this.commands_ = { | |
24 'edit': this.handleEdit.bind(this), | |
25 'copy': this.handleCopy.bind(this), | |
26 'delete': this.handleDelete.bind(this), | |
27 }; | |
28 }, | |
29 | |
30 detached: function() { | |
31 document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); | |
32 }, | |
33 | |
34 /** | |
35 * Display the command context menu at the given |x|, |y| window | |
calamity
2017/04/24 05:20:35
nit: at (|x|, |y|) in window co-ordinates.
tsergeant
2017/04/24 07:28:23
Done.
| |
36 * co-ordinates. Commands will execute on the currently selected items. | |
37 * @param {number} x | |
38 * @param {number} y | |
39 */ | |
40 openCommandMenuAtPosition: function(x, y) { | |
41 this.menuIds_ = this.getState().selection.items; | |
42 /** @type {!CrActionMenuElement} */ (this.$.dropdown) | |
43 .showAtPosition({top: y, left: x}); | |
44 }, | |
45 | |
46 /** | |
47 * Display the command context menu positioned to cover the |target| | |
48 * element. Commands will execute on the currently selected items. | |
49 * @param {!Element} target | |
50 */ | |
51 openCommandMenuAtElement: function(target) { | |
52 this.menuIds_ = this.getState().selection.items; | |
53 /** @type {!CrActionMenuElement} */ (this.$.dropdown).showAt(target); | |
54 }, | |
55 | |
56 closeCommandMenu: function() { | |
57 /** @type {!CrActionMenuElement} */ (this.$.dropdown).close(); | |
58 }, | |
59 | |
60 //////////////////////////////////////////////////////////////////////////// | |
61 // Command handlers: | |
62 | |
63 /** | |
64 * @param {!Set<string>} itemIds | |
65 * @return {boolean} | |
66 */ | |
67 canEdit: function(itemIds) { | |
68 return itemIds.size == 1; | |
69 }, | |
70 | |
71 /** @param {!Set<string>} itemIds */ | |
72 handleEdit: function(itemIds) { | |
73 var id = Array.from(itemIds)[0]; | |
74 /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) | |
75 .showEditDialog(this.getState().nodes[id]); | |
76 }, | |
77 | |
78 /** | |
79 * @param {!Set<string>} itemIds | |
80 * @return {boolean} | |
81 */ | |
82 canCopy: function(itemIds) { | |
83 return itemIds.size == 1 && | |
84 this.containsMatchingNode_(itemIds, function(node) { | |
85 return !!node.url; | |
86 }); | |
87 }, | |
88 | |
89 /** @param {!Set<string>} itemIds */ | |
90 handleCopy: function(itemIds) { | |
91 var idList = Array.from(itemIds); | |
92 chrome.bookmarkManagerPrivate.copy(idList, function() { | |
93 // TODO(jiaxi): Add toast later. | |
94 }); | |
95 }, | |
96 | |
97 /** | |
98 * @param {!Set<string>} itemIds | |
99 * @return {boolean} | |
100 */ | |
101 canDelete: function(itemIds) { | |
102 return true; | |
103 }, | |
104 | |
105 /** @param {!Set<string>} itemIds */ | |
106 handleDelete: function(itemIds) { | |
107 // TODO(tsergeant): Filter IDs so we don't try to delete children of | |
108 // something else already being deleted. | |
109 chrome.bookmarkManagerPrivate.removeTrees(Array.from(itemIds), function() { | |
110 // TODO(jiaxi): Add toast later. | |
111 }); | |
112 }, | |
113 | |
114 //////////////////////////////////////////////////////////////////////////// | |
115 // Private functions: | |
116 | |
117 /** | |
118 * @param {!Set<string>} itemIds | |
119 * @param {function(BookmarkNode):boolean} predicate | |
120 * @return {boolean} True if any node in |itemIds| returns true for | |
121 * |predicate|. | |
122 */ | |
123 containsMatchingNode_: function(itemIds, predicate) { | |
124 var matches = false; | |
125 var nodes = this.getState().nodes; | |
126 itemIds.forEach(function(id) { | |
127 if (predicate(nodes[id])) | |
128 matches = true; | |
129 }); | |
calamity
2017/04/24 05:20:35
There's a lot of depressing options here.
How do
tsergeant
2017/04/24 07:28:23
Done. Set is a really barebones type -- I wish it
calamity
2017/04/26 03:43:44
Yeah =(
| |
130 return matches; | |
131 }, | |
132 | |
133 /** | |
134 * @param {Event} e | |
135 * @private | |
136 */ | |
137 onOpenItemMenu_: function(e) { | |
138 if (e.detail.targetElement) { | |
139 this.openCommandMenuAtElement(e.detail.targetElement); | |
140 } else { | |
141 this.openCommandMenuAtPosition(e.detail.x, e.detail.y); | |
142 } | |
143 }, | |
144 | |
145 /** | |
146 * @param {Event} e | |
147 * @private | |
148 */ | |
149 onCommandClick_: function(e) { | |
150 this.closeCommandMenu(); | |
151 var command = this.commands_[e.target.getAttribute('command')]; | |
152 command(this.menuIds_); | |
153 }, | |
154 | |
155 /** | |
156 * Close the menu on mousedown so clicks can propagate to the underlying UI. | |
157 * This allows the user to right click the list while a context menu is | |
158 * showing and get another context menu. | |
159 * @param {Event} e | |
160 * @private | |
161 */ | |
162 onMenuMousedown_: function(e) { | |
163 if (e.path[0] != this.$.dropdown) | |
164 return; | |
165 | |
166 this.$.dropdown.close(); | |
167 }, | |
168 | |
169 /** @private */ | |
170 getEditActionLabel_: function() { | |
171 if (this.menuIds_.size > 1) | |
172 return; | |
173 | |
174 var id = Array.from(this.menuIds_)[0]; | |
175 var itemUrl = this.getState().nodes[id].url; | |
176 var label = itemUrl ? 'menuEdit' : 'menuRename'; | |
177 return loadTimeData.getString(label); | |
178 }, | |
179 }); | |
OLD | NEW |