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 Suggestions page object for tiling.
18 * @extends {HTMLAnchorElement}
20 function Suggestion() {
21 var el
= cr
.doc
.createElement('a');
22 el
.__proto__
= Suggestion
.prototype;
28 Suggestion
.prototype = {
29 __proto__
: HTMLAnchorElement
.prototype,
31 initialize: function() {
34 this.addEventListener('click', this.handleClick_
);
35 this.addEventListener('keydown', this.handleKeyDown_
);
40 return this.tile
.index
;
48 * Clears the DOM hierarchy for this node, setting it back to the default
49 * for a blank thumbnail. TODO(georgey) make it a template.
52 this.className
= 'suggestions filler real';
54 '<span class="thumbnail-wrapper fills-parent">' +
55 '<div class="close-button"></div>' +
56 '<span class="thumbnail fills-parent">' +
57 // thumbnail-shield provides a gradient fade effect.
58 '<div class="thumbnail-shield fills-parent"></div>' +
60 '<span class="favicon"></span>' +
62 '<div class="color-stripe"></div>' +
63 '<span class="title"></span>' +
64 '<span class="score"></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
= 'suggestions-tile-' + id
;
95 this.classList
.add('focusable');
97 var faviconDiv
= this.querySelector('.favicon');
98 var faviconUrl
= 'chrome://favicon/size/16@1x/' + data
.url
;
99 faviconDiv
.style
.backgroundImage
= url(faviconUrl
);
100 chrome
.send('getFaviconDominantColor', [faviconUrl
, this.id
]);
102 var title
= this.querySelector('.title');
103 title
.textContent
= data
.title
;
104 title
.dir
= data
.direction
;
106 var score
= this.querySelector('.score');
107 score
.textContent
= data
.score
;
110 this.title
= data
.title
;
113 thumbnailUrl
= data
.urlImage
? data
.urlImage
:
114 'chrome://thumb/' + data
.url
;
116 this.querySelector('.thumbnail').style
.backgroundImage
=
119 this.href
= data
.url
;
121 this.classList
.remove('filler');
125 * Sets the color of the favicon dominant color bar.
126 * @param {string} color The css-parsable value for the color.
128 set stripeColor(color
) {
129 this.querySelector('.color-stripe').style
.backgroundColor
= color
;
133 * Handles a click on the tile.
134 * @param {Event} e The click event.
136 handleClick_: function(e
) {
137 if (e
.target
.classList
.contains('close-button')) {
141 // Records the index of this tile.
142 chrome
.send('metricsHandler:recordInHistogram',
143 ['NewTabPage.SuggestedSite', this.index
, 8]);
144 chrome
.send('suggestedSitesAction',
145 [ntp
.NtpFollowAction
.CLICKED_TILE
]);
150 * Allow blacklisting suggestions site using the keyboard.
151 * @param {Event} e The keydown event.
153 handleKeyDown_: function(e
) {
154 if (!cr
.isMac
&& e
.keyCode
== 46 || // Del
155 cr
.isMac
&& e
.metaKey
&& e
.keyCode
== 8) { // Cmd + Backspace
161 * Permanently removes a page from Suggestions.
163 blacklist_: function() {
164 this.showUndoNotification_();
165 chrome
.send('blacklistURLFromSuggestions', [this.data_
.url
]);
167 chrome
.send('getSuggestions');
168 this.classList
.add('blacklisted');
172 * Shows notification that you can undo blacklisting.
174 showUndoNotification_: function() {
175 var data
= this.data_
;
177 var doUndo = function() {
178 chrome
.send('removeURLsFromSuggestionsBlacklist', [data
.url
]);
179 self
.updateForData(data
);
184 text
: loadTimeData
.getString('undothumbnailremove'),
189 chrome
.send('clearSuggestionsURLsBlacklist');
191 text
: loadTimeData
.getString('restoreThumbnailsShort'),
194 ntp
.showNotification(
195 loadTimeData
.getString('thumbnailremovednotification'),
200 * Set the size and position of the suggestions tile.
201 * @param {number} size The total size of |this|.
202 * @param {number} x The x-position.
203 * @param {number} y The y-position.
205 setBounds: function(size
, x
, y
) {
206 this.style
.width
= size
+ 'px';
207 this.style
.height
= heightForWidth(size
) + 'px';
209 this.style
.left
= x
+ 'px';
210 this.style
.right
= x
+ 'px';
211 this.style
.top
= y
+ 'px';
215 * Returns whether this element can be 'removed' from chrome (i.e. whether
216 * the user can drag it onto the trash and expect something to happen).
217 * @return {boolean} True, since suggestions pages can always be
220 canBeRemoved: function() {
225 * Removes this element from chrome, i.e. blacklists it.
227 removeFromChrome: function() {
229 this.parentNode
.classList
.add('finishing-drag');
233 * Called when a drag of this tile has ended (after all animations have
236 finalizeDrag: function() {
237 this.parentNode
.classList
.remove('finishing-drag');
241 * Called when a drag is starting on the tile. Updates dataTransfer with
242 * data for this tile (for dragging outside of the NTP).
243 * @param {Event.DataTransfer} dataTransfer The drag event data store.
245 setDragData: function(dataTransfer
) {
246 dataTransfer
.setData('Text', this.data_
.title
);
247 dataTransfer
.setData('URL', this.data_
.url
);
251 var suggestionsPageGridValues
= {
252 // The fewest tiles we will show in a row.
254 // The suggestions we will show in a row.
257 // The smallest a tile can be.
259 // The biggest a tile can be. 212 (max thumbnail width) + 2.
262 // The padding between tiles, as a fraction of the tile width.
263 tileSpacingFraction
: 1 / 8,
265 TilePage
.initGridValues(suggestionsPageGridValues
);
268 * Calculates the height for a Suggestion tile for a given width. The size
269 * is based on the thumbnail, which should have a 212:132 ratio.
270 * @return {number} The height.
272 function heightForWidth(width
) {
273 // The 2s are for borders, the 36 is for the title and score.
274 return (width
- 2) * 132 / 212 + 2 + 36;
277 var THUMBNAIL_COUNT
= 8;
280 * Creates a new SuggestionsPage object.
282 * @extends {TilePage}
284 function SuggestionsPage() {
285 var el
= new TilePage(suggestionsPageGridValues
);
286 el
.__proto__
= SuggestionsPage
.prototype;
292 SuggestionsPage
.prototype = {
293 __proto__
: TilePage
.prototype,
295 initialize: function() {
296 this.classList
.add('suggestions-page');
298 this.suggestionsTiles_
= this.getElementsByClassName('suggestions real');
300 this.addEventListener('carddeselected', this.handleCardDeselected_
);
301 this.addEventListener('cardselected', this.handleCardSelected_
);
305 * Create blank (filler) tiles.
308 createTiles_: function() {
309 for (var i
= 0; i
< THUMBNAIL_COUNT
; i
++) {
310 this.appendTile(new Suggestion());
315 * Update the tiles after a change to |this.data_|.
317 updateTiles_: function() {
318 for (var i
= 0; i
< THUMBNAIL_COUNT
; i
++) {
319 var page
= this.data_
[i
];
320 var tile
= this.suggestionsTiles_
[i
];
322 if (i
>= this.data_
.length
)
325 tile
.updateForData(page
);
330 * Handles the 'card deselected' event (i.e. the user clicked to another
332 * @param {Event} e The CardChanged event.
334 handleCardDeselected_: function(e
) {
335 if (!document
.documentElement
.classList
.contains('starting-up')) {
336 chrome
.send('suggestedSitesAction',
337 [ntp
.NtpFollowAction
.CLICKED_OTHER_NTP_PANE
]);
342 * Handles the 'card selected' event (i.e. the user clicked to select the
344 * @param {Event} e The CardChanged event.
346 handleCardSelected_: function(e
) {
347 if (!document
.documentElement
.classList
.contains('starting-up'))
348 chrome
.send('suggestedSitesSelected');
352 * Array of suggestions data objects.
359 var startTime
= Date
.now();
361 // The first time data is set, create the tiles.
364 this.data_
= data
.slice(0, THUMBNAIL_COUNT
);
366 this.data_
= refreshData(this.data_
, data
);
370 this.updateFocusableElement();
371 logEvent('suggestions.layout: ' + (Date
.now() - startTime
));
375 shouldAcceptDrag: function(e
) {
380 heightForWidth
: heightForWidth
,
384 * Executed once the NTP has loaded. Checks if the Suggested pane is
385 * shown or not. If it is shown, the 'suggestedSitesSelected' message is sent
386 * to the C++ code, to record the fact that the user has seen this pane.
388 SuggestionsPage
.onLoaded = function() {
389 if (ntp
.getCardSlider() &&
390 ntp
.getCardSlider().currentCardValue
&&
391 ntp
.getCardSlider().currentCardValue
.classList
392 .contains('suggestions-page')) {
393 chrome
.send('suggestedSitesSelected');
398 * We've gotten additional data for Suggestions page. Update our old data with
399 * the new data. The ordering of the new data is not important, except when a
400 * page is pinned. Thus we try to minimize re-ordering.
401 * @param {Array} oldData The current Suggestions page list.
402 * @param {Array} newData The new Suggestions page list.
403 * @return {Array} The merged page list that should replace the current page
406 function refreshData(oldData
, newData
) {
407 oldData
= oldData
.slice(0, THUMBNAIL_COUNT
);
408 newData
= newData
.slice(0, THUMBNAIL_COUNT
);
410 // Copy over pinned sites directly.
411 for (var i
= 0; i
< newData
.length
; i
++) {
412 if (newData
[i
].pinned
) {
413 oldData
[i
] = newData
[i
];
414 // Mark the entry as 'updated' so we don't try to update again.
415 oldData
[i
].updated
= true;
416 // Mark the newData page as 'used' so we don't try to re-use it.
417 newData
[i
].used
= true;
421 // Look through old pages; if they exist in the newData list, keep them
423 for (var i
= 0; i
< oldData
.length
; i
++) {
424 if (!oldData
[i
] || oldData
[i
].updated
)
427 for (var j
= 0; j
< newData
.length
; j
++) {
431 if (newData
[j
].url
== oldData
[i
].url
) {
432 // The background image and other data may have changed.
433 oldData
[i
] = newData
[j
];
434 oldData
[i
].updated
= true;
435 newData
[j
].used
= true;
441 // Look through old pages that haven't been updated yet; replace them.
442 for (var i
= 0; i
< oldData
.length
; i
++) {
443 if (oldData
[i
] && oldData
[i
].updated
)
446 for (var j
= 0; j
< newData
.length
; j
++) {
450 oldData
[i
] = newData
[j
];
451 oldData
[i
].updated
= true;
452 newData
[j
].used
= true;
456 if (oldData
[i
] && !oldData
[i
].updated
)
460 // Clear 'updated' flags so this function will work next time it's called.
461 for (var i
= 0; i
< THUMBNAIL_COUNT
; i
++) {
463 oldData
[i
].updated
= false;
470 SuggestionsPage
: SuggestionsPage
,
471 refreshData
: refreshData
,
475 document
.addEventListener('ntpLoaded', ntp
.SuggestionsPage
.onLoaded
);