OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // File Description: | 5 // File Description: |
6 // Contains all the necessary functions for rendering the NTP on mobile | 6 // Contains all the necessary functions for rendering the NTP on mobile |
7 // devices. | 7 // devices. |
8 | 8 |
9 /** | 9 /** |
10 * The event type used to determine when a touch starts. | 10 * The event type used to determine when a touch starts. |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 var syncState = SyncState.INITIAL; | 318 var syncState = SyncState.INITIAL; |
319 | 319 |
320 /** | 320 /** |
321 * Whether or not sync is enabled. It will be undefined until | 321 * Whether or not sync is enabled. It will be undefined until |
322 * setSyncEnabled() is called. | 322 * setSyncEnabled() is called. |
323 * @type {?boolean} | 323 * @type {?boolean} |
324 */ | 324 */ |
325 var syncEnabled = undefined; | 325 var syncEnabled = undefined; |
326 | 326 |
327 /** | 327 /** |
| 328 * The current most visited data being displayed. |
| 329 * @type {Array.<Object>} |
| 330 */ |
| 331 var mostVisitedData_ = []; |
| 332 |
| 333 /** |
328 * The current bookmark data being displayed. Keep a reference to this data | 334 * The current bookmark data being displayed. Keep a reference to this data |
329 * in case the sync enabled state changes. In this case, the bookmark data | 335 * in case the sync enabled state changes. In this case, the bookmark data |
330 * will need to be refiltered. | 336 * will need to be refiltered. |
331 * @type {?Object} | 337 * @type {?Object} |
332 */ | 338 */ |
333 var bookmarkData; | 339 var bookmarkData; |
334 | 340 |
335 /** | 341 /** |
336 * Keep track of any outstanding timers related to updating the sync section. | 342 * Keep track of any outstanding timers related to updating the sync section. |
337 */ | 343 */ |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 }; | 654 }; |
649 thumbnailImg.src = backgroundUrl; | 655 thumbnailImg.src = backgroundUrl; |
650 | 656 |
651 thumbnailContainer.appendChild(thumbnail); | 657 thumbnailContainer.appendChild(thumbnail); |
652 var innerBorder = createDiv('inner-border'); | 658 var innerBorder = createDiv('inner-border'); |
653 thumbnailContainer.appendChild(innerBorder); | 659 thumbnailContainer.appendChild(innerBorder); |
654 thumbnailCell.appendChild(thumbnailContainer); | 660 thumbnailCell.appendChild(thumbnailContainer); |
655 var title = createDiv('title'); | 661 var title = createDiv('title'); |
656 title.textContent = item.title; | 662 title.textContent = item.title; |
657 var spacerImg = createElement('img', 'title-spacer'); | 663 var spacerImg = createElement('img', 'title-spacer'); |
| 664 spacerImg.alt = ''; |
658 title.insertBefore(spacerImg, title.firstChild); | 665 title.insertBefore(spacerImg, title.firstChild); |
659 thumbnailCell.appendChild(title); | 666 thumbnailCell.appendChild(title); |
660 | 667 |
661 wrapClickHandler(thumbnailContainer, item, opt_clickCallback); | 668 wrapClickHandler(thumbnailCell, item, opt_clickCallback); |
662 | 669 |
663 thumbnailCell.setAttribute(CONTEXT_MENU_URL_KEY, item.url); | 670 thumbnailCell.setAttribute(CONTEXT_MENU_URL_KEY, item.url); |
664 thumbnailCell.contextMenuItem = item; | 671 thumbnailCell.contextMenuItem = item; |
665 return thumbnailCell; | 672 return thumbnailCell; |
666 } | 673 } |
667 | 674 |
668 /** | 675 /** |
669 * Creates a shortcut DOM element based on the item specified item | 676 * Creates a shortcut DOM element based on the item specified item |
670 * configuration using the favicon layout used for bookmarks. | 677 * configuration using the favicon layout used for bookmarks. |
671 * | 678 * |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 */ | 910 */ |
904 function setMostVisitedPages(data, hasBlacklistedUrls) { | 911 function setMostVisitedPages(data, hasBlacklistedUrls) { |
905 setNumberOfMostVisitedPages(data.length); | 912 setNumberOfMostVisitedPages(data.length); |
906 // limit the number of most visited items to display | 913 // limit the number of most visited items to display |
907 if (isPhone() && data.length > 6) { | 914 if (isPhone() && data.length > 6) { |
908 data.splice(6, data.length - 6); | 915 data.splice(6, data.length - 6); |
909 } else if (isTablet() && data.length > 8) { | 916 } else if (isTablet() && data.length > 8) { |
910 data.splice(8, data.length - 8); | 917 data.splice(8, data.length - 8); |
911 } | 918 } |
912 | 919 |
| 920 if (equals(data, mostVisitedData_)) |
| 921 return; |
| 922 |
913 var clickFunction = function(item) { | 923 var clickFunction = function(item) { |
914 chrome.send('metricsHandler:recordAction', ['MobileNTPMostVisited']); | 924 chrome.send('metricsHandler:recordAction', ['MobileNTPMostVisited']); |
915 window.location = item.url; | 925 window.location = item.url; |
916 }; | 926 }; |
917 populateData(findList('most_visited'), SectionType.MOST_VISITED, data, | 927 populateData(findList('most_visited'), SectionType.MOST_VISITED, data, |
918 makeMostVisitedItem, clickFunction); | 928 makeMostVisitedItem, clickFunction); |
919 computeDynamicLayout(); | 929 computeDynamicLayout(); |
| 930 |
| 931 mostVisitedData_ = data; |
920 } | 932 } |
921 | 933 |
922 /** | 934 /** |
923 * Updates the recently closed tabs. | 935 * Updates the recently closed tabs. |
924 * | 936 * |
925 * @param {Array.<Object>} List of data for displaying the list of recently | 937 * @param {Array.<Object>} List of data for displaying the list of recently |
926 * closed tabs (see C++ handler for model description). | 938 * closed tabs (see C++ handler for model description). |
927 */ | 939 */ |
928 function setRecentlyClosedTabs(data) { | 940 function setRecentlyClosedTabs(data) { |
929 var container = $('recently_closed_container'); | 941 var container = $('recently_closed_container'); |
(...skipping 1553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2483 setSyncEnabled: setSyncEnabled, | 2495 setSyncEnabled: setSyncEnabled, |
2484 snapshots: snapshots | 2496 snapshots: snapshots |
2485 }; | 2497 }; |
2486 }); | 2498 }); |
2487 | 2499 |
2488 ///////////////////////////////////////////////////////////////////////////// | 2500 ///////////////////////////////////////////////////////////////////////////// |
2489 //Utility Functions. | 2501 //Utility Functions. |
2490 ///////////////////////////////////////////////////////////////////////////// | 2502 ///////////////////////////////////////////////////////////////////////////// |
2491 | 2503 |
2492 /** | 2504 /** |
| 2505 * A best effort approach for checking simple data object equality. |
| 2506 * @param {?} val1 The first value to check equality for. |
| 2507 * @param {?} val2 The second value to check equality for. |
| 2508 * @return {boolean} Whether the two objects are equal(ish). |
| 2509 */ |
| 2510 function equals(val1, val2) { |
| 2511 if (typeof val1 != 'object' || typeof val2 != 'object') |
| 2512 return val1 === val2; |
| 2513 |
| 2514 // Object and array equality checks. |
| 2515 var keyCountVal1 = 0; |
| 2516 for (var key in val1) { |
| 2517 if (!(key in val2) || !equals(val1[key], val2[key])) |
| 2518 return false; |
| 2519 keyCountVal1++; |
| 2520 } |
| 2521 var keyCountVal2 = 0; |
| 2522 for (var key in val2) |
| 2523 keyCountVal2++; |
| 2524 if (keyCountVal1 != keyCountVal2) |
| 2525 return false; |
| 2526 return true; |
| 2527 } |
| 2528 |
| 2529 /** |
2493 * Alias for document.getElementById. | 2530 * Alias for document.getElementById. |
2494 * @param {string} id The ID of the element to find. | 2531 * @param {string} id The ID of the element to find. |
2495 * @return {HTMLElement} The found element or null if not found. | 2532 * @return {HTMLElement} The found element or null if not found. |
2496 */ | 2533 */ |
2497 function $(id) { | 2534 function $(id) { |
2498 return document.getElementById(id); | 2535 return document.getElementById(id); |
2499 } | 2536 } |
2500 | 2537 |
2501 /** | 2538 /** |
2502 * @return {boolean} Whether the device is currently in portrait mode. | 2539 * @return {boolean} Whether the device is currently in portrait mode. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2614 // NTP Entry point. | 2651 // NTP Entry point. |
2615 ///////////////////////////////////////////////////////////////////////////// | 2652 ///////////////////////////////////////////////////////////////////////////// |
2616 | 2653 |
2617 /* | 2654 /* |
2618 * Handles initializing the UI when the page has finished loading. | 2655 * Handles initializing the UI when the page has finished loading. |
2619 */ | 2656 */ |
2620 window.addEventListener('DOMContentLoaded', function(evt) { | 2657 window.addEventListener('DOMContentLoaded', function(evt) { |
2621 ntp.init(); | 2658 ntp.init(); |
2622 $('content-area').style.display = 'block'; | 2659 $('content-area').style.display = 'block'; |
2623 }); | 2660 }); |
OLD | NEW |