Chromium Code Reviews| 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..503526e715f26e907b63844b523334e13572e2a1 100644 |
| --- a/chrome/browser/resources/ntp_search/thumbnail_page.js |
| +++ b/chrome/browser/resources/ntp_search/thumbnail_page.js |
| @@ -5,7 +5,8 @@ |
| 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. |
| @@ -13,31 +14,29 @@ cr.define('ntp', function() { |
| 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} gridValues Tile page configuration object. |
| */ |
| - function MostVisited() { |
| + function Thumbnail(gridValues) { |
| var el = cr.doc.createElement('a'); |
| - el.__proto__ = MostVisited.prototype; |
| - el.initialize(); |
| + el.__proto__ = Thumbnail.prototype; |
| + el.initialize(gridValues); |
| return el; |
| } |
| - MostVisited.prototype = { |
| + Thumbnail.prototype = Tile.subclass({ |
| __proto__: HTMLAnchorElement.prototype, |
| - initialize: function() { |
| - this.reset(); |
| - |
| - this.addEventListener('click', this.handleClick_); |
| - this.addEventListener('keydown', this.handleKeyDown_); |
| - }, |
| + // TODO(pedrosimonetti): remove this property here. move to most visited. |
| + isRemovable: true, |
| - get index() { |
| - assert(this.tile); |
| - return this.tile.index; |
| + initialize: function(gridValues) { |
| + Tile.prototype.initialize.apply(this, arguments); |
| + this.reset(); |
| }, |
| get data() { |
| @@ -49,21 +48,19 @@ cr.define('ntp', function() { |
| * for a blank thumbnail. |
| */ |
| reset: function() { |
| - this.className = 'most-visited filler real'; |
| + this.className = this.baseClassName; |
| 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 class="thumbnail-wrapper">' + |
| + (this.isRemovable ? '<div class="close-button"></div>' : '') + |
| + '<span class="thumbnail-image">' + |
| '</span>' + |
| - '<span class="favicon"></span>' + |
| '</span>' + |
| - '<div class="color-stripe"></div>' + |
| '<span class="title"></span>'; |
| - this.querySelector('.close-button').title = |
| - loadTimeData.getString('removethumbnailtooltip'); |
| + if (this.isRemovable) { |
| + this.querySelector('.close-button').title = |
| + loadTimeData.getString('removethumbnailtooltip'); |
| + } |
| this.tabIndex = -1; |
| this.data_ = null; |
| @@ -89,15 +86,11 @@ cr.define('ntp', function() { |
| } |
| var id = tileID++; |
| + // TODO(pedrosimonetti): refactor |
| 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]); |
| - |
| var title = this.querySelector('.title'); |
| title.textContent = data.title; |
| title.dir = data.direction; |
| @@ -105,8 +98,8 @@ 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; |
| @@ -115,112 +108,9 @@ cr.define('ntp', function() { |
| }, |
| /** |
| - * 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. |
| */ |
| + // TODO(xci): delete? this seems to be used only by trash. |
| removeFromChrome: function() { |
| this.blacklist_(); |
| this.parentNode.classList.add('finishing-drag'); |
| @@ -238,60 +128,58 @@ cr.define('ntp', function() { |
| * Called when a drag is starting on the tile. Updates dataTransfer with |
| * data for this tile (for dragging outside of the NTP). |
| */ |
| + // TODO(xci) delete |
| 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; |
| + var THUMBNAIL_COUNT = 10; // TODO(xci) |
| /** |
| - * Creates a new MostVisitedPage object. |
| + * Creates a new ThumbnailPage object. |
| * @constructor |
| * @extends {TilePage} |
| */ |
| - function MostVisitedPage() { |
| - var el = new TilePage(mostVisitedPageGridValues); |
| - el.__proto__ = MostVisitedPage.prototype; |
| + function ThumbnailPage() { |
| + var el = new TilePage(); |
| + el.__proto__ = ThumbnailPage.prototype; |
| el.initialize(); |
| return el; |
| } |
| - MostVisitedPage.prototype = { |
| + ThumbnailPage.prototype = { |
| __proto__: TilePage.prototype, |
| + gridValues_: { |
| + tileWidth: 130, |
| + tileHeight: 78, |
| + tileHorMargin: 18, // TODO margin with CSS. There's no margin in first col |
|
Evan Stade
2012/08/07 21:57:12
TODOs must have authors and the comment should be
pedrosimonetti2
2012/08/08 08:14:22
Done.
|
| + tileVerMargin: 22, |
| + tileBorderWidth: 1, |
| + bottomPanelHorMargin: 100, |
| + |
| + tileCount: 10, // debug |
| + tileClassName: 'thumbnail', |
| + // TODO(pedrosimonetti): why not always use JavaScript? |
| + // If reinforceStyles is true, the sizes of TileCell and Tile will be |
| + // changed with JavaScript everytime one of those objects is instantiated. |
| + // If you want to use CSS to do that, you can set this value to false. |
| + reinforceStyles: true, |
| + |
| + // debug |
|
Evan Stade
2012/08/07 21:57:12
comments should be sentence-like.
pedrosimonetti2
2012/08/08 08:14:22
Done.
|
| + slowFactor: 1, |
| + debug: false |
| + }, |
| + |
| + // TODO(pedrosimonetti): document |
| + ThumbnailClass: Thumbnail, |
| + |
| 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_); |
| @@ -300,10 +188,11 @@ cr.define('ntp', function() { |
| /** |
| * Create blank (filler) tiles. |
| * @private |
| + * @param {number} count The number of Tiles to be created. |
| */ |
| - createTiles_: function() { |
| - for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
| - this.appendTile(new MostVisited()); |
| + createTiles_: function(count) { |
| + for (var i = 0; i < count; i++) { |
| + this.appendTile(new this.ThumbnailClass(this.gridValues_)); |
| } |
| }, |
| @@ -313,7 +202,12 @@ cr.define('ntp', function() { |
| updateTiles_: function() { |
| for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
|
Evan Stade
2012/08/07 21:57:12
why is this still THUMBNAIL_COUNT?
pedrosimonetti2
2012/08/08 08:14:22
Code has been refactored.
|
| var page = this.data_[i]; |
| - var tile = this.mostVisitedTiles_[i]; |
| + var tile = this.tileElements[i]; |
| + |
| + // TODO(pedrosimonetti): what do we do when there's no tile here? |
| + if (!tile) { |
|
Evan Stade
2012/08/07 21:57:12
no curlies
pedrosimonetti2
2012/08/08 08:14:22
Done.
|
| + return; |
| + } |
| if (i >= this.data_.length) |
| tile.reset(); |
| @@ -329,6 +223,7 @@ cr.define('ntp', function() { |
| */ |
| handleCardDeselected_: function(e) { |
| if (!document.documentElement.classList.contains('starting-up')) { |
| + // TODO(xci) logging |
| chrome.send('mostVisitedAction', |
| [ntp.NtpFollowAction.CLICKED_OTHER_NTP_PANE]); |
| } |
| @@ -336,51 +231,41 @@ cr.define('ntp', function() { |
| /** |
| * 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')) |
| + // TODO(xci) logging |
| chrome.send('mostVisitedSelected'); |
| }, |
| /** |
| - * 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); |
| - } |
|
Evan Stade
2012/08/07 21:57:12
this newline intentionally omitted
pedrosimonetti2
2012/08/08 08:14:22
Done.
|
| - this.updateTiles_(); |
| - logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
| + set data(data) { |
| + throw 'Each ThumbnailPage subclass should implement the data setter.'; |
|
Evan Stade
2012/08/07 21:57:12
usually we just console.error. I guess you could t
pedrosimonetti2
2012/08/08 08:14:22
Done.
|
| }, |
| /** @inheritDoc */ |
| shouldAcceptDrag: function(e) { |
| return false; |
| }, |
| - |
| - /** @inheritDoc */ |
| - heightForWidth: heightForWidth, |
| }; |
| /** |
| - * Executed once the NTP has loaded. Checks if the Most Visited pane is |
| + * Executed once the NTP has loaded. Checks if the this page's 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() { |
| + // TODO(pedrosimonetti) is it possible to keep this inside Thumbnail class? |
|
Evan Stade
2012/08/07 21:57:12
put inside the doc block.
pedrosimonetti2
2012/08/08 08:14:22
Code removed.
On 2012/08/07 21:57:12, Evan Stade
|
| + /* |
| + ThumbnailPage.onLoaded = function() { |
| if (ntp.getCardSlider() && |
| ntp.getCardSlider().currentCardValue && |
| ntp.getCardSlider().currentCardValue.classList |
| @@ -388,83 +273,11 @@ cr.define('ntp', function() { |
| 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); |