Reselect the same row when an item is removed

This commit is contained in:
Dan Stillman 2015-06-09 01:29:06 -04:00
parent 1a3ef8ab7f
commit 1d45c6c882
2 changed files with 43 additions and 16 deletions

View File

@ -459,7 +459,7 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio
var sort = false; var sort = false;
var savedSelection = this.getSelectedItems(true); var savedSelection = this.getSelectedItems(true);
var previousRow = false; var previousFirstRow = this._rowMap[ids[0]];
// Redraw the tree (for tag color and progress changes) // Redraw the tree (for tag color and progress changes)
if (action == 'redraw') { if (action == 'redraw') {
@ -569,7 +569,6 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio
// On a delete in duplicates mode, just refresh rather than figuring // On a delete in duplicates mode, just refresh rather than figuring
// out what to remove // out what to remove
if (collectionTreeRow.isDuplicates()) { if (collectionTreeRow.isDuplicates()) {
previousRow = this._rowMap[ids[0]];
yield this.refresh(); yield this.refresh();
refreshed = true; refreshed = true;
madeChanges = true; madeChanges = true;
@ -879,10 +878,6 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio
} }
else else
{ {
if (previousRow === false) {
previousRow = this._rowMap[ids[0]];
}
if (sort) { if (sort) {
yield this.sort(typeof sort == 'number' ? sort : false); yield this.sort(typeof sort == 'number' ? sort : false);
} }
@ -894,9 +889,9 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio
if (action == 'remove' || action == 'trash' || action == 'delete') { if (action == 'remove' || action == 'trash' || action == 'delete') {
// In duplicates view, select the next set on delete // In duplicates view, select the next set on delete
if (collectionTreeRow.isDuplicates()) { if (collectionTreeRow.isDuplicates()) {
if (this._rows[previousRow]) { if (this._rows[previousFirstRow]) {
// Mirror ZoteroPane.onTreeMouseDown behavior // Mirror ZoteroPane.onTreeMouseDown behavior
var itemID = this._rows[previousRow].ref.id; var itemID = this._rows[previousFirstRow].ref.id;
var setItemIDs = collectionTreeRow.ref.getSetItemsByItemID(itemID); var setItemIDs = collectionTreeRow.ref.getSetItemsByItemID(itemID);
this.selectItems(setItemIDs); this.selectItems(setItemIDs);
} }
@ -905,17 +900,17 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio
// If this was a child item and the next item at this // If this was a child item and the next item at this
// position is a top-level item, move selection one row // position is a top-level item, move selection one row
// up to select a sibling or parent // up to select a sibling or parent
if (ids.length == 1 && previousRow > 0) { if (ids.length == 1 && previousFirstRow > 0) {
var previousItem = yield Zotero.Items.getAsync(ids[0]); let previousItem = yield Zotero.Items.getAsync(ids[0]);
if (previousItem && !previousItem.isTopLevelItem()) { if (previousItem && !previousItem.isTopLevelItem()) {
if (this._rows[previousRow] && this.getLevel(previousRow) == 0) { if (this._rows[previousFirstRow] && this.getLevel(previousFirstRow) == 0) {
previousRow--; previousFirstRow--;
} }
} }
} }
if (this._rows[previousRow]) { if (previousFirstRow !== undefined && this._rows[previousFirstRow]) {
this.selection.select(previousRow); this.selection.select(previousFirstRow);
} }
// If no item at previous position, select last item in list // If no item at previous position, select last item in list
else if (this._rows[this._rows.length - 1]) { else if (this._rows[this._rows.length - 1]) {

View File

@ -1,14 +1,19 @@
describe("Zotero.ItemTreeView", function() { describe("Zotero.ItemTreeView", function() {
var win, itemsView, existingItemID; var win, zp, itemsView, existingItemID;
// Load Zotero pane and select library // Load Zotero pane and select library
before(function* () { before(function* () {
win = yield loadZoteroPane(); win = yield loadZoteroPane();
itemsView = win.ZoteroPane.itemsView; zp = win.ZoteroPane;
var item = new Zotero.Item('book'); var item = new Zotero.Item('book');
existingItemID = yield item.saveTx(); existingItemID = yield item.saveTx();
}); });
beforeEach(function* () {
yield zp.collectionsView.selectLibrary();
yield waitForItemsLoad(win)
itemsView = zp.itemsView;
})
after(function () { after(function () {
win.close(); win.close();
}); });
@ -161,6 +166,33 @@ describe("Zotero.ItemTreeView", function() {
assert.lengthOf(selected, 1); assert.lengthOf(selected, 1);
assert.equal(selected[0], id); assert.equal(selected[0], id);
}); });
it("should reselect the same row when an item is removed", function* () {
var collection = yield createDataObject('collection');
yield waitForItemsLoad(win);
itemsView = zp.itemsView;
var items = [];
var num = 10;
for (let i = 0; i < num; i++) {
let item = createUnsavedDataObject('item');
item.addToCollection(collection.id);
yield item.saveTx();
items.push(item);
}
yield Zotero.Promise.delay(2000);
assert.equal(itemsView.rowCount, num);
// Select the third item in the list
itemsView.selection.select(2);
var treeRow = itemsView.getRow(2);
yield treeRow.ref.eraseTx();
// Selection should stay on third row
assert.equal(itemsView.selection.currentIndex, 2);
yield Zotero.Items.erase(items.map(item => item.id));
})
}) })
describe("#drop()", function () { describe("#drop()", function () {