Improve counting of file attachments for Show File and Export PDFs

If a parent item and its primary attachment are selected, it should
still say "Show File" rather than "Show Files". This adds the
questionably named `Zotero.Items.numDistinctFileAttachmentsForLabel()`
to try to figure out if we're operating on 0, 1, or >1 items.

Follow-up to #4124
This commit is contained in:
Dan Stillman 2024-05-15 05:30:45 -04:00
parent c47617c809
commit 0c8d3f0829
5 changed files with 106 additions and 17 deletions

View file

@ -1324,6 +1324,43 @@ describe("Zotero.Items", function () {
});
});
describe("#numDistinctFileAttachmentsForLabel()", function () {
it("should count parent item and best attachment as one item when best-attachment state is cached", async function () {
var item1 = await createDataObject('item');
var attachment1 = await importFileAttachment('test.png', { parentItemID: item1.id });
var attachment2 = await importFileAttachment('test.png', { parentItemID: item1.id });
function getNum() {
return Zotero.Items.numDistinctFileAttachmentsForLabel(zp.getSelectedItems());
}
zp.itemsView.selection.clearSelection();
assert.equal(getNum(), 0);
// Uncached best-attachment state
await zp.selectItems([item1.id]);
assert.equal(getNum(), 1);
await zp.selectItems([item1.id, attachment1.id]);
// Should count parent item and best attachment as two item when uncached
assert.equal(getNum(), 2);
await zp.selectItems([item1.id, attachment1.id, attachment2.id]);
// Max is 2
assert.equal(getNum(), 2);
await item1.getBestAttachment();
// Cached best-attachment state
await zp.selectItems([item1.id]);
assert.equal(getNum(), 1);
await zp.selectItems([item1.id, attachment1.id]);
// Should count parent item and best attachment as one item when cached
assert.equal(getNum(), 1);
await zp.selectItems([item1.id, attachment1.id, attachment2.id]);
assert.equal(getNum(), 2);
});
});
describe("#_loadChildItems()", function () {
it("should mark child items as loaded for an attachment", async function () {
var attachment = await importPDFAttachment();