Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / resources / local_ntp / most_visited_thumbnail.js
blob3bdfd612e368626717a864b35b6d3be6b4ec1354
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 /**
7 * @fileoverview Rendering for iframed most visited thumbnails.
8 */
10 window.addEventListener('DOMContentLoaded', function() {
11 'use strict';
13 fillMostVisited(document.location, function(params, data) {
14 function logEvent(eventName) {
15 chrome.embeddedSearch.newTabPage.logEvent(eventName);
17 function logMostVisitedImpression(tileIndex, provider) {
18 chrome.embeddedSearch.newTabPage.logMostVisitedImpression(
19 tileIndex, provider);
21 function displayLink(link) {
22 document.body.appendChild(link);
23 window.parent.postMessage('linkDisplayed', '{{ORIGIN}}');
25 function showDomainElement() {
26 var link = createMostVisitedLink(
27 params, data.url, data.title, undefined, data.direction,
28 data.provider);
29 var domain = document.createElement('div');
30 domain.textContent = data.domain;
31 link.appendChild(domain);
32 displayLink(link);
34 // Called on intentionally empty tiles for which the visuals are handled
35 // externally by the page itself.
36 function showEmptyTile() {
37 displayLink(createMostVisitedLink(
38 params, data.url, data.title, undefined, data.direction,
39 data.provider));
41 // Creates and adds an image.
42 function createThumbnail(src, imageClass) {
43 var image = document.createElement('img');
44 if (imageClass) {
45 image.classList.add(imageClass);
47 image.onload = function() {
48 var link = createMostVisitedLink(
49 params, data.url, data.title, undefined, data.direction,
50 data.provider);
51 // Use blocker to prevent context menu from showing image-related items.
52 var blocker = document.createElement('span');
53 blocker.className = 'blocker';
54 link.appendChild(blocker);
55 link.appendChild(image);
56 displayLink(link);
57 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE_LOADED);
59 image.onerror = function() {
60 // If no external thumbnail fallback (etfb), and have domain.
61 if (!params.etfb && data.domain) {
62 showDomainElement();
63 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK);
64 } else {
65 showEmptyTile();
66 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK);
68 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
69 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE_LOADED);
71 image.src = src;
74 var useIcons = params['icons'] == '1';
75 if (data.dummy) {
76 showEmptyTile();
77 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
78 } else if (useIcons && data.largeIconUrl) {
79 createThumbnail(data.largeIconUrl, 'large-icon');
80 // TODO(huangs): Log event for large icons.
81 } else if (!useIcons && data.thumbnailUrl) {
82 createThumbnail(data.thumbnailUrl, 'thumbnail');
83 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE);
84 } else if (data.domain) {
85 showDomainElement();
86 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE);
87 } else {
88 showEmptyTile();
89 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
91 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);
93 // Log an impression if we know the position of the tile.
94 if (isFinite(params.pos) && data.provider) {
95 logMostVisitedImpression(parseInt(params.pos, 10), data.provider);
97 });
98 });