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