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 |
18 * @extends {HTMLAnchorElement} | 14 * @extends {HTMLAnchorElement} |
19 */ | 15 */ |
20 function MostVisited() { | 16 function MostVisited() { |
jeremycho_google
2012/08/02 03:00:49
Pass in gridValues? Also, this doesn't seem to ge
pedrosimonetti2
2012/08/03 18:14:12
Yes, we need to pass gridValues. This wasn't being
| |
21 var el = cr.doc.createElement('a'); | 17 var el = cr.doc.createElement('a'); |
22 el.__proto__ = MostVisited.prototype; | 18 el.__proto__ = MostVisited.prototype; |
23 el.initialize(); | 19 el.initialize(); |
24 | 20 |
25 return el; | 21 return el; |
26 } | 22 } |
27 | 23 |
28 MostVisited.prototype = { | 24 MostVisited.prototype = { |
29 __proto__: HTMLAnchorElement.prototype, | 25 __proto__: Thumbnail.prototype, |
30 | |
31 initialize: function() { | |
32 this.reset(); | |
33 | |
34 this.addEventListener('click', this.handleClick_); | |
35 this.addEventListener('keydown', this.handleKeyDown_); | |
36 }, | |
37 | |
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 /** | |
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 }; | 26 }; |
246 | 27 |
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; | 28 var THUMBNAIL_COUNT = 8; |
274 | 29 |
275 /** | 30 /** |
276 * Creates a new MostVisitedPage object. | 31 * Creates a new MostVisitedPage object. |
277 * @constructor | 32 * @constructor |
278 * @extends {TilePage} | 33 * @extends {TilePage} |
279 */ | 34 */ |
280 function MostVisitedPage() { | 35 function MostVisitedPage() { |
281 var el = new TilePage(mostVisitedPageGridValues); | 36 var el = new ThumbnailPage(); |
282 el.__proto__ = MostVisitedPage.prototype; | 37 el.__proto__ = MostVisitedPage.prototype; |
283 el.initialize(); | 38 el.initialize(); |
284 | 39 |
285 return el; | 40 return el; |
286 } | 41 } |
287 | 42 |
288 MostVisitedPage.prototype = { | 43 MostVisitedPage.prototype = { |
289 __proto__: TilePage.prototype, | 44 __proto__: ThumbnailPage.prototype, |
290 | 45 |
291 initialize: function() { | |
292 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 }, | |
299 | |
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) { | 46 set data(data) { |
355 var startTime = Date.now(); | 47 var startTime = Date.now(); |
356 | 48 |
357 // The first time data is set, create the tiles. | 49 // The first time data is set, create the tiles. |
358 if (!this.data_) { | 50 if (!this.data_) { |
359 this.createTiles_(); | 51 this.createTiles_(); |
360 this.data_ = data.slice(0, THUMBNAIL_COUNT); | 52 this.data_ = data.slice(0, THUMBNAIL_COUNT); |
361 } else { | 53 } else { |
362 this.data_ = refreshData(this.data_, data); | 54 this.data_ = refreshData(this.data_, data); |
363 } | 55 } |
364 | 56 |
365 this.updateTiles_(); | 57 this.updateTiles_(); |
366 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); | 58 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
367 }, | 59 }, |
368 | |
369 /** @inheritDoc */ | |
370 shouldAcceptDrag: function(e) { | |
371 return false; | |
372 }, | |
373 | |
374 /** @inheritDoc */ | |
375 heightForWidth: heightForWidth, | |
376 }; | 60 }; |
377 | 61 |
378 /** | 62 /** |
379 * Executed once the NTP has loaded. Checks if the Most Visited pane is | 63 * 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 | 64 * 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. | 65 * to the C++ code, to record the fact that the user has seen this pane. |
382 */ | 66 */ |
383 MostVisitedPage.onLoaded = function() { | 67 MostVisitedPage.onLoaded = function() { |
384 if (ntp.getCardSlider() && | 68 if (ntp.getCardSlider() && |
385 ntp.getCardSlider().currentCardValue && | 69 ntp.getCardSlider().currentCardValue && |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 return oldData; | 145 return oldData; |
462 }; | 146 }; |
463 | 147 |
464 return { | 148 return { |
465 MostVisitedPage: MostVisitedPage, | 149 MostVisitedPage: MostVisitedPage, |
466 refreshData: refreshData, | 150 refreshData: refreshData, |
467 }; | 151 }; |
468 }); | 152 }); |
469 | 153 |
470 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); | 154 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded); |
OLD | NEW |