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. Preserves the scroll position of the window when new data |
| 74 * is loaded. | |
| 75 * @param {!Array<!HistoryEntry>} results The new history results. | 75 * @param {!Array<!HistoryEntry>} results The new history results. |
| 76 */ | 76 */ |
| 77 addNewResults: function(results) { | 77 addNewResults: function(results) { |
| 78 if (results.length == 0) | 78 if (results.length == 0) |
| 79 return; | 79 return; |
| 80 | 80 |
| 81 var dateSortedData = []; | 81 var scrollPosition = this.$['infinite-list'].scrollTop; |
|
tsergeant
2016/02/02 23:47:33
The scroll position stuff had been removed previou
yingran
2016/02/04 02:13:42
yep, otherwise, it just goes back all the way to t
| |
| 82 var historyItems = []; | 82 // If it's the first time we get data, the first item will always be the |
| 83 // first card. | |
| 84 if (this.historyData.length == 0) { | |
| 85 results[0].isFirstItem = true; | |
| 86 } | |
| 87 | |
| 83 var currentDate = results[0].dateRelativeDay; | 88 var currentDate = results[0].dateRelativeDay; |
| 84 | 89 |
| 85 for (var i = 0; i < results.length; i++) { | 90 for (var i = 0; i < results.length; i++) { |
| 86 if (!currentDate) | |
| 87 continue; | |
| 88 | |
| 89 results[i].selected = false; | 91 results[i].selected = false; |
| 90 if (results[i].dateRelativeDay != currentDate) { | 92 if (results[i].dateRelativeDay != currentDate) { |
| 91 this.appendHistoryData_(currentDate, historyItems); | 93 results[i - 1].isLastItem = true; |
| 94 results[i].isFirstItem = true; | |
| 92 currentDate = results[i].dateRelativeDay; | 95 currentDate = results[i].dateRelativeDay; |
| 93 historyItems = []; | 96 } else { |
| 97 results[i].isLastItem = results[i].isLastItem || false; | |
| 98 results[i].isFirstItem = results[i].isFirstItem || false; | |
|
tsergeant
2016/02/02 23:47:33
This is a slightly confusing way to do this.
One
yingran
2016/02/04 02:13:42
Not sure about the second point, but tried the fir
| |
| 94 } | 99 } |
| 95 historyItems.push(results[i]); | 100 results[i].needsTimeGap = this.needsTimeGap_(results, i); |
| 96 } | 101 } |
| 97 | 102 |
| 98 if (currentDate) | 103 this.set('historyData', this.historyData.concat(results)); |
| 99 this.appendHistoryData_(currentDate, historyItems); | 104 this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; |
| 100 | 105 this.$['infinite-list'].scrollTop = scrollPosition; |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | |
| 102 }, | 106 }, |
| 103 | 107 |
| 104 /** | 108 /** |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | 109 * Cycle through each entry in historyData and set all items to be |
| 106 * unselected. | 110 * unselected. |
| 111 * @param {number} overallItemCount The number of checkboxes selected. | |
| 107 */ | 112 */ |
| 108 unselectAllItems: function(overallItemCount) { | 113 unselectAllItems: function(overallItemCount) { |
| 109 var historyCardData = this.historyDataByDay_; | 114 for (var i = 0; i < this.historyData.length; i++) { |
| 110 | 115 if (this.historyData[i].selected) { |
| 111 for (var i = 0; i < historyCardData.length; i++) { | 116 this.set('historyData.' + i + '.selected', false); |
| 112 var items = historyCardData[i].historyItems; | 117 overallItemCount--; |
| 113 for (var j = 0; j < items.length; j++) { | 118 if (overallItemCount == 0) |
| 114 if (items[j].selected) { | 119 break; |
| 115 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | |
| 116 '.selected', false); | |
| 117 overallItemCount--; | |
| 118 if (overallItemCount == 0) | |
| 119 break; | |
| 120 } | |
| 121 } | 120 } |
| 122 } | 121 } |
| 123 }, | 122 }, |
| 124 | 123 |
| 125 /** | 124 /** |
| 126 * Adds the given items into historyDataByDay_. Adds items to the last | |
| 127 * existing day if the date matches, creates a new element otherwise. | |
| 128 * @param {string} date The date of the history items. | |
| 129 * @param {!Array<!HistoryEntry>} historyItems The list of history items for | |
| 130 * the current date. | |
| 131 * @private | |
| 132 */ | |
| 133 appendHistoryData_: function(date, historyItems) { | |
| 134 var lastDay = this.historyDataByDay_.length - 1; | |
| 135 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { | |
| 136 this.set('historyDataByDay_.' + lastDay + '.historyItems', | |
| 137 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); | |
| 138 } else { | |
| 139 this.push('historyDataByDay_', { | |
| 140 date: date, | |
| 141 historyItems: historyItems | |
| 142 }); | |
| 143 } | |
| 144 }, | |
| 145 | |
| 146 /** | |
| 147 * Called when the card manager is scrolled. | 125 * Called when the card manager is scrolled. |
| 148 * @private | 126 * @private |
| 149 */ | 127 */ |
| 150 scrollHandler_: function() { | 128 scrollHandler_: function() { |
| 151 // Close overflow menu on scroll. | 129 // Close overflow menu on scroll. |
| 152 this.closeMenu(); | 130 this.closeMenu(); |
| 153 | 131 |
| 154 // Requests the next list of results when the scrollbar is near the bottom | 132 // Requests the next list of results when the scrollbar is near the bottom |
| 155 // of the window. | 133 // of the window. |
| 156 var scrollOffset = 10; | 134 var scrollOffset = 10; |
| 157 var scrollElem = this.$['infinite-list']; | 135 var scrollElem = this.$['infinite-list']; |
| 158 | 136 |
| 159 if (scrollElem.scrollHeight <= | 137 if (scrollElem.scrollHeight <= |
| 160 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 138 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 161 chrome.send('queryHistory', | 139 chrome.send('queryHistory', |
| 162 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 140 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 163 } | 141 } |
| 142 }, | |
| 143 | |
| 144 /** | |
| 145 * Check whether the time difference between the given history item and the | |
| 146 * next one is large enough for a spacer to be required. | |
| 147 * @param {number} index The index number of the first item being compared. | |
| 148 * @return {boolean} Whether or not time gap separator is required. | |
| 149 * @private | |
| 150 */ | |
| 151 needsTimeGap_: function(results, index) { | |
| 152 var items = results; | |
| 153 return index + 1 < items.length && | |
| 154 items[index].time - items[index + 1].time > BROWSING_GAP_TIME && | |
| 155 items[index].dateRelativeDay == items[index + 1].dateRelativeDay; | |
| 164 } | 156 } |
| 165 }); | 157 }); |
| OLD | NEW |