Index: chrome/browser/resources/md_downloads/manager.js |
diff --git a/chrome/browser/resources/md_downloads/manager.js b/chrome/browser/resources/md_downloads/manager.js |
index 91faf6a1b79d08f3c5aac0e36f91656190a8bf12..76017acc3017bd694801114478f8346b07baf784 100644 |
--- a/chrome/browser/resources/md_downloads/manager.js |
+++ b/chrome/browser/resources/md_downloads/manager.js |
@@ -8,58 +8,17 @@ |
properties: { |
hasDownloads_: { |
- observer: 'hasDownloadsChanged_', |
type: Boolean, |
+ value: false, |
}, |
items_: { |
type: Array, |
- value: function() { return []; }, |
}, |
}, |
hostAttributes: { |
loading: true, |
- }, |
- |
- observers: [ |
- 'itemsChanged_(items_.*)', |
- ], |
- |
- /** @private */ |
- clearAll_: function() { |
- this.set('items_', []); |
- }, |
- |
- /** @private */ |
- hasDownloadsChanged_: function() { |
- if (loadTimeData.getBoolean('allowDeletingHistory')) |
- this.$.toolbar.downloadsShowing = this.hasDownloads_; |
- |
- if (this.hasDownloads_) { |
- this.$['downloads-list'].fire('iron-resize'); |
- } else { |
- var isSearching = downloads.ActionService.getInstance().isSearching(); |
- var messageToShow = isSearching ? 'noSearchResults' : 'noDownloads'; |
- this.$['no-downloads'].querySelector('span').textContent = |
- loadTimeData.getString(messageToShow); |
- } |
- }, |
- |
- /** |
- * @param {number} index |
- * @param {!Array<!downloads.Data>} list |
- * @private |
- */ |
- insertItems_: function(index, list) { |
- this.splice.apply(this, ['items_', index, 0].concat(list)); |
- this.updateHideDates_(index, index + list.length); |
- this.removeAttribute('loading'); |
- }, |
- |
- /** @private */ |
- itemsChanged_: function() { |
- this.hasDownloads_ = this.items_.length > 0; |
}, |
/** |
@@ -100,65 +59,78 @@ |
}, |
/** |
- * @param {number} index |
+ * @return {number} The number of downloads shown on the page. |
* @private |
*/ |
- removeItem_: function(index) { |
- this.splice('items_', index, 1); |
- this.updateHideDates_(index, index); |
+ size_: function() { |
+ return this.items_.length; |
}, |
/** |
- * @param {number} start |
- * @param {number} end |
+ * Called when all items need to be updated. |
+ * @param {!Array<!downloads.Data>} list A list of new download data. |
* @private |
*/ |
- updateHideDates_: function(start, end) { |
- for (var i = start; i <= end; ++i) { |
- var current = this.items_[i]; |
- if (!current) |
- continue; |
- var prev = this.items_[i - 1]; |
- current.hideDate = !!prev && prev.date_string == current.date_string; |
+ updateAll_: function(list) { |
+ /** @private {!Object<number>} */ |
+ this.idToIndex_ = {}; |
+ |
+ for (var i = 0; i < list.length; ++i) { |
+ var data = list[i]; |
+ |
+ this.idToIndex_[data.id] = data.index = i; |
+ |
+ var prev = list[i - 1]; |
+ data.hideDate = !!prev && prev.date_string == data.date_string; |
} |
+ |
+ // TODO(dbeam): this resets the scroll position, which is a huge bummer. |
+ // Removing something from the bottom of the list should not scroll you |
+ // back to the top. The grand plan is to restructure how the C++ sends the |
+ // JS data so that it only gets updates (rather than the most recent set |
+ // of items). TL;DR - we can't ship with this bug. |
+ this.items_ = list; |
+ |
+ var hasDownloads = this.size_() > 0; |
+ if (!hasDownloads) { |
+ var isSearching = downloads.ActionService.getInstance().isSearching(); |
+ var messageToShow = isSearching ? 'noSearchResults' : 'noDownloads'; |
+ this.$['no-downloads'].querySelector('span').textContent = |
+ loadTimeData.getString(messageToShow); |
+ } |
+ this.hasDownloads_ = hasDownloads; |
+ |
+ if (loadTimeData.getBoolean('allowDeletingHistory')) |
+ this.$.toolbar.downloadsShowing = this.hasDownloads_; |
+ |
+ this.removeAttribute('loading'); |
}, |
/** |
- * @param {number} index |
* @param {!downloads.Data} data |
* @private |
*/ |
- updateItem_: function(index, data) { |
+ updateItem_: function(data) { |
+ var index = this.idToIndex_[data.id]; |
this.set('items_.' + index, data); |
- this.updateHideDates_(index, index); |
this.$['downloads-list'].updateSizeForItem(index); |
}, |
}); |
- Manager.clearAll = function() { |
- Manager.get().clearAll_(); |
+ Manager.size = function() { |
+ return document.querySelector('downloads-manager').size_(); |
}; |
- /** @return {!downloads.Manager} */ |
- Manager.get = function() { |
- return /** @type {!downloads.Manager} */( |
- queryRequiredElement('downloads-manager')); |
+ Manager.updateAll = function(list) { |
+ document.querySelector('downloads-manager').updateAll_(list); |
}; |
- Manager.insertItems = function(index, list) { |
- Manager.get().insertItems_(index, list); |
+ Manager.updateItem = function(item) { |
+ document.querySelector('downloads-manager').updateItem_(item); |
}; |
Manager.onLoad = function() { |
- Manager.get().onLoad_(); |
- }; |
- |
- Manager.removeItem = function(index) { |
- Manager.get().removeItem_(index); |
- }; |
- |
- Manager.updateItem = function(index, data) { |
- Manager.get().updateItem_(index, data); |
+ document.querySelector('downloads-manager').onLoad_(); |
}; |
return {Manager: Manager}; |