| 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 TilePage = ntp.TilePage; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * See description for these values in ntp_stats.h. |
| 12 * @enum {number} |
| 13 */ |
| 14 var NtpFollowAction = { |
| 15 CLICKED_TILE: 11, |
| 16 CLICKED_OTHER_NTP_PANE: 12, |
| 17 OTHER: 13 |
| 18 }; |
| 19 |
| 20 /** |
| 11 * A counter for generating unique tile IDs. | 21 * A counter for generating unique tile IDs. |
| 12 */ | 22 */ |
| 13 var tileID = 0; | 23 var tileID = 0; |
| 14 | 24 |
| 15 /** | 25 /** |
| 16 * Creates a new Most Visited object for tiling. | 26 * Creates a new Most Visited object for tiling. |
| 17 * @constructor | 27 * @constructor |
| 18 * @extends {HTMLAnchorElement} | 28 * @extends {HTMLAnchorElement} |
| 19 */ | 29 */ |
| 20 function MostVisited() { | 30 function MostVisited() { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 // Records an app launch from the most visited page (Chrome will decide | 144 // 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; | 145 // 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 | 146 // other actions like "open in new tab" from the context menu won't be |
| 137 // recorded. Can this be fixed? | 147 // recorded. Can this be fixed? |
| 138 chrome.send('recordAppLaunchByURL', | 148 chrome.send('recordAppLaunchByURL', |
| 139 [encodeURIComponent(this.href), | 149 [encodeURIComponent(this.href), |
| 140 ntp.APP_LAUNCH.NTP_MOST_VISITED]); | 150 ntp.APP_LAUNCH.NTP_MOST_VISITED]); |
| 141 // Records the index of this tile. | 151 // Records the index of this tile. |
| 142 chrome.send('metricsHandler:recordInHistogram', | 152 chrome.send('metricsHandler:recordInHistogram', |
| 143 ['NewTabPage.MostVisited', this.index, 8]); | 153 ['NewTabPage.MostVisited', this.index, 8]); |
| 154 chrome.send('mostVisitedAction', |
| 155 [NtpFollowAction.CLICKED_TILE]); |
| 144 } | 156 } |
| 145 }, | 157 }, |
| 146 | 158 |
| 147 /** | 159 /** |
| 148 * Allow blacklisting most visited site using the keyboard. | 160 * Allow blacklisting most visited site using the keyboard. |
| 149 */ | 161 */ |
| 150 handleKeyDown_: function(e) { | 162 handleKeyDown_: function(e) { |
| 151 if (!cr.isMac && e.keyCode == 46 || // Del | 163 if (!cr.isMac && e.keyCode == 46 || // Del |
| 152 cr.isMac && e.metaKey && e.keyCode == 8) { // Cmd + Backspace | 164 cr.isMac && e.metaKey && e.keyCode == 8) { // Cmd + Backspace |
| 153 this.blacklist_(); | 165 this.blacklist_(); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 return el; | 294 return el; |
| 283 } | 295 } |
| 284 | 296 |
| 285 MostVisitedPage.prototype = { | 297 MostVisitedPage.prototype = { |
| 286 __proto__: TilePage.prototype, | 298 __proto__: TilePage.prototype, |
| 287 | 299 |
| 288 initialize: function() { | 300 initialize: function() { |
| 289 this.classList.add('most-visited-page'); | 301 this.classList.add('most-visited-page'); |
| 290 this.data_ = null; | 302 this.data_ = null; |
| 291 this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real'); | 303 this.mostVisitedTiles_ = this.getElementsByClassName('most-visited real'); |
| 304 |
| 305 this.addEventListener('carddeselected', this.handleCardDeselected_); |
| 306 this.addEventListener('cardselected', this.handleCardSelected_); |
| 292 }, | 307 }, |
| 293 | 308 |
| 294 /** | 309 /** |
| 295 * Create blank (filler) tiles. | 310 * Create blank (filler) tiles. |
| 296 * @private | 311 * @private |
| 297 */ | 312 */ |
| 298 createTiles_: function() { | 313 createTiles_: function() { |
| 299 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | 314 for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
| 300 this.appendTile(new MostVisited()); | 315 this.appendTile(new MostVisited()); |
| 301 } | 316 } |
| 302 }, | 317 }, |
| 303 | 318 |
| 304 /** | 319 /** |
| 305 * Update the tiles after a change to |data_|. | 320 * Update the tiles after a change to |data_|. |
| 306 */ | 321 */ |
| 307 updateTiles_: function() { | 322 updateTiles_: function() { |
| 308 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | 323 for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
| 309 var page = this.data_[i]; | 324 var page = this.data_[i]; |
| 310 var tile = this.mostVisitedTiles_[i]; | 325 var tile = this.mostVisitedTiles_[i]; |
| 311 | 326 |
| 312 if (i >= this.data_.length) | 327 if (i >= this.data_.length) |
| 313 tile.reset(); | 328 tile.reset(); |
| 314 else | 329 else |
| 315 tile.updateForData(page); | 330 tile.updateForData(page); |
| 316 } | 331 } |
| 317 }, | 332 }, |
| 318 | 333 |
| 319 /** | 334 /** |
| 335 * Handles the 'card deselected' event (i.e. the user clicked to another |
| 336 * pane). |
| 337 * @param {Event} e The CardChanged event. |
| 338 */ |
| 339 handleCardDeselected_: function(e) { |
| 340 if (!document.documentElement.classList.contains('starting-up')) { |
| 341 chrome.send('mostVisitedAction', |
| 342 [NtpFollowAction.CLICKED_OTHER_NTP_PANE]); |
| 343 } |
| 344 }, |
| 345 |
| 346 /** |
| 347 * Handles the 'card selected' event (i.e. the user clicked to select the |
| 348 * Most Visited pane). |
| 349 * @param {Event} e The CardChanged event. |
| 350 */ |
| 351 handleCardSelected_: function(e) { |
| 352 if (!document.documentElement.classList.contains('starting-up')) { |
| 353 chrome.send('mostVisitedSelected'); |
| 354 } |
| 355 }, |
| 356 |
| 357 /** |
| 320 * Array of most visited data objects. | 358 * Array of most visited data objects. |
| 321 * @type {Array} | 359 * @type {Array} |
| 322 */ | 360 */ |
| 323 get data() { | 361 get data() { |
| 324 return this.data_; | 362 return this.data_; |
| 325 }, | 363 }, |
| 326 set data(data) { | 364 set data(data) { |
| 327 var startTime = Date.now(); | 365 var startTime = Date.now(); |
| 328 | 366 |
| 329 // The first time data is set, create the tiles. | 367 // The first time data is set, create the tiles. |
| 330 if (!this.data_) { | 368 if (!this.data_) { |
| 331 this.createTiles_(); | 369 this.createTiles_(); |
| 332 this.data_ = data.slice(0, THUMBNAIL_COUNT); | 370 this.data_ = data.slice(0, THUMBNAIL_COUNT); |
| 333 } else { | 371 } else { |
| 334 this.data_ = refreshData(this.data_, data); | 372 this.data_ = refreshData(this.data_, data); |
| 335 } | 373 } |
| 336 | 374 |
| 337 this.updateTiles_(); | 375 this.updateTiles_(); |
| 338 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); | 376 logEvent('mostVisited.layout: ' + (Date.now() - startTime)); |
| 339 }, | 377 }, |
| 340 | 378 |
| 341 /** @inheritDoc */ | 379 /** @inheritDoc */ |
| 342 shouldAcceptDrag: function(e) { | 380 shouldAcceptDrag: function(e) { |
| 343 return false; | 381 return false; |
| 344 }, | 382 }, |
| 345 | 383 |
| 384 /** |
| 385 * Executed once the NTP has loaded. Checks if the Most Visited pane is |
| 386 * shown or not. |
| 387 */ |
| 388 onLoaded: function() { |
| 389 if (ntp.getCardSlider() && ntp.getCardSlider().currentCardValue) { |
| 390 if (ntp.getCardSlider().currentCardValue.classList |
| 391 .contains('most-visited-page')) { |
| 392 chrome.send('mostVisitedSelected'); |
| 393 } |
| 394 } |
| 395 }, |
| 396 |
| 346 /** @inheritDoc */ | 397 /** @inheritDoc */ |
| 347 heightForWidth: heightForWidth, | 398 heightForWidth: heightForWidth, |
| 348 }; | 399 }; |
| 349 | 400 |
| 350 /** | 401 /** |
| 351 * We've gotten additional Most Visited data. Update our old data with the | 402 * We've gotten additional Most Visited data. Update our old data with the |
| 352 * new data. The ordering of the new data is not important, except when a | 403 * new data. The ordering of the new data is not important, except when a |
| 353 * page is pinned. Thus we try to minimize re-ordering. | 404 * page is pinned. Thus we try to minimize re-ordering. |
| 354 * @param {Array} oldData The current Most Visited page list. | 405 * @param {Array} oldData The current Most Visited page list. |
| 355 * @param {Array} newData The new Most Visited page list. | 406 * @param {Array} newData The new Most Visited page list. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 } | 468 } |
| 418 | 469 |
| 419 return oldData; | 470 return oldData; |
| 420 }; | 471 }; |
| 421 | 472 |
| 422 return { | 473 return { |
| 423 MostVisitedPage: MostVisitedPage, | 474 MostVisitedPage: MostVisitedPage, |
| 424 refreshData: refreshData, | 475 refreshData: refreshData, |
| 425 }; | 476 }; |
| 426 }); | 477 }); |
| 478 |
| 479 document.addEventListener('ntploaded', ntp.MostVisitedPage.prototype.onLoaded); |
| OLD | NEW |