| 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 19 matching lines...) Expand all Loading... |
| 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 * Called when all items need to be updated. | 35 * Called when all items need to be updated. |
| 36 * @param {!Array<!downloads.Data>} list A list of new download data. | 36 * @param {!Array<!downloads.Data>} list A list of new download data. |
| 37 * @private | 37 * @private |
| 38 */ | 38 */ |
| 39 updateAll_: function(list) { | 39 updateAll_: function(list) { |
| 40 /** @private {!Array} */ | 40 var oldIdMap = this.idMap_ || {}; |
| 41 this.items_ = list; | 41 |
| 42 /** @private {!Object<!downloads.ItemView>} */ |
| 43 this.idMap_ = {}; |
| 44 |
| 45 /** @private {!Array<!downloads.ItemView>} */ |
| 46 this.items_ = []; |
| 47 |
| 48 for (var i = 0; i < list.length; ++i) { |
| 49 var data = list[i]; |
| 50 var id = data.id; |
| 51 |
| 52 // Re-use old items when possible (saves work, preserves focus). |
| 53 var item = oldIdMap[id] || new downloads.ItemView; |
| 54 |
| 55 this.idMap_[id] = item; // Associated by ID for fast lookup. |
| 56 this.items_.push(item); // Add to sorted list for order. |
| 57 |
| 58 // Render |item| but don't actually add to the DOM yet. |this.items_| |
| 59 // must be fully created to be able to find the right spot to insert. |
| 60 item.update(data); |
| 61 |
| 62 // Collapse redundant dates. |
| 63 var prev = list[i - 1]; |
| 64 item.dateContainer.hidden = |
| 65 prev && prev.date_string == data.date_string; |
| 66 |
| 67 delete oldIdMap[id]; |
| 68 } |
| 69 |
| 70 // Remove stale, previously rendered items from the DOM. |
| 71 for (var id in oldIdMap) { |
| 72 if (oldIdMap[id].parentNode) |
| 73 oldIdMap[id].parentNode.removeChild(oldIdMap[id]); |
| 74 delete oldIdMap[id]; |
| 75 } |
| 76 |
| 77 for (var i = 0; i < this.items_.length; ++i) { |
| 78 var item = this.items_[i]; |
| 79 if (item.parentNode) // Already in the DOM; skip. |
| 80 continue; |
| 81 |
| 82 var before = null; |
| 83 // Find the next rendered item after this one, and insert before it. |
| 84 for (var j = i + 1; !before && j < this.items_.length; ++j) { |
| 85 if (this.items_[j].parentNode) |
| 86 before = this.items_[j]; |
| 87 } |
| 88 // If |before| is null, |item| will just get added at the end. |
| 89 this.node_.insertBefore(item, before); |
| 90 } |
| 42 | 91 |
| 43 var noDownloadsOrResults = $('no-downloads-or-results'); | 92 var noDownloadsOrResults = $('no-downloads-or-results'); |
| 44 noDownloadsOrResults.textContent = loadTimeData.getString( | 93 noDownloadsOrResults.textContent = loadTimeData.getString( |
| 45 this.searchText_ ? 'no_search_results' : 'no_downloads'); | 94 this.searchText_ ? 'no_search_results' : 'no_downloads'); |
| 46 | 95 |
| 47 var hasDownloads = this.size_() > 0; | 96 var hasDownloads = this.size_() > 0; |
| 48 this.node_.hidden = !hasDownloads; | 97 this.node_.hidden = !hasDownloads; |
| 49 noDownloadsOrResults.hidden = hasDownloads; | 98 noDownloadsOrResults.hidden = hasDownloads; |
| 50 | 99 |
| 51 if (loadTimeData.getBoolean('allow_deleting_history')) | 100 if (loadTimeData.getBoolean('allow_deleting_history')) |
| 52 $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0; | 101 $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0; |
| 53 }, | 102 }, |
| 54 | 103 |
| 55 /** | 104 /** |
| 105 * @param {!downloads.Data} data |
| 106 * @private |
| 107 */ |
| 108 updateItem_: function(data) { |
| 109 this.idMap_[data.id].update(data); |
| 110 }, |
| 111 |
| 112 /** |
| 56 * @return {number} The number of downloads shown on the page. | 113 * @return {number} The number of downloads shown on the page. |
| 57 * @private | 114 * @private |
| 58 */ | 115 */ |
| 59 size_: function() { | 116 size_: function() { |
| 60 return this.items_.length; | 117 return this.items_.length; |
| 61 }, | 118 }, |
| 62 | 119 |
| 63 /** @private */ | 120 /** @private */ |
| 64 clearAll_: function() { | 121 clearAll_: function() { |
| 65 if (loadTimeData.getBoolean('allow_deleting_history')) { | 122 if (loadTimeData.getBoolean('allow_deleting_history')) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 chrome.send('undo'); | 189 chrome.send('undo'); |
| 133 else if (e.command.id == 'clear-all-command') | 190 else if (e.command.id == 'clear-all-command') |
| 134 this.clearAll_(); | 191 this.clearAll_(); |
| 135 }, | 192 }, |
| 136 }; | 193 }; |
| 137 | 194 |
| 138 Manager.updateAll = function(list) { | 195 Manager.updateAll = function(list) { |
| 139 Manager.getInstance().updateAll_(list); | 196 Manager.getInstance().updateAll_(list); |
| 140 }; | 197 }; |
| 141 | 198 |
| 142 Manager.updateItem = function(item) {}; | 199 Manager.updateItem = function(item) { |
| 200 Manager.getInstance().updateItem_(item); |
| 201 }; |
| 143 | 202 |
| 144 Manager.setSearchText = function(searchText) { | 203 Manager.setSearchText = function(searchText) { |
| 145 Manager.getInstance().setSearchText_(searchText); | 204 Manager.getInstance().setSearchText_(searchText); |
| 146 }; | 205 }; |
| 147 | 206 |
| 148 Manager.onLoad = function() { | 207 Manager.onLoad = function() { |
| 149 Manager.getInstance().onLoad_(); | 208 Manager.getInstance().onLoad_(); |
| 150 }; | 209 }; |
| 151 | 210 |
| 152 Manager.size = function() { | 211 Manager.size = function() { |
| 153 return Manager.getInstance().size_(); | 212 return Manager.getInstance().size_(); |
| 154 }; | 213 }; |
| 155 | 214 |
| 156 return {Manager: Manager}; | 215 return {Manager: Manager}; |
| 157 }); | 216 }); |
| 158 | 217 |
| 159 window.addEventListener('DOMContentLoaded', downloads.Manager.onLoad); | 218 window.addEventListener('DOMContentLoaded', downloads.Manager.onLoad); |
| OLD | NEW |