Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7260)

Unified Diff: chrome/browser/resources/md_downloads/manager.js

Issue 1224623013: Port downloads.ItemView to a Polymer component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@p-dl-rough-draft2
Patch Set: actually include i18n changes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 16caf24f01915b06f8b6d791fa21138699fbeb57..3371629e77ae747e1ddc371c61178af9d7a1e508 100644
--- a/chrome/browser/resources/md_downloads/manager.js
+++ b/chrome/browser/resources/md_downloads/manager.js
@@ -37,8 +37,57 @@ cr.define('downloads', function() {
* @private
*/
updateAll_: function(list) {
- /** @private {!Array} */
- this.items_ = list;
+ var oldIdMap = this.idMap_ || {};
+
+ /** @private {!Object<!downloads.ItemView>} */
+ this.idMap_ = {};
+
+ /** @private {!Array<!downloads.ItemView>} */
+ this.items_ = [];
+
+ for (var i = 0; i < list.length; ++i) {
+ var data = list[i];
+ var id = data.id;
+
+ // Re-use old items when possible (saves work, preserves focus).
+ var item = oldIdMap[id] || new downloads.ItemView;
+
+ this.idMap_[id] = item; // Associated by ID for fast lookup.
+ this.items_.push(item); // Add to sorted list for order.
+
+ // Render |item| but don't actually add to the DOM yet. |this.items_|
+ // must be fully created to be able to find the right spot to insert.
+ item.update(data);
+
+ // Collapse redundant dates.
+ var prev = list[i - 1];
+ item.dateContainer.hidden =
+ prev && prev.date_string == data.date_string;
+
+ delete oldIdMap[id];
+ }
+
+ // Remove stale, previously rendered items from the DOM.
+ for (var id in oldIdMap) {
+ if (oldIdMap[id].parentNode)
+ oldIdMap[id].parentNode.removeChild(oldIdMap[id]);
+ delete oldIdMap[id];
+ }
+
+ for (var i = 0; i < this.items_.length; ++i) {
+ var item = this.items_[i];
+ if (item.parentNode) // Already in the DOM; skip.
+ continue;
+
+ var before = null;
+ // Find the next rendered item after this one, and insert before it.
+ for (var j = i + 1; !before && j < this.items_.length; ++j) {
+ if (this.items_[j].parentNode)
+ before = this.items_[j];
+ }
+ // If |before| is null, |item| will just get added at the end.
+ this.node_.insertBefore(item, before);
+ }
var noDownloadsOrResults = $('no-downloads-or-results');
noDownloadsOrResults.textContent = loadTimeData.getString(
@@ -53,6 +102,14 @@ cr.define('downloads', function() {
},
/**
+ * @param {!downloads.Data} data
+ * @private
+ */
+ updateItem_: function(data) {
+ this.idMap_[data.id].update(data);
+ },
+
+ /**
* @return {number} The number of downloads shown on the page.
* @private
*/
@@ -139,7 +196,9 @@ cr.define('downloads', function() {
Manager.getInstance().updateAll_(list);
};
- Manager.updateItem = function(item) {};
+ Manager.updateItem = function(item) {
+ Manager.getInstance().updateItem_(item);
+ };
Manager.setSearchText = function(searchText) {
Manager.getInstance().setSearchText_(searchText);

Powered by Google App Engine
This is Rietveld 408576698