Commit fa8a5d39 authored by Sam McNally's avatar Sam McNally Committed by Commit Bot

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/1345709Reviewed-by: default avatarcalamity <calamity@chromium.org>
Commit-Queue: Sam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610019}
parent ea0ccae7
...@@ -157,8 +157,8 @@ function testAdjust12() { ...@@ -157,8 +157,8 @@ function testAdjust12() {
adjust(sm, 5, 20, 10); adjust(sm, 5, 20, 10);
assertEquals(-1, sm.leadIndex, 'lead'); assertEquals(0, sm.leadIndex, 'lead');
assertEquals(-1, sm.anchorIndex, 'anchor'); assertEquals(0, sm.anchorIndex, 'anchor');
assertArrayEquals(range(0, 4), sm.selectedIndexes); assertArrayEquals(range(0, 4), sm.selectedIndexes);
} }
...@@ -175,6 +175,64 @@ function testAdjust13() { ...@@ -175,6 +175,64 @@ function testAdjust13() {
assertArrayEquals(range(0, 14), sm.selectedIndexes); 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() { function testLeadAndAnchor1() {
var sm = createSelectionModel(20, true); var sm = createSelectionModel(20, true);
...@@ -191,8 +249,8 @@ function testLeadAndAnchor2() { ...@@ -191,8 +249,8 @@ function testLeadAndAnchor2() {
sm.leadIndex = sm.anchorIndex = 10; sm.leadIndex = sm.anchorIndex = 10;
sm.selectAll(); sm.selectAll();
assertEquals(-1, sm.leadIndex, 'lead'); assertEquals(0, sm.leadIndex, 'lead');
assertEquals(-1, sm.anchorIndex, 'anchor'); assertEquals(0, sm.anchorIndex, 'anchor');
} }
function testSelectAll() { function testSelectAll() {
......
...@@ -105,8 +105,14 @@ cr.define('cr.ui', function() { ...@@ -105,8 +105,14 @@ cr.define('cr.ui', function() {
* @private * @private
*/ */
getNearestSelectedIndex_: function(index) { 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; return -1;
}
var result = Infinity; var result = Infinity;
for (var i in this.selectedIndexes_) { for (var i in this.selectedIndexes_) {
...@@ -348,8 +354,15 @@ cr.define('cr.ui', function() { ...@@ -348,8 +354,15 @@ cr.define('cr.ui', function() {
if (oldSelectedItemsCount && !this.selectedIndexes.length && if (oldSelectedItemsCount && !this.selectedIndexes.length &&
this.length_ && oldLeadIndex != -1) { this.length_ && oldLeadIndex != -1) {
// All selected items are deleted. We move selection to next item of // All selected items are deleted. We move selection to next item of
// last selected item. // last selected item, following it to its new position.
this.selectedIndexes = [Math.min(oldLeadIndex, this.length_ - 1)]; 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(); 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