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

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: Removing images (added separately in https://chromiumcodereview.appspot.com/10905035/) 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 FOREIGN_SESSION: 'foreign_session',
228 MOST_VISITED: 2, 258 FOREIGN_SESSION_HEADER: 'foreign_session_header',
229 RECENTLY_CLOSED: 3, 259 MOST_VISITED: 'most_visited',
230 SYNCED_DEVICES: 4, 260 PROMO_VC_SESSION_HEADER: 'promo_vc_session_header',
231 FOREIGN_SESSION: 5, 261 RECENTLY_CLOSED: 'recently_closed',
232 FOREIGN_SESSION_HEADER: 6, 262 SNAPSHOTS: 'snapshots',
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;
1044 if (promotions['promoMessageLong']) { 1111 promoIsAllowedOnMostVisited = !!promotions.promoIsAllowedOnMostVisited;
1045 syncPromoLegacy.innerHTML = promotions['promoMessageLong']; 1112 promoIsAllowedOnOpenTabs = !!promotions.promoIsAllowedOnOpenTabs;
1046 } 1113 promoIsAllowedAsVirtualComputer = !!promotions.promoIsAllowedAsVC;
1047 promoIsAllowed = promotions['promoIsAllowed'] === true; 1114
1115 setPromotionHtml(mostVisitedEl, promotions.promoMessage);
1116 setPromotionHtml(openTabsEl, promotions.promoMessage);
1117 setPromotionHtml(syncPromoReceivedEl, promotions.promoMessageLong);
1118
1119 promoInjectedComputerTitleText = promotions.promoVCTitle || '';
1120 promoInjectedComputerLastSyncedText = promotions.promoVCLastSynced || '';
1121 var openTabsVCTitleEl = $('promo_vc_title');
1122 if (openTabsVCTitleEl)
1123 openTabsVCTitleEl.textContent = promoInjectedComputerTitleText;
1124 var openTabsVCLastSyncEl = $('promo_vc_lastsync');
1125 if (openTabsVCLastSyncEl)
1126 openTabsVCLastSyncEl.textContent = promoInjectedComputerLastSyncedText;
1127
1048 if (promoIsAllowed) { 1128 if (promoIsAllowed) {
1049 var promoTargets = 1129 var promoButtonEls =
1050 document.getElementsByClassName('promo-action-target'); 1130 document.getElementsByClassName('promo-button');
1051 for (var i = 0, len = promoTargets.length; i < len; i++) { 1131 for (var i = 0, len = promoButtonEls.length; i < len; i++) {
1052 promoTargets[i].href = 'javascript:void(0)'; 1132 promoButtonEls[i].onclick = executePromoAction;
1053 promoTargets[i].onclick = promoAction; 1133 addActiveTouchListener(promoButtonEls[i], 'promo-button-active');
1054 } 1134 }
1055 } 1135 }
1056 promoSetVisibility(); 1136 updatePromoVisibility();
1057 } 1137 }
1058 1138
1059 /** 1139 /**
1060 * On-click handler for promo email targets. 1140 * On-click handler for promo email targets.
1061 * Performs the promo action "send email". 1141 * Performs the promo action "send email".
1062 * @param {Object} evt User interface event that triggered the action. 1142 * @param {Object} evt User interface event that triggered the action.
1063 */ 1143 */
1064 function promoAction(evt) { 1144 function executePromoAction(evt) {
1065 if (evt.preventDefault) 1145 if (evt.preventDefault)
1066 evt.preventDefault(); 1146 evt.preventDefault();
1067 evt.returnValue = false; 1147 evt.returnValue = false;
1068 chrome.send('promoActionTriggered'); 1148 chrome.send('promoActionTriggered');
1069 } 1149 }
1070 1150
1071 /** 1151 /**
1072 * Called by the browser when a context menu has been selected. 1152 * Called by the browser when a context menu has been selected.
1073 * 1153 *
1074 * @param {number} itemId The id of the item that was selected, as specified 1154 * @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; 1195 break;
1116 1196
1117 case ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE: 1197 case ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE:
1118 if (contextMenuItem != null) { 1198 if (contextMenuItem != null) {
1119 chrome.send( 1199 chrome.send(
1120 'deleteForeignSession', [contextMenuItem.sessionTag]); 1200 'deleteForeignSession', [contextMenuItem.sessionTag]);
1121 chrome.send('getForeignSessions'); 1201 chrome.send('getForeignSessions');
1122 } 1202 }
1123 break; 1203 break;
1124 1204
1205 case ContextMenuItemIds.PROMO_VC_SESSION_REMOVE:
1206 chrome.send('promoDisabled');
1207 break;
1208
1125 default: 1209 default:
1126 log.error('Unknown context menu selected id=' + itemId); 1210 log.error('Unknown context menu selected id=' + itemId);
1127 break; 1211 break;
1128 } 1212 }
1129 } 1213 }
1130 1214
1131 /** 1215 /**
1132 * Generates the full bookmark folder hierarchy and populates the scrollable 1216 * Generates the full bookmark folder hierarchy and populates the scrollable
1133 * title element. 1217 * title element.
1134 * 1218 *
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 syncEnabled = enabled; 1426 syncEnabled = enabled;
1343 1427
1344 if (enabled) { 1428 if (enabled) {
1345 if (!localStorage.getItem(SYNC_ENABLED_KEY)) { 1429 if (!localStorage.getItem(SYNC_ENABLED_KEY)) {
1346 localStorage.setItem(SYNC_ENABLED_KEY, 'true'); 1430 localStorage.setItem(SYNC_ENABLED_KEY, 'true');
1347 setCurrentBookmarkFolderData('0'); 1431 setCurrentBookmarkFolderData('0');
1348 } 1432 }
1349 } else { 1433 } else {
1350 localStorage.removeItem(SYNC_ENABLED_KEY); 1434 localStorage.removeItem(SYNC_ENABLED_KEY);
1351 } 1435 }
1436 updatePromoVisibility();
1352 1437
1353 if (bookmarkData) { 1438 if (bookmarkData) {
1354 // Bookmark data can now be displayed (or needs to be refiltered) 1439 // Bookmark data can now be displayed (or needs to be refiltered)
1355 bookmarks(bookmarkData); 1440 bookmarks(bookmarkData);
1356 } 1441 }
1357 1442
1358 updateSyncEmptyState(); 1443 updateSyncEmptyState();
1359 } catch (e) {} 1444 } catch (e) {}
1360 } 1445 }
1361 1446
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1421 if (!syncEnabled) { 1506 if (!syncEnabled) {
1422 syncEnableSync.style.display = '-webkit-box'; 1507 syncEnableSync.style.display = '-webkit-box';
1423 centerEmptySections(syncEnableSync); 1508 centerEmptySections(syncEnableSync);
1424 } else if (sessionsCount + snapshotsCount == 0) { 1509 } else if (sessionsCount + snapshotsCount == 0) {
1425 syncPromo.style.display = '-webkit-box'; 1510 syncPromo.style.display = '-webkit-box';
1426 centerEmptySections(syncPromo); 1511 centerEmptySections(syncPromo);
1427 } else { 1512 } else {
1428 openTabsList.style.display = sessionsCount == 0 ? 'none' : 'block'; 1513 openTabsList.style.display = sessionsCount == 0 ? 'none' : 'block';
1429 snapshotsList.style.display = snapshotsCount == 0 ? 'none' : 'block'; 1514 snapshotsList.style.display = snapshotsCount == 0 ? 'none' : 'block';
1430 } 1515 }
1431 promoSetVisibility(); 1516 updatePromoVisibility();
1432 } 1517 }
1433 1518
1434 /** 1519 /**
1435 * Called externally when updated snapshot data is available. 1520 * Called externally when updated snapshot data is available.
1436 * 1521 *
1437 * @param {Object} data The snapshot data 1522 * @param {Object} data The snapshot data
1438 */ 1523 */
1439 function snapshots(data) { 1524 function snapshots(data) {
1440 var list = findList('snapshots'); 1525 var list = findList('snapshots');
1441 list.innerHTML = ''; 1526 list.innerHTML = '';
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1532 }, 0); 1617 }, 0);
1533 expando.className = 'expando closed'; 1618 expando.className = 'expando closed';
1534 } else { 1619 } else {
1535 element.style.height = element.expandedHeight; 1620 element.style.height = element.expandedHeight;
1536 expando.className = 'expando open'; 1621 expando.className = 'expando open';
1537 } 1622 }
1538 } 1623 }
1539 } 1624 }
1540 1625
1541 /** 1626 /**
1627 * Initializes the promo_vc_list div to look like a foreign session
1628 * with a desktop.
1629 */
1630 function createPromoVirtualComputers() {
1631 var list = findList('promo_vc');
1632 list.innerHTML = '';
1633
1634 // Set up the container and the "virtual computer" session header.
1635 var sessionEl = createDiv();
1636 list.appendChild(sessionEl);
1637 var sessionHeader = createDiv('session-header');
1638 sessionEl.appendChild(sessionHeader);
1639
1640 // Set up the session children container and the promo as a child.
1641 var sessionChildren = createDiv('session-children-container');
1642 var promoMessage = createDiv('promo-message');
1643 promoMessage.id = 'promo_message_on_open_tabs';
1644 sessionChildren.appendChild(promoMessage);
1645 sessionEl.appendChild(sessionChildren);
1646
1647 // Add support for expanding and collapsing the children.
1648 var expando = createDiv();
1649 var expandoFunction = createExpandoFunction(expando, sessionChildren);
1650
1651 // Fill-in the contents of the "virtual computer" session header.
1652 var headerList = [{
1653 'title': promoInjectedComputerTitleText,
1654 'titleId': 'promo_vc_title',
1655 'userVisibleTimestamp': promoInjectedComputerLastSyncedText,
1656 'userVisibleTimestampId': 'promo_vc_lastsync',
1657 'iconStyle': 'laptop'
1658 }];
1659
1660 populateData(sessionHeader, SectionType.PROMO_VC_SESSION_HEADER, headerList,
1661 makeForeignSessionListEntry, expandoFunction);
1662 sessionHeader.appendChild(expando);
1663 }
1664
1665 /**
1542 * Called externally when updated synced sessions data is available. 1666 * Called externally when updated synced sessions data is available.
1543 * 1667 *
1544 * @param {Object} data The snapshot data 1668 * @param {Object} data The snapshot data
1545 */ 1669 */
1546 function setForeignSessions(data, tabSyncEnabled) { 1670 function setForeignSessions(data, tabSyncEnabled) {
1547 var list = findList('open_tabs'); 1671 var list = findList('open_tabs');
1548 list.innerHTML = ''; 1672 list.innerHTML = '';
1549 1673
1550 currentSessions = data; 1674 currentSessions = data;
1551 updateSyncEmptyState(); 1675 updateSyncEmptyState();
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 /** 1859 /**
1736 * Finds the list element corresponding to the given name. 1860 * Finds the list element corresponding to the given name.
1737 * @param {string} name The name prefix of the DOM element (<prefix>_list). 1861 * @param {string} name The name prefix of the DOM element (<prefix>_list).
1738 * @return {Element} The list element corresponding with the name. 1862 * @return {Element} The list element corresponding with the name.
1739 */ 1863 */
1740 function findList(name) { 1864 function findList(name) {
1741 return $(name + '_list'); 1865 return $(name + '_list');
1742 } 1866 }
1743 1867
1744 /** 1868 /**
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 1869 * 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 1870 * 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 1871 * is used to create the element to be inserted based on the given data
1767 * object. 1872 * object.
1768 * 1873 *
1769 * @param {holder} The dom element that the generated list items will be put 1874 * @param {holder} The dom element that the generated list items will be put
1770 * into. 1875 * into.
1771 * @param {SectionType} section The section that data is for. 1876 * @param {SectionType} section The section that data is for.
1772 * @param {Object} data The data to be populated. 1877 * @param {Object} data The data to be populated.
1773 * @param {function(Object, boolean)} decorator The function that will 1878 * @param {function(Object, boolean)} decorator The function that will
1774 * handle decorating each item in the data. 1879 * handle decorating each item in the data.
1775 * @param {function(Object, Object)} opt_clickCallback The function that is 1880 * @param {function(Object, Object)} opt_clickCallback The function that is
1776 * called when the item is clicked. 1881 * called when the item is clicked.
1777 */ 1882 */
1778 function populateData(holder, section, data, decorator, 1883 function populateData(holder, section, data, decorator,
1779 opt_clickCallback) { 1884 opt_clickCallback) {
1780 // Empty other items in the list, if present. 1885 // Empty other items in the list, if present.
1781 holder.innerHTML = ''; 1886 holder.innerHTML = '';
1782 var fragment = document.createDocumentFragment(); 1887 var fragment = document.createDocumentFragment();
1783 if (!data || data.length == 0) { 1888 if (!data || data.length == 0) {
1784 fragment.innerHTML = ''; 1889 fragment.innerHTML = '';
1785 } else { 1890 } else {
1786 data.forEach(function(item) { 1891 data.forEach(function(item) {
1787 var el = decorator(item, opt_clickCallback); 1892 var el = decorator(item, opt_clickCallback);
1788 el.setAttribute(SECTION_KEY, section); 1893 el.setAttribute(SECTION_KEY, section);
1789 el.id = getSectionTypeString(section) + fragment.childNodes.length; 1894 el.id = section + fragment.childNodes.length;
1790 fragment.appendChild(el); 1895 fragment.appendChild(el);
1791 }); 1896 });
1792 } 1897 }
1793 holder.appendChild(fragment); 1898 holder.appendChild(fragment);
1794 if (holder.classList.contains(GRID_CSS_CLASS)) 1899 if (holder.classList.contains(GRID_CSS_CLASS))
1795 centerGrid(holder); 1900 centerGrid(holder);
1796 centerEmptySections(holder); 1901 centerEmptySections(holder);
1797 } 1902 }
1798 1903
1799 /** 1904 /**
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
2265 } 2370 }
2266 if (contextMenuUrl == null) { 2371 if (contextMenuUrl == null) {
2267 contextMenuUrl = node.getAttribute(CONTEXT_MENU_URL_KEY); 2372 contextMenuUrl = node.getAttribute(CONTEXT_MENU_URL_KEY);
2268 contextMenuItem = node.contextMenuItem; 2373 contextMenuItem = node.contextMenuItem;
2269 if (section != SectionType.UNKNOWN) 2374 if (section != SectionType.UNKNOWN)
2270 break; 2375 break;
2271 } 2376 }
2272 node = node.parentNode; 2377 node = node.parentNode;
2273 } 2378 }
2274 2379
2380 var menuOptions;
2381
2275 if (section == SectionType.BOOKMARKS && 2382 if (section == SectionType.BOOKMARKS &&
2276 !contextMenuItem.folder && !isIncognito) { 2383 !contextMenuItem.folder && !isIncognito) {
2277 var menuOptions = [ 2384 menuOptions = [
2278 [ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB, 2385 [
2279 templateData.elementopeninnewtab], 2386 ContextMenuItemIds.BOOKMARK_OPEN_IN_NEW_TAB,
2280 [ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB, 2387 templateData.elementopeninnewtab
2281 templateData.elementopeninincognitotab]]; 2388 ],
2389 [
2390 ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB,
2391 templateData.elementopeninincognitotab
2392 ]
2393 ];
2282 if (contextMenuItem.editable) { 2394 if (contextMenuItem.editable) {
2283 menuOptions.push( 2395 menuOptions.push(
2284 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.bookmarkedit], 2396 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.bookmarkedit],
2285 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.bookmarkdelete]); 2397 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.bookmarkdelete]);
2286 } 2398 }
2287 if (contextMenuUrl.search('chrome://') == -1 && 2399 if (contextMenuUrl.search('chrome://') == -1 &&
2288 contextMenuUrl.search('about://') == -1) { 2400 contextMenuUrl.search('about://') == -1) {
2289 menuOptions.push( 2401 menuOptions.push([
2290 [ContextMenuItemIds.BOOKMARK_SHORTCUT, 2402 ContextMenuItemIds.BOOKMARK_SHORTCUT,
2291 templateData.bookmarkshortcut]); 2403 templateData.bookmarkshortcut
2404 ]);
2292 } 2405 }
2293 chrome.send('showContextMenu', menuOptions);
2294 } else if (section == SectionType.BOOKMARKS && 2406 } else if (section == SectionType.BOOKMARKS &&
2295 !contextMenuItem.folder && 2407 !contextMenuItem.folder &&
2296 isIncognito) { 2408 isIncognito) {
2297 chrome.send('showContextMenu', [ 2409 menuOptions = [
2298 [ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB, 2410 [
2299 templateData.elementopeninincognitotab] 2411 ContextMenuItemIds.BOOKMARK_OPEN_IN_INCOGNITO_TAB,
2300 ]); 2412 templateData.elementopeninincognitotab
2413 ]
2414 ];
2301 } else if (section == SectionType.BOOKMARKS && 2415 } else if (section == SectionType.BOOKMARKS &&
2302 contextMenuItem.folder && 2416 contextMenuItem.folder &&
2303 contextMenuItem.editable && 2417 contextMenuItem.editable &&
2304 !isIncognito) { 2418 !isIncognito) {
2305 chrome.send('showContextMenu', [ 2419 menuOptions = [
2306 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.editfolder], 2420 [ContextMenuItemIds.BOOKMARK_EDIT, templateData.editfolder],
2307 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.deletefolder], 2421 [ContextMenuItemIds.BOOKMARK_DELETE, templateData.deletefolder]
2308 ]); 2422 ];
2309 } else if (section == SectionType.MOST_VISITED) { 2423 } else if (section == SectionType.MOST_VISITED) {
2310 chrome.send('showContextMenu', [ 2424 menuOptions = [
2311 [ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB, 2425 [
2312 templateData.elementopeninnewtab], 2426 ContextMenuItemIds.MOST_VISITED_OPEN_IN_NEW_TAB,
2313 [ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB, 2427 templateData.elementopeninnewtab
2314 templateData.elementopeninincognitotab], 2428 ],
2315 [ContextMenuItemIds.MOST_VISITED_REMOVE, templateData.elementremove] 2429 [
2316 ]); 2430 ContextMenuItemIds.MOST_VISITED_OPEN_IN_INCOGNITO_TAB,
2431 templateData.elementopeninincognitotab
2432 ],
2433 [ContextMenuItemIds.MOST_VISITED_REMOVE, templateData.elementremove]
2434 ];
2317 } else if (section == SectionType.RECENTLY_CLOSED) { 2435 } else if (section == SectionType.RECENTLY_CLOSED) {
2318 chrome.send('showContextMenu', [ 2436 menuOptions = [
2319 [ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB, 2437 [
2320 templateData.elementopeninnewtab], 2438 ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_NEW_TAB,
2321 [ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB, 2439 templateData.elementopeninnewtab
2322 templateData.elementopeninincognitotab], 2440 ],
2323 [ContextMenuItemIds.RECENTLY_CLOSED_REMOVE, 2441 [
2324 templateData.elementremove] 2442 ContextMenuItemIds.RECENTLY_CLOSED_OPEN_IN_INCOGNITO_TAB,
2325 ]); 2443 templateData.elementopeninincognitotab
2444 ],
2445 [
2446 ContextMenuItemIds.RECENTLY_CLOSED_REMOVE,
2447 templateData.elementremove
2448 ]
2449 ];
2326 } else if (section == SectionType.FOREIGN_SESSION_HEADER) { 2450 } else if (section == SectionType.FOREIGN_SESSION_HEADER) {
2327 chrome.send('showContextMenu', [ 2451 menuOptions = [
2328 [ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE, 2452 [
2329 templateData.elementremove] 2453 ContextMenuItemIds.FOREIGN_SESSIONS_REMOVE,
2330 ]); 2454 templateData.elementremove
2455 ]
2456 ];
2457 } else if (section == SectionType.PROMO_VC_SESSION_HEADER) {
2458 menuOptions = [
2459 [
2460 ContextMenuItemIds.PROMO_VC_SESSION_REMOVE,
2461 templateData.elementremove
2462 ]
2463 ];
2331 } 2464 }
2465
2466 if (menuOptions)
2467 chrome.send('showContextMenu', menuOptions);
2468
2332 return false; 2469 return false;
2333 } 2470 }
2334 2471
2335 // Return an object with all the exports 2472 // Return an object with all the exports
2336 return { 2473 return {
2337 bookmarks: bookmarks, 2474 bookmarks: bookmarks,
2338 bookmarkChanged: bookmarkChanged, 2475 bookmarkChanged: bookmarkChanged,
2339 setForeignSessions: setForeignSessions, 2476 clearPromotions: clearPromotions,
2340 init: init, 2477 init: init,
2341 onCustomMenuSelected: onCustomMenuSelected, 2478 onCustomMenuSelected: onCustomMenuSelected,
2342 openSection: openSection, 2479 openSection: openSection,
2343 setFaviconDominantColor: setFaviconDominantColor, 2480 setFaviconDominantColor: setFaviconDominantColor,
2481 setForeignSessions: setForeignSessions,
2344 setIncognitoMode: setIncognitoMode, 2482 setIncognitoMode: setIncognitoMode,
2345 setMostVisitedPages: setMostVisitedPages, 2483 setMostVisitedPages: setMostVisitedPages,
2346 setPromotions: setPromotions, 2484 setPromotions: setPromotions,
2347 setRecentlyClosedTabs: setRecentlyClosedTabs, 2485 setRecentlyClosedTabs: setRecentlyClosedTabs,
2348 setSyncEnabled: setSyncEnabled, 2486 setSyncEnabled: setSyncEnabled,
2349 snapshots: snapshots 2487 snapshots: snapshots
2350 }; 2488 };
2351 }); 2489 });
2352 2490
2353 ///////////////////////////////////////////////////////////////////////////// 2491 /////////////////////////////////////////////////////////////////////////////
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
2479 // NTP Entry point. 2617 // NTP Entry point.
2480 ///////////////////////////////////////////////////////////////////////////// 2618 /////////////////////////////////////////////////////////////////////////////
2481 2619
2482 /* 2620 /*
2483 * Handles initializing the UI when the page has finished loading. 2621 * Handles initializing the UI when the page has finished loading.
2484 */ 2622 */
2485 window.addEventListener('DOMContentLoaded', function(evt) { 2623 window.addEventListener('DOMContentLoaded', function(evt) {
2486 ntp.init(); 2624 ntp.init();
2487 $('content-area').style.display = 'block'; 2625 $('content-area').style.display = 'block';
2488 }); 2626 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/ntp_android/ntp_android.css ('k') | chrome/browser/ui/webui/ntp/android/promo_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698