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

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; improving renderTiles(). 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 titleContainer invisible, but still taking up space. We make
Mathieu 2014/07/29 21:36:08 *Temporarily mark titleContainer as invisible, but
huangs 2014/07/30 17:30:39 Done.
489 ready = false; 482 // it visible again (1) on timeout, or (2) when all tiles finish loading
490 break; 483 // (using tileVisibilityBarrier).
491 } 484 tilesContainer.style.visibility = 'hidden';
485 window.setTimeout(function() {
486 if (tilesContainer) {
487 tilesContainer.style.visibility = 'visible';
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
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) {
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 // that sends bogus impression stats. However, if a tile becomes invisible
Mathieu 2014/07/29 21:36:08 *that create meaningless metrics.
huangs 2014/07/30 17:30:39 Done.
515 // 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;
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 for (; i < numExisting; ++i)
Mathieu 2014/07/29 21:36:08 Add a comment above this: "If |numDesired| is smal
huangs 2014/07/30 17:30:39 Done, with rephrasing.
526 tiles[i].elem.style.display = 'none';
527 }
528
529
500 /** 530 /**
501 * Builds a URL to display a most visited tile component in an iframe. 531 * Builds a URL to display a most visited tile component in an iframe.
502 * @param {string} filename The desired most visited component filename. 532 * @param {string} filename The desired most visited component filename.
503 * @param {number} rid The restricted ID. 533 * @param {number} rid The restricted ID.
504 * @param {string} color The text color for text in the iframe. 534 * @param {string} color The text color for text in the iframe.
505 * @param {string} fontFamily The font family for text in the iframe. 535 * @param {string} fontFamily The font family for text in the iframe.
506 * @param {number} fontSize The font size for text in the iframe. 536 * @param {number} fontSize The font size for text in the iframe.
507 * @param {number} position The position of the iframe in the UI. 537 * @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. 538 * @return {string} An URL to display the most visited component in an iframe.
509 */ 539 */
(...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 586 // 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. 587 // 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 588 // 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. 589 // first set of iframes into the most recent set of iframes.
560 // 590 //
561 // Giving iframes distinct ids seems to cause some invalidation and prevent 591 // Giving iframes distinct ids seems to cause some invalidation and prevent
562 // associating the incorrect data. 592 // associating the incorrect data.
563 // 593 //
564 // TODO(jered): Find and fix the root (probably Blink) bug. 594 // TODO(jered): Find and fix the root (probably Blink) bug.
565 595
596 // Keep this ID here. See comment above.
597 titleElement.id = 'title-' + rid;
598 titleElement.className = CLASSES.TITLE;
599 titleElement.hidden = true;
600 tileVisibilityBarrier.add();
601 titleElement.onload = function() {
602 titleElement.hidden = false;
603 tileVisibilityBarrier.remove();
604 };
566 titleElement.src = getMostVisitedIframeUrl( 605 titleElement.src = getMostVisitedIframeUrl(
567 MOST_VISITED_TITLE_IFRAME, rid, MOST_VISITED_COLOR, 606 MOST_VISITED_TITLE_IFRAME, rid, MOST_VISITED_COLOR,
568 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position); 607 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); 608 tileElement.appendChild(titleElement);
579 609
580 // The iframe which renders either a thumbnail or domain element. 610 // The iframe which renders either a thumbnail or domain element.
581 var thumbnailElement = document.createElement('iframe'); 611 var thumbnailElement = document.createElement('iframe');
582 thumbnailElement.tabIndex = '-1'; 612 thumbnailElement.tabIndex = '-1';
613 // Keep this ID here. See comment above.
614 thumbnailElement.id = 'thumb-' + rid;
615 thumbnailElement.className = CLASSES.THUMBNAIL;
616 thumbnailElement.hidden = true;
617 tileVisibilityBarrier.add();
618 thumbnailElement.onload = function() {
619 thumbnailElement.hidden = false;
620 tileElement.classList.add(CLASSES.PAGE_READY);
621 tileVisibilityBarrier.remove();
622 };
583 thumbnailElement.src = getMostVisitedIframeUrl( 623 thumbnailElement.src = getMostVisitedIframeUrl(
584 MOST_VISITED_THUMBNAIL_IFRAME, rid, MOST_VISITED_COLOR, 624 MOST_VISITED_THUMBNAIL_IFRAME, rid, MOST_VISITED_COLOR,
585 MOST_VISITED_FONT_FAMILY, MOST_VISITED_FONT_SIZE, position); 625 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); 626 tileElement.appendChild(thumbnailElement);
597 627
598 // A mask to darken the thumbnail on focus. 628 // A mask to darken the thumbnail on focus.
599 var maskElement = createAndAppendElement( 629 var maskElement = createAndAppendElement(
600 tileElement, 'div', CLASSES.THUMBNAIL_MASK); 630 tileElement, 'div', CLASSES.THUMBNAIL_MASK);
601 631
602 // The button used to blacklist this page. 632 // The button used to blacklist this page.
603 var blacklistButton = createAndAppendElement( 633 var blacklistButton = createAndAppendElement(
604 tileElement, 'div', CLASSES.BLACKLIST_BUTTON); 634 tileElement, 'div', CLASSES.BLACKLIST_BUTTON);
605 var blacklistFunction = generateBlacklistFunction(rid); 635 var blacklistFunction = generateBlacklistFunction(rid);
606 blacklistButton.addEventListener('click', blacklistFunction); 636 blacklistButton.addEventListener('click', blacklistFunction);
607 blacklistButton.title = configData.translatedStrings.removeThumbnailTooltip; 637 blacklistButton.title = configData.translatedStrings.removeThumbnailTooltip;
608 638
609 // When a tile is focused, have delete also blacklist the page. 639 // When a tile is focused, have delete also blacklist the page.
610 registerKeyHandler(tileElement, KEYCODE.DELETE, blacklistFunction); 640 registerKeyHandler(tileElement, KEYCODE.DELETE, blacklistFunction);
611 641
612 // The page favicon, if any. 642 // The page favicon, if any.
613 var faviconUrl = page.faviconUrl; 643 var faviconUrl = page.faviconUrl;
614 if (faviconUrl) { 644 if (faviconUrl) {
615 var favicon = createAndAppendElement( 645 var favicon = createAndAppendElement(tileElement, 'div', CLASSES.FAVICON);
616 tileElement, 'div', CLASSES.FAVICON);
617 favicon.style.backgroundImage = 'url(' + faviconUrl + ')'; 646 favicon.style.backgroundImage = 'url(' + faviconUrl + ')';
618 } 647 }
619 return new Tile(tileElement, rid); 648 return new Tile(tileElement, rid);
620 } else { 649 } else {
621 return new Tile(tileElement); 650 return new Tile(tileElement);
622 } 651 }
623 } 652 }
624 653
625 654
626 /** 655 /**
(...skipping 30 matching lines...) Expand all
657 686
658 /** 687 /**
659 * Hides the blacklist notification. 688 * Hides the blacklist notification.
660 */ 689 */
661 function hideNotification() { 690 function hideNotification() {
662 notification.classList.add(CLASSES.HIDE_NOTIFICATION); 691 notification.classList.add(CLASSES.HIDE_NOTIFICATION);
663 } 692 }
664 693
665 694
666 /** 695 /**
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 696 * Handles a click on the notification undo link by hiding the notification and
685 * informing Chrome. 697 * informing Chrome.
686 */ 698 */
687 function onUndo() { 699 function onUndo() {
688 userInitiatedMostVisitedChange = true; 700 userInitiatedMostVisitedChange = true;
689 hideNotification(); 701 hideNotification();
690 var lastBlacklistedRID = lastBlacklistedTile.rid; 702 var lastBlacklistedRID = lastBlacklistedTile.rid;
691 if (typeof lastBlacklistedRID != 'undefined') 703 if (typeof lastBlacklistedRID != 'undefined')
692 ntpApiHandle.undoMostVisitedDeletion(lastBlacklistedRID); 704 ntpApiHandle.undoMostVisitedDeletion(lastBlacklistedRID);
693 } 705 }
694 706
695 707
696 /** 708 /**
697 * Handles a click on the restore all notification link by hiding the 709 * Handles a click on the restore all notification link by hiding the
698 * notification and informing Chrome. 710 * notification and informing Chrome.
699 */ 711 */
700 function onRestoreAll() { 712 function onRestoreAll() {
701 userInitiatedMostVisitedChange = true; 713 userInitiatedMostVisitedChange = true;
702 hideNotification(); 714 hideNotification();
703 ntpApiHandle.undoAllMostVisitedDeletions(); 715 ntpApiHandle.undoAllMostVisitedDeletions();
704 } 716 }
705 717
706 718
707 /** 719 /**
708 * Re-renders the tiles if the number of columns has changed. As a temporary 720 * 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 721 * response to resizing. Also shows or hides extra tiles tiles according to the
710 * container. 722 * new width of the page.
711 */ 723 */
712 function onResize() { 724 function onResize() {
713 // If innerWidth is zero, then use the maximum snap size. 725 // If innerWidth is zero, then use the maximum snap size.
714 var innerWidth = window.innerWidth || 820; 726 var innerWidth = window.innerWidth || 820;
715 727 // Each tile has left and right margins that sum to TILE_MARGIN.
716 // These values should remain in sync with local_ntp.css. 728 var tileRequiredWidth = TILE_WIDTH + TILE_MARGIN;
717 // TODO(jeremycho): Delete once the root cause of crbug/240510 is resolved. 729 var availableWidth = innerWidth + TILE_MARGIN - MIN_TOTAL_HORIZONTAL_PADDING;
718 var setWidths = function(tilesContainerWidth) { 730 var newNumColumns = Math.floor(availableWidth / tileRequiredWidth);
731 if (newNumColumns < MIN_NUM_COLUMNS)
732 newNumColumns = MIN_NUM_COLUMNS;
733 else if (newNumColumns > MAX_NUM_COLUMNS)
734 newNumColumns = MAX_NUM_COLUMNS;
Mathieu 2014/07/29 21:36:08 nit: newline below this for readability
huangs 2014/07/30 17:30:39 Done.
735 if (numColumnsShown != newNumColumns) {
736 numColumnsShown = newNumColumns;
737 var tilesContainerWidth = numColumnsShown * tileRequiredWidth;
719 tilesContainer.style.width = tilesContainerWidth + 'px'; 738 tilesContainer.style.width = tilesContainerWidth + 'px';
720 if (fakebox) 739 if (fakebox) // -2 to account for border.
721 fakebox.style.width = (tilesContainerWidth - 2) + 'px'; 740 fakebox.style.width = (tilesContainerWidth - TILE_MARGIN - 2) + 'px';
722 }; 741 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 } 742 }
742 } 743 }
743 744
744 745
745 /** 746 /**
746 * Returns the tile corresponding to the specified page RID. 747 * Returns the tile corresponding to the specified page RID.
747 * @param {number} rid The page RID being looked up. 748 * @param {number} rid The page RID being looked up.
748 * @return {Tile} The corresponding tile. 749 * @return {Tile} The corresponding tile.
749 */ 750 */
750 function getTileByRid(rid) { 751 function getTileByRid(rid) {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 /** 874 /**
874 * Removes a node from its parent. 875 * Removes a node from its parent.
875 * @param {Node} node The node to remove. 876 * @param {Node} node The node to remove.
876 */ 877 */
877 function removeNode(node) { 878 function removeNode(node) {
878 node.parentNode.removeChild(node); 879 node.parentNode.removeChild(node);
879 } 880 }
880 881
881 882
882 /** 883 /**
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. 884 * @param {!Element} element The element to register the handler for.
893 * @param {number} keycode The keycode of the key to register. 885 * @param {number} keycode The keycode of the key to register.
894 * @param {!Function} handler The key handler to register. 886 * @param {!Function} handler The key handler to register.
895 */ 887 */
896 function registerKeyHandler(element, keycode, handler) { 888 function registerKeyHandler(element, keycode, handler) {
897 element.addEventListener('keydown', function(event) { 889 element.addEventListener('keydown', function(event) {
898 if (event.keyCode == keycode) 890 if (event.keyCode == keycode)
899 handler(event); 891 handler(event);
900 }); 892 });
901 } 893 }
(...skipping 15 matching lines...) Expand all
917 * Prepares the New Tab Page by adding listeners, rendering the current 909 * Prepares the New Tab Page by adding listeners, rendering the current
918 * theme, the most visited pages section, and Google-specific elements for a 910 * theme, the most visited pages section, and Google-specific elements for a
919 * Google-provided page. 911 * Google-provided page.
920 */ 912 */
921 function init() { 913 function init() {
922 tilesContainer = $(IDS.TILES); 914 tilesContainer = $(IDS.TILES);
923 notification = $(IDS.NOTIFICATION); 915 notification = $(IDS.NOTIFICATION);
924 attribution = $(IDS.ATTRIBUTION); 916 attribution = $(IDS.ATTRIBUTION);
925 ntpContents = $(IDS.NTP_CONTENTS); 917 ntpContents = $(IDS.NTP_CONTENTS);
926 918
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) { 919 if (configData.isGooglePage) {
934 var logo = document.createElement('div'); 920 var logo = document.createElement('div');
935 logo.id = IDS.LOGO; 921 logo.id = IDS.LOGO;
936 922
937 fakebox = document.createElement('div'); 923 fakebox = document.createElement('div');
938 fakebox.id = IDS.FAKEBOX; 924 fakebox.id = IDS.FAKEBOX;
939 fakebox.innerHTML = 925 fakebox.innerHTML =
940 '<input id="' + IDS.FAKEBOX_INPUT + 926 '<input id="' + IDS.FAKEBOX_INPUT +
941 '" autocomplete="off" tabindex="-1" aria-hidden="true">' + 927 '" autocomplete="off" tabindex="-1" aria-hidden="true">' +
942 '<div id=cursor></div>'; 928 '<div id="cursor"></div>';
943 929
944 ntpContents.insertBefore(fakebox, ntpContents.firstChild); 930 ntpContents.insertBefore(fakebox, ntpContents.firstChild);
945 ntpContents.insertBefore(logo, ntpContents.firstChild); 931 ntpContents.insertBefore(logo, ntpContents.firstChild);
946 } else { 932 } else {
947 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE); 933 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE);
948 } 934 }
949 935
950 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE); 936 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE);
951 notificationMessage.textContent = 937 notificationMessage.textContent =
952 configData.translatedStrings.thumbnailRemovedNotification; 938 configData.translatedStrings.thumbnailRemovedNotification;
953 var undoLink = $(IDS.UNDO_LINK); 939 var undoLink = $(IDS.UNDO_LINK);
954 undoLink.addEventListener('click', onUndo); 940 undoLink.addEventListener('click', onUndo);
955 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo); 941 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo);
956 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove; 942 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove;
957 var restoreAllLink = $(IDS.RESTORE_ALL_LINK); 943 var restoreAllLink = $(IDS.RESTORE_ALL_LINK);
958 restoreAllLink.addEventListener('click', onRestoreAll); 944 restoreAllLink.addEventListener('click', onRestoreAll);
959 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onUndo); 945 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onUndo);
960 restoreAllLink.textContent = 946 restoreAllLink.textContent =
961 configData.translatedStrings.restoreThumbnailsShort; 947 configData.translatedStrings.restoreThumbnailsShort;
962 $(IDS.ATTRIBUTION_TEXT).textContent = 948 $(IDS.ATTRIBUTION_TEXT).textContent =
963 configData.translatedStrings.attributionIntro; 949 configData.translatedStrings.attributionIntro;
964 950
965 var notificationCloseButton = $(IDS.NOTIFICATION_CLOSE_BUTTON); 951 var notificationCloseButton = $(IDS.NOTIFICATION_CLOSE_BUTTON);
966 notificationCloseButton.addEventListener('click', hideNotification); 952 notificationCloseButton.addEventListener('click', hideNotification);
967 953
968 userInitiatedMostVisitedChange = false;
969 window.addEventListener('resize', onResize); 954 window.addEventListener('resize', onResize);
970 onResize(); 955 onResize();
971 956
972 var topLevelHandle = getEmbeddedSearchApiHandle(); 957 var topLevelHandle = getEmbeddedSearchApiHandle();
973 958
974 ntpApiHandle = topLevelHandle.newTabPage; 959 ntpApiHandle = topLevelHandle.newTabPage;
975 ntpApiHandle.onthemechange = onThemeChange; 960 ntpApiHandle.onthemechange = onThemeChange;
976 ntpApiHandle.onmostvisitedchange = onMostVisitedChange; 961 ntpApiHandle.onmostvisitedchange = onMostVisitedChange;
977 962
978 ntpApiHandle.oninputstart = onInputStart; 963 ntpApiHandle.oninputstart = onInputStart;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 1025
1041 return { 1026 return {
1042 init: init, 1027 init: init,
1043 listen: listen 1028 listen: listen
1044 }; 1029 };
1045 } 1030 }
1046 1031
1047 if (!window.localNTPUnitTest) { 1032 if (!window.localNTPUnitTest) {
1048 LocalNTP().listen(); 1033 LocalNTP().listen();
1049 } 1034 }
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