| 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); |
| 217 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
| 218 }, |
| 218 | 219 |
| 219 // The first time data is set, create the tiles. | 220 /** |
| 220 if (!this.data_) { | 221 * Merges new Most Visited data into the old while minimizing re-ordering. |
| 221 this.data_ = data.slice(0, maxTileCount); | 222 * @param {Array} oldData The current page list. |
| 222 this.createTiles_(this.data_.length); | 223 * @param {Array} newData The new page list. |
| 223 } else { | 224 * @param {number} maxTileCount The maximum number of tiles to render. |
| 224 this.data_ = refreshData(this.data_, data, maxTileCount); | 225 * @return {Array} The merged page list that should replace the current page |
| 226 * list. |
| 227 * @private |
| 228 */ |
| 229 refreshData_: function(oldData, newData, maxTileCount) { |
| 230 oldData = oldData.slice(0, maxTileCount); |
| 231 newData = newData.slice(0, maxTileCount); |
| 232 |
| 233 // Look through old pages; if they exist in the newData list, keep them |
| 234 // where they are. |
| 235 for (var i = 0; i < oldData.length; i++) { |
| 236 if (!oldData[i] || oldData[i].updated) |
| 237 continue; |
| 238 |
| 239 for (var j = 0; j < newData.length; j++) { |
| 240 if (newData[j].used) |
| 241 continue; |
| 242 |
| 243 if (newData[j].url == oldData[i].url) { |
| 244 // The background image and other data may have changed. |
| 245 oldData[i] = newData[j]; |
| 246 oldData[i].updated = true; |
| 247 newData[j].used = true; |
| 248 break; |
| 249 } |
| 250 } |
| 225 } | 251 } |
| 226 | 252 |
| 227 this.updateTiles_(); | 253 // Look through old pages that haven't been updated yet; replace them. |
| 228 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); | 254 for (var i = 0; i < oldData.length; i++) { |
| 255 if (oldData[i] && oldData[i].updated) |
| 256 continue; |
| 257 |
| 258 for (var j = 0; j < newData.length; j++) { |
| 259 if (newData[j].used) |
| 260 continue; |
| 261 |
| 262 oldData[i] = newData[j]; |
| 263 oldData[i].updated = true; |
| 264 newData[j].used = true; |
| 265 break; |
| 266 } |
| 267 |
| 268 if (oldData[i] && !oldData[i].updated) |
| 269 oldData[i] = null; |
| 270 } |
| 271 |
| 272 // Clear 'updated' flags so this function will work next time it's called. |
| 273 for (var i = 0; i < maxTileCount; i++) { |
| 274 if (oldData[i]) |
| 275 oldData[i].updated = false; |
| 276 } |
| 277 |
| 278 return oldData; |
| 279 }, |
| 280 |
| 281 /** |
| 282 * Returns all non-null data entries. Null is used by Most Visited Page as |
| 283 * placeholders for blacklisted entries. |
| 284 * @private |
| 285 * @return {Array} Non-null data entries. |
| 286 */ |
| 287 getValidData_: function() { |
| 288 var validData = []; |
| 289 var data = this.data_; |
| 290 for (var i = 0, length = data.length; i < length; i++) { |
| 291 if (data[i]) |
| 292 validData.push(data[i]); |
| 293 } |
| 294 return validData; |
| 229 }, | 295 }, |
| 230 }; | 296 }; |
| 231 | 297 |
| 232 /** | 298 /** |
| 233 * Executed once the NTP has loaded. Checks if the Most Visited pane is | 299 * 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 | 300 * 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. | 301 * to the C++ code, to record the fact that the user has seen this pane. |
| 236 */ | 302 */ |
| 237 MostVisitedPage.onLoaded = function() { | 303 MostVisitedPage.onLoaded = function() { |
| 238 if (ntp.getCardSlider() && | 304 if (ntp.getCardSlider() && |
| 239 ntp.getCardSlider().currentCardValue && | 305 ntp.getCardSlider().currentCardValue && |
| 240 ntp.getCardSlider().currentCardValue.classList | 306 ntp.getCardSlider().currentCardValue.classList |
| 241 .contains('most-visited-page')) { | 307 .contains('most-visited-page')) { |
| 242 chrome.send('mostVisitedSelected'); | 308 chrome.send('mostVisitedSelected'); |
| 243 } | 309 } |
| 244 }; | 310 }; |
| 245 | 311 |
| 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 { | 312 return { |
| 319 MostVisitedPage: MostVisitedPage, | 313 MostVisitedPage: MostVisitedPage, |
| 320 refreshData: refreshData, | |
| 321 }; | 314 }; |
| 322 }); | 315 }); |
| 323 | 316 |
| 324 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); | 317 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); |
| OLD | NEW |