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 of access 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 this |
| 82 var historyItems = []; | 81 // field. |
| 82 var results = historyResults.slice(); | |
| 83 | |
| 83 var currentDate = results[0].dateRelativeDay; | 84 var currentDate = results[0].dateRelativeDay; |
| 84 | 85 |
| 85 for (var i = 0; i < results.length; i++) { | 86 // Resets the last history item for the currentDate if new history results |
| 86 if (!currentDate) | 87 // for currentDate is loaded. |
| 87 continue; | 88 var lastHistoryItem = this.historyData[this.historyData.length - 1]; |
| 88 | 89 if (lastHistoryItem && lastHistoryItem.isLastItem && |
| 89 results[i].selected = false; | 90 lastHistoryItem.dateRelativeDay == currentDate) { |
| 90 if (results[i].dateRelativeDay != currentDate) { | 91 this.set('historyData.' + (this.historyData.length - 1) + |
| 91 this.appendHistoryData_(currentDate, historyItems); | 92 '.isLastItem', false); |
| 92 currentDate = results[i].dateRelativeDay; | |
| 93 historyItems = []; | |
| 94 } | |
| 95 historyItems.push(results[i]); | |
| 96 } | 93 } |
| 97 | 94 |
| 98 if (currentDate) | 95 for (var i = 0; i < results.length; i++) { |
| 99 this.appendHistoryData_(currentDate, historyItems); | 96 // Sets the default values for these fields to prevent undefined types. |
| 97 results[i].selected = false; | |
| 98 results[i].isLastItem = false; | |
| 99 results[i].isFirstItem = false; | |
| 100 results[i].needsTimeGap = this.needsTimeGap_(results, i); | |
| 100 | 101 |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | 102 if (results[i].dateRelativeDay != currentDate) { |
| 103 results[i - 1].isLastItem = true; | |
| 104 results[i].isFirstItem = true; | |
| 105 currentDate = results[i].dateRelativeDay; | |
| 106 } | |
| 107 } | |
| 108 results[i - 1].isLastItem = true; | |
| 109 | |
| 110 // If it's the first time we get data, the first item will always be the | |
| 111 // first card. | |
| 112 if (this.historyData.length == 0) | |
| 113 results[0].isFirstItem = true; | |
| 114 | |
| 115 // Adds results to the beginning of the historyData array. | |
| 116 results.unshift('historyData'); | |
| 117 this.push.apply(this, results); | |
| 118 | |
| 119 this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; | |
| 102 }, | 120 }, |
| 103 | 121 |
| 104 /** | 122 /** |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | 123 * Cycle through each entry in historyData and set all items to be |
| 106 * unselected. | 124 * unselected. |
| 107 * @param {number} overallItemCount The number of items selected. | 125 * @param {number} overallItemCount The number of checkboxes selected. |
| 108 */ | 126 */ |
| 109 unselectAllItems: function(overallItemCount) { | 127 unselectAllItems: function(overallItemCount) { |
| 110 var historyCardData = this.historyDataByDay_; | 128 for (var i = 0; i < this.historyData.length; i++) { |
| 111 | 129 if (this.historyData[i].selected) { |
| 112 for (var i = 0; i < historyCardData.length; i++) { | 130 this.set('historyData.' + i + '.selected', false); |
| 113 var items = historyCardData[i].historyItems; | 131 overallItemCount--; |
| 114 for (var j = 0; j < items.length; j++) { | 132 if (overallItemCount == 0) |
| 115 if (items[j].selected) { | 133 break; |
| 116 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | |
| 117 '.selected', false); | |
| 118 overallItemCount--; | |
| 119 if (overallItemCount == 0) | |
| 120 return; | |
| 121 } | |
| 122 } | 134 } |
| 123 } | 135 } |
| 124 }, | 136 }, |
| 125 | 137 |
| 126 /** | 138 /** |
| 127 * Remove all selected items from the overall array so that they are also | 139 * 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 | 140 * removed from view. Make sure that the card length and positioning is |
| 129 * updated accordingly. | 141 * updated accordingly. |
| 130 * @param {number} overallItemCount The number of items selected. | 142 * @param {number} overallItemCount The number of items selected. |
| 131 */ | 143 */ |
| 132 removeDeletedHistory: function(overallItemCount) { | 144 removeDeletedHistory: function(overallItemCount) { |
| 133 var infiniteList = /** @type {IronListElement} */(this.$['infinite-list']); | 145 for (var i = this.historyData.length - 1; i >= 0; i--) { |
| 134 for (var i = 0; i < this.historyDataByDay_.length; i++) { | 146 if (!this.historyData[i].selected) |
| 135 var items = this.historyDataByDay_[i].historyItems; | 147 continue; |
| 136 var itemDeletedFromCard = false; | |
| 137 | 148 |
| 138 for (var j = items.length - 1; j >= 0; j--) { | 149 // TODO: Change to using computed properties to recompute the first and |
| 139 if (!items[j].selected) | 150 // last cards. |
| 140 continue; | |
| 141 | 151 |
| 142 this.splice('historyDataByDay_.' + i + '.historyItems', j, 1); | 152 // Resets the first history item. |
| 143 itemDeletedFromCard = true; | 153 if (this.historyData[i].isFirstItem && |
| 144 overallItemCount--; | 154 (i + 1) < this.historyData.length && |
| 145 if (overallItemCount == 0) { | 155 this.historyData[i].dateRelativeDay == |
| 146 this.removeEmptyCards_(); | 156 this.historyData[i + 1].dateRelativeDay) { |
| 147 // If the last card has been removed don't try to update its size. | 157 this.set('historyData.' + (i + 1) + '.isFirstItem', true); |
| 148 if (i < this.historyDataByDay_.length) | 158 } |
| 149 infiniteList.updateSizeForItem(i); | 159 |
| 150 return; | 160 // Resets the last history item. |
| 161 if (this.historyData[i].isLastItem && i > 0 && | |
| 162 this.historyData[i].dateRelativeDay == | |
| 163 this.historyData[i - 1].dateRelativeDay) { | |
| 164 this.set('historyData.' + (i - 1) + '.isLastItem', true); | |
| 165 | |
| 166 if (this.historyData[i - 1].needsTimeGap) { | |
|
tsergeant
2016/02/12 04:14:30
Nit: {}
yingran
2016/02/12 04:37:43
Done.
| |
| 167 this.set('historyData.' + (i - 1) + '.needsTimeGap', false); | |
| 151 } | 168 } |
| 152 } | 169 } |
| 153 if (itemDeletedFromCard) | 170 |
| 154 infiniteList.updateSizeForItem(i); | 171 // Makes sure that the time gap separators are preserved. |
| 172 if (this.historyData[i].needsTimeGap && i > 0) | |
| 173 this.set('historyData.' + (i - 1) + '.needsTimeGap', true); | |
| 174 | |
| 175 // Removes the selected item from historyData. | |
| 176 this.splice('historyData', i, 1); | |
| 177 | |
| 178 overallItemCount--; | |
| 179 if (overallItemCount == 0) | |
| 180 break; | |
| 155 } | 181 } |
| 156 }, | 182 }, |
| 157 | 183 |
| 158 /** | |
| 159 * If a day has had all the history it contains removed, remove this day from | |
| 160 * the array. | |
| 161 * @private | |
| 162 */ | |
| 163 removeEmptyCards_: function() { | |
| 164 var historyCards = this.historyDataByDay_; | |
| 165 for (var i = historyCards.length - 1; i >= 0; i--) { | |
| 166 if (historyCards[i].historyItems.length == 0) { | |
| 167 this.splice('historyDataByDay_', i, 1); | |
| 168 } | |
| 169 } | |
| 170 }, | |
| 171 | |
| 172 /** | |
| 173 * Adds the given items into historyDataByDay_. Adds items to the last | |
| 174 * existing day if the date matches, creates a new element otherwise. | |
| 175 * @param {string} date The date of the history items. | |
| 176 * @param {!Array<!HistoryEntry>} historyItems The list of history items for | |
| 177 * the current date. | |
| 178 * @private | |
| 179 */ | |
| 180 appendHistoryData_: function(date, historyItems) { | |
| 181 var lastDay = this.historyDataByDay_.length - 1; | |
| 182 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { | |
| 183 this.set('historyDataByDay_.' + lastDay + '.historyItems', | |
| 184 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); | |
| 185 } else { | |
| 186 this.push('historyDataByDay_', { | |
| 187 date: date, | |
| 188 historyItems: historyItems | |
| 189 }); | |
| 190 } | |
| 191 }, | |
| 192 | |
| 193 /** | 184 /** |
| 194 * Based on which items are selected, collect an array of the info required | 185 * Based on which items are selected, collect an array of the info required |
| 195 * for chrome.send('removeHistory', ...). | 186 * for chrome.send('removeHistory', ...). |
| 196 * @param {number} count The number of items that are selected. | 187 * @param {number} count The number of items that are selected. |
| 197 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain | 188 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain |
| 198 * information on which history-items should be deleted. | 189 * information on which history-items should be deleted. |
| 199 */ | 190 */ |
| 200 getSelectedItems: function(count) { | 191 getSelectedItems: function(count) { |
| 201 var toBeRemoved = []; | 192 var toBeRemoved = []; |
| 202 for (var i = 0; i < this.historyDataByDay_.length; i++) { | 193 for (var i = 0; i < this.historyData.length; i++) { |
| 203 var items = this.historyDataByDay_[i].historyItems; | 194 if (this.historyData[i].selected) { |
| 204 for (var j = 0; j < items.length; j++) { | 195 toBeRemoved.push({ |
| 205 if (items[j].selected) { | 196 url: this.historyData[i].url, |
| 197 timestamps: this.historyData[i].allTimestamps | |
| 198 }); | |
| 206 | 199 |
| 207 toBeRemoved.push({ | 200 count--; |
| 208 url: items[j].url, | 201 if (count == 0) |
| 209 timestamps: items[j].allTimestamps | 202 break; |
| 210 }); | |
| 211 | |
| 212 count--; | |
| 213 if (count == 0) | |
| 214 return toBeRemoved; | |
| 215 } | |
| 216 } | 203 } |
| 217 } | 204 } |
| 218 return toBeRemoved; | 205 return toBeRemoved; |
| 219 }, | 206 }, |
| 220 | 207 |
| 221 /** | 208 /** |
| 222 * Called when the card manager is scrolled. | 209 * Called when the card manager is scrolled. |
| 223 * @private | 210 * @private |
| 224 */ | 211 */ |
| 225 scrollHandler_: function() { | 212 scrollHandler_: function() { |
| 226 // Close overflow menu on scroll. | 213 // Close overflow menu on scroll. |
| 227 this.closeMenu(); | 214 this.closeMenu(); |
| 228 | 215 |
| 229 // Requests the next list of results when the scrollbar is near the bottom | 216 // Requests the next list of results when the scrollbar is near the bottom |
| 230 // of the window. | 217 // of the window. |
| 231 var scrollOffset = 10; | 218 var scrollOffset = 10; |
| 232 var scrollElem = this.$['infinite-list']; | 219 var scrollElem = this.$['infinite-list']; |
| 233 | 220 |
| 234 if (scrollElem.scrollHeight <= | 221 if (scrollElem.scrollHeight <= |
| 235 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 222 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 236 chrome.send('queryHistory', | 223 chrome.send('queryHistory', |
| 237 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 224 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 238 } | 225 } |
| 226 }, | |
| 227 | |
| 228 /** | |
| 229 * Check whether the time difference between the given history item and the | |
| 230 * next one is large enough for a spacer to be required. | |
| 231 * @param {Array<HistoryEntry>} results A list of history results. | |
| 232 * @param {number} index The index number of the first item being compared. | |
| 233 * @return {boolean} Whether or not time gap separator is required. | |
| 234 * @private | |
| 235 */ | |
| 236 needsTimeGap_: function(results, index) { | |
| 237 var currentItem = results[index]; | |
| 238 var nextItem = results[index + 1]; | |
| 239 | |
| 240 if (index + 1 >= results.length) | |
| 241 return false; | |
| 242 | |
| 243 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && | |
| 244 currentItem.dateRelativeDay == nextItem.dateRelativeDay; | |
| 239 } | 245 } |
| 240 }); | 246 }); |
| OLD | NEW |