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 Thumbnail = ntp.Thumbnail; |
9 | 9 var ThumbnailPage = ntp.ThumbnailPage; |
10 /** | |
11 * A counter for generating unique tile IDs. | |
12 */ | |
13 var tileID = 0; | |
14 | 10 |
15 /** | 11 /** |
16 * Creates a new Most Visited object for tiling. | 12 * Creates a new Most Visited object for tiling. |
17 * @constructor | 13 * @constructor |
| 14 * @extends {Thumbnail} |
18 * @extends {HTMLAnchorElement} | 15 * @extends {HTMLAnchorElement} |
| 16 * @param {Object} gridValues Tile page configuration object. |
19 */ | 17 */ |
20 function MostVisited() { | 18 function MostVisited(gridValues) { |
21 var el = cr.doc.createElement('a'); | 19 var el = cr.doc.createElement('a'); |
22 el.__proto__ = MostVisited.prototype; | 20 el.__proto__ = MostVisited.prototype; |
23 el.initialize(); | 21 el.initialize(gridValues); |
24 | 22 |
25 return el; | 23 return el; |
26 } | 24 } |
27 | 25 |
28 MostVisited.prototype = { | 26 MostVisited.prototype = { |
29 __proto__: HTMLAnchorElement.prototype, | 27 __proto__: Thumbnail.prototype, |
30 | 28 |
31 initialize: function() { | 29 initialize: function(gridValues) { |
32 this.reset(); | 30 Thumbnail.prototype.initialize.apply(this, arguments); |
33 | 31 |
34 this.addEventListener('click', this.handleClick_); | 32 this.addEventListener('click', this.handleClick_); |
35 this.addEventListener('keydown', this.handleKeyDown_); | 33 this.addEventListener('keydown', this.handleKeyDown_); |
36 }, | 34 }, |
37 | 35 |
38 get index() { | |
39 assert(this.tile); | |
40 return this.tile.index; | |
41 }, | |
42 | |
43 get data() { | |
44 return this.data_; | |
45 }, | |
46 | |
47 /** | |
48 * Clears the DOM hierarchy for this node, setting it back to the default | |
49 * for a blank thumbnail. | |
50 */ | |
51 reset: function() { | |
52 this.className = 'most-visited filler real'; | |
53 this.innerHTML = | |
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>' + | |
59 '</span>' + | |
60 '<span class="favicon"></span>' + | |
61 '</span>' + | |
62 '<div class="color-stripe"></div>' + | |
63 '<span class="title"></span>'; | |
64 | |
65 this.querySelector('.close-button').title = | |
66 loadTimeData.getString('removethumbnailtooltip'); | |
67 | |
68 this.tabIndex = -1; | |
69 this.data_ = null; | |
70 this.removeAttribute('id'); | |
71 this.title = ''; | |
72 }, | |
73 | |
74 /** | |
75 * Update the appearance of this tile according to |data|. | |
76 * @param {Object} data A dictionary of relevant data for the page. | |
77 */ | |
78 updateForData: function(data) { | |
79 if (this.classList.contains('blacklisted') && data) { | |
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) { | |
86 if (this.data_) | |
87 this.reset(); | |
88 return; | |
89 } | |
90 | |
91 var id = tileID++; | |
92 this.id = 'most-visited-tile-' + id; | |
93 this.data_ = data; | |
94 this.classList.add('focusable'); | |
95 | |
96 var faviconDiv = this.querySelector('.favicon'); | |
97 var faviconUrl = 'chrome://favicon/size/16/' + data.url; | |
98 faviconDiv.style.backgroundImage = url(faviconUrl); | |
99 chrome.send('getFaviconDominantColor', [faviconUrl, this.id]); | |
100 | |
101 var title = this.querySelector('.title'); | |
102 title.textContent = data.title; | |
103 title.dir = data.direction; | |
104 | |
105 // Sets the tooltip. | |
106 this.title = data.title; | |
107 | |
108 var thumbnailUrl = 'chrome://thumb/' + data.url; | |
109 this.querySelector('.thumbnail').style.backgroundImage = | |
110 url(thumbnailUrl); | |
111 | |
112 this.href = data.url; | |
113 | |
114 this.classList.remove('filler'); | |
115 }, | |
116 | |
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 /** | 36 /** |
126 * Handles a click on the tile. | 37 * Handles a click on the tile. |
127 * @param {Event} e The click event. | 38 * @param {Event} e The click event. |
128 */ | 39 */ |
129 handleClick_: function(e) { | 40 handleClick_: function(e) { |
130 if (e.target.classList.contains('close-button')) { | 41 if (e.target.classList.contains('close-button')) { |
131 this.blacklist_(); | 42 this.blacklist_(); |
132 e.preventDefault(); | 43 e.preventDefault(); |
133 } else { | 44 } else { |
| 45 // TODO(xci): How do we handle the logging? Maybe we should let the |
| 46 // handleClick_() be implemented by Thumbnail subclasses. |
| 47 |
134 // Records an app launch from the most visited page (Chrome will decide | 48 // 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; | 49 // 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 | 50 // other actions like "open in new tab" from the context menu won't be |
137 // recorded. Can this be fixed? | 51 // recorded. Can this be fixed? |
138 chrome.send('recordAppLaunchByURL', | 52 chrome.send('recordAppLaunchByURL', |
139 [encodeURIComponent(this.href), | 53 [encodeURIComponent(this.href), |
140 ntp.APP_LAUNCH.NTP_MOST_VISITED]); | 54 ntp.APP_LAUNCH.NTP_MOST_VISITED]); |
141 // Records the index of this tile. | 55 // Records the index of this tile. |
142 chrome.send('metricsHandler:recordInHistogram', | 56 chrome.send('metricsHandler:recordInHistogram', |
143 ['NewTabPage.MostVisited', this.index, 8]); | 57 ['NewTabPage.MostVisited', this.index, 8]); |
| 58 // TODO(xci) logging |
144 chrome.send('mostVisitedAction', | 59 chrome.send('mostVisitedAction', |
145 [ntp.NtpFollowAction.CLICKED_TILE]); | 60 [ntp.NtpFollowAction.CLICKED_TILE]); |
146 } | 61 } |
147 }, | 62 }, |
148 | 63 |
149 /** | 64 /** |
150 * Allow blacklisting most visited site using the keyboard. | 65 * Allow blacklisting most visited site using the keyboard. |
151 */ | 66 */ |
152 handleKeyDown_: function(e) { | 67 handleKeyDown_: function(e) { |
153 if (!cr.isMac && e.keyCode == 46 || // Del | 68 if (!cr.isMac && e.keyCode == 46 || // Del |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 }, | 101 }, |
187 text: loadTimeData.getString('restoreThumbnailsShort'), | 102 text: loadTimeData.getString('restoreThumbnailsShort'), |
188 }; | 103 }; |
189 | 104 |
190 ntp.showNotification( | 105 ntp.showNotification( |
191 loadTimeData.getString('thumbnailremovednotification'), | 106 loadTimeData.getString('thumbnailremovednotification'), |
192 [undo, undoAll]); | 107 [undo, undoAll]); |
193 }, | 108 }, |
194 | 109 |
195 /** | 110 /** |
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 | 111 * 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). | 112 * the user can drag it onto the trash and expect something to happen). |
214 * @return {boolean} True, since most visited pages can always be | 113 * @return {boolean} True, since most visited pages can always be |
215 * blacklisted. | 114 * blacklisted. |
216 */ | 115 */ |
217 canBeRemoved: function() { | 116 canBeRemoved: function() { |
218 return true; | 117 return this.isRemovable; |
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 }, | 118 }, |
245 }; | 119 }; |
246 | 120 |
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 | |
263 /** | |
264 * Calculates the height for a Most Visited tile for a given width. The size | |
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; | 121 var THUMBNAIL_COUNT = 8; |
274 | 122 |
275 /** | 123 /** |
276 * Creates a new MostVisitedPage object. | 124 * Creates a new MostVisitedPage object. |
277 * @constructor | 125 * @constructor |
278 * @extends {TilePage} | 126 * @extends {TilePage} |
279 */ | 127 */ |
280 function MostVisitedPage() { | 128 function MostVisitedPage() { |
281 var el = new TilePage(mostVisitedPageGridValues); | 129 var el = new ThumbnailPage(); |
282 el.__proto__ = MostVisitedPage.prototype; | 130 el.__proto__ = MostVisitedPage.prototype; |
283 el.initialize(); | 131 el.initialize(); |
284 | 132 |
285 return el; | 133 return el; |
286 } | 134 } |
287 | 135 |
288 MostVisitedPage.prototype = { | 136 MostVisitedPage.prototype = { |
289 __proto__: TilePage.prototype, | 137 __proto__: ThumbnailPage.prototype, |
| 138 |
| 139 ThumbnailClass: MostVisited, |
290 | 140 |
291 initialize: function() { | 141 initialize: function() { |
| 142 ThumbnailPage.prototype.initialize.apply(this, arguments); |
| 143 |
292 this.classList.add('most-visited-page'); | 144 this.classList.add('most-visited-page'); |
293 this.data_ = null; | |
294 this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real'); | |
295 | |
296 this.addEventListener('carddeselected', this.handleCardDeselected_); | |
297 this.addEventListener('cardselected', this.handleCardSelected_); | |
298 }, | 145 }, |
299 | 146 |
300 /** | |
301 * Create blank (filler) tiles. | |
302 * @private | |
303 */ | |
304 createTiles_: function() { | |
305 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | |
306 this.appendTile(new MostVisited()); | |
307 } | |
308 }, | |
309 | |
310 /** | |
311 * Update the tiles after a change to |data_|. | |
312 */ | |
313 updateTiles_: function() { | |
314 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | |
315 var page = this.data_[i]; | |
316 var tile = this.mostVisitedTiles_[i]; | |
317 | |
318 if (i >= this.data_.length) | |
319 tile.reset(); | |
320 else | |
321 tile.updateForData(page); | |
322 } | |
323 }, | |
324 | |
325 /** | |
326 * Handles the 'card deselected' event (i.e. the user clicked to another | |
327 * pane). | |
328 * @param {Event} e The CardChanged event. | |
329 */ | |
330 handleCardDeselected_: function(e) { | |
331 if (!document.documentElement.classList.contains('starting-up')) { | |
332 chrome.send('mostVisitedAction', | |
333 [ntp.NtpFollowAction.CLICKED_OTHER_NTP_PANE]); | |
334 } | |
335 }, | |
336 | |
337 /** | |
338 * Handles the 'card selected' event (i.e. the user clicked to select the | |
339 * Most Visited pane). | |
340 * @param {Event} e The CardChanged event. | |
341 */ | |
342 handleCardSelected_: function(e) { | |
343 if (!document.documentElement.classList.contains('starting-up')) | |
344 chrome.send('mostVisitedSelected'); | |
345 }, | |
346 | |
347 /** | |
348 * Array of most visited data objects. | |
349 * @type {Array} | |
350 */ | |
351 get data() { | |
352 return this.data_; | |
353 }, | |
354 set data(data) { | 147 set data(data) { |
355 var startTime = Date.now(); | 148 var startTime = Date.now(); |
356 | 149 |
357 // The first time data is set, create the tiles. | 150 // The first time data is set, create the tiles. |
358 if (!this.data_) { | 151 if (!this.data_) { |
359 this.createTiles_(); | |
360 this.data_ = data.slice(0, THUMBNAIL_COUNT); | 152 this.data_ = data.slice(0, THUMBNAIL_COUNT); |
| 153 this.createTiles_(this.data_.length); |
361 } else { | 154 } else { |
362 this.data_ = refreshData(this.data_, data); | 155 this.data_ = refreshData(this.data_, data); |
363 } | 156 } |
364 | 157 |
365 this.updateTiles_(); | 158 this.updateTiles_(); |
366 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); | 159 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
367 }, | 160 }, |
368 | |
369 /** @inheritDoc */ | |
370 shouldAcceptDrag: function(e) { | |
371 return false; | |
372 }, | |
373 | |
374 /** @inheritDoc */ | |
375 heightForWidth: heightForWidth, | |
376 }; | 161 }; |
377 | 162 |
378 /** | 163 /** |
379 * Executed once the NTP has loaded. Checks if the Most Visited pane is | 164 * 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 | 165 * 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. | 166 * to the C++ code, to record the fact that the user has seen this pane. |
382 */ | 167 */ |
383 MostVisitedPage.onLoaded = function() { | 168 MostVisitedPage.onLoaded = function() { |
384 if (ntp.getCardSlider() && | 169 if (ntp.getCardSlider() && |
385 ntp.getCardSlider().currentCardValue && | 170 ntp.getCardSlider().currentCardValue && |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 return oldData; | 246 return oldData; |
462 }; | 247 }; |
463 | 248 |
464 return { | 249 return { |
465 MostVisitedPage: MostVisitedPage, | 250 MostVisitedPage: MostVisitedPage, |
466 refreshData: refreshData, | 251 refreshData: refreshData, |
467 }; | 252 }; |
468 }); | 253 }); |
469 | 254 |
470 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); | 255 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); |
OLD | NEW |