Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(638)

Side by Side Diff: chrome/browser/resources/md_history/history_list.js

Issue 1641543002: MD History: Refactored design for displaying history information (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@second_patch
Patch Set: closure Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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)
149 infiniteList.updateSizeForItem(i);
150 return;
151 }
152 } 158 }
153 if (itemDeletedFromCard) 159
154 infiniteList.updateSizeForItem(i); 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)
167 this.set('historyData.' + (i - 1) + '.needsTimeGap', false);
168 }
169
170 // Makes sure that the time gap separators are preserved.
171 if (this.historyData[i].needsTimeGap && i > 0)
172 this.set('historyData.' + (i - 1) + '.needsTimeGap', true);
173
174 // Removes the selected item from historyData.
175 this.splice('historyData', i, 1);
176
177 overallItemCount--;
178 if (overallItemCount == 0)
179 break;
155 } 180 }
156 }, 181 },
157 182
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 /** 183 /**
194 * Based on which items are selected, collect an array of the info required 184 * Based on which items are selected, collect an array of the info required
195 * for chrome.send('removeHistory', ...). 185 * for chrome.send('removeHistory', ...).
196 * @param {number} count The number of items that are selected. 186 * @param {number} count The number of items that are selected.
197 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain 187 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain
198 * information on which history-items should be deleted. 188 * information on which history-items should be deleted.
199 */ 189 */
200 getSelectedItems: function(count) { 190 getSelectedItems: function(count) {
201 var toBeRemoved = []; 191 var toBeRemoved = [];
202 for (var i = 0; i < this.historyDataByDay_.length; i++) { 192 for (var i = 0; i < this.historyData.length; i++) {
203 var items = this.historyDataByDay_[i].historyItems; 193 if (this.historyData[i].selected) {
204 for (var j = 0; j < items.length; j++) { 194 toBeRemoved.push({
205 if (items[j].selected) { 195 url: this.historyData[i].url,
196 timestamps: this.historyData[i].allTimestamps
197 });
206 198
207 toBeRemoved.push({ 199 count--;
208 url: items[j].url, 200 if (count == 0)
209 timestamps: items[j].allTimestamps 201 break;
210 });
211
212 count--;
213 if (count == 0)
214 return toBeRemoved;
215 }
216 } 202 }
217 } 203 }
218 return toBeRemoved; 204 return toBeRemoved;
219 }, 205 },
220 206
221 /** 207 /**
222 * Called when the card manager is scrolled. 208 * Called when the card manager is scrolled.
223 * @private 209 * @private
224 */ 210 */
225 scrollHandler_: function() { 211 scrollHandler_: function() {
226 // Close overflow menu on scroll. 212 // Close overflow menu on scroll.
227 this.closeMenu(); 213 this.closeMenu();
228 214
229 // Requests the next list of results when the scrollbar is near the bottom 215 // Requests the next list of results when the scrollbar is near the bottom
230 // of the window. 216 // of the window.
231 var scrollOffset = 10; 217 var scrollOffset = 10;
232 var scrollElem = this.$['infinite-list']; 218 var scrollElem = this.$['infinite-list'];
233 219
234 if (scrollElem.scrollHeight <= 220 if (scrollElem.scrollHeight <=
235 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { 221 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) {
236 chrome.send('queryHistory', 222 chrome.send('queryHistory',
237 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); 223 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]);
238 } 224 }
225 },
226
227 /**
228 * Check whether the time difference between the given history item and the
229 * next one is large enough for a spacer to be required.
230 * @param {Array<HistoryEntry>} results A list of history results.
231 * @param {number} index The index number of the first item being compared.
232 * @return {boolean} Whether or not time gap separator is required.
233 * @private
234 */
235 needsTimeGap_: function(results, index) {
236 var currentItem = results[index];
237 var nextItem = results[index + 1];
238
239 if (index + 1 >= results.length)
240 return false;
241
242 return currentItem.time - nextItem.time > BROWSING_GAP_TIME &&
243 currentItem.dateRelativeDay == nextItem.dateRelativeDay;
239 } 244 }
240 }); 245 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_history/history_list.html ('k') | chrome/browser/ui/webui/md_history_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698