Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3356)

Unified Diff: chrome/browser/resources/ntp_search/thumbnail_page.js

Issue 10829131: Refactoring NTP5: new implementation of TilePage and MostVisitedPage (which now inherits from Thumb… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Manually fixing mock_data_autogen.js lines with more than 80 chars. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/ntp_search/thumbnail_page.js
diff --git a/chrome/browser/resources/ntp_search/thumbnail_page.js b/chrome/browser/resources/ntp_search/thumbnail_page.js
index 8b005b93a45cfa1f297943308c6adc8f5ed1b5fd..b0b596f549c70eddad15ca8478a8c1bc531eadab 100644
--- a/chrome/browser/resources/ntp_search/thumbnail_page.js
+++ b/chrome/browser/resources/ntp_search/thumbnail_page.js
@@ -5,41 +5,41 @@
cr.define('ntp', function() {
'use strict';
- var TilePage = ntp.TilePage;
+ var Tile = ntp.Tile2;
+ var TilePage = ntp.TilePage2;
/**
- * A counter for generating unique tile IDs.
- */
- var tileID = 0;
-
- /**
- * Creates a new Most Visited object for tiling.
+ * Creates a new Thumbnail object for tiling.
* @constructor
+ * @extends {Tile}
* @extends {HTMLAnchorElement}
+ * @param {Object} config Tile page configuration object.
*/
- function MostVisited() {
+ function Thumbnail(config) {
var el = cr.doc.createElement('a');
- el.__proto__ = MostVisited.prototype;
- el.initialize();
+ el.__proto__ = Thumbnail.prototype;
+ el.initialize(config);
return el;
}
- MostVisited.prototype = {
+ Thumbnail.prototype = Tile.subclass({
__proto__: HTMLAnchorElement.prototype,
- initialize: function() {
+ /**
+ * Initializes a Thumbnail.
+ * @param {Object} config TilePage configuration object.
+ */
+ initialize: function(config) {
+ Tile.prototype.initialize.apply(this, arguments);
+ this.classList.add('thumbnail');
this.reset();
-
- this.addEventListener('click', this.handleClick_);
- this.addEventListener('keydown', this.handleKeyDown_);
- },
-
- get index() {
- assert(this.tile);
- return this.tile.index;
},
+ /**
+ * Thumbnail data object.
+ * @type {Object}
+ */
get data() {
return this.data_;
},
@@ -49,25 +49,12 @@ cr.define('ntp', function() {
* for a blank thumbnail.
*/
reset: function() {
- this.className = 'most-visited filler real';
this.innerHTML =
- '<span class="thumbnail-wrapper fills-parent">' +
- '<div class="close-button"></div>' +
- '<span class="thumbnail fills-parent">' +
- // thumbnail-shield provides a gradient fade effect.
- '<div class="thumbnail-shield fills-parent"></div>' +
- '</span>' +
- '<span class="favicon"></span>' +
- '</span>' +
- '<div class="color-stripe"></div>' +
+ '<span class="thumbnail-image"></span>' +
'<span class="title"></span>';
- this.querySelector('.close-button').title =
- loadTimeData.getString('removethumbnailtooltip');
-
this.tabIndex = -1;
this.data_ = null;
- this.removeAttribute('id');
this.title = '';
},
@@ -76,28 +63,23 @@ cr.define('ntp', function() {
* @param {Object} data A dictionary of relevant data for the page.
*/
updateForData: function(data) {
- if (this.classList.contains('blacklisted') && data) {
- // Animate appearance of new tile.
- this.classList.add('new-tile-contents');
- }
- this.classList.remove('blacklisted');
-
+ // TODO(pedrosimonetti): Remove data.filler usage everywhere.
if (!data || data.filler) {
if (this.data_)
this.reset();
return;
}
- var id = tileID++;
- this.id = 'most-visited-tile-' + id;
this.data_ = data;
- this.classList.add('focusable');
- var faviconDiv = this.querySelector('.favicon');
- var faviconUrl = 'chrome://favicon/size/16/' + data.url;
- faviconDiv.style.backgroundImage = url(faviconUrl);
- chrome.send('getFaviconDominantColor', [faviconUrl, this.id]);
+ this.formatThumbnail_(data);
+ },
+ /**
+ * Update the appearance of this tile according to |data|.
+ * @param {Object} data A dictionary of relevant data for the page.
+ */
+ formatThumbnail_: function(data) {
var title = this.querySelector('.title');
title.textContent = data.title;
title.dir = data.direction;
@@ -105,205 +87,67 @@ cr.define('ntp', function() {
// Sets the tooltip.
this.title = data.title;
- var thumbnailUrl = 'chrome://thumb/' + data.url;
- this.querySelector('.thumbnail').style.backgroundImage =
+ var thumbnailUrl = ntp.getThumbnailUrl(data.url);
+ this.querySelector('.thumbnail-image').style.backgroundImage =
url(thumbnailUrl);
this.href = data.url;
-
- this.classList.remove('filler');
- },
-
- /**
- * Sets the color of the favicon dominant color bar.
- * @param {string} color The css-parsable value for the color.
- */
- set stripeColor(color) {
- this.querySelector('.color-stripe').style.backgroundColor = color;
- },
-
- /**
- * Handles a click on the tile.
- * @param {Event} e The click event.
- */
- handleClick_: function(e) {
- if (e.target.classList.contains('close-button')) {
- this.blacklist_();
- e.preventDefault();
- } else {
- // Records an app launch from the most visited page (Chrome will decide
- // whether the url is an app). TODO(estade): this only works for clicks;
- // other actions like "open in new tab" from the context menu won't be
- // recorded. Can this be fixed?
- chrome.send('recordAppLaunchByURL',
- [encodeURIComponent(this.href),
- ntp.APP_LAUNCH.NTP_MOST_VISITED]);
- // Records the index of this tile.
- chrome.send('metricsHandler:recordInHistogram',
- ['NewTabPage.MostVisited', this.index, 8]);
- chrome.send('mostVisitedAction',
- [ntp.NtpFollowAction.CLICKED_TILE]);
- }
- },
-
- /**
- * Allow blacklisting most visited site using the keyboard.
- */
- handleKeyDown_: function(e) {
- if (!cr.isMac && e.keyCode == 46 || // Del
- cr.isMac && e.metaKey && e.keyCode == 8) { // Cmd + Backspace
- this.blacklist_();
- }
- },
-
- /**
- * Permanently removes a page from Most Visited.
- */
- blacklist_: function() {
- this.showUndoNotification_();
- chrome.send('blacklistURLFromMostVisited', [this.data_.url]);
- this.reset();
- chrome.send('getMostVisited');
- this.classList.add('blacklisted');
- },
-
- showUndoNotification_: function() {
- var data = this.data_;
- var self = this;
- var doUndo = function() {
- chrome.send('removeURLsFromMostVisitedBlacklist', [data.url]);
- self.updateForData(data);
- }
-
- var undo = {
- action: doUndo,
- text: loadTimeData.getString('undothumbnailremove'),
- };
-
- var undoAll = {
- action: function() {
- chrome.send('clearMostVisitedURLsBlacklist');
- },
- text: loadTimeData.getString('restoreThumbnailsShort'),
- };
-
- ntp.showNotification(
- loadTimeData.getString('thumbnailremovednotification'),
- [undo, undoAll]);
- },
-
- /**
- * Set the size and position of the most visited tile.
- * @param {number} size The total size of |this|.
- * @param {number} x The x-position.
- * @param {number} y The y-position.
- * animate.
- */
- setBounds: function(size, x, y) {
- this.style.width = toCssPx(size);
- this.style.height = toCssPx(heightForWidth(size));
-
- this.style.left = toCssPx(x);
- this.style.right = toCssPx(x);
- this.style.top = toCssPx(y);
- },
-
- /**
- * Returns whether this element can be 'removed' from chrome (i.e. whether
- * the user can drag it onto the trash and expect something to happen).
- * @return {boolean} True, since most visited pages can always be
- * blacklisted.
- */
- canBeRemoved: function() {
- return true;
},
-
- /**
- * Removes this element from chrome, i.e. blacklists it.
- */
- removeFromChrome: function() {
- this.blacklist_();
- this.parentNode.classList.add('finishing-drag');
- },
-
- /**
- * Called when a drag of this tile has ended (after all animations have
- * finished).
- */
- finalizeDrag: function() {
- this.parentNode.classList.remove('finishing-drag');
- },
-
- /**
- * Called when a drag is starting on the tile. Updates dataTransfer with
- * data for this tile (for dragging outside of the NTP).
- */
- setDragData: function(dataTransfer) {
- dataTransfer.setData('Text', this.data_.title);
- dataTransfer.setData('URL', this.data_.url);
- },
- };
-
- var mostVisitedPageGridValues = {
- // The fewest tiles we will show in a row.
- minColCount: 2,
- // The most tiles we will show in a row.
- maxColCount: 4,
-
- // The smallest a tile can be.
- minTileWidth: 122,
- // The biggest a tile can be. 212 (max thumbnail width) + 2.
- maxTileWidth: 214,
-
- // The padding between tiles, as a fraction of the tile width.
- tileSpacingFraction: 1 / 8,
- };
- TilePage.initGridValues(mostVisitedPageGridValues);
+ });
/**
- * Calculates the height for a Most Visited tile for a given width. The size
- * is based on the thumbnail, which should have a 212:132 ratio.
- * @return {number} The height.
- */
- function heightForWidth(width) {
- // The 2s are for borders, the 31 is for the title.
- return (width - 2) * 132 / 212 + 2 + 31;
- }
-
- var THUMBNAIL_COUNT = 8;
-
- /**
- * Creates a new MostVisitedPage object.
+ * Creates a new ThumbnailPage object.
* @constructor
* @extends {TilePage}
*/
- function MostVisitedPage() {
- var el = new TilePage(mostVisitedPageGridValues);
- el.__proto__ = MostVisitedPage.prototype;
- el.initialize();
+ function ThumbnailPage() {
+ var el = new TilePage();
+ el.__proto__ = ThumbnailPage.prototype;
return el;
}
- MostVisitedPage.prototype = {
+ ThumbnailPage.prototype = {
__proto__: TilePage.prototype,
+ config_: {
+ // The width of a cell.
+ cellWidth: 132,
+ // The start margin of a cell (left or right according to text direction).
+ cellMarginStart: 18,
+ // The border panel horizontal margin.
+ bottomPanelHorizontalMargin: 100,
+ // The height of the tile row.
+ rowHeight: 105,
+ // The maximum number of Tiles to be displayed.
+ maxTileCount: 10
+ },
+
+ // Thumbnail class used in this TilePage.
+ ThumbnailClass: Thumbnail,
+
+ /**
+ * Initializes a ThumbnailPage.
+ */
initialize: function() {
- this.classList.add('most-visited-page');
+ this.classList.add('thumbnail-page');
this.data_ = null;
- this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real');
this.addEventListener('carddeselected', this.handleCardDeselected_);
this.addEventListener('cardselected', this.handleCardSelected_);
},
/**
- * Create blank (filler) tiles.
+ * Create blank tiles.
* @private
- */
- createTiles_: function() {
- for (var i = 0; i < THUMBNAIL_COUNT; i++) {
- this.appendTile(new MostVisited());
+ * @param {number} count The number of Tiles to be created.
+ */
+ createTiles_: function(count) {
+ var Class = this.ThumbnailClass;
+ var config = this.config_;
+ count = Math.min(count, config.maxTileCount);
+ for (var i = 0; i < count; i++) {
+ this.appendTile(new Class(config));
}
},
@@ -311,11 +155,18 @@ cr.define('ntp', function() {
* Update the tiles after a change to |data_|.
*/
updateTiles_: function() {
- for (var i = 0; i < THUMBNAIL_COUNT; i++) {
- var page = this.data_[i];
- var tile = this.mostVisitedTiles_[i];
+ var maxTileCount = this.config_.maxTileCount;
+ var data = this.data_;
+ var tiles = this.tiles;
+ for (var i = 0; i < maxTileCount; i++) {
+ var page = data[i];
+ var tile = tiles[i];
+
+ // TODO(pedrosimonetti): What do we do when there's no tile here?
+ if (!tile)
+ return;
- if (i >= this.data_.length)
+ if (i >= data.length)
tile.reset();
else
tile.updateForData(page);
@@ -328,143 +179,38 @@ cr.define('ntp', function() {
* @param {Event} e The CardChanged event.
*/
handleCardDeselected_: function(e) {
- if (!document.documentElement.classList.contains('starting-up')) {
- chrome.send('mostVisitedAction',
- [ntp.NtpFollowAction.CLICKED_OTHER_NTP_PANE]);
- }
+ console.error('ThumbnailPage: handleCardDeselected_ is not implemented.');
},
/**
* Handles the 'card selected' event (i.e. the user clicked to select the
- * Most Visited pane).
+ * this page's pane).
* @param {Event} e The CardChanged event.
*/
handleCardSelected_: function(e) {
- if (!document.documentElement.classList.contains('starting-up'))
- chrome.send('mostVisitedSelected');
+ console.error('ThumbnailPage: handleCardSelected_ is not implemented.');
},
/**
- * Array of most visited data objects.
+ * Array of thumbnail data objects.
* @type {Array}
*/
get data() {
return this.data_;
},
set data(data) {
- var startTime = Date.now();
-
- // The first time data is set, create the tiles.
- if (!this.data_) {
- this.createTiles_();
- this.data_ = data.slice(0, THUMBNAIL_COUNT);
- } else {
- this.data_ = refreshData(this.data_, data);
- }
-
- this.updateTiles_();
- logEvent('mostVisited.layout: ' + (Date.now() - startTime));
+ console.error('ThumbnailPage: data_ setter is not implemented.');
},
/** @inheritDoc */
shouldAcceptDrag: function(e) {
return false;
},
-
- /** @inheritDoc */
- heightForWidth: heightForWidth,
- };
-
- /**
- * Executed once the NTP has loaded. Checks if the Most Visited pane is
- * shown or not. If it is shown, the 'mostVisitedSelected' message is sent
- * to the C++ code, to record the fact that the user has seen this pane.
- */
- MostVisitedPage.onLoaded = function() {
- if (ntp.getCardSlider() &&
- ntp.getCardSlider().currentCardValue &&
- ntp.getCardSlider().currentCardValue.classList
- .contains('most-visited-page')) {
- chrome.send('mostVisitedSelected');
- }
- }
-
- /**
- * We've gotten additional Most Visited data. Update our old data with the
- * new data. The ordering of the new data is not important, except when a
- * page is pinned. Thus we try to minimize re-ordering.
- * @param {Array} oldData The current Most Visited page list.
- * @param {Array} newData The new Most Visited page list.
- * @return {Array} The merged page list that should replace the current page
- * list.
- */
- function refreshData(oldData, newData) {
- oldData = oldData.slice(0, THUMBNAIL_COUNT);
- newData = newData.slice(0, THUMBNAIL_COUNT);
-
- // Copy over pinned sites directly.
- for (var j = 0; j < newData.length; j++) {
- if (newData[j].pinned) {
- oldData[j] = newData[j];
- // Mark the entry as 'updated' so we don't try to update again.
- oldData[j].updated = true;
- // Mark the newData page as 'used' so we don't try to re-use it.
- newData[j].used = true;
- }
- }
-
- // Look through old pages; if they exist in the newData list, keep them
- // where they are.
- for (var i = 0; i < oldData.length; i++) {
- if (!oldData[i] || oldData[i].updated)
- continue;
-
- for (var j = 0; j < newData.length; j++) {
- if (newData[j].used)
- continue;
-
- if (newData[j].url == oldData[i].url) {
- // The background image and other data may have changed.
- oldData[i] = newData[j];
- oldData[i].updated = true;
- newData[j].used = true;
- break;
- }
- }
- }
-
- // Look through old pages that haven't been updated yet; replace them.
- for (var i = 0; i < oldData.length; i++) {
- if (oldData[i] && oldData[i].updated)
- continue;
-
- for (var j = 0; j < newData.length; j++) {
- if (newData[j].used)
- continue;
-
- oldData[i] = newData[j];
- oldData[i].updated = true;
- newData[j].used = true;
- break;
- }
-
- if (oldData[i] && !oldData[i].updated)
- oldData[i] = null;
- }
-
- // Clear 'updated' flags so this function will work next time it's called.
- for (var i = 0; i < THUMBNAIL_COUNT; i++) {
- if (oldData[i])
- oldData[i].updated = false;
- }
-
- return oldData;
};
return {
- MostVisitedPage: MostVisitedPage,
- refreshData: refreshData,
+ Thumbnail: Thumbnail,
+ ThumbnailPage: ThumbnailPage
};
});
-document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded);
« no previous file with comments | « chrome/browser/resources/ntp_search/thumbnail_page.css ('k') | chrome/browser/resources/ntp_search/tile_page.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698