Chromium Code Reviews| 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-list', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 // An array of objects sorted in reverse chronological order. | 9 // An array of history entries in reverse chronological order. |
| 10 // Each object has a date and the history items belonging to that date. | 10 historyData: { |
| 11 historyDataByDay_: { | |
| 12 type: Array, | 11 type: Array, |
| 13 value: function() { return []; } | 12 value: function() { return []; } |
| 14 }, | 13 }, |
| 15 | 14 |
| 16 // The time of access of the last element of historyDataByDay_. | 15 // The time in seconds of the last history item in historyData. |
| 17 lastVisitedTime: { | 16 lastVisitedTime: { |
| 18 type: Number, | 17 type: Number, |
| 19 value: 0 | 18 value: 0 |
| 20 }, | 19 }, |
| 21 | 20 |
| 22 menuOpen: { | 21 menuOpen: { |
| 23 type: Boolean, | 22 type: Boolean, |
| 24 value: false, | 23 value: false, |
| 25 reflectToAttribute: true | 24 reflectToAttribute: true |
| 26 }, | 25 }, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 this.menuIdentifier = e.detail.accessTime; | 62 this.menuIdentifier = e.detail.accessTime; |
| 64 | 63 |
| 65 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, | 64 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, |
| 66 cr.ui.AnchorType.BEFORE); | 65 cr.ui.AnchorType.BEFORE); |
| 67 | 66 |
| 68 menu.focus(); | 67 menu.focus(); |
| 69 } | 68 } |
| 70 }, | 69 }, |
| 71 | 70 |
| 72 /** | 71 /** |
| 73 * Split the newly updated history results into history items sorted via day | 72 * Adds the newly updated history results into historyData. Adds new fields |
| 74 * accessed. | 73 * for each result. |
| 75 * @param {!Array<!HistoryEntry>} results The new history results. | 74 * @param {!Array<!HistoryEntry>} historyResults The new history results. |
| 76 */ | 75 */ |
| 77 addNewResults: function(results) { | 76 addNewResults: function(historyResults) { |
| 78 if (results.length == 0) | 77 if (historyResults.length == 0) |
| 79 return; | 78 return; |
| 80 | 79 |
| 81 var dateSortedData = []; | 80 // Creates a copy of historyResults to prevent accidentally modifying the |
|
tsergeant
2016/02/11 02:43:02
Nit: "modifying the this"
yingran
2016/02/12 02:03:05
Done.
| |
| 82 var historyItems = []; | 81 // this field. |
| 82 var results = historyResults.slice(); | |
| 83 | |
| 83 var currentDate = results[0].dateRelativeDay; | 84 var currentDate = results[0].dateRelativeDay; |
| 85 for (var i = 0; i < results.length; i++) { | |
| 86 // Sets the default values for these fields to prevent undefined types. | |
| 87 results[i].selected = false; | |
| 88 results[i].isLastItem = false; | |
| 89 results[i].isFirstItem = false; | |
| 84 | 90 |
| 85 for (var i = 0; i < results.length; i++) { | |
| 86 if (!currentDate) | |
| 87 continue; | |
| 88 | |
| 89 results[i].selected = false; | |
| 90 if (results[i].dateRelativeDay != currentDate) { | 91 if (results[i].dateRelativeDay != currentDate) { |
| 91 this.appendHistoryData_(currentDate, historyItems); | 92 results[i - 1].isLastItem = true; |
| 93 results[i].isFirstItem = true; | |
| 92 currentDate = results[i].dateRelativeDay; | 94 currentDate = results[i].dateRelativeDay; |
| 93 historyItems = []; | |
| 94 } | 95 } |
| 95 historyItems.push(results[i]); | 96 results[i].needsTimeGap = this.needsTimeGap_(results, i); |
| 96 } | 97 } |
| 97 | 98 |
| 98 if (currentDate) | 99 // If it's the first time we get data, the first item will always be the |
| 99 this.appendHistoryData_(currentDate, historyItems); | 100 // first card. |
| 101 if (this.historyData.length == 0) | |
| 102 results[0].isFirstItem = true; | |
| 100 | 103 |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | 104 // Adds results to the beginning of the historyData array. |
| 105 results.unshift('historyData'); | |
| 106 this.push.apply(this, results); | |
| 107 | |
| 108 this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; | |
| 102 }, | 109 }, |
| 103 | 110 |
| 104 /** | 111 /** |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | 112 * Cycle through each entry in historyData and set all items to be |
| 106 * unselected. | 113 * unselected. |
| 107 * @param {number} overallItemCount The number of items selected. | 114 * @param {number} overallItemCount The number of checkboxes selected. |
| 108 */ | 115 */ |
| 109 unselectAllItems: function(overallItemCount) { | 116 unselectAllItems: function(overallItemCount) { |
| 110 var historyCardData = this.historyDataByDay_; | 117 for (var i = 0; i < this.historyData.length; i++) { |
| 111 | 118 if (this.historyData[i].selected) { |
| 112 for (var i = 0; i < historyCardData.length; i++) { | 119 this.set('historyData.' + i + '.selected', false); |
| 113 var items = historyCardData[i].historyItems; | 120 overallItemCount--; |
| 114 for (var j = 0; j < items.length; j++) { | 121 if (overallItemCount == 0) |
| 115 if (items[j].selected) { | 122 break; |
| 116 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | |
| 117 '.selected', false); | |
| 118 overallItemCount--; | |
| 119 if (overallItemCount == 0) | |
| 120 return; | |
| 121 } | |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 }, | 125 }, |
| 125 | 126 |
| 126 /** | 127 /** |
| 127 * Remove all selected items from the overall array so that they are also | 128 * 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 * removed from view. Make sure that the card length and positioning is |
| 129 * updated accordingly. | 130 * updated accordingly. |
| 130 * @param {number} overallItemCount The number of items selected. | 131 * @param {number} overallItemCount The number of items selected. |
| 131 */ | 132 */ |
| 132 removeDeletedHistory: function(overallItemCount) { | 133 removeDeletedHistory: function(overallItemCount) { |
| 133 for (var i = 0; i < this.historyDataByDay_.length; i++) { | 134 for (var i = this.historyData.length - 1; i >= 0; i--) { |
| 134 var items = this.historyDataByDay_[i].historyItems; | 135 if (!this.historyData[i].selected) |
| 135 var itemDeletedFromCard = false; | 136 continue; |
| 136 | 137 |
| 137 for (var j = items.length - 1; j >= 0; j--) { | 138 // Resets the first history item. |
|
tsergeant
2016/02/11 02:43:02
This is a bit gnarly. It feels very manual and err
yingran
2016/02/12 02:03:05
Acknowledged.
| |
| 138 if (!items[j].selected) | 139 if (this.historyData[i].isFirstItem && |
| 139 continue; | 140 this.historyData[i].dateRelativeDay == |
| 141 this.historyData[i + 1].dateRelativeDay) { | |
| 142 this.set('historyData.' + parseInt(i + 1) + '.isFirstItem', true); | |
| 143 } | |
| 140 | 144 |
| 141 this.splice('historyDataByDay_.' + i + '.historyItems', j, 1); | 145 // Resets the last history item. |
| 142 itemDeletedFromCard = true; | 146 if (this.historyData[i].isLastItem && |
| 143 overallItemCount--; | 147 this.historyData[i].dateRelativeDay == |
| 144 if (overallItemCount == 0) { | 148 this.historyData[i - 1].dateRelativeDay) { |
| 145 this.removeEmptyCards_(); | 149 this.set('historyData.' + parseInt(i - 1) + '.isLastItem', true); |
| 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 } | 150 } |
| 152 if (itemDeletedFromCard) | |
| 153 this.$['infinite-list'].updateSizeForItem(i); | |
| 154 } | |
| 155 }, | |
| 156 | 151 |
| 157 /** | 152 // Makes sure that the time gap separators are preserved. |
| 158 * If a day has had all the history it contains removed, remove this day from | 153 if (this.historyData[i].needsTimeGap && i > 0) { |
|
tsergeant
2016/02/11 02:43:02
Nit: Remove {}
yingran
2016/02/12 02:03:05
Done.
| |
| 159 * the array. | 154 this.set('historyData.' + parseInt(i - 1) + '.needsTimeGap', true); |
| 160 * @private | 155 } |
| 161 */ | 156 |
| 162 removeEmptyCards_: function() { | 157 // Removes the selected item from historyData. |
| 163 var historyCards = this.historyDataByDay_; | 158 this.splice('historyData', i, 1); |
| 164 for (var i = historyCards.length - 1; i >= 0; i--) { | 159 |
| 165 if (historyCards[i].historyItems.length == 0) { | 160 overallItemCount--; |
| 166 this.splice('historyDataByDay_', i, 1); | 161 if (overallItemCount == 0) { |
|
tsergeant
2016/02/11 02:43:02
{} here too
yingran
2016/02/12 02:03:05
Done.
| |
| 162 break; | |
| 167 } | 163 } |
| 168 } | 164 } |
| 169 }, | 165 }, |
| 170 | 166 |
| 171 /** | |
| 172 * Adds the given items into historyDataByDay_. Adds items to the last | |
| 173 * existing day if the date matches, creates a new element otherwise. | |
| 174 * @param {string} date The date of the history items. | |
| 175 * @param {!Array<!HistoryEntry>} historyItems The list of history items for | |
| 176 * the current date. | |
| 177 * @private | |
| 178 */ | |
| 179 appendHistoryData_: function(date, historyItems) { | |
| 180 var lastDay = this.historyDataByDay_.length - 1; | |
| 181 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { | |
| 182 this.set('historyDataByDay_.' + lastDay + '.historyItems', | |
| 183 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); | |
| 184 } else { | |
| 185 this.push('historyDataByDay_', { | |
| 186 date: date, | |
| 187 historyItems: historyItems | |
| 188 }); | |
| 189 } | |
| 190 }, | |
| 191 | |
| 192 /** | 167 /** |
| 193 * Based on which items are selected, collect an array of the info required | 168 * Based on which items are selected, collect an array of the info required |
| 194 * for chrome.send('removeHistory', ...). | 169 * for chrome.send('removeHistory', ...). |
| 195 * @param {number} count The number of items that are selected. | 170 * @param {number} count The number of items that are selected. |
| 196 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain | 171 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain |
| 197 * information on which history-items should be deleted. | 172 * information on which history-items should be deleted. |
| 198 */ | 173 */ |
| 199 getSelectedItems: function(count) { | 174 getSelectedItems: function(count) { |
| 200 var toBeRemoved = []; | 175 var toBeRemoved = []; |
| 201 for (var i = 0; i < this.historyDataByDay_.length; i++) { | 176 for (var i = 0; i < this.historyData.length; i++) { |
| 202 var items = this.historyDataByDay_[i].historyItems; | 177 if (this.historyData[i].selected) { |
| 203 for (var j = 0; j < items.length; j++) { | 178 toBeRemoved.push({ |
| 204 if (items[j].selected) { | 179 url: this.historyData[i].url, |
| 180 timestamps: this.historyData[i].allTimestamps | |
| 181 }); | |
| 205 | 182 |
| 206 toBeRemoved.push({ | 183 count--; |
| 207 url: items[j].url, | 184 if (count == 0) |
| 208 timestamps: items[j].allTimestamps | 185 return toBeRemoved; |
| 209 }); | |
| 210 | |
| 211 count--; | |
| 212 if (count == 0) | |
| 213 return toBeRemoved; | |
| 214 } | |
| 215 } | 186 } |
| 216 } | 187 } |
| 217 return toBeRemoved; | |
| 218 }, | 188 }, |
| 219 | 189 |
| 220 /** | 190 /** |
| 221 * Called when the card manager is scrolled. | 191 * Called when the card manager is scrolled. |
| 222 * @private | 192 * @private |
| 223 */ | 193 */ |
| 224 scrollHandler_: function() { | 194 scrollHandler_: function() { |
| 225 // Close overflow menu on scroll. | 195 // Close overflow menu on scroll. |
| 226 this.closeMenu(); | 196 this.closeMenu(); |
| 227 | 197 |
| 228 // Requests the next list of results when the scrollbar is near the bottom | 198 // Requests the next list of results when the scrollbar is near the bottom |
| 229 // of the window. | 199 // of the window. |
| 230 var scrollOffset = 10; | 200 var scrollOffset = 10; |
| 231 var scrollElem = this.$['infinite-list']; | 201 var scrollElem = this.$['infinite-list']; |
| 232 | 202 |
| 233 if (scrollElem.scrollHeight <= | 203 if (scrollElem.scrollHeight <= |
| 234 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 204 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 235 chrome.send('queryHistory', | 205 chrome.send('queryHistory', |
| 236 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 206 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 237 } | 207 } |
| 208 }, | |
| 209 | |
| 210 /** | |
| 211 * Check whether the time difference between the given history item and the | |
| 212 * next one is large enough for a spacer to be required. | |
| 213 * @param {Array<HistoryEntry>} results A list of history results. | |
| 214 * @param {number} index The index number of the first item being compared. | |
| 215 * @return {boolean} Whether or not time gap separator is required. | |
| 216 * @private | |
| 217 */ | |
| 218 needsTimeGap_: function(results, index) { | |
| 219 var currentItem = results[index]; | |
| 220 var nextItem = results[index + 1]; | |
| 221 | |
| 222 if (index + 1 >= results.length) | |
| 223 return false; | |
| 224 | |
| 225 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && | |
| 226 currentItem.dateRelativeDay == nextItem.dateRelativeDay; | |
| 238 } | 227 } |
| 239 }); | 228 }); |
| OLD | NEW |