| 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 cr.define('ntp4', function() { | 5 cr.define('ntp4', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 var localStrings = new LocalStrings; | 8 var localStrings = new LocalStrings; |
| 9 | 9 |
| 10 var APP_LAUNCH = { | 10 var APP_LAUNCH = { |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 appContents.className = 'app-contents'; | 182 appContents.className = 'app-contents'; |
| 183 | 183 |
| 184 var appImgContainer = this.ownerDocument.createElement('div'); | 184 var appImgContainer = this.ownerDocument.createElement('div'); |
| 185 appImgContainer.className = 'app-img-container'; | 185 appImgContainer.className = 'app-img-container'; |
| 186 this.appImgContainer_ = appImgContainer; | 186 this.appImgContainer_ = appImgContainer; |
| 187 | 187 |
| 188 if (!this.appData_.icon_big_exists && this.appData_.icon_small_exists) | 188 if (!this.appData_.icon_big_exists && this.appData_.icon_small_exists) |
| 189 this.useSmallIcon_ = true; | 189 this.useSmallIcon_ = true; |
| 190 | 190 |
| 191 var appImg = this.ownerDocument.createElement('img'); | 191 var appImg = this.ownerDocument.createElement('img'); |
| 192 // This is temporary (setIcon_/loadIcon will overwrite it) but is visible |
| 193 // before the page is shown (e.g. if switching from most visited to |
| 194 // bookmarks). |
| 195 appImg.src = 'chrome://theme/IDR_APP_DEFAULT_ICON'; |
| 192 this.appImg_ = appImg; | 196 this.appImg_ = appImg; |
| 193 this.setIcon(); | 197 this.setIcon_(); |
| 194 appImgContainer.appendChild(appImg); | 198 appImgContainer.appendChild(appImg); |
| 195 | 199 |
| 196 if (this.useSmallIcon_) { | 200 if (this.useSmallIcon_) { |
| 197 var imgDiv = this.ownerDocument.createElement('div'); | 201 var imgDiv = this.ownerDocument.createElement('div'); |
| 198 imgDiv.className = 'app-icon-div'; | 202 imgDiv.className = 'app-icon-div'; |
| 199 imgDiv.appendChild(appImgContainer); | 203 imgDiv.appendChild(appImgContainer); |
| 200 imgDiv.addEventListener('click', this.onClick_.bind(this)); | 204 imgDiv.addEventListener('click', this.onClick_.bind(this)); |
| 201 this.imgDiv_ = imgDiv; | 205 this.imgDiv_ = imgDiv; |
| 202 appContents.appendChild(imgDiv); | 206 appContents.appendChild(imgDiv); |
| 203 this.appImgContainer_.style.position = 'absolute'; | 207 this.appImgContainer_.style.position = 'absolute'; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 * been uninstalled. | 263 * been uninstalled. |
| 260 */ | 264 */ |
| 261 remove: function() { | 265 remove: function() { |
| 262 // Unset the ID immediately, because the app is already gone. But leave | 266 // Unset the ID immediately, because the app is already gone. But leave |
| 263 // the tile on the page as it animates out. | 267 // the tile on the page as it animates out. |
| 264 this.id = ''; | 268 this.id = ''; |
| 265 this.tile.doRemove(); | 269 this.tile.doRemove(); |
| 266 }, | 270 }, |
| 267 | 271 |
| 268 /** | 272 /** |
| 269 * Set the app's icon image from the appData. | 273 * Set the URL of the icon from |appData_|. This won't actually show the |
| 274 * icon until loadIcon() is called (for performance reasons; we don't want |
| 275 * to load icons until we have to). |
| 270 * @private | 276 * @private |
| 271 */ | 277 */ |
| 272 setIcon: function() { | 278 setIcon_: function() { |
| 273 this.appImg_.src = this.useSmallIcon_ ? this.appData_.icon_small : | 279 var src = this.useSmallIcon_ ? this.appData_.icon_small : |
| 274 this.appData_.icon_big; | 280 this.appData_.icon_big; |
| 275 if (!this.appData_.enabled || | 281 if (!this.appData_.enabled || |
| 276 (!this.appData_.offline_enabled && !navigator.onLine)) { | 282 (!this.appData_.offline_enabled && !navigator.onLine)) { |
| 277 this.appImg_.src += '?grayscale=true'; | 283 src += '?grayscale=true'; |
| 278 } | 284 } |
| 285 |
| 286 this.appImgSrc_ = src; |
| 287 }, |
| 288 |
| 289 /** |
| 290 * Shows the icon for the app. That is, it causes chrome to load the app |
| 291 * icon resource. |
| 292 */ |
| 293 loadIcon: function() { |
| 294 if (this.appImgSrc_) |
| 295 this.appImg_.src = this.appImgSrc_; |
| 296 this.appImgSrc_ = null; |
| 279 }, | 297 }, |
| 280 | 298 |
| 281 // Shows a notification text below the app icon and stuffs the attributes | 299 // Shows a notification text below the app icon and stuffs the attributes |
| 282 // necessary to show the bubble when the user clicks on the notification | 300 // necessary to show the bubble when the user clicks on the notification |
| 283 // text. | 301 // text. |
| 284 setupNotification_: function(notification) { | 302 setupNotification_: function(notification) { |
| 285 // Remove the old notification from this node (if any). | 303 // Remove the old notification from this node (if any). |
| 286 for (var i = 0; i < this.childNodes.length; i++) { | 304 if (this.appNotification_) |
| 287 if (this.childNodes[i].classList.contains('app-notification')) { | 305 this.appNotification_.parentNode.removeChild(this.appNotification_); |
| 288 this.removeChild(this.childNodes[i]); | |
| 289 break; | |
| 290 } | |
| 291 } | |
| 292 | 306 |
| 293 if (notification) { | 307 if (notification) { |
| 294 // Add a new notification to this node. | 308 // Add a new notification to this node. |
| 295 var appNotification = this.ownerDocument.createElement('span'); | 309 var appNotification = this.ownerDocument.createElement('span'); |
| 296 appNotification.className = 'app-notification'; | 310 appNotification.className = 'app-notification'; |
| 297 appNotification.textContent = notification['title']; | 311 appNotification.textContent = notification['title']; |
| 298 appNotification.addEventListener('click', | 312 appNotification.addEventListener('click', |
| 299 this.onNotificationClick_.bind(this)); | 313 this.onNotificationClick_.bind(this)); |
| 300 appNotification.notificationTitle = notification['title']; | 314 appNotification.notificationTitle = notification['title']; |
| 301 appNotification.notificationMessage = notification['body']; | 315 appNotification.notificationMessage = notification['body']; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 else | 460 else |
| 447 this.classList.remove('suppress-active'); | 461 this.classList.remove('suppress-active'); |
| 448 }, | 462 }, |
| 449 | 463 |
| 450 /** | 464 /** |
| 451 * Change the appData and update the appearance of the app. | 465 * Change the appData and update the appearance of the app. |
| 452 * @param {Object} appData The new data object that describes the app. | 466 * @param {Object} appData The new data object that describes the app. |
| 453 */ | 467 */ |
| 454 replaceAppData: function(appData) { | 468 replaceAppData: function(appData) { |
| 455 this.appData_ = appData; | 469 this.appData_ = appData; |
| 456 this.setIcon(); | 470 this.setIcon_(); |
| 471 this.loadIcon(); |
| 457 }, | 472 }, |
| 458 | 473 |
| 459 /** | 474 /** |
| 460 * The data and preferences for this app. | 475 * The data and preferences for this app. |
| 461 * @type {Object} | 476 * @type {Object} |
| 462 */ | 477 */ |
| 463 set appData(data) { | 478 set appData(data) { |
| 464 this.appData_ = data; | 479 this.appData_ = data; |
| 465 }, | 480 }, |
| 466 get appData() { | 481 get appData() { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 el.initialize(); | 556 el.initialize(); |
| 542 | 557 |
| 543 return el; | 558 return el; |
| 544 } | 559 } |
| 545 | 560 |
| 546 AppsPage.prototype = { | 561 AppsPage.prototype = { |
| 547 __proto__: TilePage.prototype, | 562 __proto__: TilePage.prototype, |
| 548 | 563 |
| 549 initialize: function() { | 564 initialize: function() { |
| 550 this.classList.add('apps-page'); | 565 this.classList.add('apps-page'); |
| 566 |
| 567 this.addEventListener('cardselected', this.onCardSelected_); |
| 551 }, | 568 }, |
| 552 | 569 |
| 553 /** | 570 /** |
| 554 * Creates an app DOM element and places it at the last position on the | 571 * Creates an app DOM element and places it at the last position on the |
| 555 * page. | 572 * page. |
| 556 * @param {Object} appData The data object that describes the app. | 573 * @param {Object} appData The data object that describes the app. |
| 557 * @param {?boolean} animate If true, the app tile plays an animation. | 574 * @param {?boolean} animate If true, the app tile plays an animation. |
| 558 */ | 575 */ |
| 559 appendApp: function(appData, animate) { | 576 appendApp: function(appData, animate) { |
| 560 if (animate) { | 577 if (animate) { |
| 561 // Select the page and scroll all the way down so the animation is | 578 // Select the page and scroll all the way down so the animation is |
| 562 // visible. | 579 // visible. |
| 563 ntp4.getCardSlider().selectCardByValue(this); | 580 ntp4.getCardSlider().selectCardByValue(this); |
| 564 this.content_.scrollTop = this.content_.scrollHeight; | 581 this.content_.scrollTop = this.content_.scrollHeight; |
| 565 } | 582 } |
| 566 this.appendTile(new App(appData), animate); | 583 this.appendTile(new App(appData), animate); |
| 567 }, | 584 }, |
| 568 | 585 |
| 586 /** |
| 587 * Handler for 'cardselected' event, fired when |this| is selected. The |
| 588 * first time this is called, we load all the app icons. |
| 589 * @private |
| 590 */ |
| 591 onCardSelected_: function(e) { |
| 592 if (this.hasBeenSelected_) |
| 593 return; |
| 594 this.hasBeenSelected_ = true; |
| 595 |
| 596 var apps = this.querySelectorAll('.app'); |
| 597 for (var i = 0; i < apps.length; i++) { |
| 598 apps[i].loadIcon(); |
| 599 } |
| 600 }, |
| 601 |
| 569 /** @inheritdoc */ | 602 /** @inheritdoc */ |
| 570 doDragOver: function(e) { | 603 doDragOver: function(e) { |
| 571 var tile = ntp4.getCurrentlyDraggingTile(); | 604 var tile = ntp4.getCurrentlyDraggingTile(); |
| 572 if (tile && !tile.querySelector('.app')) { | 605 if (tile && !tile.querySelector('.app')) { |
| 573 e.preventDefault(); | 606 e.preventDefault(); |
| 574 this.setDropEffect(e.dataTransfer); | 607 this.setDropEffect(e.dataTransfer); |
| 575 } else { | 608 } else { |
| 576 TilePage.prototype.doDragOver.call(this, e); | 609 TilePage.prototype.doDragOver.call(this, e); |
| 577 } | 610 } |
| 578 }, | 611 }, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 dataTransfer.dropEffect = 'copy'; | 708 dataTransfer.dropEffect = 'copy'; |
| 676 }, | 709 }, |
| 677 }; | 710 }; |
| 678 | 711 |
| 679 AppsPage.setPromo = function(data) { | 712 AppsPage.setPromo = function(data) { |
| 680 var store = document.querySelector('.webstore'); | 713 var store = document.querySelector('.webstore'); |
| 681 if (store) | 714 if (store) |
| 682 store.setAppsPromoData(data); | 715 store.setAppsPromoData(data); |
| 683 }; | 716 }; |
| 684 | 717 |
| 685 | |
| 686 /** | 718 /** |
| 687 * Callback invoked by chrome whenever an app preference changes. | 719 * Callback invoked by chrome whenever an app preference changes. |
| 688 * @param {Object} data An object with all the data on available | 720 * @param {Object} data An object with all the data on available |
| 689 * applications. | 721 * applications. |
| 690 */ | 722 */ |
| 691 function appsPrefChangeCallback(data) { | 723 function appsPrefChangeCallback(data) { |
| 692 for (var i = 0; i < data.apps.length; ++i) { | 724 for (var i = 0; i < data.apps.length; ++i) { |
| 693 $(data.apps[i].id).appData = data.apps[i]; | 725 $(data.apps[i].id).appData = data.apps[i]; |
| 694 } | 726 } |
| 695 } | 727 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 714 appsPrefChangeCallback: appsPrefChangeCallback, | 746 appsPrefChangeCallback: appsPrefChangeCallback, |
| 715 launchAppAfterEnable: launchAppAfterEnable, | 747 launchAppAfterEnable: launchAppAfterEnable, |
| 716 }; | 748 }; |
| 717 }); | 749 }); |
| 718 | 750 |
| 719 // TODO(estade): update the content handlers to use ntp namespace instead of | 751 // TODO(estade): update the content handlers to use ntp namespace instead of |
| 720 // making these global. | 752 // making these global. |
| 721 var appNotificationChanged = ntp4.appNotificationChanged; | 753 var appNotificationChanged = ntp4.appNotificationChanged; |
| 722 var appsPrefChangeCallback = ntp4.appsPrefChangeCallback; | 754 var appsPrefChangeCallback = ntp4.appsPrefChangeCallback; |
| 723 var launchAppAfterEnable = ntp4.launchAppAfterEnable; | 755 var launchAppAfterEnable = ntp4.launchAppAfterEnable; |
| OLD | NEW |