OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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-list', | 6 is: 'bookmarks-list', |
7 | 7 |
8 behaviors: [ | 8 behaviors: [ |
9 bookmarks.StoreClient, | 9 bookmarks.StoreClient, |
10 ], | 10 ], |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 return bookmarks.util.getDisplayedList(state); | 57 return bookmarks.util.getDisplayedList(state); |
58 }); | 58 }); |
59 this.watch('searchTerm_', function(state) { | 59 this.watch('searchTerm_', function(state) { |
60 return state.search.term; | 60 return state.search.term; |
61 }); | 61 }); |
62 this.watch('selectedFolder_', function(state) { | 62 this.watch('selectedFolder_', function(state) { |
63 return state.selectedFolder; | 63 return state.selectedFolder; |
64 }); | 64 }); |
65 this.updateFromStore(); | 65 this.updateFromStore(); |
66 | 66 |
67 this.boundOnItemKeydown_ = this.onItemKeydown_.bind(this); | |
68 this.boundOnHighlightItems_ = this.onHighlightItems_.bind(this); | |
69 | |
67 this.$.bookmarksCard.addEventListener( | 70 this.$.bookmarksCard.addEventListener( |
68 'keydown', this.onItemKeydown_.bind(this), true); | 71 'keydown', this.boundOnItemKeydown_, true); |
72 document.addEventListener('highlight-items', this.boundOnHighlightItems_); | |
73 }, | |
74 | |
75 detached: function() { | |
76 this.$.bookmarksCard.removeEventListener( | |
77 'keydown', this.boundOnItemKeydown_, true); | |
78 document.removeEventListener( | |
79 'highlight-items', this.boundOnHighlightItems_); | |
69 }, | 80 }, |
70 | 81 |
71 /** @return {HTMLElement} */ | 82 /** @return {HTMLElement} */ |
72 getDropTarget: function() { | 83 getDropTarget: function() { |
73 return this.$.message; | 84 return this.$.message; |
74 }, | 85 }, |
75 | 86 |
76 /** | 87 /** |
77 * Updates `displayedList_` using splices to be equivalent to `newValue`. This | 88 * Updates `displayedList_` using splices to be equivalent to `newValue`. This |
78 * allows the iron-list to delete sublists of items which preserves scroll and | 89 * allows the iron-list to delete sublists of items which preserves scroll and |
(...skipping 22 matching lines...) Expand all Loading... | |
101 ].concat(additions)); | 112 ].concat(additions)); |
102 }.bind(this)); | 113 }.bind(this)); |
103 } | 114 } |
104 }, | 115 }, |
105 | 116 |
106 /** @private */ | 117 /** @private */ |
107 onDisplayedListSourceChange_: function() { | 118 onDisplayedListSourceChange_: function() { |
108 this.scrollTop = 0; | 119 this.scrollTop = 0; |
109 }, | 120 }, |
110 | 121 |
122 /** | |
123 * Scroll the list so that |itemId| is visible, if it is not already. | |
124 * @param {string} itemId | |
125 * @private | |
126 */ | |
127 scrollToId_: function(itemId) { | |
128 var index = this.displayedIds_.indexOf(itemId); | |
129 var list = this.$.bookmarksCard; | |
130 if (index >= 0 && index < list.firstVisibleIndex || | |
131 index > list.lastVisibleIndex) { | |
132 list.scrollToIndex(index); | |
133 } | |
134 }, | |
135 | |
111 /** @private */ | 136 /** @private */ |
112 emptyListMessage_: function() { | 137 emptyListMessage_: function() { |
113 var emptyListMessage = this.searchTerm_ ? 'noSearchResults' : 'emptyList'; | 138 var emptyListMessage = this.searchTerm_ ? 'noSearchResults' : 'emptyList'; |
114 return loadTimeData.getString(emptyListMessage); | 139 return loadTimeData.getString(emptyListMessage); |
115 }, | 140 }, |
116 | 141 |
117 /** @private */ | 142 /** @private */ |
118 isEmptyList_: function() { | 143 isEmptyList_: function() { |
119 return this.displayedList_.length == 0; | 144 return this.displayedList_.length == 0; |
120 }, | 145 }, |
121 | 146 |
122 /** @private */ | 147 /** @private */ |
123 deselectItems_: function() { | 148 deselectItems_: function() { |
124 this.dispatch(bookmarks.actions.deselectItems()); | 149 this.dispatch(bookmarks.actions.deselectItems()); |
125 }, | 150 }, |
126 | 151 |
127 /** | 152 /** |
128 * @param{HTMLElement} el | 153 * @param{HTMLElement} el |
129 * @private | 154 * @private |
130 */ | 155 */ |
131 getIndexForItemElement_: function(el) { | 156 getIndexForItemElement_: function(el) { |
132 return this.$.bookmarksCard.modelForElement(el).index; | 157 return this.$.bookmarksCard.modelForElement(el).index; |
133 }, | 158 }, |
134 | 159 |
135 /** | 160 /** |
136 * @param {Event} e | 161 * @param {Event} e |
137 * @private | 162 * @private |
138 */ | 163 */ |
139 onOpenItemMenu_: function(e) { | 164 onOpenItemMenu_: function(e) { |
140 var index = this.displayedIds_.indexOf( | |
141 /** @type {BookmarksItemElement} */ (e.path[0]).itemId); | |
142 var list = this.$.bookmarksCard; | |
143 // If the item is not visible, scroll to it before rendering the menu. | 165 // If the item is not visible, scroll to it before rendering the menu. |
144 if (index < list.firstVisibleIndex || index > list.lastVisibleIndex) | 166 this.scrollToId_(/** @type {BookmarksItemElement} */ (e.path[0]).itemId); |
145 list.scrollToIndex(index); | |
146 }, | 167 }, |
147 | 168 |
148 /** | 169 /** |
170 * Highlight a list of items by selecting them, scrolling them into view and | |
171 * focusing the first item. | |
172 * @param {Event} e | |
173 * @private | |
174 */ | |
175 onHighlightItems_: function(e) { | |
176 var toHighlight = /** @type {!Array<string>} */ | |
177 (e.detail.filter((item) => this.displayedIds_.indexOf(item) != -1)); | |
calamity
2017/07/14 04:14:39
Comment plx. What are the cases where this comes u
tsergeant
2017/07/14 06:39:20
This is just being defensive, but I think it's a r
calamity
2017/07/17 05:09:49
Acknowledged.
| |
178 | |
179 assert(toHighlight.length > 0); | |
180 var leadId = toHighlight[0]; | |
181 this.dispatch(bookmarks.actions.updateAnchor(leadId)); | |
182 this.dispatch( | |
183 bookmarks.actions.selectAll(toHighlight, this.getState(), leadId)); | |
calamity
2017/07/14 04:14:39
So a batch of operations from a move isn't _guaran
calamity
2017/07/14 04:14:39
leadId is selected as the anchor here and in the p
tsergeant
2017/07/14 06:39:19
Yeah, this is the thing that was hard about this C
tsergeant
2017/07/14 06:39:20
Done.
calamity
2017/07/17 05:09:49
Can we add a completion callback to Drop and Paste
tsergeant
2017/07/18 01:08:54
Done.
| |
184 | |
185 // Allow iron-list time to render changes to the list. | |
calamity
2017/07/14 04:14:39
What changes need to be rendered here? The adds/re
tsergeant
2017/07/14 06:39:20
Yeah, additions to the list are what's important.
| |
186 this.async(function() { | |
187 this.scrollToId_(leadId); | |
188 var leadIndex = this.displayedIds_.indexOf(leadId); | |
189 assert(leadIndex != -1); | |
190 this.$.bookmarksCard.focusItem(leadIndex); | |
calamity
2017/07/14 04:14:39
tangent: I would love to rename bookmarksCard to l
tsergeant
2017/07/14 06:39:19
Renamed to list.
| |
191 }); | |
192 }, | |
193 | |
194 /** | |
149 * @param {KeyboardEvent} e | 195 * @param {KeyboardEvent} e |
150 * @private | 196 * @private |
151 */ | 197 */ |
152 onItemKeydown_: function(e) { | 198 onItemKeydown_: function(e) { |
153 var handled = true; | 199 var handled = true; |
154 var list = this.$.bookmarksCard; | 200 var list = this.$.bookmarksCard; |
155 var focusMoved = false; | 201 var focusMoved = false; |
156 var focusedIndex = | 202 var focusedIndex = |
157 this.getIndexForItemElement_(/** @type {HTMLElement} */ (e.target)); | 203 this.getIndexForItemElement_(/** @type {HTMLElement} */ (e.target)); |
158 var oldFocusedIndex = focusedIndex; | 204 var oldFocusedIndex = focusedIndex; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 259 |
214 if (!handled) { | 260 if (!handled) { |
215 handled = bookmarks.CommandManager.getInstance().handleKeyEvent( | 261 handled = bookmarks.CommandManager.getInstance().handleKeyEvent( |
216 e, this.getState().selection.items); | 262 e, this.getState().selection.items); |
217 } | 263 } |
218 | 264 |
219 if (handled) | 265 if (handled) |
220 e.stopPropagation(); | 266 e.stopPropagation(); |
221 }, | 267 }, |
222 }); | 268 }); |
OLD | NEW |