Fix items-count updating in right-hand pane

And some other tweaks to ZoteroPane.itemSelected()
This commit is contained in:
Dan Stillman 2015-05-08 16:01:25 -04:00
parent 84ff141edd
commit a64282118b
3 changed files with 72 additions and 15 deletions

View File

@ -1226,10 +1226,14 @@ var ZoteroPane = new function()
/**
* @return {Promise}
* @return {Promise<Boolean>} - Promise that resolves to true if an item was selected,
* or false if not (used for tests, though there could possibly
* be a better test for whether the item pane changed)
*/
this.itemSelected = function (event) {
return Zotero.spawn(function* () {
yield Zotero.DB.waitForTransaction();
var selectedItems = this.itemsView.getSelectedItems();
// Check if selection has actually changed. The onselect event that calls this
@ -1237,13 +1241,11 @@ var ZoteroPane = new function()
// such as whenever selectEventsSuppressed is set to false.
var ids = selectedItems.map(item => item.id);
ids.sort();
if (Zotero.Utilities.arrayEquals(_lastSelectedItems, ids)) {
if (ids.length && Zotero.Utilities.arrayEquals(_lastSelectedItems, ids)) {
return false;
}
_lastSelectedItems = ids;
yield Zotero.DB.waitForTransaction();
if (!this.itemsView) {
Zotero.debug("Items view not available in itemSelected", 2);
return false;
@ -1405,6 +1407,8 @@ var ZoteroPane = new function()
}
this.setItemPaneMessage(msg);
return false;
}
}
@ -1424,7 +1428,7 @@ var ZoteroPane = new function()
}
throw e;
}.bind(this));
};
}
/**

View File

@ -48,16 +48,7 @@ var loadZoteroPane = Zotero.Promise.coroutine(function* () {
// there or it should be good enough here.
yield Zotero.Promise.delay(52);
var zp = win.ZoteroPane;
var cv = zp.collectionsView;
var resolve1, resolve2;
var promise1 = new Zotero.Promise(() => resolve1 = arguments[0]);
var promise2 = new Zotero.Promise(() => resolve2 = arguments[0]);
cv.addEventListener('load', () => resolve1())
yield promise1;
cv.selection.select(0);
zp.addEventListener('itemsLoaded', () => resolve2());
yield promise2;
yield waitForItemsLoad(win, 0);
return win;
});
@ -85,6 +76,22 @@ function waitForWindow(uri) {
return deferred.promise;
}
var waitForItemsLoad = function (win, collectionRowToSelect) {
var resolve;
var promise = new Zotero.Promise(() => resolve = arguments[0]);
var zp = win.ZoteroPane;
var cv = zp.collectionsView;
cv.addEventListener('load', function () {
if (collectionRowToSelect !== undefined) {
cv.selection.select(collectionRowToSelect);
}
zp.addEventListener('itemsLoaded', function () {
resolve();
});
});
return promise;
}
/**
* Waits for a single item event. Returns a promise for the item ID(s).
*/

View File

@ -34,4 +34,50 @@ describe("ZoteroPane", function() {
assert.equal(item.getField('title'), value);
})
});
describe("#itemSelected()", function () {
it("should update the item count", function* () {
var collection = new Zotero.Collection;
collection.name = "Count Test";
var id = yield collection.save();
yield waitForItemsLoad(win);
// Unselected, with no items in view
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.unselected.zero', 0)
);
// Unselected, with one item in view
var item = new Zotero.Item('newspaperArticle');
item.setCollections([id]);
var itemID1 = yield item.save({
skipSelect: true
});
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.unselected.singular', 1)
);
// Unselected, with multiple items in view
var item = new Zotero.Item('audioRecording');
item.setCollections([id]);
var itemID2 = yield item.save({
skipSelect: true
});
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.unselected.plural', 2)
);
// Multiple items selected
var promise = zp.itemsView._getItemSelectedPromise();
zp.itemsView.rememberSelection([itemID1, itemID2]);
yield promise;
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.selected.multiple', 2)
);
})
})
})