Commit e411e02d authored by Kyle Milka's avatar Kyle Milka Committed by Commit Bot

[NTP] Keep selected bg tile when back arrow is clicked

When a background tile is selected don't remove the selected state
unless it is clicked again, another tile is selected, or the dialog
is closed.

http://go/scrcast/NTYzMjU0OTE2NDQxNzAyNHxjYTViNTI3YS1jNw

Bug: 937570
Change-Id: I99caf6b5b2afbf2dfcfa9198db042491220c63a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1696161
Commit-Queue: Kyle Milka <kmilka@chromium.org>
Reviewed-by: default avatarKristi Park <kristipark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676264}
parent 5ea405fd
...@@ -247,6 +247,7 @@ customize.richerPicker_selectedSubmenu = { ...@@ -247,6 +247,7 @@ customize.richerPicker_selectedSubmenu = {
*/ */
customize.selectedOptions = { customize.selectedOptions = {
background: null, // Contains the background image tile. background: null, // Contains the background image tile.
backgroundId: '', // The id of the selected background image tile.
// Contains the selected shortcut type's DOM element, i.e. either custom links // Contains the selected shortcut type's DOM element, i.e. either custom links
// or most visited. // or most visited.
shortcutType: null, shortcutType: null,
...@@ -443,9 +444,6 @@ customize.richerPicker_resetImageMenu = function() { ...@@ -443,9 +444,6 @@ customize.richerPicker_resetImageMenu = function() {
customize.IDS.BACKGROUNDS_MENU; customize.IDS.BACKGROUNDS_MENU;
customize.richerPicker_openBackgroundSubmenu.title = ''; customize.richerPicker_openBackgroundSubmenu.title = '';
backgroundMenu.scrollTop = 0; backgroundMenu.scrollTop = 0;
customize.richerPicker_deselectBackgroundTile(
customize.selectedOptions.background);
}; };
/** /**
...@@ -606,7 +604,7 @@ customize.getNextTile = function(deltaX, deltaY, currentElem) { ...@@ -606,7 +604,7 @@ customize.getNextTile = function(deltaX, deltaY, currentElem) {
if (configData.richerPicker) { if (configData.richerPicker) {
return customize.richerPicker_getNextTile(deltaX, deltaY, currentElem); return customize.richerPicker_getNextTile(deltaX, deltaY, currentElem);
} }
const current = currentElem.dataset.tileNum; const current = currentElem.dataset.tileIndex;
let idPrefix = 'coll_tile_'; let idPrefix = 'coll_tile_';
if ($(customize.IDS.MENU) if ($(customize.IDS.MENU)
.classList.contains(customize.CLASSES.IMAGE_DIALOG)) { .classList.contains(customize.CLASSES.IMAGE_DIALOG)) {
...@@ -699,7 +697,8 @@ customize.showCollectionSelectionDialog = function() { ...@@ -699,7 +697,8 @@ customize.showCollectionSelectionDialog = function() {
} else { } else {
customize.resetSelectionDialog(); customize.resetSelectionDialog();
} }
customize.showImageSelectionDialog(tile.dataset.name); customize.showImageSelectionDialog(
tile.dataset.name, tile.dataset.tileIndex);
} else { } else {
customize.handleError(collImgErrors); customize.handleError(collImgErrors);
} }
...@@ -747,7 +746,7 @@ customize.showCollectionSelectionDialog = function() { ...@@ -747,7 +746,7 @@ customize.showCollectionSelectionDialog = function() {
const id = coll[i].collectionId; const id = coll[i].collectionId;
const name = coll[i].collectionName; const name = coll[i].collectionName;
const imageUrl = coll[i].previewImageUrl; const imageUrl = coll[i].previewImageUrl;
const dataset = {'id': id, 'name': name, 'tileNum': i}; const dataset = {'id': id, 'name': name, 'tileIndex': i};
const tile = customize.createTileWithTitle( const tile = customize.createTileWithTitle(
'coll_tile_' + i, imageUrl, name, dataset, tileOnClickInteraction, 'coll_tile_' + i, imageUrl, name, dataset, tileOnClickInteraction,
...@@ -878,6 +877,7 @@ customize.richerPicker_selectBackgroundTile = function(tile) { ...@@ -878,6 +877,7 @@ customize.richerPicker_selectBackgroundTile = function(tile) {
return; return;
} }
customize.selectedOptions.background = tile; customize.selectedOptions.background = tile;
customize.selectedOptions.backgroundId = tile.id;
customize.richerPicker_applySelectedState(tile); customize.richerPicker_applySelectedState(tile);
customize.richerPicker_maybeToggleDone(); customize.richerPicker_maybeToggleDone();
customize.richerPicker_previewImage(tile); customize.richerPicker_previewImage(tile);
...@@ -972,8 +972,10 @@ customize.removeSelectedState = function(tile) { ...@@ -972,8 +972,10 @@ customize.removeSelectedState = function(tile) {
* chrome-search://local-ntp/ntp-background-images.js?collection_id=<collection_id> * chrome-search://local-ntp/ntp-background-images.js?collection_id=<collection_id>
* @param {string} dialogTitle The title to be displayed at the top of the * @param {string} dialogTitle The title to be displayed at the top of the
* dialog. * dialog.
* @param {number} collIndex The index of the collection this image menu belongs
* to.
*/ */
customize.showImageSelectionDialog = function(dialogTitle) { customize.showImageSelectionDialog = function(dialogTitle, collIndex) {
const firstNTile = customize.ROWS_TO_PRELOAD * customize.getTilesWide(); const firstNTile = customize.ROWS_TO_PRELOAD * customize.getTilesWide();
const tileContainer = configData.richerPicker ? const tileContainer = configData.richerPicker ?
$(customize.IDS.BACKGROUNDS_IMAGE_MENU) : $(customize.IDS.BACKGROUNDS_IMAGE_MENU) :
...@@ -1091,10 +1093,15 @@ customize.showImageSelectionDialog = function(dialogTitle) { ...@@ -1091,10 +1093,15 @@ customize.showImageSelectionDialog = function(dialogTitle) {
''); '');
dataset.attributionActionUrl = collImg[i].attributionActionUrl; dataset.attributionActionUrl = collImg[i].attributionActionUrl;
dataset.url = collImg[i].imageUrl; dataset.url = collImg[i].imageUrl;
dataset.tileNum = i; dataset.tileIndex = i;
let tileId = 'img_tile_' + i;
if (configData.richerPicker) {
tileId = 'coll_' + collIndex + '_' + tileId;
}
const tile = customize.createTile( const tile = customize.createTile(
'img_tile_' + i, collImg[i].imageUrl, dataset, tileOnClickInteraction, tileId, collImg[i].imageUrl, dataset, tileOnClickInteraction,
tileOnKeyDownInteraction); tileOnKeyDownInteraction);
tile.setAttribute('aria-label', collImg[i].attributions[0]); tile.setAttribute('aria-label', collImg[i].attributions[0]);
...@@ -1123,6 +1130,13 @@ customize.showImageSelectionDialog = function(dialogTitle) { ...@@ -1123,6 +1130,13 @@ customize.showImageSelectionDialog = function(dialogTitle) {
}); });
} }
// If an image tile was previously selected re-select it now.
const selected = $(customize.selectedOptions.backgroundId);
if (selected) {
customize.richerPicker_selectBackgroundTile(selected);
}
$(customize.IDS.TILES).focus(); $(customize.IDS.TILES).focus();
}; };
...@@ -1136,9 +1150,9 @@ customize.showImageSelectionDialog = function(dialogTitle) { ...@@ -1136,9 +1150,9 @@ customize.showImageSelectionDialog = function(dialogTitle) {
*/ */
customize.loadTile = function(tile, imageData, countLoad) { customize.loadTile = function(tile, imageData, countLoad) {
tile.style.backgroundImage = tile.style.backgroundImage =
'url(' + imageData[tile.dataset.tileNum].thumbnailImageUrl + ')'; 'url(' + imageData[tile.dataset.tileIndex].thumbnailImageUrl + ')';
customize.fadeInImageTile( customize.fadeInImageTile(
tile, imageData[tile.dataset.tileNum].thumbnailImageUrl, countLoad); tile, imageData[tile.dataset.tileIndex].thumbnailImageUrl, countLoad);
}; };
/** /**
...@@ -1256,6 +1270,7 @@ customize.richerPicker_resetSelectedOptions = function() { ...@@ -1256,6 +1270,7 @@ customize.richerPicker_resetSelectedOptions = function() {
customize.richerPicker_deselectBackgroundTile( customize.richerPicker_deselectBackgroundTile(
customize.selectedOptions.background); customize.selectedOptions.background);
customize.selectedOptions.background = null; customize.selectedOptions.background = null;
customize.selectedOptions.backgroundId = null;
// Reset color selection. // Reset color selection.
customize.richerPicker_removeSelectedState(customize.selectedOptions.color); customize.richerPicker_removeSelectedState(customize.selectedOptions.color);
...@@ -1740,7 +1755,7 @@ customize.initCustomBackgrounds = function(showErrorNotification) { ...@@ -1740,7 +1755,7 @@ customize.initCustomBackgrounds = function(showErrorNotification) {
.classList.contains(customize.CLASSES.COLLECTION_DIALOG)) { .classList.contains(customize.CLASSES.COLLECTION_DIALOG)) {
$('coll_tile_0').focus(); $('coll_tile_0').focus();
} else { } else {
$('img_tile_0').focus(); document.querySelector('[id$="img_tile_0"]').focus();
} }
} }
}; };
...@@ -1753,7 +1768,7 @@ customize.initCustomBackgrounds = function(showErrorNotification) { ...@@ -1753,7 +1768,7 @@ customize.initCustomBackgrounds = function(showErrorNotification) {
$(customize.IDS.BACKGROUNDS_IMAGE_MENU).onkeydown = function(event) { $(customize.IDS.BACKGROUNDS_IMAGE_MENU).onkeydown = function(event) {
if (customize.arrowKeys.includes(event.keyCode)) { if (customize.arrowKeys.includes(event.keyCode)) {
$('img_tile_0').focus(); document.querySelector('[id$="img_tile_0"]').focus();
} }
}; };
......
...@@ -201,7 +201,7 @@ test.customizeMenu.testMenu_SaveUserSelections = function() { ...@@ -201,7 +201,7 @@ test.customizeMenu.testMenu_SaveUserSelections = function() {
// Select a background. // Select a background.
setupFakeAsyncImageLoad('coll_tile_0'); setupFakeAsyncImageLoad('coll_tile_0');
$('coll_tile_0').click(); $('coll_tile_0').click();
const background = $('img_tile_0'); const background = $('coll_0_img_tile_0');
background.click(); background.click();
assertTrue(background.parentElement.classList.contains( assertTrue(background.parentElement.classList.contains(
test.customizeMenu.CLASSES.SELECTED)); test.customizeMenu.CLASSES.SELECTED));
...@@ -259,7 +259,7 @@ test.customizeMenu.testMenu_ApplyUserSelections = function() { ...@@ -259,7 +259,7 @@ test.customizeMenu.testMenu_ApplyUserSelections = function() {
// Select a background. // Select a background.
setupFakeAsyncImageLoad('coll_tile_0'); setupFakeAsyncImageLoad('coll_tile_0');
$('coll_tile_0').click(); $('coll_tile_0').click();
$('img_tile_0').click(); $('coll_0_img_tile_0').click();
// Select a shortcut option. // Select a shortcut option.
$(test.customizeMenu.IDS.SHORTCUTS_BUTTON).click(); $(test.customizeMenu.IDS.SHORTCUTS_BUTTON).click();
...@@ -292,7 +292,7 @@ test.customizeMenu.testMenu_CancelUserSelections = function() { ...@@ -292,7 +292,7 @@ test.customizeMenu.testMenu_CancelUserSelections = function() {
// Select a background. // Select a background.
setupFakeAsyncImageLoad('coll_tile_0'); setupFakeAsyncImageLoad('coll_tile_0');
$('coll_tile_0').click(); $('coll_tile_0').click();
$('img_tile_0').click(); $('coll_0_img_tile_0').click();
// Select a shortcut option. // Select a shortcut option.
$(test.customizeMenu.IDS.SHORTCUTS_BUTTON).click(); $(test.customizeMenu.IDS.SHORTCUTS_BUTTON).click();
...@@ -571,7 +571,7 @@ test.customizeMenu.testBackgrounds_SetCustomBackground = function() { ...@@ -571,7 +571,7 @@ test.customizeMenu.testBackgrounds_SetCustomBackground = function() {
.getElementsByClassName('bg-sel-tile') .getElementsByClassName('bg-sel-tile')
.length === 4); .length === 4);
$('img_tile_0').click(); $('coll_0_img_tile_0').click();
$(test.customizeMenu.IDS.MENU_DONE).click(); $(test.customizeMenu.IDS.MENU_DONE).click();
// No menu should be open, and setCustomBackground should have been called. // No menu should be open, and setCustomBackground should have been called.
...@@ -615,7 +615,7 @@ test.customizeMenu.testBackgrounds_CancelCustomBackground = function() { ...@@ -615,7 +615,7 @@ test.customizeMenu.testBackgrounds_CancelCustomBackground = function() {
.getElementsByClassName('bg-sel-tile') .getElementsByClassName('bg-sel-tile')
.length === 4); .length === 4);
$('img_tile_0').click(); $('coll_0_img_tile_0').click();
$(test.customizeMenu.IDS.MENU_CANCEL).click(); $(test.customizeMenu.IDS.MENU_CANCEL).click();
// No menu should be open, and setCustomBackground should NOT have been // No menu should be open, and setCustomBackground should NOT have been
...@@ -658,7 +658,7 @@ test.customizeMenu.testBackgrounds_BackArrowCustomBackground = function() { ...@@ -658,7 +658,7 @@ test.customizeMenu.testBackgrounds_BackArrowCustomBackground = function() {
.getElementsByClassName('bg-sel-tile') .getElementsByClassName('bg-sel-tile')
.length === 4); .length === 4);
$('img_tile_0').click(); $('coll_0_img_tile_0').click();
$(test.customizeMenu.IDS.MENU_BACK).click(); $(test.customizeMenu.IDS.MENU_BACK).click();
// The main backgrounds menu should be open, and no custom background set. // The main backgrounds menu should be open, and no custom background set.
...@@ -666,7 +666,7 @@ test.customizeMenu.testBackgrounds_BackArrowCustomBackground = function() { ...@@ -666,7 +666,7 @@ test.customizeMenu.testBackgrounds_BackArrowCustomBackground = function() {
assertFalse(elementIsVisible(backgroundImageSubmenu)); assertFalse(elementIsVisible(backgroundImageSubmenu));
assertEquals(0, test.customizeMenu.timesCustomBackgroundWasSet); assertEquals(0, test.customizeMenu.timesCustomBackgroundWasSet);
// Reopen the images menu, the selection should be cleared. // Reopen the images menu, the selection should still be present.
$('coll_tile_0').click(); $('coll_tile_0').click();
assertFalse(elementIsVisible(backgroundSubmenu)); assertFalse(elementIsVisible(backgroundSubmenu));
...@@ -678,7 +678,11 @@ test.customizeMenu.testBackgrounds_BackArrowCustomBackground = function() { ...@@ -678,7 +678,11 @@ test.customizeMenu.testBackgrounds_BackArrowCustomBackground = function() {
assertTrue( assertTrue(
$(test.customizeMenu.IDS.BACKGROUNDS_IMAGE_MENU) $(test.customizeMenu.IDS.BACKGROUNDS_IMAGE_MENU)
.getElementsByClassName('selected') .getElementsByClassName('selected')
.length === 0); .length === 1);
assertTrue(
$(test.customizeMenu.IDS.BACKGROUNDS_IMAGE_MENU)
.getElementsByClassName('selected')[0]
.firstChild.id === 'coll_0_img_tile_0');
}; };
// ******************************* HELPERS ******************************* // ******************************* HELPERS *******************************
......
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