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

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: Adding local_ntp_utils.js and moving Barrier there. Created 6 years, 5 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, to avoid flickering.
James Hawkins 2014/07/28 17:43:35 Optional nit: I believe the comma on this line is
huangs 2014/07/29 19:43:45 Done, with slight rephrasing.
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);
341
332 renderTiles(); 342 renderTiles();
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 */
(...skipping 83 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 hide titleContainer, then show it (1) on timeout, or (2) when
489 ready = false; 482 // all tiles finish loading (using tileVisibilityBarrier).
490 break; 483 tilesContainer.style.visibility = 'hidden';
James Hawkins 2014/07/28 17:43:35 Why are you not using tilesContainer.hidden?
huangs 2014/07/29 19:43:45 visibility='hidden' makes #mv-tiles disappear but
491 } 484 window.setTimeout(function() {
485 if (tilesContainer) {
486 tilesContainer.style.visibility = 'visible';
487 }
488 }, MOST_VISITED_PAINT_TIMEOUT_MSEC);
492 } 489 }
493 if (ready) { 490 userInitiatedMostVisitedChange = false;
494 tilesContainer.hidden = false; 491
495 userInitiatedMostVisitedChange = false; 492 tiles = [];
493 // Increment barrier to guard against race conditions.
Jered 2014/07/28 18:32:50 What is the race here? I think this add() and its
huangs 2014/07/29 19:43:45 Indeed this is not needed. I'm being pedantic w.r
494 tileVisibilityBarrier.add();
495 for (var i = 0; i < MAX_NUM_TILES_TO_SHOW; ++i) {
496 tiles.push(createTile(pages[i], i));
497 }
498 renderTiles();
499 tileVisibilityBarrier.remove();
500 }
501
502
503 /**
504 * Adds the current list of tiles to DOM.
505 */
506 function renderTiles() {
507 removeChildren(tilesContainer);
Jered 2014/07/28 18:32:50 nit: remove this function and just inline tilesCon
huangs 2014/07/29 19:43:45 Done.
508 var renderedList = tilesContainer.querySelectorAll(CLASSES.TILE);
509 var size = Math.min(tiles.length, numColumnsShown * NUM_ROWS);
Jered 2014/07/28 18:32:51 Define a function to compute this expression and r
huangs 2014/07/29 19:43:45 The other usage was in onResize(). My previous co
510 for (var i = 0; i < size; ++i) {
Jered 2014/07/28 18:32:50 nit: omit braces
huangs 2014/07/29 19:43:45 Done.
511 tilesContainer.appendChild(tiles[i].elem);
496 } 512 }
497 } 513 }
498 514
499 515
500 /** 516 /**
501 * Builds a URL to display a most visited tile component in an iframe. 517 * Builds a URL to display a most visited tile component in an iframe.
502 * @param {string} filename The desired most visited component filename. 518 * @param {string} filename The desired most visited component filename.
503 * @param {number} rid The restricted ID. 519 * @param {number} rid The restricted ID.
504 * @param {string} color The text color for text in the iframe. 520 * @param {string} color The text color for text in the iframe.
505 * @param {string} fontFamily The font family for text in the iframe. 521 * @param {string} fontFamily The font family for text in the iframe.
(...skipping 50 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 572 // 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. 573 // 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 574 // 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. 575 // first set of iframes into the most recent set of iframes.
560 // 576 //
561 // Giving iframes distinct ids seems to cause some invalidation and prevent 577 // Giving iframes distinct ids seems to cause some invalidation and prevent
562 // associating the incorrect data. 578 // associating the incorrect data.
563 // 579 //
564 // TODO(jered): Find and fix the root (probably Blink) bug. 580 // TODO(jered): Find and fix the root (probably Blink) bug.
565 581
582 // Keep this id here. See comment above.
James Hawkins 2014/07/28 17:43:34 s/id/ID/
huangs 2014/07/29 19:43:45 Done.
583 titleElement.id = 'title-' + rid;
584 titleElement.className = CLASSES.TITLE;
585 titleElement.hidden = true;
586 tileVisibilityBarrier.add();
587 titleElement.onload = function() {
588 titleElement.hidden = false;
589 tileVisibilityBarrier.remove();
590 };
566 titleElement.src = getMostVisitedIframeUrl( 591 titleElement.src = getMostVisitedIframeUrl(
567 MOST_VISITED_TITLE_IFRAME, rid, MOST_VISITED_COLOR, 592 MOST_VISITED_TITLE_IFRAME, rid, MOST_VISITED_COLOR,
568 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position); 593 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); 594 tileElement.appendChild(titleElement);
579 595
580 // The iframe which renders either a thumbnail or domain element. 596 // The iframe which renders either a thumbnail or domain element.
581 var thumbnailElement = document.createElement('iframe'); 597 var thumbnailElement = document.createElement('iframe');
582 thumbnailElement.tabIndex = '-1'; 598 thumbnailElement.tabIndex = '-1';
599 // Keep this id here. See comment above.
James Hawkins 2014/07/28 17:43:34 s/id/ID/
huangs 2014/07/29 19:43:45 Done.
600 thumbnailElement.id = 'thumb-' + rid;
601 thumbnailElement.className = CLASSES.THUMBNAIL;
602 thumbnailElement.hidden = true;
603 tileVisibilityBarrier.add();
604 thumbnailElement.onload = function() {
605 thumbnailElement.hidden = false;
606 tileElement.classList.add(CLASSES.PAGE_READY);
607 tileVisibilityBarrier.remove();
608 };
583 thumbnailElement.src = getMostVisitedIframeUrl( 609 thumbnailElement.src = getMostVisitedIframeUrl(
584 MOST_VISITED_THUMBNAIL_IFRAME, rid, MOST_VISITED_COLOR, 610 MOST_VISITED_THUMBNAIL_IFRAME, rid, MOST_VISITED_COLOR,
585 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position); 611 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); 612 tileElement.appendChild(thumbnailElement);
597 613
598 // A mask to darken the thumbnail on focus. 614 // A mask to darken the thumbnail on focus.
599 var maskElement = createAndAppendElement( 615 var maskElement = createAndAppendElement(
600 tileElement, 'div', CLASSES.THUMBNAIL_MASK); 616 tileElement, 'div', CLASSES.THUMBNAIL_MASK);
601 617
602 // The button used to blacklist this page. 618 // The button used to blacklist this page.
603 var blacklistButton = createAndAppendElement( 619 var blacklistButton = createAndAppendElement(
604 tileElement, 'div', CLASSES.BLACKLIST_BUTTON); 620 tileElement, 'div', CLASSES.BLACKLIST_BUTTON);
605 var blacklistFunction = generateBlacklistFunction(rid); 621 var blacklistFunction = generateBlacklistFunction(rid);
606 blacklistButton.addEventListener('click', blacklistFunction); 622 blacklistButton.addEventListener('click', blacklistFunction);
607 blacklistButton.title = configData.translatedStrings.removeThumbnailTooltip; 623 blacklistButton.title = configData.translatedStrings.removeThumbnailTooltip;
608 624
609 // When a tile is focused, have delete also blacklist the page. 625 // When a tile is focused, have delete also blacklist the page.
610 registerKeyHandler(tileElement, KEYCODE.DELETE, blacklistFunction); 626 registerKeyHandler(tileElement, KEYCODE.DELETE, blacklistFunction);
611 627
612 // The page favicon, if any. 628 // The page favicon, if any.
613 var faviconUrl = page.faviconUrl; 629 var faviconUrl = page.faviconUrl;
614 if (faviconUrl) { 630 if (faviconUrl) {
615 var favicon = createAndAppendElement( 631 var favicon = createAndAppendElement(tileElement, 'div', CLASSES.FAVICON);
616 tileElement, 'div', CLASSES.FAVICON);
617 favicon.style.backgroundImage = 'url(' + faviconUrl + ')'; 632 favicon.style.backgroundImage = 'url(' + faviconUrl + ')';
618 } 633 }
619 return new Tile(tileElement, rid); 634 return new Tile(tileElement, rid);
620 } else { 635 } else {
621 return new Tile(tileElement); 636 return new Tile(tileElement);
622 } 637 }
623 } 638 }
624 639
625 640
626 /** 641 /**
(...skipping 30 matching lines...) Expand all
657 672
658 /** 673 /**
659 * Hides the blacklist notification. 674 * Hides the blacklist notification.
660 */ 675 */
661 function hideNotification() { 676 function hideNotification() {
662 notification.classList.add(CLASSES.HIDE_NOTIFICATION); 677 notification.classList.add(CLASSES.HIDE_NOTIFICATION);
663 } 678 }
664 679
665 680
666 /** 681 /**
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 682 * Handles a click on the notification undo link by hiding the notification and
685 * informing Chrome. 683 * informing Chrome.
686 */ 684 */
687 function onUndo() { 685 function onUndo() {
688 userInitiatedMostVisitedChange = true; 686 userInitiatedMostVisitedChange = true;
689 hideNotification(); 687 hideNotification();
690 var lastBlacklistedRID = lastBlacklistedTile.rid; 688 var lastBlacklistedRID = lastBlacklistedTile.rid;
691 if (typeof lastBlacklistedRID != 'undefined') 689 if (typeof lastBlacklistedRID != 'undefined')
692 ntpApiHandle.undoMostVisitedDeletion(lastBlacklistedRID); 690 ntpApiHandle.undoMostVisitedDeletion(lastBlacklistedRID);
693 } 691 }
694 692
695 693
696 /** 694 /**
697 * Handles a click on the restore all notification link by hiding the 695 * Handles a click on the restore all notification link by hiding the
698 * notification and informing Chrome. 696 * notification and informing Chrome.
699 */ 697 */
700 function onRestoreAll() { 698 function onRestoreAll() {
701 userInitiatedMostVisitedChange = true; 699 userInitiatedMostVisitedChange = true;
702 hideNotification(); 700 hideNotification();
703 ntpApiHandle.undoAllMostVisitedDeletions(); 701 ntpApiHandle.undoAllMostVisitedDeletions();
704 } 702 }
705 703
706 704
707 /** 705 /**
708 * Re-renders the tiles if the number of columns has changed. As a temporary 706 * 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 707 * response to resizing. Also shows or hides extra tiles tiles according to the
710 * container. 708 * new width of the page.
711 */ 709 */
712 function onResize() { 710 function onResize() {
713 // If innerWidth is zero, then use the maximum snap size. 711 // If innerWidth is zero, then use the maximum snap size.
714 var innerWidth = window.innerWidth || 820; 712 var innerWidth = window.innerWidth || 820;
715 713 // Each tile has left and right margins that sum to TILE_MARGIN.
716 // These values should remain in sync with local_ntp.css. 714 var tileRequiredWidth = TILE_WIDTH + TILE_MARGIN;
717 // TODO(jeremycho): Delete once the root cause of crbug/240510 is resolved. 715 var availableWidth = innerWidth + TILE_MARGIN - MIN_TOTAL_HORIZONTAL_PADDING;
Jered 2014/07/28 18:32:51 Why do we have an extra TILE_MARGIN px available?
huangs 2014/07/29 19:43:45 We want to add a bit more width to account for ext
718 var setWidths = function(tilesContainerWidth) { 716 var newNumColumns = Math.floor(availableWidth / tileRequiredWidth);
717 newNumColumns =
Jered 2014/07/28 18:32:50 nit: I think this expression would be clearer if w
huangs 2014/07/29 19:43:45 Done, using "else if" for the ">" check.
718 Math.max(MIN_NUM_COLUMNS, Math.min(newNumColumns, MAX_NUM_COLUMNS));
719 if (numColumnsShown != newNumColumns) {
720 numColumnsShown = newNumColumns;
721 var tilesContainerWidth = numColumnsShown * tileRequiredWidth;
719 tilesContainer.style.width = tilesContainerWidth + 'px'; 722 tilesContainer.style.width = tilesContainerWidth + 'px';
720 if (fakebox) 723 if (fakebox) // -2 to account for border.
721 fakebox.style.width = (tilesContainerWidth - 2) + 'px'; 724 fakebox.style.width = (tilesContainerWidth - TILE_MARGIN - 2) + 'px';
722 }; 725 // Shows only rendered tiles in the first NUM_ROWS rows.
723 if (innerWidth >= 820) 726 var renderedList = tilesContainer.querySelectorAll(CLASSES.TILE);
724 setWidths(620); 727 var numVisible = Math.min(tiles.length, numColumnsShown * NUM_ROWS);
725 else if (innerWidth >= 660) 728 // Not using .hidden because it does not work for inline-block elements.
726 setWidths(460); 729 for (var i = 0; i < renderedList.length; ++i) {
James Hawkins 2014/07/28 17:43:35 nit: No braces for single line blocks.
huangs 2014/07/29 19:43:45 Done, though functionality moved to renderTiles().
727 else 730 renderedList[i].style.display = i < numVisible ? 'inline-block' : 'none';
728 setWidths(300); 731 }
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 } 732 }
742 } 733 }
743 734
744 735
745 /** 736 /**
746 * Returns the tile corresponding to the specified page RID. 737 * Returns the tile corresponding to the specified page RID.
747 * @param {number} rid The page RID being looked up. 738 * @param {number} rid The page RID being looked up.
748 * @return {Tile} The corresponding tile. 739 * @return {Tile} The corresponding tile.
749 */ 740 */
750 function getTileByRid(rid) { 741 function getTileByRid(rid) {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
917 * Prepares the New Tab Page by adding listeners, rendering the current 908 * Prepares the New Tab Page by adding listeners, rendering the current
918 * theme, the most visited pages section, and Google-specific elements for a 909 * theme, the most visited pages section, and Google-specific elements for a
919 * Google-provided page. 910 * Google-provided page.
920 */ 911 */
921 function init() { 912 function init() {
922 tilesContainer = $(IDS.TILES); 913 tilesContainer = $(IDS.TILES);
923 notification = $(IDS.NOTIFICATION); 914 notification = $(IDS.NOTIFICATION);
924 attribution = $(IDS.ATTRIBUTION); 915 attribution = $(IDS.ATTRIBUTION);
925 ntpContents = $(IDS.NTP_CONTENTS); 916 ntpContents = $(IDS.NTP_CONTENTS);
926 917
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) { 918 if (configData.isGooglePage) {
934 var logo = document.createElement('div'); 919 var logo = document.createElement('div');
935 logo.id = IDS.LOGO; 920 logo.id = IDS.LOGO;
936 921
937 fakebox = document.createElement('div'); 922 fakebox = document.createElement('div');
938 fakebox.id = IDS.FAKEBOX; 923 fakebox.id = IDS.FAKEBOX;
939 fakebox.innerHTML = 924 fakebox.innerHTML =
940 '<input id="' + IDS.FAKEBOX_INPUT + 925 '<input id="' + IDS.FAKEBOX_INPUT +
941 '" autocomplete="off" tabindex="-1" aria-hidden="true">' + 926 '" autocomplete="off" tabindex="-1" aria-hidden="true">' +
942 '<div id=cursor></div>'; 927 '<div id="cursor"></div>';
943 928
944 ntpContents.insertBefore(fakebox, ntpContents.firstChild); 929 ntpContents.insertBefore(fakebox, ntpContents.firstChild);
945 ntpContents.insertBefore(logo, ntpContents.firstChild); 930 ntpContents.insertBefore(logo, ntpContents.firstChild);
946 } else { 931 } else {
947 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE); 932 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE);
948 } 933 }
949 934
950 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE); 935 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE);
951 notificationMessage.textContent = 936 notificationMessage.textContent =
952 configData.translatedStrings.thumbnailRemovedNotification; 937 configData.translatedStrings.thumbnailRemovedNotification;
953 var undoLink = $(IDS.UNDO_LINK); 938 var undoLink = $(IDS.UNDO_LINK);
954 undoLink.addEventListener('click', onUndo); 939 undoLink.addEventListener('click', onUndo);
955 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo); 940 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo);
956 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove; 941 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove;
957 var restoreAllLink = $(IDS.RESTORE_ALL_LINK); 942 var restoreAllLink = $(IDS.RESTORE_ALL_LINK);
958 restoreAllLink.addEventListener('click', onRestoreAll); 943 restoreAllLink.addEventListener('click', onRestoreAll);
959 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onUndo); 944 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onUndo);
960 restoreAllLink.textContent = 945 restoreAllLink.textContent =
961 configData.translatedStrings.restoreThumbnailsShort; 946 configData.translatedStrings.restoreThumbnailsShort;
962 $(IDS.ATTRIBUTION_TEXT).textContent = 947 $(IDS.ATTRIBUTION_TEXT).textContent =
963 configData.translatedStrings.attributionIntro; 948 configData.translatedStrings.attributionIntro;
964 949
965 var notificationCloseButton = $(IDS.NOTIFICATION_CLOSE_BUTTON); 950 var notificationCloseButton = $(IDS.NOTIFICATION_CLOSE_BUTTON);
966 notificationCloseButton.addEventListener('click', hideNotification); 951 notificationCloseButton.addEventListener('click', hideNotification);
967 952
968 userInitiatedMostVisitedChange = false;
969 window.addEventListener('resize', onResize); 953 window.addEventListener('resize', onResize);
970 onResize(); 954 onResize();
971 955
972 var topLevelHandle = getEmbeddedSearchApiHandle(); 956 var topLevelHandle = getEmbeddedSearchApiHandle();
973 957
974 ntpApiHandle = topLevelHandle.newTabPage; 958 ntpApiHandle = topLevelHandle.newTabPage;
975 ntpApiHandle.onthemechange = onThemeChange; 959 ntpApiHandle.onthemechange = onThemeChange;
976 ntpApiHandle.onmostvisitedchange = onMostVisitedChange; 960 ntpApiHandle.onmostvisitedchange = onMostVisitedChange;
977 961
978 ntpApiHandle.oninputstart = onInputStart; 962 ntpApiHandle.oninputstart = onInputStart;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 1024
1041 return { 1025 return {
1042 init: init, 1026 init: init,
1043 listen: listen 1027 listen: listen
1044 }; 1028 };
1045 } 1029 }
1046 1030
1047 if (!window.localNTPUnitTest) { 1031 if (!window.localNTPUnitTest) {
1048 LocalNTP().listen(); 1032 LocalNTP().listen();
1049 } 1033 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698