| 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 Polymer({ | 5 Polymer({ |
| 6 is: 'history-card-manager', | 6 is: 'history-card-manager', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 // An array of objects sorted in reverse chronological order. | 9 // An array of objects sorted in reverse chronological order. |
| 10 // Each object has a date and the history items belonging to that date. | 10 // Each object has a date and the history items belonging to that date. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 if (currentDate) | 98 if (currentDate) |
| 99 this.appendHistoryData_(currentDate, historyItems); | 99 this.appendHistoryData_(currentDate, historyItems); |
| 100 | 100 |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; |
| 102 }, | 102 }, |
| 103 | 103 |
| 104 /** | 104 /** |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | 105 * Cycle through each entry in historyDataByDay_ and set all items to be |
| 106 * unselected. | 106 * unselected. |
| 107 * @param {number} overallItemCount The number of items selected. |
| 107 */ | 108 */ |
| 108 unselectAllItems: function(overallItemCount) { | 109 unselectAllItems: function(overallItemCount) { |
| 109 var historyCardData = this.historyDataByDay_; | 110 var historyCardData = this.historyDataByDay_; |
| 110 | 111 |
| 111 for (var i = 0; i < historyCardData.length; i++) { | 112 for (var i = 0; i < historyCardData.length; i++) { |
| 112 var items = historyCardData[i].historyItems; | 113 var items = historyCardData[i].historyItems; |
| 113 for (var j = 0; j < items.length; j++) { | 114 for (var j = 0; j < items.length; j++) { |
| 114 if (items[j].selected) { | 115 if (items[j].selected) { |
| 115 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | 116 this.set('historyDataByDay_.' + i + '.historyItems.' + j + |
| 116 '.selected', false); | 117 '.selected', false); |
| 117 overallItemCount--; | 118 overallItemCount--; |
| 118 if (overallItemCount == 0) | 119 if (overallItemCount == 0) |
| 119 break; | 120 return; |
| 120 } | 121 } |
| 121 } | 122 } |
| 122 } | 123 } |
| 123 }, | 124 }, |
| 124 | 125 |
| 125 /** | 126 /** |
| 127 * Remove all selected items from the overall array so that they are also |
| 128 * removed from view. Make sure that the card length and positioning is |
| 129 * updated accordingly. |
| 130 * @param {number} overallItemCount The number of items selected. |
| 131 */ |
| 132 removeDeletedHistory: function(overallItemCount) { |
| 133 for (var i = 0; i < this.historyDataByDay_.length; i++) { |
| 134 var items = this.historyDataByDay_[i].historyItems; |
| 135 var itemDeletedFromCard = false; |
| 136 |
| 137 for (var j = items.length - 1; j >= 0; j--) { |
| 138 if (!items[j].selected) |
| 139 continue; |
| 140 |
| 141 this.splice('historyDataByDay_.' + i + '.historyItems', j, 1); |
| 142 itemDeletedFromCard = true; |
| 143 overallItemCount--; |
| 144 if (overallItemCount == 0) { |
| 145 this.removeEmptyCards_(); |
| 146 // If the last card has been removed don't try to update its size. |
| 147 if (i < this.historyDataByDay_.length) |
| 148 this.$['infinite-list'].updateSizeForItem(i); |
| 149 return; |
| 150 } |
| 151 } |
| 152 if (itemDeletedFromCard) |
| 153 this.$['infinite-list'].updateSizeForItem(i); |
| 154 } |
| 155 }, |
| 156 |
| 157 /** |
| 158 * If a day has had all the history it contains removed, remove this day from |
| 159 * the array. |
| 160 * @private |
| 161 */ |
| 162 removeEmptyCards_: function() { |
| 163 var historyCards = this.historyDataByDay_; |
| 164 for (var i = historyCards.length - 1; i >= 0; i--) { |
| 165 if (historyCards[i].historyItems.length == 0) { |
| 166 this.splice('historyDataByDay_', i, 1); |
| 167 } |
| 168 } |
| 169 }, |
| 170 |
| 171 /** |
| 126 * Adds the given items into historyDataByDay_. Adds items to the last | 172 * Adds the given items into historyDataByDay_. Adds items to the last |
| 127 * existing day if the date matches, creates a new element otherwise. | 173 * existing day if the date matches, creates a new element otherwise. |
| 128 * @param {string} date The date of the history items. | 174 * @param {string} date The date of the history items. |
| 129 * @param {!Array<!HistoryEntry>} historyItems The list of history items for | 175 * @param {!Array<!HistoryEntry>} historyItems The list of history items for |
| 130 * the current date. | 176 * the current date. |
| 131 * @private | 177 * @private |
| 132 */ | 178 */ |
| 133 appendHistoryData_: function(date, historyItems) { | 179 appendHistoryData_: function(date, historyItems) { |
| 134 var lastDay = this.historyDataByDay_.length - 1; | 180 var lastDay = this.historyDataByDay_.length - 1; |
| 135 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { | 181 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { |
| 136 this.set('historyDataByDay_.' + lastDay + '.historyItems', | 182 this.set('historyDataByDay_.' + lastDay + '.historyItems', |
| 137 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); | 183 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); |
| 138 } else { | 184 } else { |
| 139 this.push('historyDataByDay_', { | 185 this.push('historyDataByDay_', { |
| 140 date: date, | 186 date: date, |
| 141 historyItems: historyItems | 187 historyItems: historyItems |
| 142 }); | 188 }); |
| 143 } | 189 } |
| 144 }, | 190 }, |
| 145 | 191 |
| 146 /** | 192 /** |
| 193 * Based on which items are selected, collect an array of the info required |
| 194 * for chrome.send('removeHistory', ...). |
| 195 * @param {number} count The number of items that are selected. |
| 196 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain |
| 197 * information on which history-items should be deleted. |
| 198 */ |
| 199 getSelectedItems: function(count) { |
| 200 var toBeRemoved = []; |
| 201 for (var i = 0; i < this.historyDataByDay_.length; i++) { |
| 202 var items = this.historyDataByDay_[i].historyItems; |
| 203 for (var j = 0; j < items.length; j++) { |
| 204 if (items[j].selected) { |
| 205 |
| 206 toBeRemoved.push({ |
| 207 url: items[j].url, |
| 208 timestamps: items[j].allTimestamps |
| 209 }); |
| 210 |
| 211 count--; |
| 212 if (count == 0) |
| 213 return toBeRemoved; |
| 214 } |
| 215 } |
| 216 } |
| 217 return toBeRemoved; |
| 218 }, |
| 219 |
| 220 /** |
| 147 * Called when the card manager is scrolled. | 221 * Called when the card manager is scrolled. |
| 148 * @private | 222 * @private |
| 149 */ | 223 */ |
| 150 scrollHandler_: function() { | 224 scrollHandler_: function() { |
| 151 // Close overflow menu on scroll. | 225 // Close overflow menu on scroll. |
| 152 this.closeMenu(); | 226 this.closeMenu(); |
| 153 | 227 |
| 154 // Requests the next list of results when the scrollbar is near the bottom | 228 // Requests the next list of results when the scrollbar is near the bottom |
| 155 // of the window. | 229 // of the window. |
| 156 var scrollOffset = 10; | 230 var scrollOffset = 10; |
| 157 var scrollElem = this.$['infinite-list']; | 231 var scrollElem = this.$['infinite-list']; |
| 158 | 232 |
| 159 if (scrollElem.scrollHeight <= | 233 if (scrollElem.scrollHeight <= |
| 160 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 234 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 161 chrome.send('queryHistory', | 235 chrome.send('queryHistory', |
| 162 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 236 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 163 } | 237 } |
| 164 } | 238 } |
| 165 }); | 239 }); |
| OLD | NEW |