Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 (function() { | |
| 6 /** | |
| 7 * The element used to vertically position the most visited section on | |
| 8 * window resize. | |
| 9 * @type {Element} | |
| 10 */ | |
| 11 var topMarginElement; | |
| 12 | |
| 13 /** | |
| 14 * The container for the tile elements. | |
| 15 * @type {Element} | |
| 16 */ | |
| 17 var tilesContainer; | |
| 18 | |
| 19 /** | |
| 20 * The notification displayed when a page is blacklisted. | |
| 21 * @type {Element} | |
| 22 */ | |
| 23 var notification; | |
| 24 | |
| 25 /** | |
| 26 * The array of rendered tiles, ordered by appearance. | |
| 27 * @type {Array.<Tile>} | |
| 28 */ | |
| 29 var tiles = []; | |
| 30 | |
| 31 /** | |
| 32 * The last blacklisted tile if any, which by definition should not be filler. | |
| 33 * @type {?Tile} | |
| 34 */ | |
| 35 var lastBlacklistedTile = null; | |
| 36 | |
| 37 /** | |
| 38 * The index of the last blacklisted tile, if any. Used to determine where to | |
| 39 * re-insert a tile on undo. | |
| 40 * @type {number} | |
| 41 */ | |
| 42 var lastBlacklistedIndex = -1; | |
| 43 | |
| 44 /** | |
| 45 * True if a page has been blacklisted and we're waiting on the | |
| 46 * onmostvisitedchange callback. See onMostVisitedChange() for how this | |
| 47 * is used. | |
| 48 * @type {boolean} | |
| 49 */ | |
| 50 var isBlacklisting = false; | |
| 51 | |
| 52 /** | |
| 53 * True if a blacklist has been undone and we're waiting on the | |
| 54 * onmostvisitedchange callback. See onMostVisitedChange() for how this | |
| 55 * is used. | |
| 56 * @type {boolean} | |
| 57 */ | |
| 58 var isUndoing = false; | |
| 59 | |
| 60 /** | |
| 61 * Current number of tiles shown based on the window width, including filler. | |
| 62 * @type {number} | |
| 63 */ | |
| 64 var numTilesShown = 0; | |
| 65 | |
| 66 /** | |
| 67 * The browser embeddedSearch.newTabPage object. | |
| 68 * @type {Object} | |
| 69 */ | |
| 70 var apiHandle; | |
| 71 | |
| 72 /** | |
| 73 * Possible background-colors of a non-custom theme. Used to determine whether | |
| 74 * the homepage should be updated to support custom or non-custom themes. | |
| 75 * @type {!Array.<string>} | |
| 76 * @const | |
| 77 */ | |
| 78 var WHITE = ['rgba(255,255,255,1)', 'rgba(0,0,0,0)']; | |
| 79 | |
| 80 /** | |
| 81 * Should be equal to mv-tile's -webkit-margin-start + width. | |
| 82 * @type {number} | |
| 83 * @const | |
| 84 */ | |
| 85 var TILE_WIDTH = 160; | |
| 86 | |
| 87 /** | |
| 88 * The height of the most visited section. | |
| 89 * @type {number} | |
| 90 * @const | |
| 91 */ | |
| 92 var MOST_VISITED_HEIGHT = 156; | |
| 93 | |
| 94 /** @type {number} @const */ | |
| 95 var MAX_NUM_TILES_TO_SHOW = 4; | |
| 96 | |
| 97 /** @type {number} @const */ | |
| 98 var MIN_NUM_TILES_TO_SHOW = 2; | |
| 99 | |
| 100 /** | |
| 101 * Minimum total padding to give to the left and right of the most visited | |
| 102 * section. Used to determine how many tiles to show. | |
| 103 * @type {number} | |
| 104 * @const | |
| 105 */ | |
| 106 var MIN_TOTAL_HORIZONTAL_PADDING = 188; | |
| 107 | |
| 108 /** | |
| 109 * Enum for classnames. | |
| 110 * @enum {string} | |
| 111 * @const | |
| 112 */ | |
| 113 var CLASSES = { | |
| 114 TILE: 'mv-tile', | |
| 115 PAGE: 'mv-page', // page tiles | |
| 116 TITLE: 'mv-title', | |
| 117 THUMBNAIL: 'mv-thumb', | |
| 118 DOMAIN: 'mv-domain', | |
| 119 BLACKLIST_BUTTON: 'mv-x', | |
| 120 FAVICON: 'mv-favicon', | |
| 121 FILLER: 'mv-filler', // filler tiles | |
| 122 BLACKLIST: 'mv-blacklist', // triggers tile blacklist animation | |
| 123 HIDE_TILE: 'mv-tile-hide', // hides tiles on small browser width | |
| 124 HIDE_BLACKLIST_BUTTON: 'mv-x-hide', // hides blacklist button during animation | |
| 125 DELAYED_HIDE_NOTIFICATION: 'mv-notice-delayed-hide', | |
| 126 HIDE_NOTIFICATION: 'mv-notice-hide' | |
| 127 }; | |
| 128 | |
| 129 /** | |
| 130 * Enum for ids. | |
| 131 * @enum {string} | |
| 132 * @const | |
| 133 */ | |
| 134 var IDS = { | |
| 135 TOP_MARGIN: 'mv-top-margin', | |
| 136 TILES: 'mv-tiles', | |
| 137 NOTIFICATION: 'mv-notice', | |
| 138 NOTIFICATION_MESSAGE: 'mv-msg', | |
| 139 UNDO_LINK: 'mv-undo', | |
| 140 RESTORE_ALL_LINK: 'mv-restore', | |
| 141 NOTIFICATION_CLOSE_BUTTON: 'mv-notice-x' | |
| 142 }; | |
| 143 | |
| 144 /** | |
| 145 * A Tile is either a rendering of a Most Visited page or "filler" used to | |
| 146 * pad out the section when not enough pages exist. | |
| 147 * | |
| 148 * @param {Element} elem The element for rendering the tile. | |
| 149 * @param {number=} opt_rid The RID for the corresponding Most Visited page. | |
| 150 * Should only be left unspecified when creating a filler tile. | |
| 151 * @constructor | |
| 152 */ | |
| 153 function Tile(elem, opt_rid) { | |
| 154 /** @type {Element} */ | |
| 155 this.elem = elem; | |
| 156 | |
| 157 /** @type {number|undefined} */ | |
| 158 this.rid = opt_rid; | |
| 159 } | |
| 160 | |
| 161 /** | |
| 162 * Updates the NTP based on the current theme. | |
| 163 * @private | |
| 164 */ | |
| 165 function onThemeChange() { | |
| 166 var info = apiHandle.themeBackgroundInfo; | |
| 167 if (!info) | |
| 168 return; | |
| 169 var background = [info.colorRgba, | |
| 170 info.imageUrl, | |
| 171 info.imageTiling, | |
| 172 info.imageHorizontalAlignment, | |
| 173 info.imageVerticalAlignment].join(' ').trim(); | |
| 174 document.body.style.background = background; | |
| 175 var isCustom = !!background && WHITE.indexOf(background) == -1; | |
| 176 document.body.classList.toggle('custom-theme', isCustom); | |
| 177 } | |
| 178 | |
| 179 /** | |
| 180 * Handles a new set of Most Visited page data. | |
| 181 */ | |
| 182 function onMostVisitedChange() { | |
| 183 var pages = apiHandle.mostVisited; | |
| 184 | |
| 185 if (isBlacklisting) { | |
| 186 // If this was called as a result of a blacklist, add a new replacement | |
| 187 // (possibly filler) tile at the end and trigger the blacklist animation. | |
| 188 var replacementTile = createTile(pages[MAX_NUM_TILES_TO_SHOW - 1]); | |
| 189 | |
| 190 tiles.push(replacementTile); | |
| 191 tilesContainer.appendChild(replacementTile.elem); | |
| 192 | |
| 193 var lastBlacklistedTileElement = lastBlacklistedTile.elem; | |
| 194 lastBlacklistedTileElement.addEventListener( | |
| 195 'webkitTransitionEnd', blacklistAnimationDone); | |
| 196 lastBlacklistedTileElement.classList.add(CLASSES.BLACKLIST); | |
| 197 // In order to animate the replacement tile sliding into place, it must | |
| 198 // be made visible. | |
| 199 updateTileVisibility(numTilesShown + 1); | |
| 200 | |
| 201 } else if (isUndoing) { | |
| 202 // If this was called as a result of an undo, re-insert the last blacklisted | |
| 203 // tile in its old location and trigger the undo animation. | |
| 204 tiles.splice( | |
| 205 lastBlacklistedIndex, 0, lastBlacklistedTile); | |
| 206 var lastBlacklistedTileElement = lastBlacklistedTile.elem; | |
| 207 tilesContainer.insertBefore( | |
| 208 lastBlacklistedTileElement, | |
| 209 tilesContainer.childNodes[lastBlacklistedIndex]); | |
| 210 lastBlacklistedTileElement.addEventListener( | |
| 211 'webkitTransitionEnd', undoAnimationDone); | |
| 212 // Force the removal to happen synchronously. | |
| 213 lastBlacklistedTileElement.scrollTop; | |
| 214 lastBlacklistedTileElement.classList.remove(CLASSES.BLACKLIST); | |
| 215 } else { | |
| 216 // Otherwise render the tiles using the new data without animation. | |
| 217 tiles = []; | |
| 218 for (var i = 0; i < MAX_NUM_TILES_TO_SHOW; ++i) { | |
| 219 tiles.push(createTile(pages[i])); | |
| 220 } | |
| 221 renderTiles(); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 /** | |
| 226 * Renders the current set of tiles without animation. | |
| 227 */ | |
| 228 function renderTiles() { | |
| 229 removeChildren(tilesContainer); | |
| 230 for (var i = 0, length = tiles.length; i < length; ++i) { | |
| 231 tilesContainer.appendChild(tiles[i].elem); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 /** | |
| 236 * Creates a Tile with the specified page data. If no data is provided, a | |
| 237 * filler Tile is created. | |
| 238 * @param {Object} page The page data. | |
| 239 * @return {Tile} The new Tile. | |
| 240 */ | |
| 241 function createTile(page) { | |
| 242 var tileElement = document.createElement('div'); | |
| 243 tileElement.classList.add(CLASSES.TILE); | |
| 244 | |
| 245 if (page) { | |
| 246 var rid = page.rid; | |
| 247 tileElement.classList.add(CLASSES.PAGE); | |
| 248 | |
| 249 // The click handler for navigating to the page identified by the RID. | |
| 250 tileElement.addEventListener('click', function() { | |
| 251 apiHandle.navigateContentWindow(rid); | |
| 252 }); | |
| 253 | |
| 254 // The shadow DOM which renders the page title. | |
| 255 var titleElement = page.titleElement; | |
| 256 if (titleElement) { | |
| 257 titleElement.classList.add(CLASSES.TITLE); | |
| 258 tileElement.appendChild(titleElement); | |
| 259 } | |
| 260 | |
| 261 // Render the thumbnail if present. Otherwise, fall back to a shadow DOM | |
| 262 // which renders the domain. | |
| 263 var thumbnailUrl = page.thumbnailUrl; | |
| 264 | |
| 265 var showDomainElement = function() { | |
| 266 var domainElement = page.domainElement; | |
| 267 if (domainElement) { | |
| 268 domainElement.classList.add(CLASSES.DOMAIN); | |
| 269 tileElement.appendChild(domainElement); | |
| 270 } | |
| 271 }; | |
| 272 if (thumbnailUrl) { | |
| 273 var image = new Image(); | |
| 274 image.onload = function() { | |
| 275 var thumbnailElement = createAndAppendElement( | |
| 276 tileElement, 'div', CLASSES.THUMBNAIL); | |
| 277 thumbnailElement.style.backgroundImage = 'url(' + thumbnailUrl + ')'; | |
| 278 }; | |
| 279 | |
| 280 image.onerror = showDomainElement; | |
| 281 image.src = thumbnailUrl; | |
| 282 } else { | |
| 283 showDomainElement(); | |
| 284 } | |
| 285 | |
| 286 // The button used to blacklist this page. | |
| 287 var blacklistButton = createAndAppendElement( | |
| 288 tileElement, 'div', CLASSES.BLACKLIST_BUTTON); | |
| 289 blacklistButton.addEventListener('click', generateBlacklistFunction(rid)); | |
| 290 // TODO(jeremycho): i18n. See | |
| 291 // https://code.google.com/p/chromium/issues/detail?id=190223 | |
|
Dan Beam
2013/03/25 21:37:39
nit: this is fine, but http://crbug.com/190223 is
jeremycho
2013/03/25 21:51:20
Done.
| |
| 292 blacklistButton.title = "Don't show on this page"; | |
| 293 | |
| 294 // The page favicon, if any. | |
| 295 var faviconUrl = page.faviconUrl; | |
| 296 if (faviconUrl) { | |
| 297 var favicon = createAndAppendElement( | |
| 298 tileElement, 'div', CLASSES.FAVICON); | |
| 299 favicon.style.backgroundImage = 'url(' + faviconUrl + ')'; | |
| 300 } | |
| 301 return new Tile(tileElement, rid); | |
| 302 } else { | |
| 303 tileElement.classList.add(CLASSES.FILLER); | |
| 304 return new Tile(tileElement); | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 /** | |
| 309 * Generates a function to be called when the page with the corresponding RID | |
| 310 * is blacklisted. | |
| 311 * @param {number} rid The RID of the page being blacklisted. | |
| 312 * @return {function(Event)} A function which handles the blacklisting of the | |
| 313 * page by displaying the notification, updating state variables, and | |
| 314 * notifying Chrome. | |
| 315 */ | |
| 316 function generateBlacklistFunction(rid) { | |
| 317 return function(e) { | |
| 318 // Prevent navigation when the page is being blacklisted. | |
| 319 e.stopPropagation(); | |
| 320 | |
| 321 showNotification(); | |
| 322 isBlacklisting = true; | |
| 323 tilesContainer.classList.add(CLASSES.HIDE_BLACKLIST_BUTTON); | |
| 324 lastBlacklistedTile = getTileByRid(rid); | |
| 325 lastBlacklistedIndex = tiles.indexOf(lastBlacklistedTile); | |
| 326 apiHandle.deleteMostVisitedItem(rid); | |
| 327 }; | |
| 328 } | |
| 329 | |
| 330 /** | |
| 331 * Shows the blacklist notification and triggers a delay to hide it. | |
| 332 */ | |
| 333 function showNotification() { | |
| 334 notification.classList.remove(CLASSES.HIDE_NOTIFICATION); | |
| 335 notification.classList.remove(CLASSES.DELAYED_HIDE_NOTIFICATION); | |
| 336 notification.scrollTop; | |
| 337 notification.classList.add(CLASSES.DELAYED_HIDE_NOTIFICATION); | |
| 338 } | |
| 339 | |
| 340 /** | |
| 341 * Hides the blacklist notification. | |
| 342 */ | |
| 343 function hideNotification() { | |
| 344 notification.classList.add(CLASSES.HIDE_NOTIFICATION); | |
| 345 } | |
| 346 | |
| 347 /** | |
| 348 * Handles the end of the blacklist animation by removing the blacklisted tile. | |
| 349 */ | |
| 350 function blacklistAnimationDone() { | |
| 351 tiles.splice(lastBlacklistedIndex, 1); | |
| 352 removeNode(lastBlacklistedTile.elem); | |
| 353 updateTileVisibility(numTilesShown); | |
| 354 isBlacklisting = false; | |
| 355 tilesContainer.classList.remove(CLASSES.HIDE_BLACKLIST_BUTTON); | |
| 356 lastBlacklistedTile.elem.removeEventListener( | |
| 357 'webkitTransitionEnd', blacklistAnimationDone); | |
| 358 } | |
| 359 | |
| 360 /** | |
| 361 * Handles a click on the notification undo link by hiding the notification and | |
| 362 * informing Chrome. | |
| 363 */ | |
| 364 function onUndo() { | |
| 365 hideNotification(); | |
| 366 var lastBlacklistedRID = lastBlacklistedTile.rid; | |
| 367 if (typeof lastBlacklistedRID != 'undefined') { | |
| 368 isUndoing = true; | |
| 369 apiHandle.undoMostVisitedDeletion(lastBlacklistedRID); | |
| 370 } | |
| 371 } | |
| 372 | |
| 373 /** | |
| 374 * Handles the end of the undo animation by removing the extraneous end tile. | |
| 375 */ | |
| 376 function undoAnimationDone() { | |
| 377 isUndoing = false; | |
| 378 tiles.splice(tiles.length - 1, 1); | |
| 379 removeNode(tilesContainer.lastElementChild); | |
| 380 updateTileVisibility(numTilesShown); | |
| 381 lastBlacklistedTile.elem.removeEventListener( | |
| 382 'webkitTransitionEnd', undoAnimationDone); | |
| 383 } | |
| 384 | |
| 385 /** | |
| 386 * Handles a click on the restore all notification link by hiding the | |
| 387 * notification and informing Chrome. | |
| 388 */ | |
| 389 function onRestoreAll() { | |
| 390 hideNotification(); | |
| 391 apiHandle.undoAllMostVisitedDeletions(); | |
| 392 } | |
| 393 | |
| 394 /** | |
| 395 * Handles a resize by vertically centering the most visited section | |
| 396 * and triggering the tile show/hide animation if necessary. | |
| 397 */ | |
| 398 function onResize() { | |
| 399 var clientHeight = document.documentElement.clientHeight; | |
| 400 topMarginElement.style.marginTop = | |
| 401 Math.max(0, (clientHeight - MOST_VISITED_HEIGHT) / 2) + 'px'; | |
| 402 | |
| 403 var clientWidth = document.documentElement.clientWidth; | |
| 404 var numTilesToShow = Math.floor( | |
| 405 (clientWidth - MIN_TOTAL_HORIZONTAL_PADDING) / TILE_WIDTH); | |
| 406 numTilesToShow = Math.max(MIN_NUM_TILES_TO_SHOW, numTilesToShow); | |
| 407 if (numTilesToShow != numTilesShown) { | |
| 408 updateTileVisibility(numTilesToShow); | |
| 409 numTilesShown = numTilesToShow; | |
| 410 } | |
| 411 } | |
| 412 | |
| 413 /** | |
| 414 * Triggers an animation to show the first numTilesToShow tiles and hide the | |
| 415 * remaining. | |
| 416 * @param {number} numTilesToShow The number of tiles to show. | |
| 417 */ | |
| 418 function updateTileVisibility(numTilesToShow) { | |
| 419 for (var i = 0, length = tiles.length; i < length; ++i) { | |
| 420 tiles[i].elem.classList.toggle(CLASSES.HIDE_TILE, i >= numTilesToShow); | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 /** | |
| 425 * Returns the tile corresponding to the specified page RID. | |
| 426 * @param {number} rid The page RID being looked up. | |
| 427 * @return {Tile} The corresponding tile. | |
| 428 */ | |
| 429 function getTileByRid(rid) { | |
| 430 for (var i = 0, length = tiles.length; i < length; ++i) { | |
| 431 var tile = tiles[i]; | |
| 432 if (tile.rid == rid) | |
| 433 return tile; | |
| 434 } | |
| 435 return null; | |
| 436 } | |
| 437 | |
| 438 /** | |
| 439 * Utility function which creates an element with an optional classname and | |
| 440 * appends it to the specified parent. | |
| 441 * @param {Element} parent The parent to append the new element. | |
| 442 * @param {string} name The name of the new element. | |
| 443 * @param {string=} opt_class The optional classname of the new element. | |
| 444 * @return {Element} The new element. | |
| 445 */ | |
| 446 function createAndAppendElement(parent, name, opt_class) { | |
| 447 var child = document.createElement(name); | |
| 448 if (opt_class) | |
| 449 child.classList.add(opt_class); | |
| 450 parent.appendChild(child); | |
| 451 return child; | |
| 452 } | |
| 453 | |
| 454 /** | |
| 455 * Removes a node from its parent. | |
| 456 * @param {Node} node The node to remove. | |
| 457 */ | |
| 458 function removeNode(node) { | |
|
Dan Beam
2013/03/25 21:37:39
I don't really see the benefit of this method, but
jeremycho
2013/03/25 21:51:20
Acknowledged. Thought it was a little cleaner tha
| |
| 459 node.parentNode.removeChild(node); | |
| 460 } | |
| 461 | |
| 462 /** | |
| 463 * Removes all the child nodes on a DOM node. | |
| 464 * @param {Node} node Node to remove children from. | |
| 465 */ | |
| 466 function removeChildren(node) { | |
| 467 node.innerHTML = ''; | |
| 468 } | |
| 469 | |
| 470 /** | |
| 471 * @return {Object} the handle to the embeddedSearch API. | |
| 472 */ | |
| 473 function getEmbeddedSearchApiHandle() { | |
| 474 if (window.cideb) | |
| 475 return window.cideb; | |
| 476 if (window.chrome && window.chrome.embeddedSearch) | |
| 477 return window.chrome.embeddedSearch; | |
| 478 return null; | |
| 479 } | |
| 480 | |
| 481 /** | |
| 482 * Prepares the New Tab Page by adding listeners, rendering the current | |
| 483 * theme, and the most visited pages section. | |
| 484 */ | |
| 485 function init() { | |
| 486 topMarginElement = document.getElementById(IDS.TOP_MARGIN); | |
| 487 tilesContainer = document.getElementById(IDS.TILES); | |
| 488 notification = document.getElementById(IDS.NOTIFICATION); | |
| 489 | |
| 490 // TODO(jeremycho): i18n. | |
| 491 var notificationMessage = document.getElementById(IDS.NOTIFICATION_MESSAGE); | |
| 492 notificationMessage.innerText = 'Thumbnail removed.'; | |
| 493 var undoLink = document.getElementById(IDS.UNDO_LINK); | |
| 494 undoLink.addEventListener('click', onUndo); | |
| 495 undoLink.innerText = 'Undo'; | |
| 496 var restoreAllLink = document.getElementById(IDS.RESTORE_ALL_LINK); | |
| 497 restoreAllLink.addEventListener('click', onRestoreAll); | |
| 498 restoreAllLink.innerText = 'Restore all'; | |
| 499 var notificationCloseButton = | |
| 500 document.getElementById(IDS.NOTIFICATION_CLOSE_BUTTON); | |
| 501 notificationCloseButton.addEventListener('click', hideNotification); | |
| 502 | |
| 503 window.addEventListener('resize', onResize); | |
| 504 onResize(); | |
| 505 | |
| 506 var topLevelHandle = getEmbeddedSearchApiHandle(); | |
| 507 // This is to inform Chrome that the NTP is instant-extended capable i.e. | |
| 508 // it should fire events like onmostvisitedchange. | |
| 509 topLevelHandle.searchBox.onsubmit = function() {}; | |
| 510 | |
| 511 apiHandle = topLevelHandle.newTabPage; | |
| 512 apiHandle.onthemechange = onThemeChange; | |
| 513 apiHandle.onmostvisitedchange = onMostVisitedChange; | |
| 514 | |
| 515 onThemeChange(); | |
| 516 onMostVisitedChange(); | |
| 517 } | |
| 518 | |
| 519 document.addEventListener('DOMContentLoaded', init); | |
| 520 })(); | |
| OLD | NEW |