| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 cr.define('downloads', function() { | 5 cr.define('downloads', function() { |
| 6 /** | 6 /** |
| 7 * Class to own and manage download items. | 7 * Class to own and manage download items. |
| 8 * @constructor | 8 * @constructor |
| 9 */ | 9 */ |
| 10 function Manager() {} | 10 function Manager() {} |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 $('downloads-summary-text').textContent = this.searchText_ ? | 26 $('downloads-summary-text').textContent = this.searchText_ ? |
| 27 loadTimeData.getStringF('searchresultsfor', this.searchText_) : ''; | 27 loadTimeData.getStringF('searchresultsfor', this.searchText_) : ''; |
| 28 | 28 |
| 29 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). | 29 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). |
| 30 function trim(s) { return s.trim(); } | 30 function trim(s) { return s.trim(); } |
| 31 chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim)); | 31 chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim)); |
| 32 }, | 32 }, |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * @return {number} A guess at how many items could be visible at once. |
| 36 * @private |
| 37 */ |
| 38 guesstimateNumberOfVisibleItems_: function() { |
| 39 var toolbarHeight = $('downloads-toolbar').offsetHeight; |
| 40 var summaryHeight = $('downloads-summary').offsetHeight; |
| 41 var nonItemSpace = toolbarHeight + summaryHeight; |
| 42 return Math.floor((window.innerHeight - nonItemSpace) / 46) + 1; |
| 43 }, |
| 44 |
| 45 /** |
| 35 * Called when all items need to be updated. | 46 * Called when all items need to be updated. |
| 36 * @param {!Array<!downloads.Data>} list A list of new download data. | 47 * @param {!Array<!downloads.Data>} list A list of new download data. |
| 37 * @private | 48 * @private |
| 38 */ | 49 */ |
| 39 updateAll_: function(list) { | 50 updateAll_: function(list) { |
| 40 /** @private {!Array} */ | 51 var oldIdMap = this.idMap_ || {}; |
| 41 this.items_ = list; | 52 |
| 53 /** @private {!Object<!downloads.ItemView>} */ |
| 54 this.idMap_ = {}; |
| 55 |
| 56 /** @private {!Array<!downloads.ItemView>} */ |
| 57 this.items_ = []; |
| 58 |
| 59 if (!this.iconLoader_) { |
| 60 var guesstimate = Math.max(this.guesstimateNumberOfVisibleItems_(), 1); |
| 61 /** @private {downloads.ThrottledIconLoader} */ |
| 62 this.iconLoader_ = new downloads.ThrottledIconLoader(guesstimate); |
| 63 } |
| 64 |
| 65 for (var i = 0; i < list.length; ++i) { |
| 66 var data = list[i]; |
| 67 var id = data.id; |
| 68 |
| 69 // Re-use old items when possible (saves work, preserves focus). |
| 70 var item = oldIdMap[id] || new downloads.ItemView(this.iconLoader_); |
| 71 |
| 72 this.idMap_[id] = item; // Associated by ID for fast lookup. |
| 73 this.items_.push(item); // Add to sorted list for order. |
| 74 |
| 75 // Render |item| but don't actually add to the DOM yet. |this.items_| |
| 76 // must be fully created to be able to find the right spot to insert. |
| 77 item.update(data); |
| 78 |
| 79 // Collapse redundant dates. |
| 80 var prev = list[i - 1]; |
| 81 item.hideDate = !!prev && prev.date_string == data.date_string; |
| 82 |
| 83 delete oldIdMap[id]; |
| 84 } |
| 85 |
| 86 // Remove stale, previously rendered items from the DOM. |
| 87 for (var id in oldIdMap) { |
| 88 if (oldIdMap[id].parentNode) |
| 89 oldIdMap[id].parentNode.removeChild(oldIdMap[id]); |
| 90 delete oldIdMap[id]; |
| 91 } |
| 92 |
| 93 for (var i = 0; i < this.items_.length; ++i) { |
| 94 var item = this.items_[i]; |
| 95 if (item.parentNode) // Already in the DOM; skip. |
| 96 continue; |
| 97 |
| 98 var before = null; |
| 99 // Find the next rendered item after this one, and insert before it. |
| 100 for (var j = i + 1; !before && j < this.items_.length; ++j) { |
| 101 if (this.items_[j].parentNode) |
| 102 before = this.items_[j]; |
| 103 } |
| 104 // If |before| is null, |item| will just get added at the end. |
| 105 this.node_.insertBefore(item, before); |
| 106 } |
| 42 | 107 |
| 43 var noDownloadsOrResults = $('no-downloads-or-results'); | 108 var noDownloadsOrResults = $('no-downloads-or-results'); |
| 44 noDownloadsOrResults.textContent = loadTimeData.getString( | 109 noDownloadsOrResults.textContent = loadTimeData.getString( |
| 45 this.searchText_ ? 'no_search_results' : 'no_downloads'); | 110 this.searchText_ ? 'no_search_results' : 'no_downloads'); |
| 46 | 111 |
| 47 var hasDownloads = this.size_() > 0; | 112 var hasDownloads = this.size_() > 0; |
| 48 this.node_.hidden = !hasDownloads; | 113 this.node_.hidden = !hasDownloads; |
| 49 noDownloadsOrResults.hidden = hasDownloads; | 114 noDownloadsOrResults.hidden = hasDownloads; |
| 50 | 115 |
| 51 if (loadTimeData.getBoolean('allow_deleting_history')) | 116 if (loadTimeData.getBoolean('allow_deleting_history')) |
| 52 $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0; | 117 $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0; |
| 53 }, | 118 }, |
| 54 | 119 |
| 55 /** | 120 /** |
| 121 * @param {!downloads.Data} data |
| 122 * @private |
| 123 */ |
| 124 updateItem_: function(data) { |
| 125 this.idMap_[data.id].update(data); |
| 126 }, |
| 127 |
| 128 /** |
| 56 * @return {number} The number of downloads shown on the page. | 129 * @return {number} The number of downloads shown on the page. |
| 57 * @private | 130 * @private |
| 58 */ | 131 */ |
| 59 size_: function() { | 132 size_: function() { |
| 60 return this.items_.length; | 133 return this.items_.length; |
| 61 }, | 134 }, |
| 62 | 135 |
| 63 /** @private */ | 136 /** @private */ |
| 64 clearAll_: function() { | 137 clearAll_: function() { |
| 65 if (loadTimeData.getBoolean('allow_deleting_history')) { | 138 if (loadTimeData.getBoolean('allow_deleting_history')) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 chrome.send('undo'); | 205 chrome.send('undo'); |
| 133 else if (e.command.id == 'clear-all-command') | 206 else if (e.command.id == 'clear-all-command') |
| 134 this.clearAll_(); | 207 this.clearAll_(); |
| 135 }, | 208 }, |
| 136 }; | 209 }; |
| 137 | 210 |
| 138 Manager.updateAll = function(list) { | 211 Manager.updateAll = function(list) { |
| 139 Manager.getInstance().updateAll_(list); | 212 Manager.getInstance().updateAll_(list); |
| 140 }; | 213 }; |
| 141 | 214 |
| 142 Manager.updateItem = function(item) {}; | 215 Manager.updateItem = function(item) { |
| 216 Manager.getInstance().updateItem_(item); |
| 217 }; |
| 143 | 218 |
| 144 Manager.setSearchText = function(searchText) { | 219 Manager.setSearchText = function(searchText) { |
| 145 Manager.getInstance().setSearchText_(searchText); | 220 Manager.getInstance().setSearchText_(searchText); |
| 146 }; | 221 }; |
| 147 | 222 |
| 148 Manager.onLoad = function() { | 223 Manager.onLoad = function() { |
| 149 Manager.getInstance().onLoad_(); | 224 Manager.getInstance().onLoad_(); |
| 150 }; | 225 }; |
| 151 | 226 |
| 152 Manager.size = function() { | 227 Manager.size = function() { |
| 153 return Manager.getInstance().size_(); | 228 return Manager.getInstance().size_(); |
| 154 }; | 229 }; |
| 155 | 230 |
| 156 return {Manager: Manager}; | 231 return {Manager: Manager}; |
| 157 }); | 232 }); |
| 158 | 233 |
| 159 window.addEventListener('DOMContentLoaded', downloads.Manager.onLoad); | 234 window.addEventListener('load', downloads.Manager.onLoad); |
| OLD | NEW |