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

Side by Side Diff: chrome/browser/resources/ntp4/page_list_view.js

Issue 10141005: switch ntp to jstemplate v2 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test once and for all Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 PageListView implementation. 6 * @fileoverview PageListView implementation.
7 * PageListView manages page list, dot list, switcher buttons and handles apps 7 * PageListView manages page list, dot list, switcher buttons and handles apps
8 * pages callbacks from backend. 8 * pages callbacks from backend.
9 * 9 *
10 * Note that you need to have AppLauncherHandler in your WebUI to use this code. 10 * Note that you need to have AppLauncherHandler in your WebUI to use this code.
11 */ 11 */
12 12
13 cr.define('ntp', function() { 13 cr.define('ntp', function() {
14 'use strict'; 14 'use strict';
15 15
16 /** 16 /**
17 * Object for accessing localized strings.
18 * @type {!LocalStrings}
19 */
20 var localStrings = new LocalStrings;
21
22 /**
23 * Creates a PageListView object. 17 * Creates a PageListView object.
24 * @constructor 18 * @constructor
25 * @extends {Object} 19 * @extends {Object}
26 */ 20 */
27 function PageListView() { 21 function PageListView() {
28 } 22 }
29 23
30 PageListView.prototype = { 24 PageListView.prototype = {
31 /** 25 /**
32 * The CardSlider object to use for changing app pages. 26 * The CardSlider object to use for changing app pages.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 new ntp.Trash(this.trash); 137 new ntp.Trash(this.trash);
144 138
145 this.pageSwitcherStart = opt_pageSwitcherStart; 139 this.pageSwitcherStart = opt_pageSwitcherStart;
146 if (this.pageSwitcherStart) 140 if (this.pageSwitcherStart)
147 ntp.initializePageSwitcher(this.pageSwitcherStart); 141 ntp.initializePageSwitcher(this.pageSwitcherStart);
148 142
149 this.pageSwitcherEnd = opt_pageSwitcherEnd; 143 this.pageSwitcherEnd = opt_pageSwitcherEnd;
150 if (this.pageSwitcherEnd) 144 if (this.pageSwitcherEnd)
151 ntp.initializePageSwitcher(this.pageSwitcherEnd); 145 ntp.initializePageSwitcher(this.pageSwitcherEnd);
152 146
153 this.shownPage = templateData.shown_page_type; 147 this.shownPage = loadTimeData.getInteger('shown_page_type');
154 this.shownPageIndex = templateData.shown_page_index; 148 this.shownPageIndex = loadTimeData.getInteger('shown_page_index');
155 149
156 if (templateData.showApps) { 150 if (loadTimeData.getBoolean('showApps')) {
157 // Request data on the apps so we can fill them in. 151 // Request data on the apps so we can fill them in.
158 // Note that this is kicked off asynchronously. 'getAppsCallback' will 152 // Note that this is kicked off asynchronously. 'getAppsCallback' will
159 // be invoked at some point after this function returns. 153 // be invoked at some point after this function returns.
160 chrome.send('getApps'); 154 chrome.send('getApps');
161 } else { 155 } else {
162 // No apps page. 156 // No apps page.
163 if (this.shownPage == templateData['apps_page_id']) { 157 if (this.shownPage == loadTimeData.getInteger('apps_page_id')) {
164 this.shownPage = templateData['most_visited_page_id']; 158 this.shownPage = loadTimeData.getInteger('most_visited_page_id');
165 this.shownPageIndex = 0; 159 this.shownPageIndex = 0;
166 } 160 }
167 161
168 document.body.classList.add('bare-minimum'); 162 document.body.classList.add('bare-minimum');
169 } 163 }
170 164
171 document.addEventListener('keydown', this.onDocKeyDown_.bind(this)); 165 document.addEventListener('keydown', this.onDocKeyDown_.bind(this));
172 // Prevent touch events from triggering any sort of native scrolling. 166 // Prevent touch events from triggering any sort of native scrolling.
173 document.addEventListener('touchmove', function(e) { 167 document.addEventListener('touchmove', function(e) {
174 e.preventDefault(); 168 e.preventDefault();
(...skipping 13 matching lines...) Expand all
188 // because it needs to be called before the card slider's handler. 182 // because it needs to be called before the card slider's handler.
189 var cardSlider = this.cardSlider; 183 var cardSlider = this.cardSlider;
190 cardSliderFrame.addEventListener('mousewheel', function(e) { 184 cardSliderFrame.addEventListener('mousewheel', function(e) {
191 if (cardSlider.currentCardValue.handleMouseWheel(e)) { 185 if (cardSlider.currentCardValue.handleMouseWheel(e)) {
192 e.preventDefault(); // Prevent default scroll behavior. 186 e.preventDefault(); // Prevent default scroll behavior.
193 e.stopImmediatePropagation(); // Prevent horizontal card flipping. 187 e.stopImmediatePropagation(); // Prevent horizontal card flipping.
194 } 188 }
195 }); 189 });
196 190
197 this.cardSlider.initialize( 191 this.cardSlider.initialize(
198 templateData.isSwipeTrackingFromScrollEventsEnabled); 192 loadTimeData.getBoolean('isSwipeTrackingFromScrollEventsEnabled'));
199 193
200 // Handle events from the card slider. 194 // Handle events from the card slider.
201 this.pageList.addEventListener('cardSlider:card_changed', 195 this.pageList.addEventListener('cardSlider:card_changed',
202 this.onCardChanged_.bind(this)); 196 this.onCardChanged_.bind(this));
203 this.pageList.addEventListener('cardSlider:card_added', 197 this.pageList.addEventListener('cardSlider:card_added',
204 this.onCardAdded_.bind(this)); 198 this.onCardAdded_.bind(this));
205 this.pageList.addEventListener('cardSlider:card_removed', 199 this.pageList.addEventListener('cardSlider:card_removed',
206 this.onCardRemoved_.bind(this)); 200 this.onCardRemoved_.bind(this));
207 201
208 // Ensure the slider is resized appropriately with the window. 202 // Ensure the slider is resized appropriately with the window.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 255
262 this.eventTracker.add(page, 'pagelayout', this.onPageLayout_.bind(this)); 256 this.eventTracker.add(page, 'pagelayout', this.onPageLayout_.bind(this));
263 }, 257 },
264 258
265 /** 259 /**
266 * Called by chrome when an app has changed positions. 260 * Called by chrome when an app has changed positions.
267 * @param {Object} appData The data for the app. This contains page and 261 * @param {Object} appData The data for the app. This contains page and
268 * position indices. 262 * position indices.
269 */ 263 */
270 appMoved: function(appData) { 264 appMoved: function(appData) {
271 assert(templateData.showApps); 265 assert(loadTimeData.getBoolean('showApps'));
272 266
273 var app = $(appData.id); 267 var app = $(appData.id);
274 assert(app, 'trying to move an app that doesn\'t exist'); 268 assert(app, 'trying to move an app that doesn\'t exist');
275 app.remove(false); 269 app.remove(false);
276 270
277 this.appsPages[appData.page_index].insertApp(appData); 271 this.appsPages[appData.page_index].insertApp(appData);
278 }, 272 },
279 273
280 /** 274 /**
281 * Called by chrome when an existing app has been disabled or 275 * Called by chrome when an existing app has been disabled or
282 * removed/uninstalled from chrome. 276 * removed/uninstalled from chrome.
283 * @param {Object} appData A data structure full of relevant information for 277 * @param {Object} appData A data structure full of relevant information for
284 * the app. 278 * the app.
285 * @param {boolean} isUninstall True if the app is being uninstalled; 279 * @param {boolean} isUninstall True if the app is being uninstalled;
286 * false if the app is being disabled. 280 * false if the app is being disabled.
287 * @param {boolean} fromPage True if the removal was from the current page. 281 * @param {boolean} fromPage True if the removal was from the current page.
288 */ 282 */
289 appRemoved: function(appData, isUninstall, fromPage) { 283 appRemoved: function(appData, isUninstall, fromPage) {
290 assert(templateData.showApps); 284 assert(loadTimeData.getBoolean('showApps'));
291 285
292 var app = $(appData.id); 286 var app = $(appData.id);
293 assert(app, 'trying to remove an app that doesn\'t exist'); 287 assert(app, 'trying to remove an app that doesn\'t exist');
294 288
295 if (!isUninstall) 289 if (!isUninstall)
296 app.replaceAppData(appData); 290 app.replaceAppData(appData);
297 else 291 else
298 app.remove(!!fromPage); 292 app.remove(!!fromPage);
299 }, 293 },
300 294
(...skipping 15 matching lines...) Expand all
316 /** 310 /**
317 * Callback invoked by chrome with the apps available. 311 * Callback invoked by chrome with the apps available.
318 * 312 *
319 * Note that calls to this function can occur at any time, not just in 313 * Note that calls to this function can occur at any time, not just in
320 * response to a getApps request. For example, when a user 314 * response to a getApps request. For example, when a user
321 * installs/uninstalls an app on another synchronized devices. 315 * installs/uninstalls an app on another synchronized devices.
322 * @param {Object} data An object with all the data on available 316 * @param {Object} data An object with all the data on available
323 * applications. 317 * applications.
324 */ 318 */
325 getAppsCallback: function(data) { 319 getAppsCallback: function(data) {
326 assert(templateData.showApps); 320 assert(loadTimeData.getBoolean('showApps'));
327 321
328 var startTime = Date.now(); 322 var startTime = Date.now();
329 323
330 // Remember this to select the correct card when done rebuilding. 324 // Remember this to select the correct card when done rebuilding.
331 var prevCurrentCard = this.cardSlider.currentCard; 325 var prevCurrentCard = this.cardSlider.currentCard;
332 326
333 // Make removal of pages and dots as quick as possible with less DOM 327 // Make removal of pages and dots as quick as possible with less DOM
334 // operations, reflows, or repaints. We set currentCard = 0 and remove 328 // operations, reflows, or repaints. We set currentCard = 0 and remove
335 // from the end to not encounter any auto-magic card selections in the 329 // from the end to not encounter any auto-magic card selections in the
336 // process and we hide the card slider throughout. 330 // process and we hide the card slider throughout.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 var lastAppsPageIndex = (lastAppsPage != null) ? 367 var lastAppsPageIndex = (lastAppsPage != null) ?
374 Array.prototype.indexOf.call(this.tilePages, lastAppsPage) : -1; 368 Array.prototype.indexOf.call(this.tilePages, lastAppsPage) : -1;
375 var nextPageAfterApps = lastAppsPageIndex != -1 ? 369 var nextPageAfterApps = lastAppsPageIndex != -1 ?
376 this.tilePages[lastAppsPageIndex + 1] : null; 370 this.tilePages[lastAppsPageIndex + 1] : null;
377 371
378 // Add the apps, creating pages as necessary 372 // Add the apps, creating pages as necessary
379 for (var i = 0; i < apps.length; i++) { 373 for (var i = 0; i < apps.length; i++) {
380 var app = apps[i]; 374 var app = apps[i];
381 var pageIndex = app.page_index || 0; 375 var pageIndex = app.page_index || 0;
382 while (pageIndex >= this.appsPages.length) { 376 while (pageIndex >= this.appsPages.length) {
383 var pageName = localStrings.getString('appDefaultPageName'); 377 var pageName = loadTimeData.getString('appDefaultPageName');
384 if (this.appsPages.length < pageNames.length) 378 if (this.appsPages.length < pageNames.length)
385 pageName = pageNames[this.appsPages.length]; 379 pageName = pageNames[this.appsPages.length];
386 380
387 var origPageCount = this.appsPages.length; 381 var origPageCount = this.appsPages.length;
388 this.appendTilePage(new ntp.AppsPage(), pageName, true, 382 this.appendTilePage(new ntp.AppsPage(), pageName, true,
389 nextPageAfterApps); 383 nextPageAfterApps);
390 // Confirm that appsPages is a live object, updated when a new page is 384 // Confirm that appsPages is a live object, updated when a new page is
391 // added (otherwise we'd have an infinite loop) 385 // added (otherwise we'd have an infinite loop)
392 assert(this.appsPages.length == origPageCount + 1, 386 assert(this.appsPages.length == origPageCount + 1,
393 'expected new page'); 387 'expected new page');
(...skipping 24 matching lines...) Expand all
418 } 412 }
419 }, 413 },
420 414
421 /** 415 /**
422 * Called by chrome when a new app has been added to chrome or has been 416 * Called by chrome when a new app has been added to chrome or has been
423 * enabled if previously disabled. 417 * enabled if previously disabled.
424 * @param {Object} appData A data structure full of relevant information for 418 * @param {Object} appData A data structure full of relevant information for
425 * the app. 419 * the app.
426 */ 420 */
427 appAdded: function(appData, opt_highlight) { 421 appAdded: function(appData, opt_highlight) {
428 assert(templateData.showApps); 422 assert(loadTimeData.getBoolean('showApps'));
429 423
430 if (appData.id == this.highlightAppId) { 424 if (appData.id == this.highlightAppId) {
431 opt_highlight = true; 425 opt_highlight = true;
432 this.highlightAppId = null; 426 this.highlightAppId = null;
433 } 427 }
434 428
435 var pageIndex = appData.page_index || 0; 429 var pageIndex = appData.page_index || 0;
436 430
437 if (pageIndex >= this.appsPages.length) { 431 if (pageIndex >= this.appsPages.length) {
438 while (pageIndex >= this.appsPages.length) { 432 while (pageIndex >= this.appsPages.length) {
439 this.appendTilePage(new ntp.AppsPage(), 433 this.appendTilePage(new ntp.AppsPage(),
440 localStrings.getString('appDefaultPageName'), 434 loadTimeData.getString('appDefaultPageName'),
441 true); 435 true);
442 } 436 }
443 this.updateSliderCards(); 437 this.updateSliderCards();
444 } 438 }
445 439
446 var page = this.appsPages[pageIndex]; 440 var page = this.appsPages[pageIndex];
447 var app = $(appData.id); 441 var app = $(appData.id);
448 if (app) 442 if (app)
449 app.replaceAppData(appData); 443 app.replaceAppData(appData);
450 else 444 else
451 page.appendApp(appData, opt_highlight); 445 page.appendApp(appData, opt_highlight);
452 }, 446 },
453 447
454 /** 448 /**
455 * Callback invoked by chrome whenever an app preference changes. 449 * Callback invoked by chrome whenever an app preference changes.
456 * @param {Object} data An object with all the data on available 450 * @param {Object} data An object with all the data on available
457 * applications. 451 * applications.
458 */ 452 */
459 appsPrefChangedCallback: function(data) { 453 appsPrefChangedCallback: function(data) {
460 assert(templateData.showApps); 454 assert(loadTimeData.getBoolean('showApps'));
461 455
462 for (var i = 0; i < data.apps.length; ++i) { 456 for (var i = 0; i < data.apps.length; ++i) {
463 $(data.apps[i].id).appData = data.apps[i]; 457 $(data.apps[i].id).appData = data.apps[i];
464 } 458 }
465 459
466 // Set the App dot names. Skip the first dot (Most Visited). 460 // Set the App dot names. Skip the first dot (Most Visited).
467 var dots = this.dotList.getElementsByClassName('dot'); 461 var dots = this.dotList.getElementsByClassName('dot');
468 var start = this.mostVisitedPage ? 1 : 0; 462 var start = this.mostVisitedPage ? 1 : 0;
469 for (var i = start; i < dots.length; ++i) { 463 for (var i = start; i < dots.length; ++i) {
470 dots[i].displayTitle = data.appPageNames[i - start] || ''; 464 dots[i].displayTitle = data.appPageNames[i - start] || '';
471 } 465 }
472 }, 466 },
473 467
474 /** 468 /**
475 * Invoked whenever the pages in apps-page-list have changed so that 469 * Invoked whenever the pages in apps-page-list have changed so that
476 * the Slider knows about the new elements. 470 * the Slider knows about the new elements.
477 */ 471 */
478 updateSliderCards: function() { 472 updateSliderCards: function() {
479 var pageNo = Math.max(0, Math.min(this.cardSlider.currentCard, 473 var pageNo = Math.max(0, Math.min(this.cardSlider.currentCard,
480 this.tilePages.length - 1)); 474 this.tilePages.length - 1));
481 this.cardSlider.setCards(Array.prototype.slice.call(this.tilePages), 475 this.cardSlider.setCards(Array.prototype.slice.call(this.tilePages),
482 pageNo); 476 pageNo);
483 switch (this.shownPage) { 477 switch (this.shownPage) {
484 case templateData['apps_page_id']: 478 case loadTimeData.getInteger('apps_page_id'):
485 this.cardSlider.selectCardByValue( 479 this.cardSlider.selectCardByValue(
486 this.appsPages[Math.min(this.shownPageIndex, 480 this.appsPages[Math.min(this.shownPageIndex,
487 this.appsPages.length - 1)]); 481 this.appsPages.length - 1)]);
488 break; 482 break;
489 case templateData['most_visited_page_id']: 483 case loadTimeData.getInteger('most_visited_page_id'):
490 if (this.mostVisitedPage) 484 if (this.mostVisitedPage)
491 this.cardSlider.selectCardByValue(this.mostVisitedPage); 485 this.cardSlider.selectCardByValue(this.mostVisitedPage);
492 break; 486 break;
493 case templateData['suggestions_page_id']: 487 case loadTimeData.getInteger('suggestions_page_id'):
494 if (this.suggestionsPage) 488 if (this.suggestionsPage)
495 this.cardSlider.selectCardByValue(this.suggestionsPage); 489 this.cardSlider.selectCardByValue(this.suggestionsPage);
496 break; 490 break;
497 } 491 }
498 }, 492 },
499 493
500 /** 494 /**
501 * Called whenever tiles should be re-arranging themselves out of the way 495 * Called whenever tiles should be re-arranging themselves out of the way
502 * of a moving or insert tile. 496 * of a moving or insert tile.
503 */ 497 */
504 enterRearrangeMode: function() { 498 enterRearrangeMode: function() {
505 if (templateData.showApps) { 499 if (loadTimeData.getBoolean('showApps')) {
506 var tempPage = new ntp.AppsPage(); 500 var tempPage = new ntp.AppsPage();
507 tempPage.classList.add('temporary'); 501 tempPage.classList.add('temporary');
508 var pageName = localStrings.getString('appDefaultPageName'); 502 var pageName = loadTimeData.getString('appDefaultPageName');
509 this.appendTilePage(tempPage, pageName, true); 503 this.appendTilePage(tempPage, pageName, true);
510 } 504 }
511 505
512 if (ntp.getCurrentlyDraggingTile().firstChild.canBeRemoved()) 506 if (ntp.getCurrentlyDraggingTile().firstChild.canBeRemoved())
513 $('footer').classList.add('showing-trash-mode'); 507 $('footer').classList.add('showing-trash-mode');
514 508
515 document.documentElement.classList.add('dragging-mode'); 509 document.documentElement.classList.add('dragging-mode');
516 }, 510 },
517 511
518 /** 512 /**
519 * Invoked whenever some app is released 513 * Invoked whenever some app is released
520 */ 514 */
521 leaveRearrangeMode: function() { 515 leaveRearrangeMode: function() {
522 var tempPage = document.querySelector('.tile-page.temporary'); 516 var tempPage = document.querySelector('.tile-page.temporary');
523 if (tempPage) { 517 if (tempPage) {
524 var dot = tempPage.navigationDot; 518 var dot = tempPage.navigationDot;
525 if (!tempPage.tileCount && 519 if (!tempPage.tileCount &&
526 tempPage != this.cardSlider.currentCardValue) { 520 tempPage != this.cardSlider.currentCardValue) {
527 this.removeTilePageAndDot_(tempPage, true); 521 this.removeTilePageAndDot_(tempPage, true);
528 } else { 522 } else {
529 tempPage.classList.remove('temporary'); 523 tempPage.classList.remove('temporary');
530 this.saveAppPageName(tempPage, 524 this.saveAppPageName(tempPage,
531 localStrings.getString('appDefaultPageName')); 525 loadTimeData.getString('appDefaultPageName'));
532 } 526 }
533 } 527 }
534 528
535 $('footer').classList.remove('showing-trash-mode'); 529 $('footer').classList.remove('showing-trash-mode');
536 document.documentElement.classList.remove('dragging-mode'); 530 document.documentElement.classList.remove('dragging-mode');
537 }, 531 },
538 532
539 /** 533 /**
540 * Callback for the 'pagelayout' event. 534 * Callback for the 'pagelayout' event.
541 * @param {Event} e The event. 535 * @param {Event} e The event.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 * @param {Event} e The cardSlider:card_changed event. 595 * @param {Event} e The cardSlider:card_changed event.
602 * @private 596 * @private
603 */ 597 */
604 onCardChanged_: function(e) { 598 onCardChanged_: function(e) {
605 var page = e.cardSlider.currentCardValue; 599 var page = e.cardSlider.currentCardValue;
606 600
607 // Don't change shownPage until startup is done (and page changes actually 601 // Don't change shownPage until startup is done (and page changes actually
608 // reflect user actions). 602 // reflect user actions).
609 if (!this.isStartingUp_()) { 603 if (!this.isStartingUp_()) {
610 if (page.classList.contains('apps-page')) { 604 if (page.classList.contains('apps-page')) {
611 this.shownPage = templateData.apps_page_id; 605 this.shownPage = loadTimeData.getInteger('apps_page_id');
612 this.shownPageIndex = this.getAppsPageIndex(page); 606 this.shownPageIndex = this.getAppsPageIndex(page);
613 } else if (page.classList.contains('most-visited-page')) { 607 } else if (page.classList.contains('most-visited-page')) {
614 this.shownPage = templateData.most_visited_page_id; 608 this.shownPage = loadTimeData.getInteger('most_visited_page_id');
615 this.shownPageIndex = 0; 609 this.shownPageIndex = 0;
616 } else if (page.classList.contains('suggestions-page')) { 610 } else if (page.classList.contains('suggestions-page')) {
617 this.shownPage = templateData.suggestions_page_id; 611 this.shownPage = loadTimeData.getInteger('suggestions_page_id');
618 this.shownPageIndex = 0; 612 this.shownPageIndex = 0;
619 } else { 613 } else {
620 console.error('unknown page selected'); 614 console.error('unknown page selected');
621 } 615 }
622 chrome.send('pageSelected', [this.shownPage, this.shownPageIndex]); 616 chrome.send('pageSelected', [this.shownPage, this.shownPageIndex]);
623 } 617 }
624 618
625 // Update the active dot 619 // Update the active dot
626 var curDot = this.dotList.getElementsByClassName('selected')[0]; 620 var curDot = this.dotList.getElementsByClassName('selected')[0];
627 if (curDot) 621 if (curDot)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 if (page.navigationDot) 729 if (page.navigationDot)
736 page.navigationDot.remove(opt_animate); 730 page.navigationDot.remove(opt_animate);
737 this.cardSlider.removeCard(page); 731 this.cardSlider.removeCard(page);
738 }, 732 },
739 }; 733 };
740 734
741 return { 735 return {
742 PageListView: PageListView 736 PageListView: PageListView
743 }; 737 };
744 }); 738 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/ntp4/other_sessions.js ('k') | chrome/browser/resources/ntp4/recently_closed.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698