Commit 2c37fcdf authored by Yue Cen's avatar Yue Cen Committed by Commit Bot

Fast app reinstall: Disable install button when no app is selected

Bug: 905125
Change-Id: I76b2e9279ffadcec025a7cb730da7275de0eb367
Reviewed-on: https://chromium-review.googlesource.com/c/1335030Reviewed-by: default avatarAlexander Alekseev <alemate@chromium.org>
Commit-Queue: Yue Cen <rsgingerrs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608147}
parent 830ebc99
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
let appWindow;
let appOrigin;
function generateContents(appIcon, appTitle, appPackageName) { function generateContents(appIcon, appTitle, appPackageName) {
const doc = document; const doc = document;
const recommendAppsContainer = doc.getElementById('recommend-apps-container'); const recommendAppsContainer = doc.getElementById('recommend-apps-container');
...@@ -97,6 +100,8 @@ function removeRippleCircle_(e) { ...@@ -97,6 +100,8 @@ function removeRippleCircle_(e) {
function toggleCheckStatus_(e) { function toggleCheckStatus_(e) {
const item = e.currentTarget.parentNode; const item = e.currentTarget.parentNode;
item.classList.toggle('checked'); item.classList.toggle('checked');
sendNumberOfSelectedApps();
} }
function getSelectedPackages() { function getSelectedPackages() {
...@@ -139,3 +144,22 @@ function isConfirmKey_(e) { ...@@ -139,3 +144,22 @@ function isConfirmKey_(e) {
return e.keyCode === 13 // Enter return e.keyCode === 13 // Enter
|| e.keyCode === 32; // Space || e.keyCode === 32; // Space
} }
/**
* Send the number of selected apps back to the embedding page.
*/
function sendNumberOfSelectedApps() {
if (appWindow && appOrigin) {
const checkedItems = document.querySelectorAll('.checked');
appWindow.postMessage(
{type: 'NUM_OF_SELECTED_APPS', numOfSelected: checkedItems.length},
appOrigin);
}
}
function onMessage_(e) {
appWindow = e.source;
appOrigin = e.origin;
}
window.addEventListener('message', onMessage_);
...@@ -59,6 +59,7 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() { ...@@ -59,6 +59,7 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() {
*/ */
ensureInitialized_: function() { ensureInitialized_: function() {
$('recommend-apps-screen').screen = this; $('recommend-apps-screen').screen = this;
window.addEventListener('message', this.onMessage);
}, },
/** /**
...@@ -77,7 +78,7 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() { ...@@ -77,7 +78,7 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() {
}, },
setWebview: function(contents) { setWebview: function(contents) {
var appListView = this.getElement_('app-list-view'); const appListView = this.getElement_('app-list-view');
appListView.src = appListView.src =
'data:text/html;charset=utf-8,' + encodeURIComponent(contents); 'data:text/html;charset=utf-8,' + encodeURIComponent(contents);
}, },
...@@ -91,21 +92,26 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() { ...@@ -91,21 +92,26 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() {
// Hide the loading throbber and show the recommend app list. // Hide the loading throbber and show the recommend app list.
this.setThrobberVisible(false); this.setThrobberVisible(false);
var appListView = this.getElement_('app-list-view'); const appListView = this.getElement_('app-list-view');
var subtitle = this.getElement_('subtitle'); const subtitle = this.getElement_('subtitle');
subtitle.innerText = loadTimeData.getStringF( subtitle.innerText = loadTimeData.getStringF(
'recommendAppsScreenDescription', appList.length); 'recommendAppsScreenDescription', appList.length);
appListView.addEventListener('contentload', () => { appListView.addEventListener('contentload', () => {
appListView.contentWindow.postMessage('initialMessage', '*');
appListView.executeScript({file: 'recommend_app_list_view.js'}, () => { appListView.executeScript({file: 'recommend_app_list_view.js'}, () => {
appList.forEach(function(app, index) { appList.forEach(function(app, index) {
var generateItemScript = 'generateContents("' + app.icon + '", "' + let generateItemScript = 'generateContents("' + app.icon + '", "' +
app.name + '", "' + app.package_name + '");'; app.name + '", "' + app.package_name + '");';
var generateContents = {code: generateItemScript}; const generateContents = {code: generateItemScript};
appListView.executeScript(generateContents); appListView.executeScript(generateContents);
}); });
var addScrollShadowEffectScript = 'addScrollShadowEffect();'; const addScrollShadowEffectScript = 'addScrollShadowEffect();';
appListView.executeScript({code: addScrollShadowEffectScript}); appListView.executeScript({code: addScrollShadowEffectScript});
const getNumOfSelectedAppsScript = 'sendNumberOfSelectedApps();';
appListView.executeScript({code: getNumOfSelectedAppsScript});
this.onGenerateContents(); this.onGenerateContents();
}); });
}); });
...@@ -132,12 +138,15 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() { ...@@ -132,12 +138,15 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() {
* Handles Install button click. * Handles Install button click.
*/ */
onInstall: function() { onInstall: function() {
var appListView = this.getElement_('app-list-view'); // Only start installation if the button is not disabled.
appListView.executeScript( if (!this.getElement_('recommend-apps-install-button').disabled) {
{code: 'getSelectedPackages();'}, function(result) { const appListView = this.getElement_('app-list-view');
console.log(result[0]); appListView.executeScript(
chrome.send('recommendAppsInstall', result[0]); {code: 'getSelectedPackages();'}, function(result) {
}); console.log(result[0]);
chrome.send('recommendAppsInstall', result[0]);
});
}
}, },
/** /**
...@@ -152,6 +161,18 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() { ...@@ -152,6 +161,18 @@ login.createScreen('RecommendAppsScreen', 'recommend-apps', function() {
chrome.send('recommendAppsRetry'); chrome.send('recommendAppsRetry');
}, },
/**
* Handles the message sent from the WebView.
*/
onMessage: function(event) {
if (event.data.type && (event.data.type === 'NUM_OF_SELECTED_APPS')) {
const numOfSelected = event.data.numOfSelected;
$('recommend-apps-screen')
.getElement('recommend-apps-install-button')
.disabled = (numOfSelected === 0);
}
},
/** /**
* This is called to show/hide the loading UI. * This is called to show/hide the loading UI.
* @param {boolean} visible whether to show loading UI. * @param {boolean} visible whether to show loading UI.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment