OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('ntp', function() { | 5 cr.define('ntp', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 var TilePage = ntp.TilePage; | 8 var Tile = ntp.Tile2; |
| 9 var TilePage = ntp.TilePage2; |
9 | 10 |
10 /** | 11 /** |
11 * A counter for generating unique tile IDs. | 12 * Creates a new Thumbnail object for tiling. |
| 13 * @constructor |
| 14 * @extends {Tile} |
| 15 * @extends {HTMLAnchorElement} |
| 16 * @param {Object} config Tile page configuration object. |
12 */ | 17 */ |
13 var tileID = 0; | 18 function Thumbnail(config) { |
14 | |
15 /** | |
16 * Creates a new Most Visited object for tiling. | |
17 * @constructor | |
18 * @extends {HTMLAnchorElement} | |
19 */ | |
20 function MostVisited() { | |
21 var el = cr.doc.createElement('a'); | 19 var el = cr.doc.createElement('a'); |
22 el.__proto__ = MostVisited.prototype; | 20 el.__proto__ = Thumbnail.prototype; |
23 el.initialize(); | 21 el.initialize(config); |
24 | 22 |
25 return el; | 23 return el; |
26 } | 24 } |
27 | 25 |
28 MostVisited.prototype = { | 26 Thumbnail.prototype = Tile.subclass({ |
29 __proto__: HTMLAnchorElement.prototype, | 27 __proto__: HTMLAnchorElement.prototype, |
30 | 28 |
31 initialize: function() { | 29 /** |
| 30 * Initializes a Thumbnail. |
| 31 * @param {Object} config TilePage configuration object. |
| 32 */ |
| 33 initialize: function(config) { |
| 34 Tile.prototype.initialize.apply(this, arguments); |
| 35 this.classList.add('thumbnail'); |
32 this.reset(); | 36 this.reset(); |
33 | |
34 this.addEventListener('click', this.handleClick_); | |
35 this.addEventListener('keydown', this.handleKeyDown_); | |
36 }, | 37 }, |
37 | 38 |
38 get index() { | 39 /** |
39 assert(this.tile); | 40 * Thumbnail data object. |
40 return this.tile.index; | 41 * @type {Object} |
41 }, | 42 */ |
42 | |
43 get data() { | 43 get data() { |
44 return this.data_; | 44 return this.data_; |
45 }, | 45 }, |
46 | 46 |
47 /** | 47 /** |
48 * Clears the DOM hierarchy for this node, setting it back to the default | 48 * Clears the DOM hierarchy for this node, setting it back to the default |
49 * for a blank thumbnail. | 49 * for a blank thumbnail. |
50 */ | 50 */ |
51 reset: function() { | 51 reset: function() { |
52 this.className = 'most-visited filler real'; | |
53 this.innerHTML = | 52 this.innerHTML = |
54 '<span class="thumbnail-wrapper fills-parent">' + | 53 '<span class="thumbnail-image"></span>' + |
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>' + | |
59 '</span>' + | |
60 '<span class="favicon"></span>' + | |
61 '</span>' + | |
62 '<div class="color-stripe"></div>' + | |
63 '<span class="title"></span>'; | 54 '<span class="title"></span>'; |
64 | 55 |
65 this.querySelector('.close-button').title = | |
66 loadTimeData.getString('removethumbnailtooltip'); | |
67 | |
68 this.tabIndex = -1; | 56 this.tabIndex = -1; |
69 this.data_ = null; | 57 this.data_ = null; |
70 this.removeAttribute('id'); | |
71 this.title = ''; | 58 this.title = ''; |
72 }, | 59 }, |
73 | 60 |
74 /** | 61 /** |
75 * Update the appearance of this tile according to |data|. | 62 * Update the appearance of this tile according to |data|. |
76 * @param {Object} data A dictionary of relevant data for the page. | 63 * @param {Object} data A dictionary of relevant data for the page. |
77 */ | 64 */ |
78 updateForData: function(data) { | 65 updateForData: function(data) { |
79 if (this.classList.contains('blacklisted') && data) { | 66 // TODO(pedrosimonetti): Remove data.filler usage everywhere. |
80 // Animate appearance of new tile. | |
81 this.classList.add('new-tile-contents'); | |
82 } | |
83 this.classList.remove('blacklisted'); | |
84 | |
85 if (!data || data.filler) { | 67 if (!data || data.filler) { |
86 if (this.data_) | 68 if (this.data_) |
87 this.reset(); | 69 this.reset(); |
88 return; | 70 return; |
89 } | 71 } |
90 | 72 |
91 var id = tileID++; | |
92 this.id = 'most-visited-tile-' + id; | |
93 this.data_ = data; | 73 this.data_ = data; |
94 this.classList.add('focusable'); | |
95 | 74 |
96 var faviconDiv = this.querySelector('.favicon'); | 75 this.formatThumbnail_(data); |
97 var faviconUrl = 'chrome://favicon/size/16/' + data.url; | 76 }, |
98 faviconDiv.style.backgroundImage = url(faviconUrl); | |
99 chrome.send('getFaviconDominantColor', [faviconUrl, this.id]); | |
100 | 77 |
| 78 /** |
| 79 * Update the appearance of this tile according to |data|. |
| 80 * @param {Object} data A dictionary of relevant data for the page. |
| 81 */ |
| 82 formatThumbnail_: function(data) { |
101 var title = this.querySelector('.title'); | 83 var title = this.querySelector('.title'); |
102 title.textContent = data.title; | 84 title.textContent = data.title; |
103 title.dir = data.direction; | 85 title.dir = data.direction; |
104 | 86 |
105 // Sets the tooltip. | 87 // Sets the tooltip. |
106 this.title = data.title; | 88 this.title = data.title; |
107 | 89 |
108 var thumbnailUrl = 'chrome://thumb/' + data.url; | 90 var thumbnailUrl = ntp.getThumbnailUrl(data.url); |
109 this.querySelector('.thumbnail').style.backgroundImage = | 91 this.querySelector('.thumbnail-image').style.backgroundImage = |
110 url(thumbnailUrl); | 92 url(thumbnailUrl); |
111 | 93 |
112 this.href = data.url; | 94 this.href = data.url; |
113 | |
114 this.classList.remove('filler'); | |
115 }, | 95 }, |
116 | 96 }); |
117 /** | |
118 * Sets the color of the favicon dominant color bar. | |
119 * @param {string} color The css-parsable value for the color. | |
120 */ | |
121 set stripeColor(color) { | |
122 this.querySelector('.color-stripe').style.backgroundColor = color; | |
123 }, | |
124 | |
125 /** | |
126 * Handles a click on the tile. | |
127 * @param {Event} e The click event. | |
128 */ | |
129 handleClick_: function(e) { | |
130 if (e.target.classList.contains('close-button')) { | |
131 this.blacklist_(); | |
132 e.preventDefault(); | |
133 } else { | |
134 // Records an app launch from the most visited page (Chrome will decide | |
135 // whether the url is an app). TODO(estade): this only works for clicks; | |
136 // other actions like "open in new tab" from the context menu won't be | |
137 // recorded. Can this be fixed? | |
138 chrome.send('recordAppLaunchByURL', | |
139 [encodeURIComponent(this.href), | |
140 ntp.APP_LAUNCH.NTP_MOST_VISITED]); | |
141 // Records the index of this tile. | |
142 chrome.send('metricsHandler:recordInHistogram', | |
143 ['NewTabPage.MostVisited', this.index, 8]); | |
144 chrome.send('mostVisitedAction', | |
145 [ntp.NtpFollowAction.CLICKED_TILE]); | |
146 } | |
147 }, | |
148 | |
149 /** | |
150 * Allow blacklisting most visited site using the keyboard. | |
151 */ | |
152 handleKeyDown_: function(e) { | |
153 if (!cr.isMac && e.keyCode == 46 || // Del | |
154 cr.isMac && e.metaKey && e.keyCode == 8) { // Cmd + Backspace | |
155 this.blacklist_(); | |
156 } | |
157 }, | |
158 | |
159 /** | |
160 * Permanently removes a page from Most Visited. | |
161 */ | |
162 blacklist_: function() { | |
163 this.showUndoNotification_(); | |
164 chrome.send('blacklistURLFromMostVisited', [this.data_.url]); | |
165 this.reset(); | |
166 chrome.send('getMostVisited'); | |
167 this.classList.add('blacklisted'); | |
168 }, | |
169 | |
170 showUndoNotification_: function() { | |
171 var data = this.data_; | |
172 var self = this; | |
173 var doUndo = function() { | |
174 chrome.send('removeURLsFromMostVisitedBlacklist', [data.url]); | |
175 self.updateForData(data); | |
176 } | |
177 | |
178 var undo = { | |
179 action: doUndo, | |
180 text: loadTimeData.getString('undothumbnailremove'), | |
181 }; | |
182 | |
183 var undoAll = { | |
184 action: function() { | |
185 chrome.send('clearMostVisitedURLsBlacklist'); | |
186 }, | |
187 text: loadTimeData.getString('restoreThumbnailsShort'), | |
188 }; | |
189 | |
190 ntp.showNotification( | |
191 loadTimeData.getString('thumbnailremovednotification'), | |
192 [undo, undoAll]); | |
193 }, | |
194 | |
195 /** | |
196 * Set the size and position of the most visited tile. | |
197 * @param {number} size The total size of |this|. | |
198 * @param {number} x The x-position. | |
199 * @param {number} y The y-position. | |
200 * animate. | |
201 */ | |
202 setBounds: function(size, x, y) { | |
203 this.style.width = toCssPx(size); | |
204 this.style.height = toCssPx(heightForWidth(size)); | |
205 | |
206 this.style.left = toCssPx(x); | |
207 this.style.right = toCssPx(x); | |
208 this.style.top = toCssPx(y); | |
209 }, | |
210 | |
211 /** | |
212 * Returns whether this element can be 'removed' from chrome (i.e. whether | |
213 * the user can drag it onto the trash and expect something to happen). | |
214 * @return {boolean} True, since most visited pages can always be | |
215 * blacklisted. | |
216 */ | |
217 canBeRemoved: function() { | |
218 return true; | |
219 }, | |
220 | |
221 /** | |
222 * Removes this element from chrome, i.e. blacklists it. | |
223 */ | |
224 removeFromChrome: function() { | |
225 this.blacklist_(); | |
226 this.parentNode.classList.add('finishing-drag'); | |
227 }, | |
228 | |
229 /** | |
230 * Called when a drag of this tile has ended (after all animations have | |
231 * finished). | |
232 */ | |
233 finalizeDrag: function() { | |
234 this.parentNode.classList.remove('finishing-drag'); | |
235 }, | |
236 | |
237 /** | |
238 * Called when a drag is starting on the tile. Updates dataTransfer with | |
239 * data for this tile (for dragging outside of the NTP). | |
240 */ | |
241 setDragData: function(dataTransfer) { | |
242 dataTransfer.setData('Text', this.data_.title); | |
243 dataTransfer.setData('URL', this.data_.url); | |
244 }, | |
245 }; | |
246 | |
247 var mostVisitedPageGridValues = { | |
248 // The fewest tiles we will show in a row. | |
249 minColCount: 2, | |
250 // The most tiles we will show in a row. | |
251 maxColCount: 4, | |
252 | |
253 // The smallest a tile can be. | |
254 minTileWidth: 122, | |
255 // The biggest a tile can be. 212 (max thumbnail width) + 2. | |
256 maxTileWidth: 214, | |
257 | |
258 // The padding between tiles, as a fraction of the tile width. | |
259 tileSpacingFraction: 1 / 8, | |
260 }; | |
261 TilePage.initGridValues(mostVisitedPageGridValues); | |
262 | 97 |
263 /** | 98 /** |
264 * Calculates the height for a Most Visited tile for a given width. The size | 99 * Creates a new ThumbnailPage object. |
265 * is based on the thumbnail, which should have a 212:132 ratio. | |
266 * @return {number} The height. | |
267 */ | |
268 function heightForWidth(width) { | |
269 // The 2s are for borders, the 31 is for the title. | |
270 return (width - 2) * 132 / 212 + 2 + 31; | |
271 } | |
272 | |
273 var THUMBNAIL_COUNT = 8; | |
274 | |
275 /** | |
276 * Creates a new MostVisitedPage object. | |
277 * @constructor | 100 * @constructor |
278 * @extends {TilePage} | 101 * @extends {TilePage} |
279 */ | 102 */ |
280 function MostVisitedPage() { | 103 function ThumbnailPage() { |
281 var el = new TilePage(mostVisitedPageGridValues); | 104 var el = new TilePage(); |
282 el.__proto__ = MostVisitedPage.prototype; | 105 el.__proto__ = ThumbnailPage.prototype; |
283 el.initialize(); | |
284 | 106 |
285 return el; | 107 return el; |
286 } | 108 } |
287 | 109 |
288 MostVisitedPage.prototype = { | 110 ThumbnailPage.prototype = { |
289 __proto__: TilePage.prototype, | 111 __proto__: TilePage.prototype, |
290 | 112 |
| 113 config_: { |
| 114 // The width of a cell. |
| 115 cellWidth: 132, |
| 116 // The start margin of a cell (left or right according to text direction). |
| 117 cellMarginStart: 18, |
| 118 // The border panel horizontal margin. |
| 119 bottomPanelHorizontalMargin: 100, |
| 120 // The height of the tile row. |
| 121 rowHeight: 105, |
| 122 // The maximum number of Tiles to be displayed. |
| 123 maxTileCount: 10 |
| 124 }, |
| 125 |
| 126 // Thumbnail class used in this TilePage. |
| 127 ThumbnailClass: Thumbnail, |
| 128 |
| 129 /** |
| 130 * Initializes a ThumbnailPage. |
| 131 */ |
291 initialize: function() { | 132 initialize: function() { |
292 this.classList.add('most-visited-page'); | 133 this.classList.add('thumbnail-page'); |
293 this.data_ = null; | 134 this.data_ = null; |
294 this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real'); | |
295 | 135 |
296 this.addEventListener('carddeselected', this.handleCardDeselected_); | 136 this.addEventListener('carddeselected', this.handleCardDeselected_); |
297 this.addEventListener('cardselected', this.handleCardSelected_); | 137 this.addEventListener('cardselected', this.handleCardSelected_); |
298 }, | 138 }, |
299 | 139 |
300 /** | 140 /** |
301 * Create blank (filler) tiles. | 141 * Create blank tiles. |
302 * @private | 142 * @private |
| 143 * @param {number} count The number of Tiles to be created. |
303 */ | 144 */ |
304 createTiles_: function() { | 145 createTiles_: function(count) { |
305 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | 146 var Class = this.ThumbnailClass; |
306 this.appendTile(new MostVisited()); | 147 var config = this.config_; |
| 148 count = Math.min(count, config.maxTileCount); |
| 149 for (var i = 0; i < count; i++) { |
| 150 this.appendTile(new Class(config)); |
307 } | 151 } |
308 }, | 152 }, |
309 | 153 |
310 /** | 154 /** |
311 * Update the tiles after a change to |data_|. | 155 * Update the tiles after a change to |data_|. |
312 */ | 156 */ |
313 updateTiles_: function() { | 157 updateTiles_: function() { |
314 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | 158 var maxTileCount = this.config_.maxTileCount; |
315 var page = this.data_[i]; | 159 var data = this.data_; |
316 var tile = this.mostVisitedTiles_[i]; | 160 var tiles = this.tiles; |
| 161 for (var i = 0; i < maxTileCount; i++) { |
| 162 var page = data[i]; |
| 163 var tile = tiles[i]; |
317 | 164 |
318 if (i >= this.data_.length) | 165 // TODO(pedrosimonetti): What do we do when there's no tile here? |
| 166 if (!tile) |
| 167 return; |
| 168 |
| 169 if (i >= data.length) |
319 tile.reset(); | 170 tile.reset(); |
320 else | 171 else |
321 tile.updateForData(page); | 172 tile.updateForData(page); |
322 } | 173 } |
323 }, | 174 }, |
324 | 175 |
325 /** | 176 /** |
326 * Handles the 'card deselected' event (i.e. the user clicked to another | 177 * Handles the 'card deselected' event (i.e. the user clicked to another |
327 * pane). | 178 * pane). |
328 * @param {Event} e The CardChanged event. | 179 * @param {Event} e The CardChanged event. |
329 */ | 180 */ |
330 handleCardDeselected_: function(e) { | 181 handleCardDeselected_: function(e) { |
331 if (!document.documentElement.classList.contains('starting-up')) { | 182 console.error('ThumbnailPage: handleCardDeselected_ is not implemented.'); |
332 chrome.send('mostVisitedAction', | |
333 [ntp.NtpFollowAction.CLICKED_OTHER_NTP_PANE]); | |
334 } | |
335 }, | 183 }, |
336 | 184 |
337 /** | 185 /** |
338 * Handles the 'card selected' event (i.e. the user clicked to select the | 186 * Handles the 'card selected' event (i.e. the user clicked to select the |
339 * Most Visited pane). | 187 * this page's pane). |
340 * @param {Event} e The CardChanged event. | 188 * @param {Event} e The CardChanged event. |
341 */ | 189 */ |
342 handleCardSelected_: function(e) { | 190 handleCardSelected_: function(e) { |
343 if (!document.documentElement.classList.contains('starting-up')) | 191 console.error('ThumbnailPage: handleCardSelected_ is not implemented.'); |
344 chrome.send('mostVisitedSelected'); | |
345 }, | 192 }, |
346 | 193 |
347 /** | 194 /** |
348 * Array of most visited data objects. | 195 * Array of thumbnail data objects. |
349 * @type {Array} | 196 * @type {Array} |
350 */ | 197 */ |
351 get data() { | 198 get data() { |
352 return this.data_; | 199 return this.data_; |
353 }, | 200 }, |
354 set data(data) { | 201 set data(data) { |
355 var startTime = Date.now(); | 202 console.error('ThumbnailPage: data_ setter is not implemented.'); |
356 | |
357 // The first time data is set, create the tiles. | |
358 if (!this.data_) { | |
359 this.createTiles_(); | |
360 this.data_ = data.slice(0, THUMBNAIL_COUNT); | |
361 } else { | |
362 this.data_ = refreshData(this.data_, data); | |
363 } | |
364 | |
365 this.updateTiles_(); | |
366 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); | |
367 }, | 203 }, |
368 | 204 |
369 /** @inheritDoc */ | 205 /** @inheritDoc */ |
370 shouldAcceptDrag: function(e) { | 206 shouldAcceptDrag: function(e) { |
371 return false; | 207 return false; |
372 }, | 208 }, |
373 | |
374 /** @inheritDoc */ | |
375 heightForWidth: heightForWidth, | |
376 }; | |
377 | |
378 /** | |
379 * Executed once the NTP has loaded. Checks if the Most Visited pane is | |
380 * shown or not. If it is shown, the 'mostVisitedSelected' message is sent | |
381 * to the C++ code, to record the fact that the user has seen this pane. | |
382 */ | |
383 MostVisitedPage.onLoaded = function() { | |
384 if (ntp.getCardSlider() && | |
385 ntp.getCardSlider().currentCardValue && | |
386 ntp.getCardSlider().currentCardValue.classList | |
387 .contains('most-visited-page')) { | |
388 chrome.send('mostVisitedSelected'); | |
389 } | |
390 } | |
391 | |
392 /** | |
393 * We've gotten additional Most Visited data. Update our old data with the | |
394 * new data. The ordering of the new data is not important, except when a | |
395 * page is pinned. Thus we try to minimize re-ordering. | |
396 * @param {Array} oldData The current Most Visited page list. | |
397 * @param {Array} newData The new Most Visited page list. | |
398 * @return {Array} The merged page list that should replace the current page | |
399 * list. | |
400 */ | |
401 function refreshData(oldData, newData) { | |
402 oldData = oldData.slice(0, THUMBNAIL_COUNT); | |
403 newData = newData.slice(0, THUMBNAIL_COUNT); | |
404 | |
405 // Copy over pinned sites directly. | |
406 for (var j = 0; j < newData.length; j++) { | |
407 if (newData[j].pinned) { | |
408 oldData[j] = newData[j]; | |
409 // Mark the entry as 'updated' so we don't try to update again. | |
410 oldData[j].updated = true; | |
411 // Mark the newData page as 'used' so we don't try to re-use it. | |
412 newData[j].used = true; | |
413 } | |
414 } | |
415 | |
416 // Look through old pages; if they exist in the newData list, keep them | |
417 // where they are. | |
418 for (var i = 0; i < oldData.length; i++) { | |
419 if (!oldData[i] || oldData[i].updated) | |
420 continue; | |
421 | |
422 for (var j = 0; j < newData.length; j++) { | |
423 if (newData[j].used) | |
424 continue; | |
425 | |
426 if (newData[j].url == oldData[i].url) { | |
427 // The background image and other data may have changed. | |
428 oldData[i] = newData[j]; | |
429 oldData[i].updated = true; | |
430 newData[j].used = true; | |
431 break; | |
432 } | |
433 } | |
434 } | |
435 | |
436 // Look through old pages that haven't been updated yet; replace them. | |
437 for (var i = 0; i < oldData.length; i++) { | |
438 if (oldData[i] && oldData[i].updated) | |
439 continue; | |
440 | |
441 for (var j = 0; j < newData.length; j++) { | |
442 if (newData[j].used) | |
443 continue; | |
444 | |
445 oldData[i] = newData[j]; | |
446 oldData[i].updated = true; | |
447 newData[j].used = true; | |
448 break; | |
449 } | |
450 | |
451 if (oldData[i] && !oldData[i].updated) | |
452 oldData[i] = null; | |
453 } | |
454 | |
455 // Clear 'updated' flags so this function will work next time it's called. | |
456 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | |
457 if (oldData[i]) | |
458 oldData[i].updated = false; | |
459 } | |
460 | |
461 return oldData; | |
462 }; | 209 }; |
463 | 210 |
464 return { | 211 return { |
465 MostVisitedPage: MostVisitedPage, | 212 Thumbnail: Thumbnail, |
466 refreshData: refreshData, | 213 ThumbnailPage: ThumbnailPage |
467 }; | 214 }; |
468 }); | 215 }); |
469 | 216 |
470 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); | |
OLD | NEW |