OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 cr.define('ntp', function() { | 5 cr.define('ntp', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 var Thumbnail = ntp.Thumbnail; | 8 var Thumbnail = ntp.Thumbnail; |
9 var ThumbnailPage = ntp.ThumbnailPage; | 9 var ThumbnailPage = ntp.ThumbnailPage; |
10 | 10 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 handleCardSelected_: function(e) { | 202 handleCardSelected_: function(e) { |
203 if (!document.documentElement.classList.contains('starting-up')) | 203 if (!document.documentElement.classList.contains('starting-up')) |
204 chrome.send('mostVisitedSelected'); | 204 chrome.send('mostVisitedSelected'); |
205 }, | 205 }, |
206 | 206 |
207 /** | 207 /** |
208 * Sets the data that will be used to create Thumbnails. | 208 * Sets the data that will be used to create Thumbnails. |
209 * TODO(pedrosimonetti): Move data handling related code to TilePage. Make | 209 * TODO(pedrosimonetti): Move data handling related code to TilePage. Make |
210 * sure the new logic works with Apps without requiring duplicating code. | 210 * sure the new logic works with Apps without requiring duplicating code. |
211 * @param {Array} data The array of data. | 211 * @param {Array} data The array of data. |
212 * @type (Array} | |
213 */ | 212 */ |
214 set data(data) { | 213 setData: function(data) { |
215 var startTime = Date.now(); | 214 var startTime = Date.now(); |
216 var maxTileCount = this.config_.maxTileCount; | 215 ThumbnailPage.prototype.setData.apply(this, arguments); |
217 | |
218 // The first time data is set, create the tiles. | |
219 if (!this.data_) { | |
220 this.data_ = data.slice(0, maxTileCount); | |
221 this.createTiles_(this.data_.length); | |
222 } else { | |
223 this.data_ = refreshData(this.data_, data, maxTileCount); | |
224 } | |
225 | |
226 this.updateTiles_(); | |
227 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); | 216 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
228 }, | 217 }, |
229 }; | 218 }; |
230 | 219 |
231 /** | 220 /** |
232 * Executed once the NTP has loaded. Checks if the Most Visited pane is | 221 * Executed once the NTP has loaded. Checks if the Most Visited pane is |
233 * shown or not. If it is shown, the 'mostVisitedSelected' message is sent | 222 * shown or not. If it is shown, the 'mostVisitedSelected' message is sent |
234 * to the C++ code, to record the fact that the user has seen this pane. | 223 * to the C++ code, to record the fact that the user has seen this pane. |
235 */ | 224 */ |
236 MostVisitedPage.onLoaded = function() { | 225 MostVisitedPage.onLoaded = function() { |
237 if (ntp.getCardSlider() && | 226 if (ntp.getCardSlider() && |
238 ntp.getCardSlider().currentCardValue && | 227 ntp.getCardSlider().currentCardValue && |
239 ntp.getCardSlider().currentCardValue.classList | 228 ntp.getCardSlider().currentCardValue.classList |
240 .contains('most-visited-page')) { | 229 .contains('most-visited-page')) { |
241 chrome.send('mostVisitedSelected'); | 230 chrome.send('mostVisitedSelected'); |
242 } | 231 } |
243 }; | 232 }; |
244 | 233 |
245 /** | |
246 * We've gotten additional Most Visited data. Update our old data with the | |
247 * new data. The ordering of the new data is not important, except when a | |
248 * page is pinned. Thus we try to minimize re-ordering. | |
249 * @param {Array} oldData The current Most Visited page list. | |
250 * @param {Array} newData The new Most Visited page list. | |
251 * @return {Array} The merged page list that should replace the current page | |
252 * list. | |
253 */ | |
254 function refreshData(oldData, newData, maxTileCount) { | |
255 oldData = oldData.slice(0, maxTileCount); | |
256 newData = newData.slice(0, maxTileCount); | |
257 | |
258 // Copy over pinned sites directly. | |
259 for (var j = 0; j < newData.length; j++) { | |
260 if (newData[j].pinned) { | |
261 oldData[j] = newData[j]; | |
262 // Mark the entry as 'updated' so we don't try to update again. | |
263 oldData[j].updated = true; | |
264 // Mark the newData page as 'used' so we don't try to re-use it. | |
265 newData[j].used = true; | |
266 } | |
267 } | |
268 | |
269 // Look through old pages; if they exist in the newData list, keep them | |
270 // where they are. | |
271 for (var i = 0; i < oldData.length; i++) { | |
272 if (!oldData[i] || oldData[i].updated) | |
273 continue; | |
274 | |
275 for (var j = 0; j < newData.length; j++) { | |
276 if (newData[j].used) | |
277 continue; | |
278 | |
279 if (newData[j].url == oldData[i].url) { | |
280 // The background image and other data may have changed. | |
281 oldData[i] = newData[j]; | |
282 oldData[i].updated = true; | |
283 newData[j].used = true; | |
284 break; | |
285 } | |
286 } | |
287 } | |
288 | |
289 // Look through old pages that haven't been updated yet; replace them. | |
290 for (var i = 0; i < oldData.length; i++) { | |
291 if (oldData[i] && oldData[i].updated) | |
292 continue; | |
293 | |
294 for (var j = 0; j < newData.length; j++) { | |
295 if (newData[j].used) | |
296 continue; | |
297 | |
298 oldData[i] = newData[j]; | |
299 oldData[i].updated = true; | |
300 newData[j].used = true; | |
301 break; | |
302 } | |
303 | |
304 if (oldData[i] && !oldData[i].updated) | |
305 oldData[i] = null; | |
306 } | |
307 | |
308 // Clear 'updated' flags so this function will work next time it's called. | |
309 for (var i = 0; i < maxTileCount; i++) { | |
310 if (oldData[i]) | |
311 oldData[i].updated = false; | |
312 } | |
313 | |
314 return oldData; | |
315 } | |
316 | |
317 return { | 234 return { |
318 MostVisitedPage: MostVisitedPage, | 235 MostVisitedPage: MostVisitedPage, |
319 refreshData: refreshData, | |
320 }; | 236 }; |
321 }); | 237 }); |
322 | 238 |
323 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); | 239 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); |
OLD | NEW |