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

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: Review comments 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 25 matching lines...) Expand all
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) {
178 for (var i = this.historyData.length - 1; i >= 0; i--) { 159 for (var i = this.historyData.length - 1; i >= 0; i--) {
179 if (!this.historyData[i].selected) 160 if (!this.historyData[i].selected)
180 continue; 161 continue;
181 162
182 // TODO: Change to using computed properties to recompute the first and
183 // last cards.
184
185 // Resets the first history item.
186 if (this.historyData[i].isFirstItem &&
187 (i + 1) < this.historyData.length &&
188 this.historyData[i].dateRelativeDay ==
189 this.historyData[i + 1].dateRelativeDay) {
190 this.set('historyData.' + (i + 1) + '.isFirstItem', true);
191 }
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. 163 // Removes the selected item from historyData.
208 this.splice('historyData', i, 1); 164 this.splice('historyData', i, 1);
209 165
210 overallItemCount--; 166 overallItemCount--;
211 if (overallItemCount == 0) 167 if (overallItemCount == 0)
212 break; 168 break;
213 } 169 }
214 }, 170 },
215 171
216 /** 172 /**
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { 210 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) {
255 this.loading_ = true; 211 this.loading_ = true;
256 chrome.send('queryHistory', 212 chrome.send('queryHistory',
257 [this.searchTerm, 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); 213 [this.searchTerm, 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]);
258 } 214 }
259 }, 215 },
260 216
261 /** 217 /**
262 * Check whether the time difference between the given history item and the 218 * Check whether the time difference between the given history item and the
263 * next one is large enough for a spacer to be required. 219 * next one is large enough for a spacer to be required.
264 * @param {Array<HistoryEntry>} results A list of history results. 220 * @param {HistoryEntry} item
265 * @param {number} index The index number of the first item being compared. 221 * @param {number} index The index of |item| in |historyData|.
222 * @param {number} length The length of |historyData|.
266 * @return {boolean} Whether or not time gap separator is required. 223 * @return {boolean} Whether or not time gap separator is required.
267 * @private 224 * @private
268 */ 225 */
269 needsTimeGap_: function(results, index) { 226 needsTimeGap_: function(item, index, length) {
270 // TODO(tsergeant): Allow the final item from one batch of results to have a 227 if (index >= length - 1 || length == 0)
271 // timegap once more results are added.
272 if (index == results.length - 1)
273 return false; 228 return false;
274 229
275 var currentItem = results[index]; 230 var currentItem = this.historyData[index];
276 var nextItem = results[index + 1]; 231 var nextItem = this.historyData[index + 1];
277 232
278 if (this.searchTerm) 233 if (this.searchTerm)
279 return currentItem.dateShort != nextItem.dateShort; 234 return currentItem.dateShort != nextItem.dateShort;
280 235
281 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && 236 return currentItem.time - nextItem.time > BROWSING_GAP_TIME &&
282 currentItem.dateRelativeDay == nextItem.dateRelativeDay; 237 currentItem.dateRelativeDay == nextItem.dateRelativeDay;
283 }, 238 },
284 239
285 hasResults: function(historyDataLength) { 240 hasResults: function(historyDataLength) {
286 return historyDataLength > 0; 241 return historyDataLength > 0;
287 }, 242 },
288 243
289 noResultsMessage_: function(searchTerm, isLoading) { 244 noResultsMessage_: function(searchTerm, isLoading) {
290 if (isLoading) 245 if (isLoading)
291 return ''; 246 return '';
292 var messageId = searchTerm !== '' ? 'noSearchResults' : 'noResults'; 247 var messageId = searchTerm !== '' ? 'noSearchResults' : 'noResults';
293 return loadTimeData.getString(messageId); 248 return loadTimeData.getString(messageId);
294 } 249 },
250
251 /**
252 * True if the given item is the beginning of a new card.
253 * @param {HistoryEntry} item
254 * @param {number} i Index of |item| within |historyData|.
255 * @param {number} length
256 * @return {boolean}
257 * @private
258 */
259 isCardStart_: function(item, i, length) {
260 if (length == 0 || i > length - 1)
261 return false;
262 return i == 0 ||
263 this.historyData[i].dateRelativeDay !=
264 this.historyData[i - 1].dateRelativeDay;
265 },
266
267 /**
268 * True if the given item is the end of a card.
269 * @param {HistoryEntry} item
270 * @param {number} i Index of |item| within |historyData|.
271 * @param {number} length
272 * @return {boolean}
273 * @private
274 */
275 isCardEnd_: function(item, i, length) {
276 if (length == 0 || i > length - 1)
277 return false;
278 return i == length - 1 ||
279 this.historyData[i].dateRelativeDay !=
280 this.historyData[i + 1].dateRelativeDay;
281 },
295 }); 282 });
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