Chromium Code Reviews| 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 ], |
| 11 | 11 |
| 12 properties: { | 12 properties: { |
| 13 /** @private {Array<string>} */ | 13 /** @private {Array<{id: string}>} */ |
| 14 displayedList_: { | 14 displayedList_: { |
| 15 type: Array, | 15 type: Array, |
| 16 value: function() { | 16 value: function() { |
| 17 // Use an empty list during initialization so that the databinding to | 17 // Use an empty list during initialization so that the databinding to |
| 18 // hide #bookmarksCard takes effect. | 18 // hide #bookmarksCard takes effect. |
| 19 return []; | 19 return []; |
| 20 }, | 20 }, |
| 21 }, | 21 }, |
| 22 | 22 |
| 23 /** @private {Array<string>} */ | |
| 24 displayedIds_: { | |
| 25 type: Array, | |
| 26 observer: 'onDisplayedIdsChanged_', | |
| 27 }, | |
| 28 | |
| 23 /** @private */ | 29 /** @private */ |
| 24 searchTerm_: String, | 30 searchTerm_: String, |
| 25 }, | 31 }, |
| 26 | 32 |
| 27 listeners: { | 33 listeners: { |
| 28 'click': 'deselectItems_', | 34 'click': 'deselectItems_', |
| 29 }, | 35 }, |
| 30 | 36 |
| 31 attached: function() { | 37 attached: function() { |
| 32 this.watch('displayedList_', function(state) { | 38 var list = /** @type {IronListElement} */ (this.$.bookmarksCard); |
| 39 list.scrollTarget = this; | |
| 40 | |
| 41 this.watch('displayedIds_', function(state) { | |
| 33 return bookmarks.util.getDisplayedList(state); | 42 return bookmarks.util.getDisplayedList(state); |
| 34 }); | 43 }); |
| 35 this.watch('searchTerm_', function(state) { | 44 this.watch('searchTerm_', function(state) { |
| 36 return state.search.term; | 45 return state.search.term; |
| 37 }); | 46 }); |
| 38 this.updateFromStore(); | 47 this.updateFromStore(); |
| 39 }, | 48 }, |
| 40 | 49 |
| 50 /** @return {HTMLElement} */ | |
| 41 getDropTarget: function() { | 51 getDropTarget: function() { |
| 42 return this.$.message; | 52 return this.$.message; |
| 43 }, | 53 }, |
| 44 | 54 |
| 55 /** | |
| 56 * Updates `displayedList_` using splices to be equivalent to `newValue`. This | |
| 57 * allows the iron-list to delete sublists of items which preserves scroll and | |
| 58 * focus on incremental update. | |
|
tsergeant
2017/05/17 03:43:59
I think you should also comment somewhere (here or
calamity
2017/05/17 05:33:53
Added to displayedList_ above.
tsergeant
2017/05/17 07:05:38
Great, thanks
| |
| 59 * @param {Array<string>} newValue | |
| 60 * @param {Array<string>} oldValue | |
| 61 */ | |
| 62 onDisplayedIdsChanged_: function(newValue, oldValue) { | |
| 63 if (!oldValue) { | |
| 64 this.displayedList_ = this.displayedIds_.map(function(id) { | |
| 65 return {id: id}; | |
| 66 }); | |
| 67 } else { | |
| 68 var splices = Polymer.ArraySplice.calculateSplices(newValue, oldValue); | |
| 69 splices.forEach(function(splice) { | |
| 70 // TODO(calamity): Could use notifySplices to improve performance here. | |
| 71 var additions = | |
| 72 newValue.slice(splice.index, splice.index + splice.addedCount) | |
| 73 .map(function(id) { | |
| 74 return {id: id}; | |
| 75 }); | |
| 76 this.splice.apply(this, [ | |
| 77 'displayedList_', splice.index, splice.removed.length | |
| 78 ].concat(additions)); | |
| 79 }.bind(this)); | |
| 80 } | |
| 81 }, | |
| 82 | |
| 45 /** @private */ | 83 /** @private */ |
| 46 emptyListMessage_: function() { | 84 emptyListMessage_: function() { |
| 47 var emptyListMessage = this.searchTerm_ ? 'noSearchResults' : 'emptyList'; | 85 var emptyListMessage = this.searchTerm_ ? 'noSearchResults' : 'emptyList'; |
| 48 return loadTimeData.getString(emptyListMessage); | 86 return loadTimeData.getString(emptyListMessage); |
| 49 }, | 87 }, |
| 50 | 88 |
| 51 /** @private */ | 89 /** @private */ |
| 52 isEmptyList_: function() { | 90 isEmptyList_: function() { |
| 53 return this.displayedList_.length == 0; | 91 return this.displayedList_.length == 0; |
| 54 }, | 92 }, |
| 55 | 93 |
| 56 /** @private */ | 94 /** @private */ |
| 57 deselectItems_: function() { | 95 deselectItems_: function() { |
| 58 this.dispatch(bookmarks.actions.deselectItems()); | 96 this.dispatch(bookmarks.actions.deselectItems()); |
| 59 }, | 97 }, |
| 60 }); | 98 }); |
| OLD | NEW |