Fix alphabetical sorting of child attachments in new databases

Broken after global schema introduction
This commit is contained in:
Dan Stillman 2021-04-18 01:43:37 -04:00
parent 633710c90b
commit 749e28dc1f
3 changed files with 49 additions and 2 deletions

View file

@ -1749,7 +1749,10 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
//
if (!isNew) {
// If attachment title changes, update parent attachments
if (this._changed.itemData && this._changed.itemData[110] && this.isAttachment() && parentItemID) {
let titleFieldID = Zotero.ItemFields.getID('title');
if (this._changed.itemData
&& this._changed.itemData[titleFieldID]
&& this.isAttachment() && parentItemID) {
reloadParentChildItems[parentItemID] = true;
}
}

View file

@ -598,11 +598,12 @@ Zotero.Items = function() {
//
// Attachments
//
var titleFieldID = Zotero.ItemFields.getID('title');
var sql = "SELECT parentItemID, A.itemID, value AS title, "
+ "CASE WHEN DI.itemID IS NULL THEN 0 ELSE 1 END AS trashed "
+ "FROM itemAttachments A "
+ "JOIN items I ON (A.parentItemID=I.itemID) "
+ "LEFT JOIN itemData ID ON (fieldID=110 AND A.itemID=ID.itemID) "
+ `LEFT JOIN itemData ID ON (fieldID=${titleFieldID} AND A.itemID=ID.itemID) `
+ "LEFT JOIN itemDataValues IDV USING (valueID) "
+ "LEFT JOIN deletedItems DI USING (itemID) "
+ "WHERE libraryID=?"

View file

@ -684,6 +684,49 @@ describe("Zotero.Item", function () {
assert.equal(attachments[0], attachment.id);
})
it("should return child attachments sorted alphabetically", async function () {
var item = await createDataObject('item');
var titles = ['B', 'C', 'A'];
var attachments = [];
for (let title of titles) {
let attachment = new Zotero.Item("attachment");
attachment.attachmentLinkMode = 'linked_url';
attachment.parentID = item.id;
attachment.setField('title', title);
await attachment.saveTx();
attachments.push(attachment);
}
attachments = item.getAttachments().map(id => Zotero.Items.get(id));
assert.equal(attachments[0].getField('title'), 'A');
assert.equal(attachments[1].getField('title'), 'B');
assert.equal(attachments[2].getField('title'), 'C');
});
it("should return re-sorted child attachments after one is modified", async function () {
var item = await createDataObject('item');
var titles = ['B', 'C', 'A'];
var attachments = [];
for (let title of titles) {
let attachment = new Zotero.Item("attachment");
attachment.attachmentLinkMode = 'linked_url';
attachment.parentID = item.id;
attachment.setField('title', title);
await attachment.saveTx();
attachments.push(attachment);
}
attachments[0].setField('title', 'D');
await attachments[0].saveTx();
attachments = item.getAttachments().map(id => Zotero.Items.get(id));
assert.equal(attachments[0].getField('title'), 'A');
assert.equal(attachments[1].getField('title'), 'C');
assert.equal(attachments[2].getField('title'), 'D');
});
it("#should ignore trashed child attachments by default", function* () {
var item = yield createDataObject('item');
var attachment = new Zotero.Item("attachment");