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

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

Issue 1880783002: MD History: Use computed functions for card borders and time gaps (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update comment Created 4 years, 8 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-list', 6 is: 'history-list',
7 7
8 properties: { 8 properties: {
9 // An array of history entries in reverse chronological order. 9 // An array of history entries in reverse chronological order.
10 historyData: { 10 historyData: {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 98
99 if (historyResults.length == 0) 99 if (historyResults.length == 0)
100 return; 100 return;
101 101
102 // Creates a copy of historyResults to prevent accidentally modifying this 102 // Creates a copy of historyResults to prevent accidentally modifying this
103 // field. 103 // field.
104 var results = historyResults.slice(); 104 var results = historyResults.slice();
105 105
106 var currentDate = results[0].dateRelativeDay; 106 var currentDate = results[0].dateRelativeDay;
107 107
108 // Resets the last history item for the currentDate if new history results
109 // for currentDate is loaded.
110 if (this.historyData && this.historyData.length > 0) {
111 var lastHistoryItem = this.historyData[this.historyData.length - 1];
112 if (lastHistoryItem && lastHistoryItem.dateRelativeDay == currentDate) {
113 this.set('historyData.' + (this.historyData.length - 1) +
114 '.isLastItem', false);
115 }
116 }
117
118 for (var i = 0; i < results.length; i++) { 108 for (var i = 0; i < results.length; i++) {
119 // Sets the default values for these fields to prevent undefined types. 109 // Sets the default values for these fields to prevent undefined types.
120 results[i].selected = false; 110 results[i].selected = false;
121 results[i].isLastItem = false;
122 results[i].isFirstItem = false;
123 results[i].needsTimeGap = this.needsTimeGap_(results, i);
124 results[i].readableTimestamp = 111 results[i].readableTimestamp =
125 searchTerm == '' ? results[i].dateTimeOfDay : results[i].dateShort; 112 searchTerm == '' ? results[i].dateTimeOfDay : results[i].dateShort;
126 113
127 if (results[i].dateRelativeDay != currentDate) { 114 if (results[i].dateRelativeDay != currentDate) {
128 results[i - 1].isLastItem = true;
129 results[i].isFirstItem = true;
130 currentDate = results[i].dateRelativeDay; 115 currentDate = results[i].dateRelativeDay;
131 } 116 }
132 } 117 }
133 results[i - 1].isLastItem = true;
134
135 if (!this.historyData || this.historyData.length == 0)
136 results[0].isFirstItem = true;
137 118
138 if (this.historyData) { 119 if (this.historyData) {
139 // If we have previously received data, push the new items onto the 120 // If we have previously received data, push the new items onto the
140 // existing array. 121 // existing array.
141 results.unshift('historyData'); 122 results.unshift('historyData');
142 this.push.apply(this, results); 123 this.push.apply(this, results);
143 } else { 124 } else {
144 // The first time we receive data, use set() to ensure the iron-list is 125 // The first time we receive data, use set() to ensure the iron-list is
145 // initialized correctly. 126 // initialized correctly.
146 this.set('historyData', results); 127 this.set('historyData', results);
(...skipping 21 matching lines...) Expand all
168 } 149 }
169 }, 150 },
170 151
171 /** 152 /**
172 * Remove all selected items from the overall array so that they are also 153 * Remove all selected items from the overall array so that they are also
173 * removed from view. Make sure that the card length and positioning is 154 * removed from view. Make sure that the card length and positioning is
174 * updated accordingly. 155 * updated accordingly.
175 * @param {number} overallItemCount The number of items selected. 156 * @param {number} overallItemCount The number of items selected.
176 */ 157 */
177 removeDeletedHistory: function(overallItemCount) { 158 removeDeletedHistory: function(overallItemCount) {
159 var spliceinfo = [];
Dan Beam 2016/04/12 01:28:16 nit: splices
tsergeant 2016/04/12 05:40:27 Done.
178 for (var i = this.historyData.length - 1; i >= 0; i--) { 160 for (var i = this.historyData.length - 1; i >= 0; i--) {
179 if (!this.historyData[i].selected) 161 if (!this.historyData[i].selected)
180 continue; 162 continue;
181 163
182 // TODO: Change to using computed properties to recompute the first and 164 // Removes the selected item from historyData. Use unshift so spliceInfo
183 // last cards. 165 // ends up in index order.
184 166 spliceinfo.unshift({
185 // Resets the first history item. 167 index: i,
186 if (this.historyData[i].isFirstItem && 168 removed: [this.historyData[i]],
187 (i + 1) < this.historyData.length && 169 addedCount: 0,
188 this.historyData[i].dateRelativeDay == 170 object: this.historyData,
189 this.historyData[i + 1].dateRelativeDay) { 171 type: 'splice'
190 this.set('historyData.' + (i + 1) + '.isFirstItem', true); 172 });
191 } 173 this.historyData.splice(i, 1);
192
193 // Resets the last history item.
194 if (this.historyData[i].isLastItem && i > 0 &&
195 this.historyData[i].dateRelativeDay ==
196 this.historyData[i - 1].dateRelativeDay) {
197 this.set('historyData.' + (i - 1) + '.isLastItem', true);
198
199 if (this.historyData[i - 1].needsTimeGap)
200 this.set('historyData.' + (i - 1) + '.needsTimeGap', false);
201 }
202
203 // Makes sure that the time gap separators are preserved.
204 if (this.historyData[i].needsTimeGap && i > 0)
205 this.set('historyData.' + (i - 1) + '.needsTimeGap', true);
206
207 // Removes the selected item from historyData.
208 this.splice('historyData', i, 1);
209 174
210 overallItemCount--; 175 overallItemCount--;
211 if (overallItemCount == 0) 176 if (overallItemCount == 0)
212 break; 177 break;
213 } 178 }
179 this.notifySplices('historyData', spliceinfo);
Dan Beam 2016/04/12 01:28:16 why are you using notifySplices? https://www.polym
tsergeant 2016/04/12 05:40:27 The old way we were doing this, with `this.splice(
214 }, 180 },
215 181
216 /** 182 /**
217 * Based on which items are selected, collect an array of the info required 183 * Based on which items are selected, collect an array of the info required
218 * for chrome.send('removeHistory', ...). 184 * for chrome.send('removeHistory', ...).
219 * @param {number} count The number of items that are selected. 185 * @param {number} count The number of items that are selected.
220 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain 186 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain
221 * information on which history-items should be deleted. 187 * information on which history-items should be deleted.
222 */ 188 */
223 getSelectedItems: function(count) { 189 getSelectedItems: function(count) {
(...skipping 30 matching lines...) Expand all
254 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { 220 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) {
255 this.loading_ = true; 221 this.loading_ = true;
256 chrome.send('queryHistory', 222 chrome.send('queryHistory',
257 [this.searchTerm, 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); 223 [this.searchTerm, 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]);
258 } 224 }
259 }, 225 },
260 226
261 /** 227 /**
262 * Check whether the time difference between the given history item and the 228 * Check whether the time difference between the given history item and the
263 * next one is large enough for a spacer to be required. 229 * next one is large enough for a spacer to be required.
264 * @param {Array<HistoryEntry>} results A list of history results. 230 * @param {HistoryEntry} item
265 * @param {number} index The index number of the first item being compared. 231 * @param {number} index The index of |item| in |historyData|
232 * @param {number} length The length of |historyData|
Dan Beam 2016/04/12 01:28:16 nit: periods after [semi-complete] sentences
tsergeant 2016/04/12 05:40:27 Done.
266 * @return {boolean} Whether or not time gap separator is required. 233 * @return {boolean} Whether or not time gap separator is required.
267 * @private 234 * @private
268 */ 235 */
269 needsTimeGap_: function(results, index) { 236 needsTimeGap_: function(item, index, length) {
270 // TODO(tsergeant): Allow the final item from one batch of results to have a 237 if (index >= length - 1 || length == 0)
Dan Beam 2016/04/12 01:28:16 when should index be longer than the length of the
tsergeant 2016/04/12 05:40:27 Strangely, the iron-list will still compute these
271 // timegap once more results are added.
272 if (index == results.length - 1)
273 return false; 238 return false;
274 239
275 var currentItem = results[index]; 240 var currentItem = this.historyData[index];
276 var nextItem = results[index + 1]; 241 var nextItem = this.historyData[index + 1];
277 242
278 if (this.searchTerm) 243 if (this.searchTerm)
279 return currentItem.dateShort != nextItem.dateShort; 244 return currentItem.dateShort != nextItem.dateShort;
280 245
281 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && 246 return currentItem.time - nextItem.time > BROWSING_GAP_TIME &&
282 currentItem.dateRelativeDay == nextItem.dateRelativeDay; 247 currentItem.dateRelativeDay == nextItem.dateRelativeDay;
283 }, 248 },
284 249
285 hasResults: function(historyDataLength) { 250 hasResults: function(historyDataLength) {
286 return historyDataLength > 0; 251 return historyDataLength > 0;
287 }, 252 },
288 253
289 noResultsMessage_: function(searchTerm, isLoading) { 254 noResultsMessage_: function(searchTerm, isLoading) {
290 if (isLoading) 255 if (isLoading)
291 return ''; 256 return '';
292 var messageId = searchTerm !== '' ? 'noSearchResults' : 'noResults'; 257 var messageId = searchTerm !== '' ? 'noSearchResults' : 'noResults';
293 return loadTimeData.getString(messageId); 258 return loadTimeData.getString(messageId);
294 } 259 },
260
Dan Beam 2016/04/12 01:28:16 nit: jsdoc for both of these methods?
tsergeant 2016/04/12 05:40:27 Done.
261 isCardStart_: function(item, i, length) {
262 if (length == 0 || i > length - 1)
263 return false;
264 return i == 0 ||
265 this.historyData[i].dateRelativeDay !=
266 this.historyData[i - 1].dateRelativeDay;
267 },
268
269 isCardEnd_: function(item, i, length) {
270 if (length == 0 || i > length - 1)
271 return false;
272 return i == length - 1 ||
273 this.historyData[i].dateRelativeDay !=
274 this.historyData[i + 1].dateRelativeDay;
275 },
295 }); 276 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_history/history_list.html ('k') | chrome/test/data/webui/md_history/history_item_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698