Commit 7824734c authored by Sam McNally's avatar Sam McNally Committed by Commit Bot

Reland "Fix selection jumping around when removing multiple items in cr-list."

This is a reland of fa8a5d39

Original change's description:
> Fix selection jumping around when removing multiple items in cr-list.
>
> Previously, when the selected item is removed, the selection is changed
> to the index after where the old lead index was. When multiple items are
> removed, the logical next item may move multiple positions. Avoid this
> by moving to the item that hasn't been removed from prior to the
> permutation - using its position after the permutation.
>
> Bug: 708299
> Change-Id: I9393c9f00d5453ef3c3d5378758bb63ed29c43b7
> Reviewed-on: https://chromium-review.googlesource.com/c/1345709
> Reviewed-by: calamity <calamity@chromium.org>
> Commit-Queue: Sam McNally <sammc@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#610019}

Bug: 708299
Tbr: calamity@chromium.org
Change-Id: I4de9726b919f88a1efe98f036687de07b23715c2
Reviewed-on: https://chromium-review.googlesource.com/c/1347630Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Commit-Queue: Sam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610358}
parent c9c0e00c
......@@ -157,8 +157,8 @@ function testAdjust12() {
adjust(sm, 5, 20, 10);
assertEquals(-1, sm.leadIndex, 'lead');
assertEquals(-1, sm.anchorIndex, 'anchor');
assertEquals(0, sm.leadIndex, 'lead');
assertEquals(0, sm.anchorIndex, 'anchor');
assertArrayEquals(range(0, 4), sm.selectedIndexes);
}
......@@ -175,6 +175,64 @@ function testAdjust13() {
assertArrayEquals(range(0, 14), sm.selectedIndexes);
}
function testAdjust14() {
var sm = createSelectionModel(5, true);
sm.selectedIndexes = [2, 3];
sm.leadIndex = sm.anchorIndex = 3;
adjust(sm, 2, 2, 0);
assertEquals(2, sm.leadIndex, 'lead');
assertEquals(2, sm.anchorIndex, 'anchor');
assertArrayEquals(range(2, 2), sm.selectedIndexes);
}
function testAdjust15() {
var sm = createSelectionModel(7, true);
sm.selectedIndexes = [1, 3, 5];
sm.leadIndex = sm.anchorIndex = 1;
adjust(sm, 1, 1, 0);
adjust(sm, 2, 1, 0);
adjust(sm, 3, 1, 0);
assertEquals(3, sm.leadIndex, 'lead');
assertEquals(3, sm.anchorIndex, 'anchor');
assertArrayEquals(range(3, 3), sm.selectedIndexes);
}
function testAdjust16() {
var sm = createSelectionModel(7, true);
sm.selectedIndexes = [1, 3, 5];
sm.leadIndex = sm.anchorIndex = 3;
adjust(sm, 1, 1, 0);
adjust(sm, 2, 1, 0);
adjust(sm, 3, 1, 0);
assertEquals(3, sm.leadIndex, 'lead');
assertEquals(3, sm.anchorIndex, 'anchor');
assertArrayEquals(range(3, 3), sm.selectedIndexes);
}
function testAdjust17() {
var sm = createSelectionModel(7, true);
sm.selectedIndexes = [1, 3, 5];
sm.leadIndex = sm.anchorIndex = 5;
adjust(sm, 1, 1, 0);
adjust(sm, 2, 1, 0);
adjust(sm, 3, 1, 0);
assertEquals(3, sm.leadIndex, 'lead');
assertEquals(3, sm.anchorIndex, 'anchor');
assertArrayEquals(range(3, 3), sm.selectedIndexes);
}
function testLeadAndAnchor1() {
var sm = createSelectionModel(20, true);
......@@ -191,8 +249,8 @@ function testLeadAndAnchor2() {
sm.leadIndex = sm.anchorIndex = 10;
sm.selectAll();
assertEquals(-1, sm.leadIndex, 'lead');
assertEquals(-1, sm.anchorIndex, 'anchor');
assertEquals(0, sm.leadIndex, 'lead');
assertEquals(0, sm.anchorIndex, 'anchor');
}
function testSelectAll() {
......
......@@ -105,8 +105,14 @@ cr.define('cr.ui', function() {
* @private
*/
getNearestSelectedIndex_: function(index) {
if (index == -1)
if (index == -1) {
// If no index is provided, pick the first selected index if there is
// one.
if (this.selectedIndexes.length) {
return this.selectedIndexes[0];
}
return -1;
}
var result = Infinity;
for (var i in this.selectedIndexes_) {
......@@ -348,8 +354,15 @@ cr.define('cr.ui', function() {
if (oldSelectedItemsCount && !this.selectedIndexes.length &&
this.length_ && oldLeadIndex != -1) {
// All selected items are deleted. We move selection to next item of
// last selected item.
this.selectedIndexes = [Math.min(oldLeadIndex, this.length_ - 1)];
// last selected item, following it to its new position.
let newSelectedIndex = Math.min(oldLeadIndex, this.length_ - 1);
for (let i = oldLeadIndex + 1; i < permutation.length; ++i) {
if (permutation[i] != -1) {
newSelectedIndex = permutation[i];
break;
}
}
this.selectedIndexes = [newSelectedIndex];
}
this.endChange();
......
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