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>} results The new history results. |
| 76 */ | 75 */ |
| 77 addNewResults: function(results) { | 76 addNewResults: function(results) { |
| 78 if (results.length == 0) | 77 if (results.length == 0) |
| 79 return; | 78 return; |
| 80 | 79 |
| 81 var dateSortedData = []; | |
| 82 var historyItems = []; | |
| 83 var currentDate = results[0].dateRelativeDay; | 80 var currentDate = results[0].dateRelativeDay; |
| 84 | 81 |
| 85 for (var i = 0; i < results.length; i++) { | 82 for (var i = 0; i < results.length; i++) { |
| 86 if (!currentDate) | 83 // Sets the default values for these fields to prevent undefined types. |
| 87 continue; | 84 results[i].selected = false; |
| 85 results[i].isLastItem = false; | |
| 86 results[i].isFirstItem = false; | |
| 88 | 87 |
| 89 results[i].selected = false; | |
| 90 if (results[i].dateRelativeDay != currentDate) { | 88 if (results[i].dateRelativeDay != currentDate) { |
| 91 this.appendHistoryData_(currentDate, historyItems); | 89 results[i - 1].isLastItem = true; |
| 90 results[i].isFirstItem = true; | |
| 92 currentDate = results[i].dateRelativeDay; | 91 currentDate = results[i].dateRelativeDay; |
| 93 historyItems = []; | |
| 94 } | 92 } |
| 95 historyItems.push(results[i]); | 93 results[i].needsTimeGap = this.needsTimeGap_(results, i); |
| 96 } | 94 } |
| 97 | 95 |
| 98 if (currentDate) | 96 // If it's the first time we get data, the first item will always be the |
| 99 this.appendHistoryData_(currentDate, historyItems); | 97 // first card. |
| 98 if (this.historyData.length == 0) | |
| 99 results[0].isFirstItem = true; | |
| 100 | 100 |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | 101 results.unshift('historyData'); |
|
tsergeant
2016/02/08 03:44:51
Would be good to have a comment here explaining wh
calamity
2016/02/09 12:25:29
It's safer to slice the results at the beginning i
yingran
2016/02/11 00:41:42
Done.
yingran
2016/02/11 00:41:42
Done.
| |
| 102 this.push.apply(this, results); | |
|
tsergeant
2016/02/08 03:44:51
Should use `Polymer.Base.push.apply`
(Right, Chri
Dan Beam
2016/02/08 23:50:47
no, that's only if you're @override-ing push() you
| |
| 103 | |
| 104 this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; | |
| 102 }, | 105 }, |
| 103 | 106 |
| 104 /** | 107 /** |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | 108 * Cycle through each entry in historyData and set all items to be |
| 106 * unselected. | 109 * unselected. |
| 110 * @param {number} overallItemCount The number of checkboxes selected. | |
| 107 */ | 111 */ |
| 108 unselectAllItems: function(overallItemCount) { | 112 unselectAllItems: function(overallItemCount) { |
| 109 var historyCardData = this.historyDataByDay_; | 113 for (var i = 0; i < this.historyData.length; i++) { |
| 110 | 114 if (this.historyData[i].selected) { |
| 111 for (var i = 0; i < historyCardData.length; i++) { | 115 this.set('historyData.' + i + '.selected', false); |
| 112 var items = historyCardData[i].historyItems; | 116 overallItemCount--; |
| 113 for (var j = 0; j < items.length; j++) { | 117 if (overallItemCount == 0) |
| 114 if (items[j].selected) { | 118 break; |
| 115 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | |
| 116 '.selected', false); | |
| 117 overallItemCount--; | |
| 118 if (overallItemCount == 0) | |
| 119 break; | |
| 120 } | |
| 121 } | 119 } |
| 122 } | 120 } |
| 123 }, | 121 }, |
| 124 | 122 |
| 125 /** | 123 /** |
| 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. | 124 * Called when the card manager is scrolled. |
| 148 * @private | 125 * @private |
| 149 */ | 126 */ |
| 150 scrollHandler_: function() { | 127 scrollHandler_: function() { |
| 151 // Close overflow menu on scroll. | 128 // Close overflow menu on scroll. |
| 152 this.closeMenu(); | 129 this.closeMenu(); |
| 153 | 130 |
| 154 // Requests the next list of results when the scrollbar is near the bottom | 131 // Requests the next list of results when the scrollbar is near the bottom |
| 155 // of the window. | 132 // of the window. |
| 156 var scrollOffset = 10; | 133 var scrollOffset = 10; |
| 157 var scrollElem = this.$['infinite-list']; | 134 var scrollElem = this.$['infinite-list']; |
| 158 | 135 |
| 159 if (scrollElem.scrollHeight <= | 136 if (scrollElem.scrollHeight <= |
| 160 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 137 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 161 chrome.send('queryHistory', | 138 chrome.send('queryHistory', |
| 162 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 139 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 163 } | 140 } |
| 141 }, | |
| 142 | |
| 143 /** | |
| 144 * Check whether the time difference between the given history item and the | |
| 145 * next one is large enough for a spacer to be required. | |
| 146 * @param {number} index The index number of the first item being compared. | |
|
calamity
2016/02/09 12:25:29
Missing a param.
yingran
2016/02/11 00:41:42
Done.
| |
| 147 * @return {boolean} Whether or not time gap separator is required. | |
| 148 * @private | |
| 149 */ | |
| 150 needsTimeGap_: function(results, index) { | |
| 151 var items = results; | |
|
calamity
2016/02/09 12:25:29
Can we just remove this..?
Actually, on second th
yingran
2016/02/11 00:41:42
Done.
| |
| 152 return index + 1 < items.length && | |
| 153 items[index].time - items[index + 1].time > BROWSING_GAP_TIME && | |
| 154 items[index].dateRelativeDay == items[index + 1].dateRelativeDay; | |
| 164 } | 155 } |
| 165 }); | 156 }); |
| OLD | NEW |