Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: chrome/browser/resources/local_ntp/local_ntp.js

Issue 412073002: Local NTP: prevent tiles from reloading on resize by moving them into single <div>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing comments. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 5
6 /** 6 /**
7 * @fileoverview The local InstantExtended NTP. 7 * @fileoverview The local InstantExtended NTP.
8 */ 8 */
9 9
10 10
11 /** 11 /**
12 * Controls rendering the new tab page for InstantExtended. 12 * Controls rendering the new tab page for InstantExtended.
13 * @return {Object} A limited interface for testing the local NTP. 13 * @return {Object} A limited interface for testing the local NTP.
14 */ 14 */
15 function LocalNTP() { 15 function LocalNTP() {
16 <include src="../../../../ui/webui/resources/js/assert.js"> 16 <include src="../../../../ui/webui/resources/js/assert.js">
17 <include src="local_ntp_util.js">
17 <include src="window_disposition_util.js"> 18 <include src="window_disposition_util.js">
18 19
19 20
20 /** 21 /**
21 * Enum for classnames. 22 * Enum for classnames.
22 * @enum {string} 23 * @enum {string}
23 * @const 24 * @const
24 */ 25 */
25 var CLASSES = { 26 var CLASSES = {
26 ALTERNATE_LOGO: 'alternate-logo', // Shows white logo if required by theme 27 ALTERNATE_LOGO: 'alternate-logo', // Shows white logo if required by theme
27 BLACKLIST: 'mv-blacklist', // triggers tile blacklist animation 28 BLACKLIST: 'mv-blacklist', // triggers tile blacklist animation
28 BLACKLIST_BUTTON: 'mv-x', 29 BLACKLIST_BUTTON: 'mv-x',
29 DELAYED_HIDE_NOTIFICATION: 'mv-notice-delayed-hide', 30 DELAYED_HIDE_NOTIFICATION: 'mv-notice-delayed-hide',
30 FAKEBOX_DISABLE: 'fakebox-disable', // Makes fakebox non-interactive 31 FAKEBOX_DISABLE: 'fakebox-disable', // Makes fakebox non-interactive
31 FAKEBOX_FOCUS: 'fakebox-focused', // Applies focus styles to the fakebox 32 FAKEBOX_FOCUS: 'fakebox-focused', // Applies focus styles to the fakebox
32 // Applies drag focus style to the fakebox 33 // Applies drag focus style to the fakebox
33 FAKEBOX_DRAG_FOCUS: 'fakebox-drag-focused', 34 FAKEBOX_DRAG_FOCUS: 'fakebox-drag-focused',
34 FAVICON: 'mv-favicon', 35 FAVICON: 'mv-favicon',
35 HIDE_BLACKLIST_BUTTON: 'mv-x-hide', // hides blacklist button during animation 36 HIDE_BLACKLIST_BUTTON: 'mv-x-hide', // hides blacklist button during animation
36 HIDE_FAKEBOX_AND_LOGO: 'hide-fakebox-logo', 37 HIDE_FAKEBOX_AND_LOGO: 'hide-fakebox-logo',
37 HIDE_NOTIFICATION: 'mv-notice-hide', 38 HIDE_NOTIFICATION: 'mv-notice-hide',
38 // Vertically centers the most visited section for a non-Google provided page. 39 // Vertically centers the most visited section for a non-Google provided page.
39 NON_GOOGLE_PAGE: 'non-google-page', 40 NON_GOOGLE_PAGE: 'non-google-page',
40 PAGE: 'mv-page', // page tiles 41 PAGE: 'mv-page', // page tiles
41 PAGE_READY: 'mv-page-ready', // page tile when ready 42 PAGE_READY: 'mv-page-ready', // page tile when ready
42 ROW: 'mv-row', // tile row
43 RTL: 'rtl', // Right-to-left language text. 43 RTL: 'rtl', // Right-to-left language text.
44 THUMBNAIL: 'mv-thumb', 44 THUMBNAIL: 'mv-thumb',
45 THUMBNAIL_MASK: 'mv-mask', 45 THUMBNAIL_MASK: 'mv-mask',
46 TILE: 'mv-tile', 46 TILE: 'mv-tile',
47 TITLE: 'mv-title' 47 TITLE: 'mv-title'
48 }; 48 };
49 49
50 50
51 /** 51 /**
52 * Enum for HTML element ids. 52 * Enum for HTML element ids.
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 162
163 /** 163 /**
164 * Current number of tiles columns shown based on the window width, including 164 * Current number of tiles columns shown based on the window width, including
165 * those that just contain filler. 165 * those that just contain filler.
166 * @type {number} 166 * @type {number}
167 */ 167 */
168 var numColumnsShown = 0; 168 var numColumnsShown = 0;
169 169
170 170
171 /** 171 /**
172 * True if the user initiated the current most visited change and false 172 * A flag to indicate Most Visited changed caused by user action. If true, then
173 * otherwise. 173 * in onMostVisitedChange() tiles remain visible so no flickering occurs.
174 * @type {boolean} 174 * @type {boolean}
175 */ 175 */
176 var userInitiatedMostVisitedChange = false; 176 var userInitiatedMostVisitedChange = false;
177 177
178 178
179 /** 179 /**
180 * A barrier to make tiles visible the moment all tiles are loaded.
181 * @type {Barrier}
182 */
183 var tileVisibilityBarrier = new Barrier(function() {
184 tilesContainer.style.visibility = 'visible';
185 });
186
187
188 /**
180 * The browser embeddedSearch.newTabPage object. 189 * The browser embeddedSearch.newTabPage object.
181 * @type {Object} 190 * @type {Object}
182 */ 191 */
183 var ntpApiHandle; 192 var ntpApiHandle;
184 193
185 194
186 /** 195 /**
187 * The browser embeddedSearch.searchBox object. 196 * The browser embeddedSearch.searchBox object.
188 * @type {Object} 197 * @type {Object}
189 */ 198 */
(...skipping 16 matching lines...) Expand all
206 215
207 /** 216 /**
208 * Total tile width. Should be equal to mv-tile's width + 2 * border-width. 217 * Total tile width. Should be equal to mv-tile's width + 2 * border-width.
209 * @private {number} 218 * @private {number}
210 * @const 219 * @const
211 */ 220 */
212 var TILE_WIDTH = 140; 221 var TILE_WIDTH = 140;
213 222
214 223
215 /** 224 /**
216 * Margin between tiles. Should be equal to mv-tile's -webkit-margin-start. 225 * Margin between tiles. Should be equal to mv-tile's total horizontal margin.
217 * @private {number} 226 * @private {number}
218 * @const 227 * @const
219 */ 228 */
220 var TILE_MARGIN_START = 20; 229 var TILE_MARGIN = 20;
221 230
222 231
223 /** @type {number} @const */ 232 /** @type {number} @const */
224 var MAX_NUM_TILES_TO_SHOW = 8; 233 var MAX_NUM_TILES_TO_SHOW = 8;
225 234
226 235
227 /** @type {number} @const */ 236 /** @type {number} @const */
228 var MIN_NUM_COLUMNS = 2; 237 var MIN_NUM_COLUMNS = 2;
229 238
230 239
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 331
323 var background = [convertToRGBAColor(info.backgroundColorRgba), 332 var background = [convertToRGBAColor(info.backgroundColorRgba),
324 info.imageUrl, 333 info.imageUrl,
325 info.imageTiling, 334 info.imageTiling,
326 info.imageHorizontalAlignment, 335 info.imageHorizontalAlignment,
327 info.imageVerticalAlignment].join(' ').trim(); 336 info.imageVerticalAlignment].join(' ').trim();
328 document.body.style.background = background; 337 document.body.style.background = background;
329 document.body.classList.toggle(CLASSES.ALTERNATE_LOGO, info.alternateLogo); 338 document.body.classList.toggle(CLASSES.ALTERNATE_LOGO, info.alternateLogo);
330 updateThemeAttribution(info.attributionUrl); 339 updateThemeAttribution(info.attributionUrl);
331 setCustomThemeStyle(info); 340 setCustomThemeStyle(info);
332 renderTiles(); 341
342 renderTiles(true);
333 } 343 }
334 344
335 345
336 /** 346 /**
337 * Updates the NTP style according to theme. 347 * Updates the NTP style according to theme.
338 * @param {Object=} opt_themeInfo The information about the theme. If it is 348 * @param {Object=} opt_themeInfo The information about the theme. If it is
339 * omitted the style will be reverted to the default. 349 * omitted the style will be reverted to the default.
340 * @private 350 * @private
341 */ 351 */
342 function setCustomThemeStyle(opt_themeInfo) { 352 function setCustomThemeStyle(opt_themeInfo) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 function convertToRGBAColor(color) { 435 function convertToRGBAColor(color) {
426 return 'rgba(' + color[0] + ',' + color[1] + ',' + color[2] + ',' + 436 return 'rgba(' + color[0] + ',' + color[1] + ',' + color[2] + ',' +
427 color[3] / 255 + ')'; 437 color[3] / 255 + ')';
428 } 438 }
429 439
430 440
431 /** 441 /**
432 * Handles a new set of Most Visited page data. 442 * Handles a new set of Most Visited page data.
433 */ 443 */
434 function onMostVisitedChange() { 444 function onMostVisitedChange() {
435 var pages = ntpApiHandle.mostVisited;
436
437 if (isBlacklisting) { 445 if (isBlacklisting) {
438 // Trigger the blacklist animation and re-render the tiles when it 446 // Trigger the blacklist animation, which then triggers reloadAllTiles().
439 // completes.
440 var lastBlacklistedTileElement = lastBlacklistedTile.elem; 447 var lastBlacklistedTileElement = lastBlacklistedTile.elem;
441 lastBlacklistedTileElement.addEventListener( 448 lastBlacklistedTileElement.addEventListener(
442 'webkitTransitionEnd', blacklistAnimationDone); 449 'webkitTransitionEnd', blacklistAnimationDone);
443 lastBlacklistedTileElement.classList.add(CLASSES.BLACKLIST); 450 lastBlacklistedTileElement.classList.add(CLASSES.BLACKLIST);
444
445 } else { 451 } else {
446 // Otherwise render the tiles using the new data without animation. 452 reloadAllTiles();
447 tiles = [];
448 for (var i = 0; i < MAX_NUM_TILES_TO_SHOW; ++i) {
449 tiles.push(createTile(pages[i], i));
450 }
451 if (!userInitiatedMostVisitedChange) {
452 tilesContainer.hidden = true;
453 window.setTimeout(function() {
454 if (tilesContainer) {
455 tilesContainer.hidden = false;
456 }
457 }, MOST_VISITED_PAINT_TIMEOUT_MSEC);
458 }
459 renderTiles();
460 } 453 }
461 } 454 }
462 455
463 456
464 /** 457 /**
465 * Renders the current set of tiles. 458 * Handles the end of the blacklist animation by showing the notification and
459 * re-rendering the new set of tiles.
466 */ 460 */
467 function renderTiles() { 461 function blacklistAnimationDone() {
468 var rows = tilesContainer.children; 462 showNotification();
469 for (var i = 0; i < rows.length; ++i) { 463 isBlacklisting = false;
470 removeChildren(rows[i]); 464 tilesContainer.classList.remove(CLASSES.HIDE_BLACKLIST_BUTTON);
471 } 465 lastBlacklistedTile.elem.removeEventListener(
472 466 'webkitTransitionEnd', blacklistAnimationDone);
473 for (var i = 0, length = tiles.length; 467 // Need to call explicitly to re-render the tiles, since the initial
474 i < Math.min(length, numColumnsShown * NUM_ROWS); ++i) { 468 // onmostvisitedchange issued by the blacklist function only triggered
475 rows[Math.floor(i / numColumnsShown)].appendChild(tiles[i].elem); 469 // the animation.
476 } 470 reloadAllTiles();
477 } 471 }
478 472
479 473
480 /** 474 /**
481 * Shows most visited tiles if all child iframes are loaded, and hides them 475 * Fetches new data, creates, and renders tiles.
482 * otherwise.
483 */ 476 */
484 function updateMostVisitedVisibility() { 477 function reloadAllTiles() {
485 var iframes = tilesContainer.querySelectorAll('iframe'); 478 var pages = ntpApiHandle.mostVisited;
486 var ready = true; 479
487 for (var i = 0, numIframes = iframes.length; i < numIframes; i++) { 480 if (!userInitiatedMostVisitedChange) {
488 if (iframes[i].hidden) { 481 // Temporarily make titleContainer invisible, but it still takes up space.
489 ready = false; 482 // We make it visible again (1) on timeout, or (2) when all tiles finish
490 break; 483 // loading (using tileVisibilityBarrier).
491 } 484 tilesContainer.style.visibility = 'hidden';
485 window.setTimeout(function() {
486 if (tilesContainer) {
487 tilesContainer.style.visibility = 'visible';
Jered 2014/07/30 17:52:14 You should also reset the barrier here. Otherwise,
huangs 2014/07/30 21:14:47 Doing both: (1) making Barrier cancellable and doi
488 }
489 }, MOST_VISITED_PAINT_TIMEOUT_MSEC);
492 } 490 }
493 if (ready) { 491 userInitiatedMostVisitedChange = false;
494 tilesContainer.hidden = false; 492
495 userInitiatedMostVisitedChange = false; 493 tiles = [];
496 } 494 // Javascript is single-threaded, so don't need to wrap the loop with
Jered 2014/07/30 17:52:14 nit: omit this comment. Readers should already kno
huangs 2014/07/30 21:14:47 Done.
495 // tileVisibilityBarrier.add() and tileVisibilityBarrier.remove().
496 for (var i = 0; i < MAX_NUM_TILES_TO_SHOW; ++i)
497 tiles.push(createTile(pages[i], i));
498 renderTiles(true);
497 } 499 }
498 500
499 501
502 /**
503 * Renders the current list of visible tiles to DOM, and hides tiles that are
504 * already in the DOM but should not be seen.
505 * @param {boolean} clearAll Whether to clear existing rendered tiles.
506 */
507 function renderTiles(clearAll) {
Jered 2014/07/30 17:52:14 Instead of adding this boolean parameter, either c
huangs 2014/07/30 21:14:47 Done.
508 var numExisting = 0;
509 if (clearAll)
510 tilesContainer.innerHTML = '';
511 else
512 numExisting = tilesContainer.querySelectorAll('.' + CLASSES.TILE).length;
513 // Only add visible tiles to the DOM, to avoid creating invisible tiles that
514 // produce meaningless impression metrics. However, if a tile becomes
515 // invisible then we leave it in DOM to prevent reload if they're shown again.
516 var numDesired = Math.min(tiles.length, numColumnsShown * NUM_ROWS);
517 var i;
Jered 2014/07/30 17:52:14 nit: you can say var i in the loop initializers be
huangs 2014/07/30 21:14:47 Done.
518 for (i = numExisting; i < numDesired; ++i)
519 tilesContainer.appendChild(tiles[i].elem);
520
521 // Show only the desired tiles. Not using .hidden because it does not work
522 // for inline-block elements.
523 for (i = 0; i < numDesired; ++i)
524 tiles[i].elem.style.display = 'inline-block';
525 // If |numDesired| < |numExisting| then hide extra tiles (e.g., this occurs
526 // when window is downsized).
527 for (; i < numExisting; ++i)
528 tiles[i].elem.style.display = 'none';
529 }
530
531
500 /** 532 /**
501 * Builds a URL to display a most visited tile component in an iframe. 533 * Builds a URL to display a most visited tile component in an iframe.
502 * @param {string} filename The desired most visited component filename. 534 * @param {string} filename The desired most visited component filename.
503 * @param {number} rid The restricted ID. 535 * @param {number} rid The restricted ID.
504 * @param {string} color The text color for text in the iframe. 536 * @param {string} color The text color for text in the iframe.
505 * @param {string} fontFamily The font family for text in the iframe. 537 * @param {string} fontFamily The font family for text in the iframe.
506 * @param {number} fontSize The font size for text in the iframe. 538 * @param {number} fontSize The font size for text in the iframe.
507 * @param {number} position The position of the iframe in the UI. 539 * @param {number} position The position of the iframe in the UI.
508 * @return {string} An URL to display the most visited component in an iframe. 540 * @return {string} An URL to display the most visited component in an iframe.
509 */ 541 */
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 // begins loading RIDs n, n+1, ..., n+k-1; after the second event, these get 588 // begins loading RIDs n, n+1, ..., n+k-1; after the second event, these get
557 // destroyed and a new set begins loading RIDs n+k, n+k+1, ..., n+2k-1. 589 // destroyed and a new set begins loading RIDs n+k, n+k+1, ..., n+2k-1.
558 // Now due to crbug.com/68841, Chrome incorrectly loads the content for the 590 // Now due to crbug.com/68841, Chrome incorrectly loads the content for the
559 // first set of iframes into the most recent set of iframes. 591 // first set of iframes into the most recent set of iframes.
560 // 592 //
561 // Giving iframes distinct ids seems to cause some invalidation and prevent 593 // Giving iframes distinct ids seems to cause some invalidation and prevent
562 // associating the incorrect data. 594 // associating the incorrect data.
563 // 595 //
564 // TODO(jered): Find and fix the root (probably Blink) bug. 596 // TODO(jered): Find and fix the root (probably Blink) bug.
565 597
598 // Keep this ID here. See comment above.
599 titleElement.id = 'title-' + rid;
600 titleElement.className = CLASSES.TITLE;
601 titleElement.hidden = true;
602 tileVisibilityBarrier.add();
603 titleElement.onload = function() {
604 titleElement.hidden = false;
605 tileVisibilityBarrier.remove();
606 };
566 titleElement.src = getMostVisitedIframeUrl( 607 titleElement.src = getMostVisitedIframeUrl(
567 MOST_VISITED_TITLE_IFRAME, rid, MOST_VISITED_COLOR, 608 MOST_VISITED_TITLE_IFRAME, rid, MOST_VISITED_COLOR,
568 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position); 609 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position);
569
570 // Keep this id here. See comment above.
571 titleElement.id = 'title-' + rid;
572 titleElement.hidden = true;
573 titleElement.onload = function() {
574 titleElement.hidden = false;
575 updateMostVisitedVisibility();
576 };
577 titleElement.className = CLASSES.TITLE;
578 tileElement.appendChild(titleElement); 610 tileElement.appendChild(titleElement);
579 611
580 // The iframe which renders either a thumbnail or domain element. 612 // The iframe which renders either a thumbnail or domain element.
581 var thumbnailElement = document.createElement('iframe'); 613 var thumbnailElement = document.createElement('iframe');
582 thumbnailElement.tabIndex = '-1'; 614 thumbnailElement.tabIndex = '-1';
615 // Keep this ID here. See comment above.
616 thumbnailElement.id = 'thumb-' + rid;
617 thumbnailElement.className = CLASSES.THUMBNAIL;
618 thumbnailElement.hidden = true;
619 tileVisibilityBarrier.add();
620 thumbnailElement.onload = function() {
621 thumbnailElement.hidden = false;
622 tileElement.classList.add(CLASSES.PAGE_READY);
623 tileVisibilityBarrier.remove();
624 };
583 thumbnailElement.src = getMostVisitedIframeUrl( 625 thumbnailElement.src = getMostVisitedIframeUrl(
584 MOST_VISITED_THUMBNAIL_IFRAME, rid, MOST_VISITED_COLOR, 626 MOST_VISITED_THUMBNAIL_IFRAME, rid, MOST_VISITED_COLOR,
585 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position); 627 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position);
586
587 // Keep this id here. See comment above.
588 thumbnailElement.id = 'thumb-' + rid;
589 thumbnailElement.hidden = true;
590 thumbnailElement.onload = function() {
591 thumbnailElement.hidden = false;
592 tileElement.classList.add(CLASSES.PAGE_READY);
593 updateMostVisitedVisibility();
594 };
595 thumbnailElement.className = CLASSES.THUMBNAIL;
596 tileElement.appendChild(thumbnailElement); 628 tileElement.appendChild(thumbnailElement);
597 629
598 // A mask to darken the thumbnail on focus. 630 // A mask to darken the thumbnail on focus.
599 var maskElement = createAndAppendElement( 631 var maskElement = createAndAppendElement(
600 tileElement, 'div', CLASSES.THUMBNAIL_MASK); 632 tileElement, 'div', CLASSES.THUMBNAIL_MASK);
601 633
602 // The button used to blacklist this page. 634 // The button used to blacklist this page.
603 var blacklistButton = createAndAppendElement( 635 var blacklistButton = createAndAppendElement(
604 tileElement, 'div', CLASSES.BLACKLIST_BUTTON); 636 tileElement, 'div', CLASSES.BLACKLIST_BUTTON);
605 var blacklistFunction = generateBlacklistFunction(rid); 637 var blacklistFunction = generateBlacklistFunction(rid);
606 blacklistButton.addEventListener('click', blacklistFunction); 638 blacklistButton.addEventListener('click', blacklistFunction);
607 blacklistButton.title = configData.translatedStrings.removeThumbnailTooltip; 639 blacklistButton.title = configData.translatedStrings.removeThumbnailTooltip;
608 640
609 // When a tile is focused, have delete also blacklist the page. 641 // When a tile is focused, have delete also blacklist the page.
610 registerKeyHandler(tileElement, KEYCODE.DELETE, blacklistFunction); 642 registerKeyHandler(tileElement, KEYCODE.DELETE, blacklistFunction);
611 643
612 // The page favicon, if any. 644 // The page favicon, if any.
613 var faviconUrl = page.faviconUrl; 645 var faviconUrl = page.faviconUrl;
614 if (faviconUrl) { 646 if (faviconUrl) {
615 var favicon = createAndAppendElement( 647 var favicon = createAndAppendElement(tileElement, 'div', CLASSES.FAVICON);
616 tileElement, 'div', CLASSES.FAVICON);
617 favicon.style.backgroundImage = 'url(' + faviconUrl + ')'; 648 favicon.style.backgroundImage = 'url(' + faviconUrl + ')';
618 } 649 }
619 return new Tile(tileElement, rid); 650 return new Tile(tileElement, rid);
620 } else { 651 } else {
621 return new Tile(tileElement); 652 return new Tile(tileElement);
622 } 653 }
623 } 654 }
624 655
625 656
626 /** 657 /**
(...skipping 30 matching lines...) Expand all
657 688
658 /** 689 /**
659 * Hides the blacklist notification. 690 * Hides the blacklist notification.
660 */ 691 */
661 function hideNotification() { 692 function hideNotification() {
662 notification.classList.add(CLASSES.HIDE_NOTIFICATION); 693 notification.classList.add(CLASSES.HIDE_NOTIFICATION);
663 } 694 }
664 695
665 696
666 /** 697 /**
667 * Handles the end of the blacklist animation by showing the notification and
668 * re-rendering the new set of tiles.
669 */
670 function blacklistAnimationDone() {
671 showNotification();
672 isBlacklisting = false;
673 tilesContainer.classList.remove(CLASSES.HIDE_BLACKLIST_BUTTON);
674 lastBlacklistedTile.elem.removeEventListener(
675 'webkitTransitionEnd', blacklistAnimationDone);
676 // Need to call explicitly to re-render the tiles, since the initial
677 // onmostvisitedchange issued by the blacklist function only triggered
678 // the animation.
679 onMostVisitedChange();
680 }
681
682
683 /**
684 * Handles a click on the notification undo link by hiding the notification and 698 * Handles a click on the notification undo link by hiding the notification and
685 * informing Chrome. 699 * informing Chrome.
686 */ 700 */
687 function onUndo() { 701 function onUndo() {
688 userInitiatedMostVisitedChange = true; 702 userInitiatedMostVisitedChange = true;
689 hideNotification(); 703 hideNotification();
690 var lastBlacklistedRID = lastBlacklistedTile.rid; 704 var lastBlacklistedRID = lastBlacklistedTile.rid;
691 if (typeof lastBlacklistedRID != 'undefined') 705 if (typeof lastBlacklistedRID != 'undefined')
692 ntpApiHandle.undoMostVisitedDeletion(lastBlacklistedRID); 706 ntpApiHandle.undoMostVisitedDeletion(lastBlacklistedRID);
693 } 707 }
694 708
695 709
696 /** 710 /**
697 * Handles a click on the restore all notification link by hiding the 711 * Handles a click on the restore all notification link by hiding the
698 * notification and informing Chrome. 712 * notification and informing Chrome.
699 */ 713 */
700 function onRestoreAll() { 714 function onRestoreAll() {
701 userInitiatedMostVisitedChange = true; 715 userInitiatedMostVisitedChange = true;
702 hideNotification(); 716 hideNotification();
703 ntpApiHandle.undoAllMostVisitedDeletions(); 717 ntpApiHandle.undoAllMostVisitedDeletions();
704 } 718 }
705 719
706 720
707 /** 721 /**
708 * Re-renders the tiles if the number of columns has changed. As a temporary 722 * Resizes elements because the number of tile columns may need to change in
709 * fix for crbug/240510, updates the width of the fakebox and most visited tiles 723 * response to resizing. Also shows or hides extra tiles tiles according to the
710 * container. 724 * new width of the page.
711 */ 725 */
712 function onResize() { 726 function onResize() {
713 // If innerWidth is zero, then use the maximum snap size. 727 // If innerWidth is zero, then use the maximum snap size.
714 var innerWidth = window.innerWidth || 820; 728 var innerWidth = window.innerWidth || 820;
729 // Each tile has left and right margins that sum to TILE_MARGIN.
730 var tileRequiredWidth = TILE_WIDTH + TILE_MARGIN;
731 var availableWidth = innerWidth + TILE_MARGIN - MIN_TOTAL_HORIZONTAL_PADDING;
Jered 2014/07/30 17:52:14 I still don't understand this math. Imagine MIN_TO
huangs 2014/07/30 21:14:47 Yeah it would be bizarre if the extra margin creat
732 var newNumColumns = Math.floor(availableWidth / tileRequiredWidth);
733 if (newNumColumns < MIN_NUM_COLUMNS)
734 newNumColumns = MIN_NUM_COLUMNS;
735 else if (newNumColumns > MAX_NUM_COLUMNS)
736 newNumColumns = MAX_NUM_COLUMNS;
715 737
716 // These values should remain in sync with local_ntp.css. 738 if (numColumnsShown != newNumColumns) {
717 // TODO(jeremycho): Delete once the root cause of crbug/240510 is resolved. 739 numColumnsShown = newNumColumns;
718 var setWidths = function(tilesContainerWidth) { 740 var tilesContainerWidth = numColumnsShown * tileRequiredWidth;
719 tilesContainer.style.width = tilesContainerWidth + 'px'; 741 tilesContainer.style.width = tilesContainerWidth + 'px';
720 if (fakebox) 742 if (fakebox) // -2 to account for border.
721 fakebox.style.width = (tilesContainerWidth - 2) + 'px'; 743 fakebox.style.width = (tilesContainerWidth - TILE_MARGIN - 2) + 'px';
722 }; 744 renderTiles(false);
723 if (innerWidth >= 820)
724 setWidths(620);
725 else if (innerWidth >= 660)
726 setWidths(460);
727 else
728 setWidths(300);
729
730 var tileRequiredWidth = TILE_WIDTH + TILE_MARGIN_START;
731 // Adds margin-start to the available width to compensate the extra margin
732 // counted above for the first tile (which does not have a margin-start).
733 var availableWidth = innerWidth + TILE_MARGIN_START -
734 MIN_TOTAL_HORIZONTAL_PADDING;
735 var numColumnsToShow = Math.floor(availableWidth / tileRequiredWidth);
736 numColumnsToShow = Math.max(MIN_NUM_COLUMNS,
737 Math.min(MAX_NUM_COLUMNS, numColumnsToShow));
738 if (numColumnsToShow != numColumnsShown) {
739 numColumnsShown = numColumnsToShow;
740 renderTiles();
741 } 745 }
742 } 746 }
743 747
744 748
745 /** 749 /**
746 * Returns the tile corresponding to the specified page RID. 750 * Returns the tile corresponding to the specified page RID.
747 * @param {number} rid The page RID being looked up. 751 * @param {number} rid The page RID being looked up.
748 * @return {Tile} The corresponding tile. 752 * @return {Tile} The corresponding tile.
749 */ 753 */
750 function getTileByRid(rid) { 754 function getTileByRid(rid) {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 /** 877 /**
874 * Removes a node from its parent. 878 * Removes a node from its parent.
875 * @param {Node} node The node to remove. 879 * @param {Node} node The node to remove.
876 */ 880 */
877 function removeNode(node) { 881 function removeNode(node) {
878 node.parentNode.removeChild(node); 882 node.parentNode.removeChild(node);
879 } 883 }
880 884
881 885
882 /** 886 /**
883 * Removes all the child nodes on a DOM node.
884 * @param {Node} node Node to remove children from.
885 */
886 function removeChildren(node) {
887 node.innerHTML = '';
888 }
889
890
891 /**
892 * @param {!Element} element The element to register the handler for. 887 * @param {!Element} element The element to register the handler for.
893 * @param {number} keycode The keycode of the key to register. 888 * @param {number} keycode The keycode of the key to register.
894 * @param {!Function} handler The key handler to register. 889 * @param {!Function} handler The key handler to register.
895 */ 890 */
896 function registerKeyHandler(element, keycode, handler) { 891 function registerKeyHandler(element, keycode, handler) {
897 element.addEventListener('keydown', function(event) { 892 element.addEventListener('keydown', function(event) {
898 if (event.keyCode == keycode) 893 if (event.keyCode == keycode)
899 handler(event); 894 handler(event);
900 }); 895 });
901 } 896 }
(...skipping 15 matching lines...) Expand all
917 * Prepares the New Tab Page by adding listeners, rendering the current 912 * Prepares the New Tab Page by adding listeners, rendering the current
918 * theme, the most visited pages section, and Google-specific elements for a 913 * theme, the most visited pages section, and Google-specific elements for a
919 * Google-provided page. 914 * Google-provided page.
920 */ 915 */
921 function init() { 916 function init() {
922 tilesContainer = $(IDS.TILES); 917 tilesContainer = $(IDS.TILES);
923 notification = $(IDS.NOTIFICATION); 918 notification = $(IDS.NOTIFICATION);
924 attribution = $(IDS.ATTRIBUTION); 919 attribution = $(IDS.ATTRIBUTION);
925 ntpContents = $(IDS.NTP_CONTENTS); 920 ntpContents = $(IDS.NTP_CONTENTS);
926 921
927 for (var i = 0; i < NUM_ROWS; i++) {
928 var row = document.createElement('div');
929 row.classList.add(CLASSES.ROW);
930 tilesContainer.appendChild(row);
931 }
932
933 if (configData.isGooglePage) { 922 if (configData.isGooglePage) {
934 var logo = document.createElement('div'); 923 var logo = document.createElement('div');
935 logo.id = IDS.LOGO; 924 logo.id = IDS.LOGO;
936 925
937 fakebox = document.createElement('div'); 926 fakebox = document.createElement('div');
938 fakebox.id = IDS.FAKEBOX; 927 fakebox.id = IDS.FAKEBOX;
939 fakebox.innerHTML = 928 fakebox.innerHTML =
940 '<input id="' + IDS.FAKEBOX_INPUT + 929 '<input id="' + IDS.FAKEBOX_INPUT +
941 '" autocomplete="off" tabindex="-1" aria-hidden="true">' + 930 '" autocomplete="off" tabindex="-1" aria-hidden="true">' +
942 '<div id=cursor></div>'; 931 '<div id="cursor"></div>';
943 932
944 ntpContents.insertBefore(fakebox, ntpContents.firstChild); 933 ntpContents.insertBefore(fakebox, ntpContents.firstChild);
945 ntpContents.insertBefore(logo, ntpContents.firstChild); 934 ntpContents.insertBefore(logo, ntpContents.firstChild);
946 } else { 935 } else {
947 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE); 936 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE);
948 } 937 }
949 938
950 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE); 939 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE);
951 notificationMessage.textContent = 940 notificationMessage.textContent =
952 configData.translatedStrings.thumbnailRemovedNotification; 941 configData.translatedStrings.thumbnailRemovedNotification;
953 var undoLink = $(IDS.UNDO_LINK); 942 var undoLink = $(IDS.UNDO_LINK);
954 undoLink.addEventListener('click', onUndo); 943 undoLink.addEventListener('click', onUndo);
955 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo); 944 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo);
956 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove; 945 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove;
957 var restoreAllLink = $(IDS.RESTORE_ALL_LINK); 946 var restoreAllLink = $(IDS.RESTORE_ALL_LINK);
958 restoreAllLink.addEventListener('click', onRestoreAll); 947 restoreAllLink.addEventListener('click', onRestoreAll);
959 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onUndo); 948 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onUndo);
960 restoreAllLink.textContent = 949 restoreAllLink.textContent =
961 configData.translatedStrings.restoreThumbnailsShort; 950 configData.translatedStrings.restoreThumbnailsShort;
962 $(IDS.ATTRIBUTION_TEXT).textContent = 951 $(IDS.ATTRIBUTION_TEXT).textContent =
963 configData.translatedStrings.attributionIntro; 952 configData.translatedStrings.attributionIntro;
964 953
965 var notificationCloseButton = $(IDS.NOTIFICATION_CLOSE_BUTTON); 954 var notificationCloseButton = $(IDS.NOTIFICATION_CLOSE_BUTTON);
966 notificationCloseButton.addEventListener('click', hideNotification); 955 notificationCloseButton.addEventListener('click', hideNotification);
967 956
968 userInitiatedMostVisitedChange = false;
969 window.addEventListener('resize', onResize); 957 window.addEventListener('resize', onResize);
970 onResize(); 958 onResize();
971 959
972 var topLevelHandle = getEmbeddedSearchApiHandle(); 960 var topLevelHandle = getEmbeddedSearchApiHandle();
973 961
974 ntpApiHandle = topLevelHandle.newTabPage; 962 ntpApiHandle = topLevelHandle.newTabPage;
975 ntpApiHandle.onthemechange = onThemeChange; 963 ntpApiHandle.onthemechange = onThemeChange;
976 ntpApiHandle.onmostvisitedchange = onMostVisitedChange; 964 ntpApiHandle.onmostvisitedchange = onMostVisitedChange;
977 965
978 ntpApiHandle.oninputstart = onInputStart; 966 ntpApiHandle.oninputstart = onInputStart;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 1028
1041 return { 1029 return {
1042 init: init, 1030 init: init,
1043 listen: listen 1031 listen: listen
1044 }; 1032 };
1045 } 1033 }
1046 1034
1047 if (!window.localNTPUnitTest) { 1035 if (!window.localNTPUnitTest) {
1048 LocalNTP().listen(); 1036 LocalNTP().listen();
1049 } 1037 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/local_ntp/local_ntp.css ('k') | chrome/browser/resources/local_ntp/local_ntp_util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698