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-list', | 6 is: 'history-list', |
7 | 7 |
8 properties: { | 8 properties: { |
9 // The search term for the current query. Set when the query returns. | 9 // The search term for the current query. Set when the query returns. |
10 searchedTerm: { | 10 searchedTerm: { |
11 type: String, | 11 type: String, |
12 value: '', | 12 value: '', |
13 }, | 13 }, |
14 | 14 |
15 lastSearchedTerm_: String, | 15 lastSearchedTerm_: String, |
16 | 16 |
17 querying: Boolean, | 17 querying: Boolean, |
18 | 18 |
19 // An array of history entries in reverse chronological order. | 19 // An array of history entries in reverse chronological order. |
20 historyData: Array, | 20 historyData_: Array, |
21 | 21 |
22 resultLoadingDisabled_: { | 22 resultLoadingDisabled_: { |
23 type: Boolean, | 23 type: Boolean, |
24 value: false, | 24 value: false, |
25 }, | 25 }, |
26 }, | 26 }, |
27 | 27 |
28 listeners: { | 28 listeners: { |
29 'infinite-list.scroll': 'closeMenu_', | 29 'infinite-list.scroll': 'closeMenu_', |
30 'tap': 'closeMenu_', | 30 'tap': 'closeMenu_', |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 }, | 74 }, |
75 | 75 |
76 /** | 76 /** |
77 * Disables history result loading when there are no more history results. | 77 * Disables history result loading when there are no more history results. |
78 */ | 78 */ |
79 disableResultLoading: function() { | 79 disableResultLoading: function() { |
80 this.resultLoadingDisabled_ = true; | 80 this.resultLoadingDisabled_ = true; |
81 }, | 81 }, |
82 | 82 |
83 /** | 83 /** |
84 * Adds the newly updated history results into historyData. Adds new fields | 84 * Adds the newly updated history results into historyData_. Adds new fields |
85 * for each result. | 85 * for each result. |
86 * @param {!Array<!HistoryEntry>} historyResults The new history results. | 86 * @param {!Array<!HistoryEntry>} historyResults The new history results. |
87 */ | 87 */ |
88 addNewResults: function(historyResults) { | 88 addNewResults: function(historyResults) { |
89 /** @type {IronScrollThresholdElement} */(this.$['scroll-threshold']) | 89 /** @type {IronScrollThresholdElement} */(this.$['scroll-threshold']) |
90 .clearTriggers(); | 90 .clearTriggers(); |
91 | 91 |
92 if (this.lastSearchedTerm_ != this.searchedTerm) { | 92 if (this.lastSearchedTerm_ != this.searchedTerm) { |
93 this.resultLoadingDisabled_ = false; | 93 this.resultLoadingDisabled_ = false; |
94 if (this.historyData) | 94 if (this.historyData_) |
95 this.splice('historyData', 0, this.historyData.length); | 95 this.splice('historyData_', 0, this.historyData_.length); |
96 this.lastSearchedTerm_ = this.searchedTerm; | 96 this.lastSearchedTerm_ = this.searchedTerm; |
97 } | 97 } |
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 for (var i = 0; i < results.length; i++) { | 108 for (var i = 0; i < results.length; i++) { |
109 // Sets the default values for these fields to prevent undefined types. | 109 // Sets the default values for these fields to prevent undefined types. |
110 results[i].selected = false; | 110 results[i].selected = false; |
111 results[i].readableTimestamp = | 111 results[i].readableTimestamp = |
112 this.searchedTerm == '' ? | 112 this.searchedTerm == '' ? |
113 results[i].dateTimeOfDay : results[i].dateShort; | 113 results[i].dateTimeOfDay : results[i].dateShort; |
114 | 114 |
115 if (results[i].dateRelativeDay != currentDate) { | 115 if (results[i].dateRelativeDay != currentDate) { |
116 currentDate = results[i].dateRelativeDay; | 116 currentDate = results[i].dateRelativeDay; |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 if (this.historyData) { | 120 if (this.historyData_) { |
121 // If we have previously received data, push the new items onto the | 121 // If we have previously received data, push the new items onto the |
122 // existing array. | 122 // existing array. |
123 results.unshift('historyData'); | 123 results.unshift('historyData_'); |
124 this.push.apply(this, results); | 124 this.push.apply(this, results); |
125 } else { | 125 } else { |
126 // The first time we receive data, use set() to ensure the iron-list is | 126 // The first time we receive data, use set() to ensure the iron-list is |
127 // initialized correctly. | 127 // initialized correctly. |
128 this.set('historyData', results); | 128 this.set('historyData_', results); |
129 } | 129 } |
130 }, | 130 }, |
131 | 131 |
132 /** | 132 /** |
133 * Cycle through each entry in historyData and set all items to be | 133 * Cycle through each entry in historyData_ and set all items to be |
134 * unselected. | 134 * unselected. |
135 * @param {number} overallItemCount The number of checkboxes selected. | 135 * @param {number} overallItemCount The number of checkboxes selected. |
136 */ | 136 */ |
137 unselectAllItems: function(overallItemCount) { | 137 unselectAllItems: function(overallItemCount) { |
138 if (this.historyData === undefined) | 138 if (this.historyData_ === undefined) |
139 return; | 139 return; |
140 | 140 |
141 for (var i = 0; i < this.historyData.length; i++) { | 141 for (var i = 0; i < this.historyData_.length; i++) { |
142 if (this.historyData[i].selected) { | 142 if (this.historyData_[i].selected) { |
143 this.set('historyData.' + i + '.selected', false); | 143 this.set('historyData_.' + i + '.selected', false); |
144 overallItemCount--; | 144 overallItemCount--; |
145 if (overallItemCount == 0) | 145 if (overallItemCount == 0) |
146 break; | 146 break; |
147 } | 147 } |
148 } | 148 } |
149 }, | 149 }, |
150 | 150 |
151 /** | 151 /** |
152 * Remove the given |items| from the list. Expected to be called after the | 152 * Remove the given |items| from the list. Expected to be called after the |
153 * items are removed from the backend. | 153 * items are removed from the backend. |
154 * @param {!Array<!HistoryEntry>} removalList | 154 * @param {!Array<!HistoryEntry>} removalList |
155 * @private | 155 * @private |
156 */ | 156 */ |
157 removeDeletedHistory_: function(removalList) { | 157 removeDeletedHistory_: function(removalList) { |
158 // This set is only for speed. Note that set inclusion for objects is by | 158 // This set is only for speed. Note that set inclusion for objects is by |
159 // reference, so this relies on the HistoryEntry objects never being copied. | 159 // reference, so this relies on the HistoryEntry objects never being copied. |
160 var deletedItems = new Set(removalList); | 160 var deletedItems = new Set(removalList); |
161 var splices = []; | 161 var splices = []; |
162 | 162 |
163 for (var i = this.historyData.length - 1; i >= 0; i--) { | 163 for (var i = this.historyData_.length - 1; i >= 0; i--) { |
164 var item = this.historyData[i]; | 164 var item = this.historyData_[i]; |
165 if (deletedItems.has(item)) { | 165 if (deletedItems.has(item)) { |
166 // Removes the selected item from historyData. Use unshift so | 166 // Removes the selected item from historyData_. Use unshift so |
167 // |splices| ends up in index order. | 167 // |splices| ends up in index order. |
168 splices.unshift({ | 168 splices.unshift({ |
169 index: i, | 169 index: i, |
170 removed: [item], | 170 removed: [item], |
171 addedCount: 0, | 171 addedCount: 0, |
172 object: this.historyData, | 172 object: this.historyData_, |
173 type: 'splice' | 173 type: 'splice' |
174 }); | 174 }); |
175 this.historyData.splice(i, 1); | 175 this.historyData_.splice(i, 1); |
176 } | 176 } |
177 } | 177 } |
178 // notifySplices gives better performance than individually splicing as it | 178 // notifySplices gives better performance than individually splicing as it |
179 // batches all of the updates together. | 179 // batches all of the updates together. |
180 this.notifySplices('historyData', splices); | 180 this.notifySplices('historyData_', splices); |
181 }, | 181 }, |
182 | 182 |
183 /** | 183 /** |
184 * Performs a request to the backend to delete all selected items. If | 184 * Performs a request to the backend to delete all selected items. If |
185 * successful, removes them from the view. | 185 * successful, removes them from the view. |
186 */ | 186 */ |
187 deleteSelected: function() { | 187 deleteSelected: function() { |
188 var toBeRemoved = this.historyData.filter(function(item) { | 188 var toBeRemoved = this.historyData_.filter(function(item) { |
189 return item.selected; | 189 return item.selected; |
190 }); | 190 }); |
191 md_history.BrowserService.getInstance() | 191 md_history.BrowserService.getInstance() |
192 .deleteItems(toBeRemoved) | 192 .deleteItems(toBeRemoved) |
193 .then(function(items) { | 193 .then(function(items) { |
194 this.removeDeletedHistory_(items); | 194 this.removeDeletedHistory_(items); |
195 this.fire('unselect-all'); | 195 this.fire('unselect-all'); |
196 }.bind(this)); | 196 }.bind(this)); |
197 }, | 197 }, |
198 | 198 |
199 /** | 199 /** |
200 * Called when the page is scrolled to near the bottom of the list. | 200 * Called when the page is scrolled to near the bottom of the list. |
201 * @private | 201 * @private |
202 */ | 202 */ |
203 loadMoreData_: function() { | 203 loadMoreData_: function() { |
204 if (this.resultLoadingDisabled_ || this.querying) | 204 if (this.resultLoadingDisabled_ || this.querying) |
205 return; | 205 return; |
206 | 206 |
207 this.fire('load-more-history'); | 207 this.fire('load-more-history'); |
208 }, | 208 }, |
209 | 209 |
210 /** | 210 /** |
211 * Check whether the time difference between the given history item and the | 211 * Check whether the time difference between the given history item and the |
212 * next one is large enough for a spacer to be required. | 212 * next one is large enough for a spacer to be required. |
213 * @param {HistoryEntry} item | 213 * @param {HistoryEntry} item |
214 * @param {number} index The index of |item| in |historyData|. | 214 * @param {number} index The index of |item| in |historyData_|. |
215 * @param {number} length The length of |historyData|. | 215 * @param {number} length The length of |historyData_|. |
216 * @return {boolean} Whether or not time gap separator is required. | 216 * @return {boolean} Whether or not time gap separator is required. |
217 * @private | 217 * @private |
218 */ | 218 */ |
219 needsTimeGap_: function(item, index, length) { | 219 needsTimeGap_: function(item, index, length) { |
220 if (index >= length - 1 || length == 0) | 220 if (index >= length - 1 || length == 0) |
221 return false; | 221 return false; |
222 | 222 |
223 var currentItem = this.historyData[index]; | 223 var currentItem = this.historyData_[index]; |
224 var nextItem = this.historyData[index + 1]; | 224 var nextItem = this.historyData_[index + 1]; |
225 | 225 |
226 if (this.searchedTerm) | 226 if (this.searchedTerm) |
227 return currentItem.dateShort != nextItem.dateShort; | 227 return currentItem.dateShort != nextItem.dateShort; |
228 | 228 |
229 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && | 229 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && |
230 currentItem.dateRelativeDay == nextItem.dateRelativeDay; | 230 currentItem.dateRelativeDay == nextItem.dateRelativeDay; |
231 }, | 231 }, |
232 | 232 |
233 hasResults: function(historyDataLength) { | 233 hasResults: function(historyDataLength) { |
234 return historyDataLength > 0; | 234 return historyDataLength > 0; |
235 }, | 235 }, |
236 | 236 |
237 noResultsMessage_: function(searchedTerm, isLoading) { | 237 noResultsMessage_: function(searchedTerm, isLoading) { |
238 if (isLoading) | 238 if (isLoading) |
239 return ''; | 239 return ''; |
240 var messageId = searchedTerm !== '' ? 'noSearchResults' : 'noResults'; | 240 var messageId = searchedTerm !== '' ? 'noSearchResults' : 'noResults'; |
241 return loadTimeData.getString(messageId); | 241 return loadTimeData.getString(messageId); |
242 }, | 242 }, |
243 | 243 |
244 /** | 244 /** |
245 * True if the given item is the beginning of a new card. | 245 * True if the given item is the beginning of a new card. |
246 * @param {HistoryEntry} item | 246 * @param {HistoryEntry} item |
247 * @param {number} i Index of |item| within |historyData|. | 247 * @param {number} i Index of |item| within |historyData_|. |
248 * @param {number} length | 248 * @param {number} length |
249 * @return {boolean} | 249 * @return {boolean} |
250 * @private | 250 * @private |
251 */ | 251 */ |
252 isCardStart_: function(item, i, length) { | 252 isCardStart_: function(item, i, length) { |
253 if (length == 0 || i > length - 1) | 253 if (length == 0 || i > length - 1) |
254 return false; | 254 return false; |
255 return i == 0 || | 255 return i == 0 || |
256 this.historyData[i].dateRelativeDay != | 256 this.historyData_[i].dateRelativeDay != |
257 this.historyData[i - 1].dateRelativeDay; | 257 this.historyData_[i - 1].dateRelativeDay; |
258 }, | 258 }, |
259 | 259 |
260 /** | 260 /** |
261 * True if the given item is the end of a card. | 261 * True if the given item is the end of a card. |
262 * @param {HistoryEntry} item | 262 * @param {HistoryEntry} item |
263 * @param {number} i Index of |item| within |historyData|. | 263 * @param {number} i Index of |item| within |historyData_|. |
264 * @param {number} length | 264 * @param {number} length |
265 * @return {boolean} | 265 * @return {boolean} |
266 * @private | 266 * @private |
267 */ | 267 */ |
268 isCardEnd_: function(item, i, length) { | 268 isCardEnd_: function(item, i, length) { |
269 if (length == 0 || i > length - 1) | 269 if (length == 0 || i > length - 1) |
270 return false; | 270 return false; |
271 return i == length - 1 || | 271 return i == length - 1 || |
272 this.historyData[i].dateRelativeDay != | 272 this.historyData_[i].dateRelativeDay != |
273 this.historyData[i + 1].dateRelativeDay; | 273 this.historyData_[i + 1].dateRelativeDay; |
274 }, | 274 }, |
275 }); | 275 }); |
OLD | NEW |