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

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

Issue 10882024: Add webui handler for promotions on Android NTP. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Histograms for all events. Created 8 years, 3 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
OLDNEW
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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 * @type {boolean} 207 * @type {boolean}
208 */ 208 */
209 var hasRecentlyClosedTabs = false; 209 var hasRecentlyClosedTabs = false;
210 210
211 /** 211 /**
212 * Whether promo is not allowed or not (external to NTP). 212 * Whether promo is not allowed or not (external to NTP).
213 * @type {boolean} 213 * @type {boolean}
214 */ 214 */
215 var promoIsAllowed = false; 215 var promoIsAllowed = false;
216 216
217 /**
218 * Whether promo should be shown on Most Visited page (externally set).
219 * @type {boolean}
220 */
221 var promoIsAllowedOnMostVisited = false;
222
223 /**
224 * Whether promo should be shown on Open Tabs page (externally set).
225 * @type {boolean}
226 */
227 var promoIsAllowedOnOpenTabs = false;
228
229 /**
230 * Whether promo should show a virtual computer on Open Tabs (externally set).
231 * @type {boolean}
232 */
233 var promoIsAllowedAsVirtualComputer = false;
234
235 /**
236 * Promo-injected title of a virtual computer on an open tabs pane.
237 * @type {string}
238 */
239 var promoInjectedComputerTitleText = '';
240
241 /**
242 * Promo-injected last synced text of a virtual computer on an open tabs pane.
243 * @type {string}
244 */
245 var promoInjectedComputerLastSyncedText = '';
246
217 function setIncognitoMode(incognito) { 247 function setIncognitoMode(incognito) {
218 isIncognito = incognito; 248 isIncognito = incognito;
219 } 249 }
220 250
221 /** 251 /**
222 * The different sections that are displayed. 252 * The different sections that are displayed.
223 * @enum {number} 253 * @enum {number}
224 */ 254 */
225 var SectionType = { 255 var SectionType = {
226 BOOKMARKS: 0, 256 BOOKMARKS: 'bookmarks',
227 INCOGNITO: 1, 257 MOST_VISITED: 'most_visited',
228 MOST_VISITED: 2, 258 RECENTLY_CLOSED: 'recently_closed',
Dan Beam 2012/08/30 18:52:26 nit: alpha sort now
aruslan 2012/08/30 21:38:17 Done.
229 RECENTLY_CLOSED: 3, 259 FOREIGN_SESSION: 'foreign_session',
230 SYNCED_DEVICES: 4, 260 FOREIGN_SESSION_HEADER: 'foreign_session_header',
231 FOREIGN_SESSION: 5, 261 SNAPSHOTS: 'snapshots',
232 FOREIGN_SESSION_HEADER: 6, 262 PROMO_VC_SESSION_HEADER: 'promo_vc_session_header',
233 SNAPSHOTS: 7, 263 UNKNOWN: 'unknown',
234 UNKNOWN: 100,
235 }; 264 };
236 265
237 /** 266 /**
238 * The different ids used of our custom context menu. Sent to the ChromeView 267 * The different ids used of our custom context menu. Sent to the ChromeView
239 * and sent back when a menu is selected. 268 * and sent back when a menu is selected.
240 * @enum {number} 269 * @enum {number}
241 */ 270 */
242 var ContextMenuItemIds = { 271 var ContextMenuItemIds = {
243 BOOKMARK_EDIT: 0, 272 BOOKMARK_EDIT: 0,
244 BOOKMARK_DELETE: 1, 273 BOOKMARK_DELETE: 1,
245 BOOKMARK_OPEN_IN_NEW_TAB: 2, 274 BOOKMARK_OPEN_IN_NEW_TAB: 2,
246 BOOKMARK_OPEN_IN_INCOGNITO_TAB: 3, 275 BOOKMARK_OPEN_IN_INCOGNITO_TAB: 3,
247 BOOKMARK_SHORTCUT: 4, 276 BOOKMARK_SHORTCUT: 4,
248 277
249 MOST_VISITED_OPEN_IN_NEW_TAB: 10, 278 MOST_VISITED_OPEN_IN_NEW_TAB: 10,
250 MOST_VISITED_OPEN_IN_INCOGNITO_TAB: 11, 279 MOST_VISITED_OPEN_IN_INCOGNITO_TAB: 11,
251 MOST_VISITED_REMOVE: 12, 280 MOST_VISITED_REMOVE: 12,
252 281
253 RECENTLY_CLOSED_OPEN_IN_NEW_TAB: 20, 282 RECENTLY_CLOSED_OPEN_IN_NEW_TAB: 20,
254 RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB: 21, 283 RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB: 21,
255 RECENTLY_CLOSED_REMOVE: 22, 284 RECENTLY_CLOSED_REMOVE: 22,
256 285
257 FOREIGN_SESSIONS_REMOVE: 30, 286 FOREIGN_SESSIONS_REMOVE: 30,
287
288 PROMO_VC_SESSION_REMOVE: 40,
258 }; 289 };
259 290
260 /** 291 /**
261 * The URL of the element for the context menu. 292 * The URL of the element for the context menu.
262 * @type {string} 293 * @type {string}
263 */ 294 */
264 var contextMenuUrl = null; 295 var contextMenuUrl = null;
265 296
266 var contextMenuItem = null; 297 var contextMenuItem = null;
267 298
268 var currentSnapshots = null; 299 var currentSnapshots = null;
269 300
270 var currentSessions = null; 301 var currentSessions = null;
271 302
272 /** 303 /**
273 * The possible states of the sync section 304 * The possible states of the sync section
274 * @enum {number} 305 * @enum {number}
275 */ 306 */
276 var SyncState = { 307 var SyncState = {
277 INITIAL: 0, 308 INITIAL: 0,
278 WAITING_FOR_DATA: 1, 309 WAITING_FOR_DATA: 1,
279 DISPLAYING_LOADING: 2, 310 DISPLAYING_LOADING: 2,
280 DISPLAYED_LOADING: 3, 311 DISPLAYED_LOADING: 3,
281 LOADED: 3, 312 LOADED: 4,
282 }; 313 };
283 314
284 /** 315 /**
285 * The current state of the sync section. 316 * The current state of the sync section.
286 */ 317 */
287 var syncState = SyncState.INITIAL; 318 var syncState = SyncState.INITIAL;
288 319
289 /** 320 /**
290 * Whether or not sync is enabled. It will be undefined until 321 * Whether or not sync is enabled. It will be undefined until
291 * setSyncEnabled() is called. 322 * setSyncEnabled() is called.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 // incognito NTPs have a bookmarks section. 422 // incognito NTPs have a bookmarks section.
392 if (getPaneIndexFromHash() < 0) 423 if (getPaneIndexFromHash() < 0)
393 document.location.hash = '#bookmarks'; 424 document.location.hash = '#bookmarks';
394 425
395 // Initialize common widgets. 426 // Initialize common widgets.
396 var titleScrollers = 427 var titleScrollers =
397 document.getElementsByClassName('section-title-wrapper'); 428 document.getElementsByClassName('section-title-wrapper');
398 for (var i = 0, len = titleScrollers.length; i < len; i++) 429 for (var i = 0, len = titleScrollers.length; i < len; i++)
399 initializeTitleScroller(titleScrollers[i]); 430 initializeTitleScroller(titleScrollers[i]);
400 431
432 // Initialize virtual computers for the sync promo.
433 createPromoVirtualComputers();
434
401 chrome.send('getMostVisited'); 435 chrome.send('getMostVisited');
402 chrome.send('getRecentlyClosedTabs'); 436 chrome.send('getRecentlyClosedTabs');
403 chrome.send('getForeignSessions'); 437 chrome.send('getForeignSessions');
404 chrome.send('getPromotions'); 438 chrome.send('getPromotions');
405 439
406 setCurrentBookmarkFolderData( 440 setCurrentBookmarkFolderData(
407 localStorage.getItem(DEFAULT_BOOKMARK_FOLDER_KEY)); 441 localStorage.getItem(DEFAULT_BOOKMARK_FOLDER_KEY));
408 442
409 addMainSection('incognito'); 443 addMainSection('incognito');
410 addMainSection('most_visited'); 444 addMainSection('most_visited');
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 854
821 var titleContainer = createElement('span', 'title'); 855 var titleContainer = createElement('span', 'title');
822 sessionOuterDiv.appendChild(titleContainer); 856 sessionOuterDiv.appendChild(titleContainer);
823 857
824 // Extra container to allow title & last-sync time to stack vertically. 858 // Extra container to allow title & last-sync time to stack vertically.
825 var sessionInnerDiv = createDiv(null); 859 var sessionInnerDiv = createDiv(null);
826 titleContainer.appendChild(sessionInnerDiv); 860 titleContainer.appendChild(sessionInnerDiv);
827 861
828 var title = createDiv('session-name'); 862 var title = createDiv('session-name');
829 title.textContent = item.title; 863 title.textContent = item.title;
864 title.id = item.titleId || '';
830 sessionInnerDiv.appendChild(title); 865 sessionInnerDiv.appendChild(title);
831 866
832 var lastSynced = createDiv('session-last-synced'); 867 var lastSynced = createDiv('session-last-synced');
833 lastSynced.textContent = 868 lastSynced.textContent =
834 templateData.opentabslastsynced + ': ' + item.userVisibleTimestamp; 869 templateData.opentabslastsynced + ': ' + item.userVisibleTimestamp;
870 lastSynced.id = item.userVisibleTimestampId || '';
835 sessionInnerDiv.appendChild(lastSynced); 871 sessionInnerDiv.appendChild(lastSynced);
836 872
837 sessionOuterDiv.addEventListener('click', function(evt) { 873 sessionOuterDiv.addEventListener('click', function(evt) {
838 var clickCallback = 874 var clickCallback =
839 opt_clickCallback ? opt_clickCallback : itemShortcutClickHandler; 875 opt_clickCallback ? opt_clickCallback : itemShortcutClickHandler;
840 clickCallback(item, evt); 876 clickCallback(item, evt);
841 }); 877 });
842 return sessionOuterDiv; 878 return sessionOuterDiv;
843 } 879 }
844 880
845 /** 881 /**
846 * Saves the number of most visited pages and updates promo visibility. 882 * Saves the number of most visited pages and updates promo visibility.
847 * @param {number} n Number of most visited pages. 883 * @param {number} n Number of most visited pages.
848 */ 884 */
849 function setNumberOfMostVisitedPages(n) { 885 function setNumberOfMostVisitedPages(n) {
850 numberOfMostVisitedPages = n; 886 numberOfMostVisitedPages = n;
851 promoSetVisibility(); 887 updatePromoVisibility();
852 } 888 }
853 889
854 /** 890 /**
855 * Saves the recently closed tabs flag and updates promo visibility. 891 * Saves the recently closed tabs flag and updates promo visibility.
856 * @param {boolean} anyTabs Whether there are any recently closed tabs. 892 * @param {boolean} anyTabs Whether there are any recently closed tabs.
857 */ 893 */
858 function setHasRecentlyClosedTabs(anyTabs) { 894 function setHasRecentlyClosedTabs(anyTabs) {
859 hasRecentlyClosedTabs = anyTabs; 895 hasRecentlyClosedTabs = anyTabs;
860 promoSetVisibility(); 896 updatePromoVisibility();
861 } 897 }
862 898
863 /** 899 /**
864 * Updates the most visited pages. 900 * Updates the most visited pages.
865 * 901 *
866 * @param {Array.<Object>} List of data for displaying the list of most 902 * @param {Array.<Object>} List of data for displaying the list of most
867 * visited pages (see C++ handler for model description). 903 * visited pages (see C++ handler for model description).
868 * @param {boolean} hasBlacklistedUrls Whether any blacklisted URLs are 904 * @param {boolean} hasBlacklistedUrls Whether any blacklisted URLs are
869 * present. 905 * present.
870 */ 906 */
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
960 996
961 // update the shadows on the breadcrumb bar 997 // update the shadows on the breadcrumb bar
962 computeDynamicLayout(); 998 computeDynamicLayout();
963 } 999 }
964 1000
965 /** 1001 /**
966 * Checks if promo is allowed and MostVisited requirements are satisfied. 1002 * Checks if promo is allowed and MostVisited requirements are satisfied.
967 * @return {boolean} Whether the promo should be shown on most_visited. 1003 * @return {boolean} Whether the promo should be shown on most_visited.
968 */ 1004 */
969 function shouldPromoBeShownOnMostVisited() { 1005 function shouldPromoBeShownOnMostVisited() {
970 return promoIsAllowed && 1006 return promoIsAllowed && promoIsAllowedOnMostVisited &&
971 (numberOfMostVisitedPages >= 2) && 1007 numberOfMostVisitedPages >= 2 && !hasRecentlyClosedTabs;
972 (!hasRecentlyClosedTabs);
973 } 1008 }
974 1009
975 /** 1010 /**
976 * Checks if promo is allowed and OpenTabs requirements are satisfied. 1011 * Checks if promo is allowed and OpenTabs requirements are satisfied.
977 * @return {boolean} Whether the promo should be shown on open_tabs. 1012 * @return {boolean} Whether the promo should be shown on open_tabs.
978 */ 1013 */
979 function shouldPromoBeShownOnOpenTabs() { 1014 function shouldPromoBeShownOnOpenTabs() {
980 var snapshotsCount = 1015 var snapshotsCount =
981 currentSnapshots == null ? 0 : currentSnapshots.length; 1016 currentSnapshots == null ? 0 : currentSnapshots.length;
982 var sessionsCount = currentSessions == null ? 0 : currentSessions.length; 1017 var sessionsCount = currentSessions == null ? 0 : currentSessions.length;
983 return promoIsAllowed && 1018 return promoIsAllowed && promoIsAllowedOnOpenTabs &&
984 (snapshotsCount + sessionsCount != 0); 1019 (snapshotsCount + sessionsCount != 0);
985 } 1020 }
986 1021
987 /** 1022 /**
988 * Checks if promo is allowed and SyncPromo requirements are satisfied. 1023 * Checks if promo is allowed and SyncPromo requirements are satisfied.
989 * @return {boolean} Whether the promo should be shown on sync_promo. 1024 * @return {boolean} Whether the promo should be shown on sync_promo.
990 */ 1025 */
991 function shouldPromoBeShownOnSyncPromo() { 1026 function shouldPromoBeShownOnSync() {
992 var snapshotsCount = 1027 var snapshotsCount =
993 currentSnapshots == null ? 0 : currentSnapshots.length; 1028 currentSnapshots == null ? 0 : currentSnapshots.length;
994 var sessionsCount = currentSessions == null ? 0 : currentSessions.length; 1029 var sessionsCount = currentSessions == null ? 0 : currentSessions.length;
995 return promoIsAllowed && 1030 return promoIsAllowed && promoIsAllowedOnOpenTabs &&
996 (snapshotsCount + sessionsCount == 0); 1031 (snapshotsCount + sessionsCount == 0);
997 } 1032 }
998 1033
999 /** 1034 /**
1000 * Records a promo impression on a given section if necessary. 1035 * Records a promo impression on a given section if necessary.
1001 * @param {string} section Active section name to check. 1036 * @param {string} section Active section name to check.
1002 */ 1037 */
1003 function promoUpdateImpressions(section) { 1038 function promoUpdateImpressions(section) {
1004 if (section == 'most_visited' && shouldPromoBeShownOnMostVisited()) { 1039 if (section == 'most_visited' && shouldPromoBeShownOnMostVisited())
1005 chrome.send('recordImpression', ['most_visited']); 1040 chrome.send('recordImpression', ['most_visited']);
1006 } else if (section == 'open_tabs' && shouldPromoBeShownOnOpenTabs()) { 1041 else if (section == 'open_tabs' && shouldPromoBeShownOnOpenTabs())
1007 chrome.send('recordImpression', ['open_tabs']); 1042 chrome.send('recordImpression', ['open_tabs']);
1008 } else if (section == 'open_tabs' && shouldPromoBeShownOnSyncPromo()) { 1043 else if (section == 'open_tabs' && shouldPromoBeShownOnSync())
1009 chrome.send('recordImpression', ['sync_promo']); 1044 chrome.send('recordImpression', ['sync_promo']);
1045 }
1046
1047 /**
1048 * Updates the visibility on all promo-related items as necessary.
1049 */
1050 function updatePromoVisibility() {
1051 var mostVisitedEl = $('promo_message_on_most_visited');
1052 var openTabsVCEl = $('promo_vc_list');
1053 var syncPromoLegacyEl = $('promo_message_on_sync_promo_legacy');
1054 var syncPromoReceivedEl = $('promo_message_on_sync_promo_received');
1055 mostVisitedEl.style.display =
1056 shouldPromoBeShownOnMostVisited() ? 'block' : 'none';
1057 syncPromoReceivedEl.style.display =
1058 shouldPromoBeShownOnSync() ? 'block' : 'none';
1059 syncPromoLegacyEl.style.display =
1060 shouldPromoBeShownOnSync() ? 'none' : 'block';
1061 openTabsVCEl.style.display =
1062 (shouldPromoBeShownOnOpenTabs() && promoIsAllowedAsVirtualComputer) ?
1063 'block' : 'none';
1064 }
1065
1066 /**
1067 * Called from native.
1068 * Clears the promotion.
1069 */
1070 function clearPromotions() {
1071 setPromotions({});
1072 }
1073
1074 /**
1075 * Set the element to a parsed and sanitized promotion HTML string.
1076 * @param {Element} el The element to set the promotion string to.
1077 * @param {string} html The promotion HTML string.
1078 * @throws {Error} In case of non supported markup.
1079 */
1080 function setPromotionHtml(el, html) {
1081 if (!el) return;
1082 el.innerHTML = '';
1083 if (!html) return;
1084 var tags = ['BR', 'DIV', 'BUTTON', 'SPAN'];
1085 var attrs = {
1086 class: function(node, value) { return true; },
1087 style: function(node, value) { return true; },
1088 };
1089 try {
1090 var fragment = parseHtmlSubset(html, tags, attrs);
1091 el.appendChild(fragment);
1092 } catch (err) {
1093 console.error(err.toString());
1094 // Ignore all errors while parsing or setting the element.
1010 } 1095 }
1011 } 1096 }
1012 1097
1013 /**
1014 * Sets the visibility on all promo-related items as necessary.
1015 */
1016 function promoSetVisibility() {
1017 var mostVisited = $('promo_message_on_most_visited');
1018 var openTabs = $('promo_message_on_open_tabs');
1019 if (shouldPromoBeShownOnMostVisited()) {
1020 mostVisited.style.display = 'block';
1021 } else {
1022 mostVisited.style.display = 'none';
1023 }
1024 if (shouldPromoBeShownOnOpenTabs()) {
1025 openTabs.style.display = 'block';
1026 } else {
1027 openTabs.style.display = 'none';
1028 }
1029 }
1030
1031 /** 1098 /**
1032 * Called from native. 1099 * Called from native.
1033 * Sets the text for all promo-related items, updates 1100 * Sets the text for all promo-related items, updates
1034 * promo-send-email-target items to send email on click and 1101 * promo-send-email-target items to send email on click and
1035 * updates the visibility of items. 1102 * updates the visibility of items.
1036 * @param {Object} promotions Dictionary used to fill-in the text. 1103 * @param {Object} promotions Dictionary used to fill-in the text.
1037 */ 1104 */
1038 function setPromotions(promotions) { 1105 function setPromotions(promotions) {
1039 var mostVisited = $('promo_message_on_most_visited'); 1106 var mostVisitedEl = $('promo_message_on_most_visited');
1040 var openTabs = $('promo_message_on_open_tabs'); 1107 var openTabsEl = $('promo_message_on_open_tabs');
1041 var syncPromoLegacy = $('promo_message_on_sync_promo_legacy'); 1108 var syncPromoReceivedEl = $('promo_message_on_sync_promo_received');
1042 mostVisited.innerHTML = promotions['promoMessage']; 1109
1043 openTabs.innerHTML = promotions['promoMessage']; 1110 promoIsAllowed = promotions.promoIsAllowed === true;
Dan Beam 2012/08/30 18:52:26 !!promotions.promoIsAllowed or Boolean(promotions.
aruslan 2012/08/30 21:38:17 Done.
1044 if (promotions['promoMessageLong']) { 1111 promoIsAllowedOnMostVisited =
1045 syncPromoLegacy.innerHTML = promotions['promoMessageLong']; 1112 promotions.promoIsAllowedOnMostVisited === true;
1046 } 1113 promoIsAllowedOnOpenTabs = promotions.promoIsAllowedOnOpenTabs === true;
1047 promoIsAllowed = promotions['promoIsAllowed'] === true; 1114 promoIsAllowedAsVirtualComputer = promotions.promoIsAllowedAsVC === true;
1115
1116 setPromotionHtml(mostVisitedEl, promotions.promoMessage);
1117 setPromotionHtml(openTabsEl, promotions.promoMessage);
1118 setPromotionHtml(syncPromoReceivedEl, promotions.promoMessageLong);
1119
1120 promoInjectedComputerTitleText = promotions.promoVCTitle || '';
1121 promoInjectedComputerLastSyncedText = promotions.promoVCLastSynced || '';
1122 var openTabsVCTitleEl = $('promo_vc_title');
1123 if (openTabsVCTitleEl)
Dan Beam 2012/08/30 18:52:26 is it valid to not have these elements? if no, I w
aruslan 2012/08/30 21:38:17 Yes, it's valid not to have this element; those th
1124 openTabsVCTitleEl.textContent = promoInjectedComputerTitleText;
1125 var openTabsVCLastSyncEl = $('promo_vc_lastsync');
1126 if (openTabsVCLastSyncEl)
1127 openTabsVCLastSyncEl.textContent = promoInjectedComputerLastSyncedText;
1128
1048 if (promoIsAllowed) { 1129 if (promoIsAllowed) {
1049 var promoTargets = 1130 var promoButtonEls =
1050 document.getElementsByClassName('promo-action-target'); 1131 document.getElementsByClassName('promo-button');
Dan Beam 2012/08/30 18:52:26 nit: I prefer document.querySelectorAll('.promo-bu
aruslan 2012/08/30 21:38:17 Good point regarding Selector APIs (couldn't say i
1051 for (var i = 0, len = promoTargets.length; i < len; i++) { 1132 for (var i = 0, len = promoButtonEls.length; i < len; i++) {
1052 promoTargets[i].href = 'javascript:void(0)'; 1133 promoButtonEls[i].onclick = executePromoAction;
1053 promoTargets[i].onclick = promoAction; 1134 addActiveTouchListener(promoButtonEls[i], 'promo-button-active');
1054 } 1135 }
1055 } 1136 }
1056 promoSetVisibility(); 1137 updatePromoVisibility();
1057 } 1138 }
1058 1139
1059 /** 1140 /**
1060 * On-click handler for promo email targets. 1141 * On-click handler for promo email targets.
1061 * Performs the promo action "send email". 1142 * Performs the promo action "send email".
1062 * @param {Object} evt User interface event that triggered the action. 1143 * @param {Object} evt User interface event that triggered the action.
1063 */ 1144 */
1064 function promoAction(evt) { 1145 function executePromoAction(evt) {
1065 if (evt.preventDefault) 1146 if (evt.preventDefault)
1066 evt.preventDefault(); 1147 evt.preventDefault();
1067 evt.returnValue = false; 1148 evt.returnValue = false;
1068 chrome.send('promoActionTriggered'); 1149 chrome.send('promoActionTriggered');
1069 } 1150 }
1070 1151
1071 /** 1152 /**
1072 * Called by the browser when a context menu has been selected. 1153 * Called by the browser when a context menu has been selected.
1073 * 1154 *
1074 * @param {number} itemId The id of the item that was selected, as specified 1155 * @param {number} itemId The id of the item that was selected, as specified
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 break; 1196 break;
1116 1197
1117 case ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE: 1198 case ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE:
1118 if (contextMenuItem != null) { 1199 if (contextMenuItem != null) {
1119 chrome.send( 1200 chrome.send(
1120 'deleteForeignSession', [contextMenuItem.sessionTag]); 1201 'deleteForeignSession', [contextMenuItem.sessionTag]);
1121 chrome.send('getForeignSessions'); 1202 chrome.send('getForeignSessions');
1122 } 1203 }
1123 break; 1204 break;
1124 1205
1206 case ContextMenuItemIds.PROMO_VC_SESSION_REMOVE:
1207 chrome.send('promoDisabled');
1208 break;
1209
1125 default: 1210 default:
1126 log.error('Unknown context menu selected id=' + itemId); 1211 log.error('Unknown context menu selected id=' + itemId);
1127 break; 1212 break;
1128 } 1213 }
1129 } 1214 }
1130 1215
1131 /** 1216 /**
1132 * Generates the full bookmark folder hierarchy and populates the scrollable 1217 * Generates the full bookmark folder hierarchy and populates the scrollable
1133 * title element. 1218 * title element.
1134 * 1219 *
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 syncEnabled = enabled; 1427 syncEnabled = enabled;
1343 1428
1344 if (enabled) { 1429 if (enabled) {
1345 if (!localStorage.getItem(SYNC_ENABLED_KEY)) { 1430 if (!localStorage.getItem(SYNC_ENABLED_KEY)) {
1346 localStorage.setItem(SYNC_ENABLED_KEY, 'true'); 1431 localStorage.setItem(SYNC_ENABLED_KEY, 'true');
1347 setCurrentBookmarkFolderData('0'); 1432 setCurrentBookmarkFolderData('0');
1348 } 1433 }
1349 } else { 1434 } else {
1350 localStorage.removeItem(SYNC_ENABLED_KEY); 1435 localStorage.removeItem(SYNC_ENABLED_KEY);
1351 } 1436 }
1437 updatePromoVisibility();
1352 1438
1353 if (bookmarkData) { 1439 if (bookmarkData) {
1354 // Bookmark data can now be displayed (or needs to be refiltered) 1440 // Bookmark data can now be displayed (or needs to be refiltered)
1355 bookmarks(bookmarkData); 1441 bookmarks(bookmarkData);
1356 } 1442 }
1357 1443
1358 updateSyncEmptyState(); 1444 updateSyncEmptyState();
1359 } catch (e) {} 1445 } catch (e) {}
1360 } 1446 }
1361 1447
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1421 if (!syncEnabled) { 1507 if (!syncEnabled) {
1422 syncEnableSync.style.display = '-webkit-box'; 1508 syncEnableSync.style.display = '-webkit-box';
1423 centerEmptySections(syncEnableSync); 1509 centerEmptySections(syncEnableSync);
1424 } else if (sessionsCount + snapshotsCount == 0) { 1510 } else if (sessionsCount + snapshotsCount == 0) {
1425 syncPromo.style.display = '-webkit-box'; 1511 syncPromo.style.display = '-webkit-box';
1426 centerEmptySections(syncPromo); 1512 centerEmptySections(syncPromo);
1427 } else { 1513 } else {
1428 openTabsList.style.display = sessionsCount == 0 ? 'none' : 'block'; 1514 openTabsList.style.display = sessionsCount == 0 ? 'none' : 'block';
1429 snapshotsList.style.display = snapshotsCount == 0 ? 'none' : 'block'; 1515 snapshotsList.style.display = snapshotsCount == 0 ? 'none' : 'block';
1430 } 1516 }
1431 promoSetVisibility(); 1517 updatePromoVisibility();
1432 } 1518 }
1433 1519
1434 /** 1520 /**
1435 * Called externally when updated snapshot data is available. 1521 * Called externally when updated snapshot data is available.
1436 * 1522 *
1437 * @param {Object} data The snapshot data 1523 * @param {Object} data The snapshot data
1438 */ 1524 */
1439 function snapshots(data) { 1525 function snapshots(data) {
1440 var list = findList('snapshots'); 1526 var list = findList('snapshots');
1441 list.innerHTML = ''; 1527 list.innerHTML = '';
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1532 }, 0); 1618 }, 0);
1533 expando.className = 'expando closed'; 1619 expando.className = 'expando closed';
1534 } else { 1620 } else {
1535 element.style.height = element.expandedHeight; 1621 element.style.height = element.expandedHeight;
1536 expando.className = 'expando open'; 1622 expando.className = 'expando open';
1537 } 1623 }
1538 } 1624 }
1539 } 1625 }
1540 1626
1541 /** 1627 /**
1628 * Initializes the promo_vc_list div to look like a foreign session
1629 * with a desktop.
1630 */
1631 function createPromoVirtualComputers() {
1632 var list = findList('promo_vc');
1633 list.innerHTML = '';
1634
1635 // Set up the container and the "virtual computer" session header.
1636 var sessionEl = createDiv();
1637 list.appendChild(sessionEl);
1638 var sessionHeader = createDiv('session-header');
1639 sessionEl.appendChild(sessionHeader);
1640
1641 // Set up the session children container and the promo as a child.
1642 var sessionChildren = createDiv('session-children-container');
1643 var promoMessage = createDiv('promo-message');
1644 promoMessage.id = 'promo_message_on_open_tabs';
1645 sessionChildren.appendChild(promoMessage);
1646 sessionEl.appendChild(sessionChildren);
1647
1648 // Add support for expanding and collapsing the children.
1649 var expando = createDiv();
1650 var expandoFunction = createExpandoFunction(expando, sessionChildren);
1651
1652 // Fill-in the contents of the "virtual computer" session header.
1653 var headerList = [{
1654 'title': promoInjectedComputerTitleText,
1655 'titleId': 'promo_vc_title',
1656 'userVisibleTimestamp': promoInjectedComputerLastSyncedText,
1657 'userVisibleTimestampId': 'promo_vc_lastsync',
1658 'iconStyle': 'laptop'
1659 }];
1660
1661 populateData(sessionHeader, SectionType.PROMO_VC_SESSION_HEADER, headerList,
1662 makeForeignSessionListEntry, expandoFunction);
1663 sessionHeader.appendChild(expando);
1664 }
1665
1666 /**
1542 * Called externally when updated synced sessions data is available. 1667 * Called externally when updated synced sessions data is available.
1543 * 1668 *
1544 * @param {Object} data The snapshot data 1669 * @param {Object} data The snapshot data
1545 */ 1670 */
1546 function setForeignSessions(data, tabSyncEnabled) { 1671 function setForeignSessions(data, tabSyncEnabled) {
1547 var list = findList('open_tabs'); 1672 var list = findList('open_tabs');
1548 list.innerHTML = ''; 1673 list.innerHTML = '';
1549 1674
1550 currentSessions = data; 1675 currentSessions = data;
1551 updateSyncEmptyState(); 1676 updateSyncEmptyState();
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 /** 1860 /**
1736 * Finds the list element corresponding to the given name. 1861 * Finds the list element corresponding to the given name.
1737 * @param {string} name The name prefix of the DOM element (<prefix>_list). 1862 * @param {string} name The name prefix of the DOM element (<prefix>_list).
1738 * @return {Element} The list element corresponding with the name. 1863 * @return {Element} The list element corresponding with the name.
1739 */ 1864 */
1740 function findList(name) { 1865 function findList(name) {
1741 return $(name + '_list'); 1866 return $(name + '_list');
1742 } 1867 }
1743 1868
1744 /** 1869 /**
1745 * Gets the SectionType String from the enum SectionType.
1746 */
1747 function getSectionTypeString(section) {
1748 switch (section) {
1749 case SectionType.BOOKMARKS:
1750 return 'bookmarks';
1751 case SectionType.MOST_VISITED:
1752 return 'most_visited';
1753 case SectionType.RECENTLY_CLOSED:
1754 return 'recently_closed';
1755 case SectionType.SYNCED_DEVICES:
1756 return 'synced_devices';
1757 case SectionType.UNKNOWN:
1758 default:
1759 return 'unknown';
1760 }
1761 }
1762
1763 /**
1764 * Render the given data into the given list, and hide or show the entire 1870 * Render the given data into the given list, and hide or show the entire
1765 * container based on whether there are any elements. The decorator function 1871 * container based on whether there are any elements. The decorator function
1766 * is used to create the element to be inserted based on the given data 1872 * is used to create the element to be inserted based on the given data
1767 * object. 1873 * object.
1768 * 1874 *
1769 * @param {holder} The dom element that the generated list items will be put 1875 * @param {holder} The dom element that the generated list items will be put
1770 * into. 1876 * into.
1771 * @param {SectionType} section The section that data is for. 1877 * @param {SectionType} section The section that data is for.
1772 * @param {Object} data The data to be populated. 1878 * @param {Object} data The data to be populated.
1773 * @param {function(Object, boolean)} decorator The function that will 1879 * @param {function(Object, boolean)} decorator The function that will
1774 * handle decorating each item in the data. 1880 * handle decorating each item in the data.
1775 * @param {function(Object, Object)} opt_clickCallback The function that is 1881 * @param {function(Object, Object)} opt_clickCallback The function that is
1776 * called when the item is clicked. 1882 * called when the item is clicked.
1777 */ 1883 */
1778 function populateData(holder, section, data, decorator, 1884 function populateData(holder, section, data, decorator,
1779 opt_clickCallback) { 1885 opt_clickCallback) {
1780 // Empty other items in the list, if present. 1886 // Empty other items in the list, if present.
1781 holder.innerHTML = ''; 1887 holder.innerHTML = '';
1782 var fragment = document.createDocumentFragment(); 1888 var fragment = document.createDocumentFragment();
1783 if (!data || data.length == 0) { 1889 if (!data || data.length == 0) {
1784 fragment.innerHTML = ''; 1890 fragment.innerHTML = '';
1785 } else { 1891 } else {
1786 data.forEach(function(item) { 1892 data.forEach(function(item) {
1787 var el = decorator(item, opt_clickCallback); 1893 var el = decorator(item, opt_clickCallback);
1788 el.setAttribute(SECTION_KEY, section); 1894 el.setAttribute(SECTION_KEY, section);
1789 el.id = getSectionTypeString(section) + fragment.childNodes.length; 1895 el.id = section + fragment.childNodes.length;
1790 fragment.appendChild(el); 1896 fragment.appendChild(el);
1791 }); 1897 });
1792 } 1898 }
1793 holder.appendChild(fragment); 1899 holder.appendChild(fragment);
1794 if (holder.classList.contains(GRID_CSS_CLASS)) 1900 if (holder.classList.contains(GRID_CSS_CLASS))
1795 centerGrid(holder); 1901 centerGrid(holder);
1796 centerEmptySections(holder); 1902 centerEmptySections(holder);
1797 } 1903 }
1798 1904
1799 /** 1905 /**
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
2265 } 2371 }
2266 if (contextMenuUrl == null) { 2372 if (contextMenuUrl == null) {
2267 contextMenuUrl = node.getAttribute(CONTEXT_MENU_URL_KEY); 2373 contextMenuUrl = node.getAttribute(CONTEXT_MENU_URL_KEY);
2268 contextMenuItem = node.contextMenuItem; 2374 contextMenuItem = node.contextMenuItem;
2269 if (section != SectionType.UNKNOWN) 2375 if (section != SectionType.UNKNOWN)
2270 break; 2376 break;
2271 } 2377 }
2272 node = node.parentNode; 2378 node = node.parentNode;
2273 } 2379 }
2274 2380
2381 var menuOptions;
2382
2275 if (section == SectionType.BOOKMARKS && 2383 if (section == SectionType.BOOKMARKS &&
2276 !contextMenuItem.folder && !isIncognito) { 2384 !contextMenuItem.folder && !isIncognito) {
2277 var menuOptions = [ 2385 menuOptions = [
2278 [ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB, 2386 [
2279 templateData.elementopeninnewtab], 2387 ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB,
2280 [ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB, 2388 templateData.elementopeninnewtab
2281 templateData.elementopeninincognitotab]]; 2389 ],
2390 [
2391 ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB,
2392 templateData.elementopeninincognitotab
2393 ]
2394 ];
2282 if (contextMenuItem.editable) { 2395 if (contextMenuItem.editable) {
2283 menuOptions.push( 2396 menuOptions.push(
2284 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.bookmarkedit], 2397 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.bookmarkedit],
2285 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.bookmarkdelete]); 2398 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.bookmarkdelete]);
2286 } 2399 }
2287 if (contextMenuUrl.search('chrome://') == -1 && 2400 if (contextMenuUrl.search('chrome://') == -1 &&
2288 contextMenuUrl.search('about://') == -1) { 2401 contextMenuUrl.search('about://') == -1) {
2289 menuOptions.push( 2402 menuOptions.push([
2290 [ContextMenuItemIds.BOOKMARK_SHORTCUT, 2403 ContextMenuItemIds.BOOKMARK_SHORTCUT,
2291 templateData.bookmarkshortcut]); 2404 templateData.bookmarkshortcut
2405 ]);
2292 } 2406 }
2293 chrome.send('showContextMenu', menuOptions);
2294 } else if (section == SectionType.BOOKMARKS && 2407 } else if (section == SectionType.BOOKMARKS &&
2295 !contextMenuItem.folder && 2408 !contextMenuItem.folder &&
2296 isIncognito) { 2409 isIncognito) {
2297 chrome.send('showContextMenu', [ 2410 menuOptions = [
2298 [ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB, 2411 [
2299 templateData.elementopeninincognitotab] 2412 ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB,
2300 ]); 2413 templateData.elementopeninincognitotab
2414 ]
2415 ];
2301 } else if (section == SectionType.BOOKMARKS && 2416 } else if (section == SectionType.BOOKMARKS &&
2302 contextMenuItem.folder && 2417 contextMenuItem.folder &&
2303 contextMenuItem.editable && 2418 contextMenuItem.editable &&
2304 !isIncognito) { 2419 !isIncognito) {
2305 chrome.send('showContextMenu', [ 2420 menuOptions = [
2306 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.editfolder], 2421 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.editfolder],
2307 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.deletefolder], 2422 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.deletefolder]
2308 ]); 2423 ];
2309 } else if (section == SectionType.MOST_VISITED) { 2424 } else if (section == SectionType.MOST_VISITED) {
2310 chrome.send('showContextMenu', [ 2425 menuOptions = [
2311 [ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB, 2426 [
2312 templateData.elementopeninnewtab], 2427 ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB,
2313 [ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB, 2428 templateData.elementopeninnewtab
2314 templateData.elementopeninincognitotab], 2429 ],
2315 [ContextMenuItemIds.MOST_VISITED_REMOVE, templateData.elementremove] 2430 [
2316 ]); 2431 ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB,
2432 templateData.elementopeninincognitotab
2433 ],
2434 [ContextMenuItemIds.MOST_VISITED_REMOVE, templateData.elementremove]
2435 ];
2317 } else if (section == SectionType.RECENTLY_CLOSED) { 2436 } else if (section == SectionType.RECENTLY_CLOSED) {
2318 chrome.send('showContextMenu', [ 2437 menuOptions = [
2319 [ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB, 2438 [
2320 templateData.elementopeninnewtab], 2439 ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB,
2321 [ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB, 2440 templateData.elementopeninnewtab
2322 templateData.elementopeninincognitotab], 2441 ],
2323 [ContextMenuItemIds.RECENTLY_CLOSED_REMOVE, 2442 [
2324 templateData.elementremove] 2443 ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB,
2325 ]); 2444 templateData.elementopeninincognitotab
2445 ],
2446 [
2447 ContextMenuItemIds.RECENTLY_CLOSED_REMOVE,
2448 templateData.elementremove
2449 ]
2450 ];
2326 } else if (section == SectionType.FOREIGN_SESSION_HEADER) { 2451 } else if (section == SectionType.FOREIGN_SESSION_HEADER) {
2327 chrome.send('showContextMenu', [ 2452 menuOptions = [
2328 [ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE, 2453 [
2329 templateData.elementremove] 2454 ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE,
2330 ]); 2455 templateData.elementremove
2456 ]
2457 ];
2458 } else if (section == SectionType.PROMO_VC_SESSION_HEADER) {
2459 menuOptions = [
2460 [
2461 ContextMenuItemIds.PROMO_VC_SESSION_REMOVE,
2462 templateData.elementremove
2463 ]
2464 ];
2331 } 2465 }
2466
2467 if (menuOptions)
2468 chrome.send('showContextMenu', menuOptions);
2469
2332 return false; 2470 return false;
2333 } 2471 }
2334 2472
2335 // Return an object with all the exports 2473 // Return an object with all the exports
2336 return { 2474 return {
2337 bookmarks: bookmarks, 2475 bookmarks: bookmarks,
2338 bookmarkChanged: bookmarkChanged, 2476 bookmarkChanged: bookmarkChanged,
2339 setForeignSessions: setForeignSessions, 2477 clearPromotions: clearPromotions,
2340 init: init, 2478 init: init,
2341 onCustomMenuSelected: onCustomMenuSelected, 2479 onCustomMenuSelected: onCustomMenuSelected,
2342 openSection: openSection, 2480 openSection: openSection,
2343 setFaviconDominantColor: setFaviconDominantColor, 2481 setFaviconDominantColor: setFaviconDominantColor,
2482 setForeignSessions: setForeignSessions,
2344 setIncognitoMode: setIncognitoMode, 2483 setIncognitoMode: setIncognitoMode,
2345 setMostVisitedPages: setMostVisitedPages, 2484 setMostVisitedPages: setMostVisitedPages,
2346 setPromotions: setPromotions, 2485 setPromotions: setPromotions,
2347 setRecentlyClosedTabs: setRecentlyClosedTabs, 2486 setRecentlyClosedTabs: setRecentlyClosedTabs,
2348 setSyncEnabled: setSyncEnabled, 2487 setSyncEnabled: setSyncEnabled,
2349 snapshots: snapshots 2488 snapshots: snapshots
2350 }; 2489 };
2351 }); 2490 });
2352 2491
2353 ///////////////////////////////////////////////////////////////////////////// 2492 /////////////////////////////////////////////////////////////////////////////
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
2479 // NTP Entry point. 2618 // NTP Entry point.
2480 ///////////////////////////////////////////////////////////////////////////// 2619 /////////////////////////////////////////////////////////////////////////////
2481 2620
2482 /* 2621 /*
2483 * Handles initializing the UI when the page has finished loading. 2622 * Handles initializing the UI when the page has finished loading.
2484 */ 2623 */
2485 window.addEventListener('DOMContentLoaded', function(evt) { 2624 window.addEventListener('DOMContentLoaded', function(evt) {
2486 ntp.init(); 2625 ntp.init();
2487 $('content-area').style.display = 'block'; 2626 $('content-area').style.display = 'block';
2488 }); 2627 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698