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
3 // found in the LICENSE file.
5 cr.define('ntp', function() {
8 var TilePage = ntp.TilePage;
11 * A counter for generating unique tile IDs.
16 * Creates a new Most Visited object for tiling.
18 * @extends {HTMLAnchorElement}
20 function MostVisited() {
21 var el = cr.doc.createElement('a');
22 el.__proto__ = MostVisited.prototype;
28 MostVisited.prototype = {
29 __proto__: HTMLAnchorElement.prototype,
31 initialize: function() {
34 this.addEventListener('click', this.handleClick_);
35 this.addEventListener('keydown', this.handleKeyDown_);
36 this.addEventListener('mouseover', this.handleMouseOver_);
41 return this.tile.index;
49 * Clears the DOM hierarchy for this node, setting it back to the default
50 * for a blank thumbnail.
53 this.className = 'most-visited filler real';
55 '<span class="thumbnail-wrapper fills-parent">' +
56 '<div class="close-button"></div>' +
57 '<span class="thumbnail fills-parent">' +
58 // thumbnail-shield provides a gradient fade effect.
59 '<div class="thumbnail-shield fills-parent"></div>' +
61 '<span class="favicon"></span>' +
63 '<div class="color-stripe"></div>' +
64 '<span class="title"></span>';
66 this.querySelector('.close-button').title =
67 loadTimeData.getString('removethumbnailtooltip');
71 this.removeAttribute('id');
76 * Update the appearance of this tile according to |data|.
77 * @param {Object} data A dictionary of relevant data for the page.
79 updateForData: function(data) {
80 if (this.classList.contains('blacklisted') && data) {
81 // Animate appearance of new tile.
82 this.classList.add('new-tile-contents');
84 this.classList.remove('blacklisted');
86 if (!data || data.filler) {
93 this.id = 'most-visited-tile-' + id;
95 this.classList.add('focusable');
97 var faviconDiv = this.querySelector('.favicon');
98 faviconDiv.style.backgroundImage = getFaviconImageSet(data.url);
100 // The favicon should have the same dominant color regardless of the
101 // device pixel ratio the favicon is requested for.
102 chrome.send('getFaviconDominantColor',
103 [getFaviconUrlForCurrentDevicePixelRatio(data.url), this.id]);
105 var title = this.querySelector('.title');
106 title.textContent = data.title;
107 title.dir = data.direction;
110 this.title = data.title;
112 var thumbnailUrl = 'chrome://thumb/' + data.url;
113 this.querySelector('.thumbnail').style.backgroundImage =
116 this.href = data.url;
118 this.classList.remove('filler');
122 * Sets the color of the favicon dominant color bar.
123 * @param {string} color The css-parsable value for the color.
125 set stripeColor(color) {
126 this.querySelector('.color-stripe').style.backgroundColor = color;
130 * Handles a click on the tile.
131 * @param {Event} e The click event.
133 handleClick_: function(e) {
134 if (e.target.classList.contains('close-button')) {
138 ntp.logTimeToClick('MostVisited');
139 // Records an app launch from the most visited page (Chrome will decide
140 // whether the url is an app). TODO(estade): this only works for clicks;
141 // other actions like "open in new tab" from the context menu won't be
142 // recorded. Can this be fixed?
143 chrome.send('recordAppLaunchByURL',
144 [encodeURIComponent(this.href),
145 ntp.APP_LAUNCH.NTP_MOST_VISITED]);
146 // Records the index of this tile.
147 chrome.send('metricsHandler:recordInHistogram',
148 ['NewTabPage.MostVisited', this.index, 8]);
149 // Records the action. This will be available as a time-stamped stream
150 // server-side and can be used to compute time-to-long-dwell.
151 chrome.send('metricsHandler:recordAction', ['MostVisited_Clicked']);
152 chrome.send('mostVisitedAction',
153 [ntp.NtpFollowAction.CLICKED_TILE]);
158 * Allow blacklisting most visited site using the keyboard.
160 handleKeyDown_: function(e) {
161 if (!cr.isMac && e.keyCode == 46 || // Del
162 cr.isMac && e.metaKey && e.keyCode == 8) { // Cmd + Backspace
168 * The mouse has entered a Most Visited tile div. Only log the first
169 * mouseover event. By doing this we solve the issue with the mouseover
170 * event listener that bubbles up to the parent, which would cause it to
171 * fire multiple times even if the mouse stays within one tile.
173 handleMouseOver_: function(e) {
175 var ancestor = findAncestor(e.relatedTarget, function(node) {
178 // If ancestor is null, mouse is entering the parent element.
179 if (ancestor == null)
180 chrome.send('metricsHandler:logMouseover');
184 * Permanently removes a page from Most Visited.
186 blacklist_: function() {
187 this.showUndoNotification_();
188 chrome.send('blacklistURLFromMostVisited', [this.data_.url]);
190 chrome.send('getMostVisited');
191 this.classList.add('blacklisted');
194 showUndoNotification_: function() {
195 var data = this.data_;
197 var doUndo = function() {
198 chrome.send('removeURLsFromMostVisitedBlacklist', [data.url]);
199 self.updateForData(data);
204 text: loadTimeData.getString('undothumbnailremove'),
209 chrome.send('clearMostVisitedURLsBlacklist');
211 text: loadTimeData.getString('restoreThumbnailsShort'),
214 ntp.showNotification(
215 loadTimeData.getString('thumbnailremovednotification'),
220 * Set the size and position of the most visited tile.
221 * @param {number} size The total size of |this|.
222 * @param {number} x The x-position.
223 * @param {number} y The y-position.
226 setBounds: function(size, x, y) {
227 this.style.width = toCssPx(size);
228 this.style.height = toCssPx(heightForWidth(size));
230 this.style.left = toCssPx(x);
231 this.style.right = toCssPx(x);
232 this.style.top = toCssPx(y);
236 * Returns whether this element can be 'removed' from chrome (i.e. whether
237 * the user can drag it onto the trash and expect something to happen).
238 * @return {boolean} True, since most visited pages can always be
241 canBeRemoved: function() {
246 * Removes this element from chrome, i.e. blacklists it.
248 removeFromChrome: function() {
250 this.parentNode.classList.add('finishing-drag');
254 * Called when a drag of this tile has ended (after all animations have
257 finalizeDrag: function() {
258 this.parentNode.classList.remove('finishing-drag');
262 * Called when a drag is starting on the tile. Updates dataTransfer with
263 * data for this tile (for dragging outside of the NTP).
265 setDragData: function(dataTransfer) {
266 dataTransfer.setData('Text', this.data_.title);
267 dataTransfer.setData('URL', this.data_.url);
271 var mostVisitedPageGridValues = {
272 // The fewest tiles we will show in a row.
274 // The most tiles we will show in a row.
277 // The smallest a tile can be.
279 // The biggest a tile can be. 212 (max thumbnail width) + 2.
282 // The padding between tiles, as a fraction of the tile width.
283 tileSpacingFraction: 1 / 8,
285 TilePage.initGridValues(mostVisitedPageGridValues);
288 * Calculates the height for a Most Visited tile for a given width. The size
289 * is based on the thumbnail, which should have a 212:132 ratio.
290 * @return {number} The height.
292 function heightForWidth(width) {
293 // The 2s are for borders, the 31 is for the title.
294 return (width - 2) * 132 / 212 + 2 + 31;
297 var THUMBNAIL_COUNT = 8;
300 * Creates a new MostVisitedPage object.
302 * @extends {TilePage}
304 function MostVisitedPage() {
305 var el = new TilePage(mostVisitedPageGridValues);
306 el.__proto__ = MostVisitedPage.prototype;
312 MostVisitedPage.prototype = {
313 __proto__: TilePage.prototype,
315 initialize: function() {
316 this.classList.add('most-visited-page');
318 this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real');
320 this.addEventListener('carddeselected', this.handleCardDeselected_);
321 this.addEventListener('cardselected', this.handleCardSelected_);
325 * Create blank (filler) tiles.
328 createTiles_: function() {
329 for (var i = 0; i < THUMBNAIL_COUNT; i++) {
330 this.appendTile(new MostVisited());
335 * Update the tiles after a change to |data_|.
337 updateTiles_: function() {
338 for (var i = 0; i < THUMBNAIL_COUNT; i++) {
339 var page = this.data_[i];
340 var tile = this.mostVisitedTiles_[i];
342 if (i >= this.data_.length)
345 tile.updateForData(page);
350 * Handles the 'card deselected' event (i.e. the user clicked to another
352 * @param {Event} e The CardChanged event.
354 handleCardDeselected_: function(e) {
355 if (!document.documentElement.classList.contains('starting-up')) {
356 chrome.send('mostVisitedAction',
357 [ntp.NtpFollowAction.CLICKED_OTHER_NTP_PANE]);
362 * Handles the 'card selected' event (i.e. the user clicked to select the
363 * Most Visited pane).
364 * @param {Event} e The CardChanged event.
366 handleCardSelected_: function(e) {
367 if (!document.documentElement.classList.contains('starting-up'))
368 chrome.send('mostVisitedSelected');
372 * Array of most visited data objects.
379 var startTime = Date.now();
381 // The first time data is set, create the tiles.
384 this.data_ = data.slice(0, THUMBNAIL_COUNT);
386 this.data_ = refreshData(this.data_, data);
390 this.updateFocusableElement();
391 logEvent('mostVisited.layout: ' + (Date.now() - startTime));
395 shouldAcceptDrag: function(e) {
400 heightForWidth: heightForWidth,
404 * Executed once the NTP has loaded. Checks if the Most Visited pane is
405 * shown or not. If it is shown, the 'mostVisitedSelected' message is sent
406 * to the C++ code, to record the fact that the user has seen this pane.
408 MostVisitedPage.onLoaded = function() {
409 if (ntp.getCardSlider() &&
410 ntp.getCardSlider().currentCardValue &&
411 ntp.getCardSlider().currentCardValue.classList
412 .contains('most-visited-page')) {
413 chrome.send('mostVisitedSelected');
418 * We've gotten additional Most Visited data. Update our old data with the
419 * new data. The ordering of the new data is not important, except when a
420 * page is pinned. Thus we try to minimize re-ordering.
421 * @param {Array} oldData The current Most Visited page list.
422 * @param {Array} newData The new Most Visited page list.
423 * @return {Array} The merged page list that should replace the current page
426 function refreshData(oldData, newData) {
427 oldData = oldData.slice(0, THUMBNAIL_COUNT);
428 newData = newData.slice(0, THUMBNAIL_COUNT);
430 // Copy over pinned sites directly.
431 for (var j = 0; j < newData.length; j++) {
432 if (newData[j].pinned) {
433 oldData[j] = newData[j];
434 // Mark the entry as 'updated' so we don't try to update again.
435 oldData[j].updated = true;
436 // Mark the newData page as 'used' so we don't try to re-use it.
437 newData[j].used = true;
441 // Look through old pages; if they exist in the newData list, keep them
443 for (var i = 0; i < oldData.length; i++) {
444 if (!oldData[i] || oldData[i].updated)
447 for (var j = 0; j < newData.length; j++) {
451 if (newData[j].url == oldData[i].url) {
452 // The background image and other data may have changed.
453 oldData[i] = newData[j];
454 oldData[i].updated = true;
455 newData[j].used = true;
461 // Look through old pages that haven't been updated yet; replace them.
462 for (var i = 0; i < oldData.length; i++) {
463 if (oldData[i] && oldData[i].updated)
466 for (var j = 0; j < newData.length; j++) {
470 oldData[i] = newData[j];
471 oldData[i].updated = true;
472 newData[j].used = true;
476 if (oldData[i] && !oldData[i].updated)
480 // Clear 'updated' flags so this function will work next time it's called.
481 for (var i = 0; i < THUMBNAIL_COUNT; i++) {
483 oldData[i].updated = false;
490 MostVisitedPage: MostVisitedPage,
491 refreshData: refreshData,
495 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded);