Chromium Code Reviews| 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 // Single iframe for NTP tiles. | 5 // Single iframe for NTP tiles. |
| 6 (function() { | 6 (function() { |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 }; | 268 }; |
| 269 | 269 |
| 270 | 270 |
| 271 /** | 271 /** |
| 272 * Called when the host page wants to add a suggestion tile. | 272 * Called when the host page wants to add a suggestion tile. |
| 273 * For Most Visited, it grabs the data from Chrome and pass on. | 273 * For Most Visited, it grabs the data from Chrome and pass on. |
| 274 * For host page generated it just passes the data. | 274 * For host page generated it just passes the data. |
| 275 * @param {object} args Data for the tile to be rendered. | 275 * @param {object} args Data for the tile to be rendered. |
| 276 */ | 276 */ |
| 277 var addTile = function(args) { | 277 var addTile = function(args) { |
| 278 if (args.rid) { | 278 if (args.rid) { |
|
huangs
2015/05/11 21:27:11
Should there be some code to allow server NTP to p
fserb
2015/05/19 19:02:15
Acknowledged.
| |
| 279 var data = chrome.embeddedSearch.searchBox.getMostVisitedItemData(args.rid); | 279 var data = chrome.embeddedSearch.searchBox.getMostVisitedItemData(args.rid); |
| 280 data.tid = data.rid; | 280 data.tid = data.rid; |
| 281 data.thumbnailUrls = [data.thumbnailUrl]; | |
| 281 data.faviconUrl = 'chrome-search://favicon/size/16@' + | 282 data.faviconUrl = 'chrome-search://favicon/size/16@' + |
| 282 window.devicePixelRatio + 'x/' + data.renderViewId + '/' + data.tid; | 283 window.devicePixelRatio + 'x/' + data.renderViewId + '/' + data.tid; |
| 283 tiles.appendChild(renderTile(data)); | 284 tiles.appendChild(renderTile(data)); |
| 284 logEvent(LOG_TYPE.NTP_CLIENT_SIDE_SUGGESTION); | 285 logEvent(LOG_TYPE.NTP_CLIENT_SIDE_SUGGESTION); |
| 285 } else if (args.id) { | 286 } else if (args.id) { |
| 286 tiles.appendChild(renderTile(args)); | 287 tiles.appendChild(renderTile(args)); |
| 287 logEvent(LOG_TYPE.NTP_SERVER_SIDE_SUGGESTION); | 288 logEvent(LOG_TYPE.NTP_SERVER_SIDE_SUGGESTION); |
| 288 } else { | 289 } else { |
| 289 tiles.appendChild(renderTile(null)); | 290 tiles.appendChild(renderTile(null)); |
| 290 } | 291 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 logEvent(LOG_TYPE.NTP_MOUSEOVER); | 350 logEvent(LOG_TYPE.NTP_MOUSEOVER); |
| 350 }); | 351 }); |
| 351 | 352 |
| 352 var title = tile.querySelector('.mv-title'); | 353 var title = tile.querySelector('.mv-title'); |
| 353 title.innerText = data.title; | 354 title.innerText = data.title; |
| 354 title.style.direction = data.direction || 'ltr'; | 355 title.style.direction = data.direction || 'ltr'; |
| 355 if (NUM_TITLE_LINES > 1) { | 356 if (NUM_TITLE_LINES > 1) { |
| 356 title.classList.add('multiline'); | 357 title.classList.add('multiline'); |
| 357 } | 358 } |
| 358 | 359 |
| 359 var hasIcon = USE_ICONS && data.largeIconUrl; | 360 if (USE_ICONS) { |
| 360 var hasThumb = !USE_ICONS && data.thumbnailUrl; | 361 var thumb = tile.querySelector('.mv-thumb'); |
| 361 var thumb = tile.querySelector('.mv-thumb'); | 362 if (data.largeIconUrl) { |
| 362 if (hasIcon || hasThumb) { | 363 var img = document.createElement('img'); |
| 363 var img = document.createElement('img'); | 364 img.title = data.title; |
| 364 img.title = data.title; | |
| 365 if (hasIcon) { | |
| 366 img.src = data.largeIconUrl; | 365 img.src = data.largeIconUrl; |
| 367 img.classList.add('large-icon'); | 366 img.classList.add('large-icon'); |
| 368 } else { // hasThumb | 367 loadedCounter += 1; |
| 369 img.src = data.thumbnailUrl; | 368 img.addEventListener('load', countLoad); |
| 370 img.classList.add('thumbnail'); | |
| 371 } | |
| 372 loadedCounter += 1; | |
| 373 img.addEventListener('load', countLoad); | |
| 374 if (data.largeIconUrl) { | |
| 375 img.addEventListener('load', function(ev) { | 369 img.addEventListener('load', function(ev) { |
| 376 thumb.classList.add('large-icon-outer'); | 370 thumb.classList.add('large-icon-outer'); |
| 377 }); | 371 }); |
| 372 img.addEventListener('error', countLoad); | |
|
huangs
2015/05/11 21:07:30
Remove this, and just call countLoad() from within
fserb
2015/05/19 19:02:15
Acknowledged.
| |
| 373 img.addEventListener('error', function(ev) { | |
| 374 thumb.classList.add('failed-img'); | |
| 375 thumb.removeChild(img); | |
| 376 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); | |
| 377 }); | |
| 378 thumb.appendChild(img); | |
| 379 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); | |
| 380 } else { | |
| 381 thumb.classList.add('failed-img'); | |
| 378 } | 382 } |
| 383 } else { // THUMBNAILS | |
| 384 var thumb = tile.querySelector('.mv-thumb'); | |
| 385 var img = document.createElement('img'); | |
| 386 var loaded = false; | |
| 387 | |
| 388 // We keep track of the outcome of loading possible thumbnails for this | |
| 389 // tile. Possible values: | |
| 390 // - null: waiting for load/error | |
| 391 // - false: error | |
| 392 // - a string: URL that loaded correctly. | |
| 393 // This is populated by acceptImage/rejectImage and loadBestImage | |
| 394 // decides the best one to load. | |
| 395 var results = []; | |
| 396 | |
| 397 var loadBestImage = function() { | |
|
huangs
2015/05/11 21:07:30
It seems the first image loaded will just dominate
huangs
2015/05/11 21:27:11
Ah NVM; the "null" ensures first best image gets l
fserb
2015/05/19 19:02:15
Acknowledged.
fserb
2015/05/19 19:02:15
Acknowledged.
| |
| 398 if (loaded) { | |
| 399 return; | |
| 400 } | |
| 401 for (i = 0; i < results.length; ++i) { | |
| 402 if (results[i] === null) { | |
| 403 return; | |
| 404 } | |
| 405 if (results[i] != false) { | |
| 406 img.src = results[i]; | |
| 407 loaded = true; | |
| 408 return; | |
| 409 } | |
| 410 } | |
| 411 thumb.classList.add('failed-img'); | |
| 412 thumb.removeChild(img); | |
| 413 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); | |
| 414 }; | |
| 415 | |
| 416 var acceptImage = function(idx, url) { | |
|
huangs
2015/05/11 21:27:11
I'd suggest removing acceptImage() and rejectImage
fserb
2015/05/19 19:02:15
Done.
| |
| 417 return function(ev) { | |
| 418 results[idx] = url; | |
| 419 loadBestImage(); | |
| 420 }; | |
| 421 }; | |
| 422 | |
| 423 var rejectImage = function(idx) { | |
| 424 return function(ev) { | |
| 425 results[idx] = false; | |
| 426 loadBestImage(); | |
| 427 }; | |
| 428 }; | |
| 429 | |
| 430 // Get all thumbnailUrls for the tile. | |
| 431 // They are ordered from best one to be used to worst. | |
| 432 for (var i = 0; i < data.thumbnailUrls.length; ++i) { | |
| 433 results.push(null); | |
| 434 } | |
| 435 for (var i = 0; i < data.thumbnailUrls.length; ++i) { | |
| 436 if (data.thumbnailUrls[i]) { | |
| 437 var image = new Image(); | |
| 438 image.src = data.thumbnailUrls[i]; | |
| 439 image.onload = acceptImage(i, data.thumbnailUrls[i]); | |
| 440 image.onerror = rejectImage(i); | |
| 441 } else { | |
| 442 rejectImage(i)(null); | |
| 443 } | |
| 444 } | |
| 445 | |
| 446 img.title = data.title; | |
| 447 img.classList.add('thumbnail'); | |
| 448 loadedCounter += 1; | |
| 449 img.addEventListener('load', countLoad); | |
| 379 img.addEventListener('error', countLoad); | 450 img.addEventListener('error', countLoad); |
|
huangs
2015/05/11 21:07:30
Remove this, and just call countLoad() from within
fserb
2015/05/19 19:02:15
Acknowledged.
| |
| 380 img.addEventListener('error', function(ev) { | 451 img.addEventListener('error', function(ev) { |
| 381 thumb.classList.add('failed-img'); | 452 thumb.classList.add('failed-img'); |
| 382 thumb.removeChild(img); | 453 thumb.removeChild(img); |
| 383 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); | 454 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); |
| 384 }); | 455 }); |
| 385 thumb.appendChild(img); | 456 thumb.appendChild(img); |
| 386 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); | 457 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); |
| 387 } else { | |
| 388 thumb.classList.add('failed-img'); | |
| 389 } | |
| 390 | 458 |
| 391 if (!USE_ICONS) { | |
| 392 var favicon = tile.querySelector('.mv-favicon'); | 459 var favicon = tile.querySelector('.mv-favicon'); |
| 393 if (data.faviconUrl) { | 460 if (data.faviconUrl) { |
| 394 var fi = document.createElement('img'); | 461 var fi = document.createElement('img'); |
| 395 fi.src = data.faviconUrl; | 462 fi.src = data.faviconUrl; |
| 396 // Set the title to empty so screen readers won't say the image name. | 463 // Set the title to empty so screen readers won't say the image name. |
| 397 fi.title = ''; | 464 fi.title = ''; |
| 398 loadedCounter += 1; | 465 loadedCounter += 1; |
| 399 fi.addEventListener('load', countLoad); | 466 fi.addEventListener('load', countLoad); |
| 400 fi.addEventListener('error', countLoad); | 467 fi.addEventListener('error', countLoad); |
| 401 fi.addEventListener('error', function(ev) { | 468 fi.addEventListener('error', function(ev) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 var html = document.querySelector('html'); | 519 var html = document.querySelector('html'); |
| 453 html.dir = 'rtl'; | 520 html.dir = 'rtl'; |
| 454 } | 521 } |
| 455 | 522 |
| 456 window.addEventListener('message', handlePostMessage); | 523 window.addEventListener('message', handlePostMessage); |
| 457 }; | 524 }; |
| 458 | 525 |
| 459 | 526 |
| 460 window.addEventListener('DOMContentLoaded', init); | 527 window.addEventListener('DOMContentLoaded', init); |
| 461 })(); | 528 })(); |
| OLD | NEW |