Index: chrome/browser/resources/md_downloads/crisper.js |
diff --git a/chrome/browser/resources/md_downloads/crisper.js b/chrome/browser/resources/md_downloads/crisper.js |
index 3b32922d2d9a9d6b8a4f0cadd1246e8961f651f1..da4452387f9851bc529b874c8665cd0736bb3498 100644 |
--- a/chrome/browser/resources/md_downloads/crisper.js |
+++ b/chrome/browser/resources/md_downloads/crisper.js |
@@ -1531,27 +1531,6 @@ |
/** @constructor */ |
function ActionService() {} |
- /** |
- * @param {string} searchText Input typed by the user into a search box. |
- * @return {Array<string>} A list of terms extracted from |searchText|. |
- */ |
- ActionService.splitTerms = function(searchText) { |
- /** |
- * @param {string} s |
- * @return {string} |s| without whitespace at the beginning or end. |
- */ |
- function trim(s) { return s.trim(); } |
- |
- /** |
- * @param {string} s |
- * @return {boolean} Whether |s| is empty. |
- */ |
- function notEmpty(s) { return s.length > 0; } |
- |
- // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). |
- return searchText.split(/"([^"]*)"/).map(trim).filter(notEmpty); |
- }; |
- |
ActionService.prototype = { |
/** @param {string} id ID of the download to cancel. */ |
cancel: chromeSendWithId('cancel'), |
@@ -1583,7 +1562,7 @@ |
* (i.e. has a non-empty search term). |
*/ |
isSearching: function() { |
- return !!this.searchText_; |
+ return this.searchText_.length > 0; |
}, |
/** Opens the current local destination for downloads. */ |
@@ -1615,7 +1594,10 @@ |
return; |
this.searchText_ = searchText; |
- chrome.send('getDownloads', ActionService.splitTerms(searchText)); |
+ |
+ // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). |
+ function trim(s) { return s.trim(); } |
+ chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim)); |
}, |
/** |
@@ -16955,58 +16937,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; |
}, |
/** |
@@ -17047,64 +16988,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); |
- }, |
- |
- /** |
- * @param {number} start |
- * @param {number} end |
+ size_: function() { |
+ return this.items_.length; |
+ }, |
+ |
+ /** |
+ * 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; |
- } |
- }, |
- |
- /** |
- * @param {number} index |
+ 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 {!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 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}; |