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 /** | 5 /** |
6 * @fileoverview New tab page | 6 * @fileoverview New tab page |
7 * This is the main code for the new tab page used by touch-enabled Chrome | 7 * This is the main code for the new tab page used by touch-enabled Chrome |
8 * browsers. For now this is still a prototype. | 8 * browsers. For now this is still a prototype. |
9 */ | 9 */ |
10 | 10 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 themeChanged(); | 130 themeChanged(); |
131 | 131 |
132 dotList = getRequiredElement('dot-list'); | 132 dotList = getRequiredElement('dot-list'); |
133 pageList = getRequiredElement('page-list'); | 133 pageList = getRequiredElement('page-list'); |
134 trash = getRequiredElement('trash'); | 134 trash = getRequiredElement('trash'); |
135 new ntp4.Trash(trash); | 135 new ntp4.Trash(trash); |
136 | 136 |
137 shownPage = templateData['shown_page_type']; | 137 shownPage = templateData['shown_page_type']; |
138 shownPageIndex = templateData['shown_page_index']; | 138 shownPageIndex = templateData['shown_page_index']; |
139 | 139 |
140 // When a new app has been installed, we will be opened with a hash value | |
141 // that corresponds to the new app ID. | |
142 highlightAppId = getAndClearAppIDHash(); | |
143 | |
144 // Request data on the apps so we can fill them in. | 140 // Request data on the apps so we can fill them in. |
145 // Note that this is kicked off asynchronously. 'getAppsCallback' will be | 141 // Note that this is kicked off asynchronously. 'getAppsCallback' will be |
146 // invoked at some point after this function returns. | 142 // invoked at some point after this function returns. |
147 chrome.send('getApps'); | 143 chrome.send('getApps'); |
148 | 144 |
149 // Prevent touch events from triggering any sort of native scrolling | 145 // Prevent touch events from triggering any sort of native scrolling |
150 document.addEventListener('touchmove', function(e) { | 146 document.addEventListener('touchmove', function(e) { |
151 e.preventDefault(); | 147 e.preventDefault(); |
152 }, true); | 148 }, true); |
153 | 149 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 * @param {string} id The identifier name. | 223 * @param {string} id The identifier name. |
228 * @return {!Element} the Element. | 224 * @return {!Element} the Element. |
229 */ | 225 */ |
230 function getRequiredElement(id) { | 226 function getRequiredElement(id) { |
231 var element = document.getElementById(id); | 227 var element = document.getElementById(id); |
232 assert(element, 'Missing required element: ' + id); | 228 assert(element, 'Missing required element: ' + id); |
233 return element; | 229 return element; |
234 } | 230 } |
235 | 231 |
236 /** | 232 /** |
237 * Gets the app ID stored in the hash of the URL, and resets the hash to | |
238 * empty. If there is not an app-id in the hash, does nothing. | |
239 * @return {String} The value of the app-id query string parameter. | |
240 */ | |
241 function getAndClearAppIDHash() { | |
242 var hash = location.hash; | |
243 if (hash.indexOf('#app-id=') == 0) { | |
244 var appID = hash.split('=')[1]; | |
245 // Clear the hash so if the user bookmarks this page, they'll just get | |
246 // chrome://newtab/. | |
247 window.history.replaceState({}, '', '/'); | |
248 return appID; | |
249 } | |
250 return ''; | |
251 } | |
252 | |
253 /** | |
254 * Callback invoked by chrome with the apps available. | 233 * Callback invoked by chrome with the apps available. |
255 * | 234 * |
256 * Note that calls to this function can occur at any time, not just in | 235 * Note that calls to this function can occur at any time, not just in |
257 * response to a getApps request. For example, when a user installs/uninstalls | 236 * response to a getApps request. For example, when a user installs/uninstalls |
258 * an app on another synchronized devices. | 237 * an app on another synchronized devices. |
259 * @param {Object} data An object with all the data on available | 238 * @param {Object} data An object with all the data on available |
260 * applications. | 239 * applications. |
261 */ | 240 */ |
262 function getAppsCallback(data) { | 241 function getAppsCallback(data) { |
263 var startTime = Date.now(); | 242 var startTime = Date.now(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 if (appsPages.length < pageNames.length) | 290 if (appsPages.length < pageNames.length) |
312 pageName = pageNames[appsPages.length]; | 291 pageName = pageNames[appsPages.length]; |
313 | 292 |
314 var origPageCount = appsPages.length; | 293 var origPageCount = appsPages.length; |
315 appendAppsPage(new ntp4.AppsPage(), pageName); | 294 appendAppsPage(new ntp4.AppsPage(), pageName); |
316 // Confirm that appsPages is a live object, updated when a new page is | 295 // Confirm that appsPages is a live object, updated when a new page is |
317 // added (otherwise we'd have an infinite loop) | 296 // added (otherwise we'd have an infinite loop) |
318 assert(appsPages.length == origPageCount + 1, 'expected new page'); | 297 assert(appsPages.length == origPageCount + 1, 'expected new page'); |
319 } | 298 } |
320 | 299 |
321 if (app.id == highlightAppId) { | 300 if (app.id == highlightAppId) |
322 highlightApp = app; | 301 highlightApp = app; |
323 highlightAppId = null; | 302 else |
324 } else { | |
325 appsPages[pageIndex].appendApp(app); | 303 appsPages[pageIndex].appendApp(app); |
326 } | |
327 } | 304 } |
328 | 305 |
329 ntp4.AppsPage.setPromo(data.showPromo ? data : null); | 306 ntp4.AppsPage.setPromo(data.showPromo ? data : null); |
330 | 307 |
331 // Tell the slider about the pages. | 308 // Tell the slider about the pages. |
332 updateSliderCards(); | 309 updateSliderCards(); |
333 | 310 |
334 if (highlightApp) | 311 if (highlightApp) |
335 appAdded(highlightApp, true); | 312 appAdded(highlightApp, true); |
336 | 313 |
337 // Mark the current page. | 314 // Mark the current page. |
338 cardSlider.currentCardValue.navigationDot.classList.add('selected'); | 315 cardSlider.currentCardValue.navigationDot.classList.add('selected'); |
339 logEvent('apps.layout: ' + (Date.now() - startTime)); | 316 logEvent('apps.layout: ' + (Date.now() - startTime)); |
340 | 317 |
341 document.documentElement.classList.remove('starting-up'); | 318 document.documentElement.classList.remove('starting-up'); |
342 } | 319 } |
343 | 320 |
344 /** | 321 /** |
345 * Called by chrome when a new app has been added to chrome. | 322 * Called by chrome when a new app has been added to chrome. |
346 * @param {Object} app A data structure full of relevant information for the | 323 * @param {Object} app A data structure full of relevant information for the |
347 * app. | 324 * app. |
348 */ | 325 */ |
349 function appAdded(app, opt_highlight) { | 326 function appAdded(app, opt_highlight) { |
350 // If the page is already open when a new app is installed, the hash will | 327 if (app.id == highlightAppId) { |
351 // be set once again. | |
352 var appID = getAndClearAppIDHash(); | |
353 if (appID == app.id) | |
354 opt_highlight = true; | 328 opt_highlight = true; |
| 329 highlightAppId = null; |
| 330 } |
355 | 331 |
356 var pageIndex = app.page_index || 0; | 332 var pageIndex = app.page_index || 0; |
357 | 333 |
358 if (pageIndex >= appsPages.length) { | 334 if (pageIndex >= appsPages.length) { |
359 while (pageIndex >= appsPages.length) { | 335 while (pageIndex >= appsPages.length) { |
360 appendAppsPage(new ntp4.AppsPage(), ''); | 336 appendAppsPage(new ntp4.AppsPage(), ''); |
361 } | 337 } |
362 updateSliderCards(); | 338 updateSliderCards(); |
363 } | 339 } |
364 | 340 |
365 var page = appsPages[pageIndex]; | 341 var page = appsPages[pageIndex]; |
366 if (opt_highlight) | 342 page.appendApp(app, opt_highlight); |
367 cardSlider.selectCardByValue(page); | |
368 page.appendApp(app, true); | |
369 } | 343 } |
370 | 344 |
371 /** | 345 /** |
| 346 * Sets that an app should be highlighted if it is added. Called right before |
| 347 * appAdded for new installs. |
| 348 */ |
| 349 function setAppToBeHighlighted(appId) { |
| 350 highlightAppId = appId; |
| 351 } |
| 352 |
| 353 /** |
372 * Called by chrome when an existing app has been removed/uninstalled from | 354 * Called by chrome when an existing app has been removed/uninstalled from |
373 * chrome. | 355 * chrome. |
374 * @param {Object} appData A data structure full of relevant information for | 356 * @param {Object} appData A data structure full of relevant information for |
375 * the app. | 357 * the app. |
376 */ | 358 */ |
377 function appRemoved(appData) { | 359 function appRemoved(appData) { |
378 var app = $(appData.id); | 360 var app = $(appData.id); |
379 assert(app, 'trying to remove an app that doesn\'t exist'); | 361 assert(app, 'trying to remove an app that doesn\'t exist'); |
380 | 362 |
381 var tile = findAncestorByClass(app, 'tile'); | 363 var tile = findAncestorByClass(app, 'tile'); |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 appsPrefChangeCallback: appsPrefChangeCallback, | 750 appsPrefChangeCallback: appsPrefChangeCallback, |
769 assert: assert, | 751 assert: assert, |
770 enterRearrangeMode: enterRearrangeMode, | 752 enterRearrangeMode: enterRearrangeMode, |
771 getAppsCallback: getAppsCallback, | 753 getAppsCallback: getAppsCallback, |
772 getAppsPageIndex: getAppsPageIndex, | 754 getAppsPageIndex: getAppsPageIndex, |
773 getCardSlider: getCardSlider, | 755 getCardSlider: getCardSlider, |
774 initialize: initialize, | 756 initialize: initialize, |
775 isRTL: isRTL, | 757 isRTL: isRTL, |
776 leaveRearrangeMode: leaveRearrangeMode, | 758 leaveRearrangeMode: leaveRearrangeMode, |
777 saveAppPageName: saveAppPageName, | 759 saveAppPageName: saveAppPageName, |
| 760 setAppToBeHighlighted: setAppToBeHighlighted, |
778 setBookmarksData: setBookmarksData, | 761 setBookmarksData: setBookmarksData, |
779 setMostVisitedPages: setMostVisitedPages, | 762 setMostVisitedPages: setMostVisitedPages, |
780 setRecentlyClosedTabs: setRecentlyClosedTabs, | 763 setRecentlyClosedTabs: setRecentlyClosedTabs, |
781 showNotification: showNotification, | 764 showNotification: showNotification, |
782 themeChanged: themeChanged | 765 themeChanged: themeChanged |
783 }; | 766 }; |
784 }); | 767 }); |
785 | 768 |
786 // publish ntp globals | 769 // publish ntp globals |
787 // TODO(estade): update the content handlers to use ntp namespace instead of | 770 // TODO(estade): update the content handlers to use ntp namespace instead of |
788 // making these global. | 771 // making these global. |
789 var assert = ntp4.assert; | 772 var assert = ntp4.assert; |
790 var getAppsCallback = ntp4.getAppsCallback; | 773 var getAppsCallback = ntp4.getAppsCallback; |
791 var appsPrefChangeCallback = ntp4.appsPrefChangeCallback; | 774 var appsPrefChangeCallback = ntp4.appsPrefChangeCallback; |
792 var themeChanged = ntp4.themeChanged; | 775 var themeChanged = ntp4.themeChanged; |
793 var recentlyClosedTabs = ntp4.setRecentlyClosedTabs; | 776 var recentlyClosedTabs = ntp4.setRecentlyClosedTabs; |
794 var setMostVisitedPages = ntp4.setMostVisitedPages; | 777 var setMostVisitedPages = ntp4.setMostVisitedPages; |
795 | 778 |
796 document.addEventListener('DOMContentLoaded', ntp4.initialize); | 779 document.addEventListener('DOMContentLoaded', ntp4.initialize); |
OLD | NEW |