Chromium Code Reviews| Index: chrome/browser/resources/md_history/history_list.js |
| diff --git a/chrome/browser/resources/md_history/history_card_manager.js b/chrome/browser/resources/md_history/history_list.js |
| similarity index 46% |
| rename from chrome/browser/resources/md_history/history_card_manager.js |
| rename to chrome/browser/resources/md_history/history_list.js |
| index 99225473f348c2a32338717022a36c4ca5ec6f37..b1dba8fc3b6e65ef3c8d163568634e2efd18032d 100644 |
| --- a/chrome/browser/resources/md_history/history_card_manager.js |
| +++ b/chrome/browser/resources/md_history/history_list.js |
| @@ -3,17 +3,16 @@ |
| // found in the LICENSE file. |
| Polymer({ |
| - is: 'history-card-manager', |
| + is: 'history-list', |
| properties: { |
| - // An array of objects sorted in reverse chronological order. |
| - // Each object has a date and the history items belonging to that date. |
| - historyDataByDay_: { |
| + // An array of history entries in reverse chronological order. |
| + historyData: { |
| type: Array, |
| value: function() { return []; } |
| }, |
| - // The time of access of the last element of historyDataByDay_. |
| + // The time in seconds of the last history item in historyData. |
| lastVisitedTime: { |
| type: Number, |
| value: 0 |
| @@ -70,55 +69,57 @@ Polymer({ |
| }, |
| /** |
| - * Split the newly updated history results into history items sorted via day |
| - * accessed. |
| - * @param {!Array<!HistoryEntry>} results The new history results. |
| + * Adds the newly updated history results into historyData. Adds new fields |
| + * for each result. |
| + * @param {!Array<!HistoryEntry>} historyResults The new history results. |
| */ |
| - addNewResults: function(results) { |
| - if (results.length == 0) |
| + addNewResults: function(historyResults) { |
| + if (historyResults.length == 0) |
| return; |
| - var dateSortedData = []; |
| - var historyItems = []; |
| - var currentDate = results[0].dateRelativeDay; |
| + // 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.
|
| + // this field. |
| + var results = historyResults.slice(); |
| + var currentDate = results[0].dateRelativeDay; |
| for (var i = 0; i < results.length; i++) { |
| - if (!currentDate) |
| - continue; |
| - |
| + // Sets the default values for these fields to prevent undefined types. |
| results[i].selected = false; |
| + results[i].isLastItem = false; |
| + results[i].isFirstItem = false; |
| + |
| if (results[i].dateRelativeDay != currentDate) { |
| - this.appendHistoryData_(currentDate, historyItems); |
| + results[i - 1].isLastItem = true; |
| + results[i].isFirstItem = true; |
| currentDate = results[i].dateRelativeDay; |
| - historyItems = []; |
| } |
| - historyItems.push(results[i]); |
| + results[i].needsTimeGap = this.needsTimeGap_(results, i); |
| } |
| - if (currentDate) |
| - this.appendHistoryData_(currentDate, historyItems); |
| + // If it's the first time we get data, the first item will always be the |
| + // first card. |
| + if (this.historyData.length == 0) |
| + results[0].isFirstItem = true; |
| + |
| + // Adds results to the beginning of the historyData array. |
| + results.unshift('historyData'); |
| + this.push.apply(this, results); |
| - this.lastVisitedTime = historyItems[historyItems.length - 1].time; |
| + this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; |
| }, |
| /** |
| - * Cycle through each entry in historyDataByDay_ and set all items to be |
| + * Cycle through each entry in historyData and set all items to be |
| * unselected. |
| - * @param {number} overallItemCount The number of items selected. |
| + * @param {number} overallItemCount The number of checkboxes selected. |
| */ |
| unselectAllItems: function(overallItemCount) { |
| - var historyCardData = this.historyDataByDay_; |
| - |
| - for (var i = 0; i < historyCardData.length; i++) { |
| - var items = historyCardData[i].historyItems; |
| - for (var j = 0; j < items.length; j++) { |
| - if (items[j].selected) { |
| - this.set('historyDataByDay_.' + i + '.historyItems.' + j + |
| - '.selected', false); |
| - overallItemCount--; |
| - if (overallItemCount == 0) |
| - return; |
| - } |
| + for (var i = 0; i < this.historyData.length; i++) { |
| + if (this.historyData[i].selected) { |
| + this.set('historyData.' + i + '.selected', false); |
| + overallItemCount--; |
| + if (overallItemCount == 0) |
| + break; |
| } |
| } |
| }, |
| @@ -130,62 +131,36 @@ Polymer({ |
| * @param {number} overallItemCount The number of items selected. |
| */ |
| removeDeletedHistory: function(overallItemCount) { |
| - for (var i = 0; i < this.historyDataByDay_.length; i++) { |
| - var items = this.historyDataByDay_[i].historyItems; |
| - var itemDeletedFromCard = false; |
| + for (var i = this.historyData.length - 1; i >= 0; i--) { |
| + if (!this.historyData[i].selected) |
| + continue; |
| - for (var j = items.length - 1; j >= 0; j--) { |
| - if (!items[j].selected) |
| - continue; |
| + // 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.
|
| + if (this.historyData[i].isFirstItem && |
| + this.historyData[i].dateRelativeDay == |
| + this.historyData[i + 1].dateRelativeDay) { |
| + this.set('historyData.' + parseInt(i + 1) + '.isFirstItem', true); |
| + } |
| - this.splice('historyDataByDay_.' + i + '.historyItems', j, 1); |
| - itemDeletedFromCard = true; |
| - overallItemCount--; |
| - if (overallItemCount == 0) { |
| - this.removeEmptyCards_(); |
| - // If the last card has been removed don't try to update its size. |
| - if (i < this.historyDataByDay_.length) |
| - this.$['infinite-list'].updateSizeForItem(i); |
| - return; |
| - } |
| + // Resets the last history item. |
| + if (this.historyData[i].isLastItem && |
| + this.historyData[i].dateRelativeDay == |
| + this.historyData[i - 1].dateRelativeDay) { |
| + this.set('historyData.' + parseInt(i - 1) + '.isLastItem', true); |
| } |
| - if (itemDeletedFromCard) |
| - this.$['infinite-list'].updateSizeForItem(i); |
| - } |
| - }, |
| - /** |
| - * If a day has had all the history it contains removed, remove this day from |
| - * the array. |
| - * @private |
| - */ |
| - removeEmptyCards_: function() { |
| - var historyCards = this.historyDataByDay_; |
| - for (var i = historyCards.length - 1; i >= 0; i--) { |
| - if (historyCards[i].historyItems.length == 0) { |
| - this.splice('historyDataByDay_', i, 1); |
| + // Makes sure that the time gap separators are preserved. |
| + if (this.historyData[i].needsTimeGap && i > 0) { |
|
tsergeant
2016/02/11 02:43:02
Nit: Remove {}
yingran
2016/02/12 02:03:05
Done.
|
| + this.set('historyData.' + parseInt(i - 1) + '.needsTimeGap', true); |
| } |
| - } |
| - }, |
| - /** |
| - * Adds the given items into historyDataByDay_. Adds items to the last |
| - * existing day if the date matches, creates a new element otherwise. |
| - * @param {string} date The date of the history items. |
| - * @param {!Array<!HistoryEntry>} historyItems The list of history items for |
| - * the current date. |
| - * @private |
| - */ |
| - appendHistoryData_: function(date, historyItems) { |
| - var lastDay = this.historyDataByDay_.length - 1; |
| - if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { |
| - this.set('historyDataByDay_.' + lastDay + '.historyItems', |
| - this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); |
| - } else { |
| - this.push('historyDataByDay_', { |
| - date: date, |
| - historyItems: historyItems |
| - }); |
| + // Removes the selected item from historyData. |
| + this.splice('historyData', i, 1); |
| + |
| + overallItemCount--; |
| + if (overallItemCount == 0) { |
|
tsergeant
2016/02/11 02:43:02
{} here too
yingran
2016/02/12 02:03:05
Done.
|
| + break; |
| + } |
| } |
| }, |
| @@ -198,23 +173,18 @@ Polymer({ |
| */ |
| getSelectedItems: function(count) { |
| var toBeRemoved = []; |
| - for (var i = 0; i < this.historyDataByDay_.length; i++) { |
| - var items = this.historyDataByDay_[i].historyItems; |
| - for (var j = 0; j < items.length; j++) { |
| - if (items[j].selected) { |
| - |
| - toBeRemoved.push({ |
| - url: items[j].url, |
| - timestamps: items[j].allTimestamps |
| - }); |
| - |
| - count--; |
| - if (count == 0) |
| - return toBeRemoved; |
| - } |
| + for (var i = 0; i < this.historyData.length; i++) { |
| + if (this.historyData[i].selected) { |
| + toBeRemoved.push({ |
| + url: this.historyData[i].url, |
| + timestamps: this.historyData[i].allTimestamps |
| + }); |
| + |
| + count--; |
| + if (count == 0) |
| + return toBeRemoved; |
| } |
| } |
| - return toBeRemoved; |
| }, |
| /** |
| @@ -235,5 +205,24 @@ Polymer({ |
| chrome.send('queryHistory', |
| ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| } |
| + }, |
| + |
| + /** |
| + * Check whether the time difference between the given history item and the |
| + * next one is large enough for a spacer to be required. |
| + * @param {Array<HistoryEntry>} results A list of history results. |
| + * @param {number} index The index number of the first item being compared. |
| + * @return {boolean} Whether or not time gap separator is required. |
| + * @private |
| + */ |
| + needsTimeGap_: function(results, index) { |
| + var currentItem = results[index]; |
| + var nextItem = results[index + 1]; |
| + |
| + if (index + 1 >= results.length) |
| + return false; |
| + |
| + return currentItem.time - nextItem.time > BROWSING_GAP_TIME && |
| + currentItem.dateRelativeDay == nextItem.dateRelativeDay; |
| } |
| }); |