Index: chrome/browser/resources/ntp_search/most_visited_page.js |
diff --git a/chrome/browser/resources/ntp_search/most_visited_page.js b/chrome/browser/resources/ntp_search/most_visited_page.js |
index 8b005b93a45cfa1f297943308c6adc8f5ed1b5fd..e66fb5694ef935323cbcdfe2d91f465a8f6e0867 100644 |
--- a/chrome/browser/resources/ntp_search/most_visited_page.js |
+++ b/chrome/browser/resources/ntp_search/most_visited_page.js |
@@ -5,70 +5,49 @@ |
cr.define('ntp', function() { |
'use strict'; |
- var TilePage = ntp.TilePage; |
- |
- /** |
- * A counter for generating unique tile IDs. |
- */ |
- var tileID = 0; |
+ var Thumbnail = ntp.Thumbnail; |
+ var ThumbnailPage = ntp.ThumbnailPage; |
/** |
* Creates a new Most Visited object for tiling. |
* @constructor |
+ * @extends {Thumbnail} |
* @extends {HTMLAnchorElement} |
+ * @param {Object} config Tile page configuration object. |
*/ |
- function MostVisited() { |
+ function MostVisited(config) { |
var el = cr.doc.createElement('a'); |
el.__proto__ = MostVisited.prototype; |
- el.initialize(); |
+ el.initialize(config); |
return el; |
} |
MostVisited.prototype = { |
- __proto__: HTMLAnchorElement.prototype, |
+ __proto__: Thumbnail.prototype, |
- initialize: function() { |
- this.reset(); |
+ /** |
+ * Initializes a MostVisited Thumbnail. |
+ * @param {Object} config TilePage configuration object. |
+ */ |
+ initialize: function(config) { |
+ Thumbnail.prototype.initialize.apply(this, arguments); |
this.addEventListener('click', this.handleClick_); |
this.addEventListener('keydown', this.handleKeyDown_); |
}, |
- get index() { |
- assert(this.tile); |
- return this.tile.index; |
- }, |
- |
- get data() { |
- return this.data_; |
- }, |
- |
/** |
* Clears the DOM hierarchy for this node, setting it back to the default |
* 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="title"></span>'; |
- |
- this.querySelector('.close-button').title = |
- loadTimeData.getString('removethumbnailtooltip'); |
- |
- this.tabIndex = -1; |
- this.data_ = null; |
- this.removeAttribute('id'); |
- this.title = ''; |
+ Thumbnail.prototype.reset.apply(this, arguments); |
+ |
+ var closeButton = cr.doc.createElement('div'); |
+ closeButton.className = 'close-button'; |
+ closeButton.title = loadTimeData.getString('removethumbnailtooltip'); |
+ this.appendChild(closeButton); |
}, |
/** |
@@ -82,49 +61,13 @@ cr.define('ntp', function() { |
} |
this.classList.remove('blacklisted'); |
- 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]); |
- |
- var title = this.querySelector('.title'); |
- title.textContent = data.title; |
- title.dir = data.direction; |
- |
- // Sets the tooltip. |
- this.title = data.title; |
- |
- var thumbnailUrl = 'chrome://thumb/' + data.url; |
- this.querySelector('.thumbnail').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; |
+ Thumbnail.prototype.updateForData.apply(this, arguments); |
}, |
/** |
* Handles a click on the tile. |
* @param {Event} e The click event. |
+ * @private |
*/ |
handleClick_: function(e) { |
if (e.target.classList.contains('close-button')) { |
@@ -148,6 +91,7 @@ cr.define('ntp', function() { |
/** |
* Allow blacklisting most visited site using the keyboard. |
+ * @private |
*/ |
handleKeyDown_: function(e) { |
if (!cr.isMac && e.keyCode == 46 || // Del |
@@ -158,6 +102,7 @@ cr.define('ntp', function() { |
/** |
* Permanently removes a page from Most Visited. |
+ * @private |
*/ |
blacklist_: function() { |
this.showUndoNotification_(); |
@@ -167,13 +112,17 @@ cr.define('ntp', function() { |
this.classList.add('blacklisted'); |
}, |
+ /** |
+ * Shows the undo notification when blacklisting a most visited site. |
+ * @private |
+ */ |
showUndoNotification_: function() { |
var data = this.data_; |
var self = this; |
var doUndo = function() { |
chrome.send('removeURLsFromMostVisitedBlacklist', [data.url]); |
self.updateForData(data); |
- } |
+ }; |
var undo = { |
action: doUndo, |
@@ -193,22 +142,6 @@ cr.define('ntp', function() { |
}, |
/** |
- * 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 |
@@ -216,61 +149,8 @@ cr.define('ntp', function() { |
*/ |
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. |
@@ -278,7 +158,7 @@ cr.define('ntp', function() { |
* @extends {TilePage} |
*/ |
function MostVisitedPage() { |
- var el = new TilePage(mostVisitedPageGridValues); |
+ var el = new ThumbnailPage(); |
el.__proto__ = MostVisitedPage.prototype; |
el.initialize(); |
@@ -286,45 +166,23 @@ cr.define('ntp', function() { |
} |
MostVisitedPage.prototype = { |
- __proto__: TilePage.prototype, |
- |
- initialize: function() { |
- this.classList.add('most-visited-page'); |
- this.data_ = null; |
- this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real'); |
+ __proto__: ThumbnailPage.prototype, |
- this.addEventListener('carddeselected', this.handleCardDeselected_); |
- this.addEventListener('cardselected', this.handleCardSelected_); |
- }, |
+ ThumbnailClass: MostVisited, |
/** |
- * Create blank (filler) tiles. |
- * @private |
+ * Initializes a MostVisitedPage. |
*/ |
- createTiles_: function() { |
- for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
- this.appendTile(new MostVisited()); |
- } |
- }, |
+ initialize: function() { |
+ ThumbnailPage.prototype.initialize.apply(this, arguments); |
- /** |
- * 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]; |
- |
- if (i >= this.data_.length) |
- tile.reset(); |
- else |
- tile.updateForData(page); |
- } |
+ this.classList.add('most-visited-page'); |
}, |
/** |
* Handles the 'card deselected' event (i.e. the user clicked to another |
* pane). |
+ * @private |
* @param {Event} e The CardChanged event. |
*/ |
handleCardDeselected_: function(e) { |
@@ -336,43 +194,39 @@ cr.define('ntp', function() { |
/** |
* Handles the 'card selected' event (i.e. the user clicked to select the |
- * Most Visited pane). |
+ * this page's pane). |
+ * @private |
* @param {Event} e The CardChanged event. |
*/ |
handleCardSelected_: function(e) { |
- if (!document.documentElement.classList.contains('starting-up')) |
+ if (!document.documentElement.classList.contains('starting-up')) { |
+ this.layout_(); |
chrome.send('mostVisitedSelected'); |
+ } |
}, |
/** |
- * Array of most visited data objects. |
- * @type {Array} |
+ * Sets the data that will be used to create Thumbnails. |
+ * TODO(pedrosimonetti): Move data handling related code to TilePage. Make |
+ * sure the new logic works with Apps without requiring duplicating code. |
+ * @param {Array} data The array of data. |
+ * @type (Array} |
*/ |
- get data() { |
- return this.data_; |
- }, |
set data(data) { |
var startTime = Date.now(); |
+ var maxTileCount = this.config_.maxTileCount; |
// The first time data is set, create the tiles. |
if (!this.data_) { |
- this.createTiles_(); |
- this.data_ = data.slice(0, THUMBNAIL_COUNT); |
+ this.data_ = data.slice(0, maxTileCount); |
+ this.createTiles_(this.data_.length); |
} else { |
- this.data_ = refreshData(this.data_, data); |
+ this.data_ = refreshData(this.data_, data, maxTileCount); |
} |
this.updateTiles_(); |
logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
}, |
- |
- /** @inheritDoc */ |
- shouldAcceptDrag: function(e) { |
- return false; |
- }, |
- |
- /** @inheritDoc */ |
- heightForWidth: heightForWidth, |
}; |
/** |
@@ -385,9 +239,10 @@ cr.define('ntp', function() { |
ntp.getCardSlider().currentCardValue && |
ntp.getCardSlider().currentCardValue.classList |
.contains('most-visited-page')) { |
+ ntp.getCardSlider().currentCardValue.layout_(true); |
chrome.send('mostVisitedSelected'); |
} |
- } |
+ }; |
/** |
* We've gotten additional Most Visited data. Update our old data with the |
@@ -398,9 +253,9 @@ cr.define('ntp', function() { |
* @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); |
+ function refreshData(oldData, newData, maxTileCount) { |
+ oldData = oldData.slice(0, maxTileCount); |
+ newData = newData.slice(0, maxTileCount); |
// Copy over pinned sites directly. |
for (var j = 0; j < newData.length; j++) { |
@@ -453,13 +308,13 @@ cr.define('ntp', function() { |
} |
// Clear 'updated' flags so this function will work next time it's called. |
- for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
+ for (var i = 0; i < maxTileCount; i++) { |
if (oldData[i]) |
oldData[i].updated = false; |
} |
return oldData; |
- }; |
+ } |
return { |
MostVisitedPage: MostVisitedPage, |