Fix sorting by Item Type and feed item itemID

Regression from a8ed30ce80
This commit is contained in:
Dan Stillman 2022-07-10 17:31:01 -04:00
parent 3a160b2630
commit 56e7f0a10f
2 changed files with 18 additions and 7 deletions

View file

@ -1826,6 +1826,10 @@ var ItemTree = class ItemTree extends LibraryTree {
getSortFields() { getSortFields() {
var fields = [this.getSortField()]; var fields = [this.getSortField()];
if (!this._isValidSortField(fields[0])) {
Zotero.logError(`'${fields[0]}' is not a valid sort field -- skipping`);
fields.shift();
}
var secondaryField = this._getSecondarySortField(); var secondaryField = this._getSecondarySortField();
if (secondaryField) { if (secondaryField) {
fields.push(secondaryField); fields.push(secondaryField);
@ -1849,12 +1853,8 @@ var ItemTree = class ItemTree extends LibraryTree {
fallbackFields = Zotero.Prefs.get('fallbackSort').split(','); fallbackFields = Zotero.Prefs.get('fallbackSort').split(',');
} }
fields = Zotero.Utilities.arrayUnique(fields.concat(fallbackFields)); fields = Zotero.Utilities.arrayUnique(fields.concat(fallbackFields));
var validFields = fields.filter(x => this._isValidSortField(x));
if (validFields.length) {
fields = validFields;
}
// If no valid fields, use default fallback // If no valid fields, use default fallback
else { if (!fields.length) {
Zotero.logError(`No valid fields in getSortFields() (${fields.join(',')}) ` Zotero.logError(`No valid fields in getSortFields() (${fields.join(',')}) `
+ '-- resetting'); + '-- resetting');
Zotero.Prefs.clear('fallbackSort'); Zotero.Prefs.clear('fallbackSort');
@ -1874,7 +1874,9 @@ var ItemTree = class ItemTree extends LibraryTree {
} }
_isValidSortField(field) { _isValidSortField(field) {
return field == 'year' return field == 'itemType'
|| field == 'year'
|| field == 'id' // feeds
|| !!Zotero.ItemFields.getID(field) || !!Zotero.ItemFields.getID(field)
|| Zotero.Items.primaryFields.includes(field); || Zotero.Items.primaryFields.includes(field);
} }

View file

@ -152,7 +152,7 @@ describe("Zotero.ItemTree", function() {
}) })
describe("#getSortFields()", function () { describe("#getSortFields()", function () {
before(async function () { beforeEach(async function () {
itemsView = zp.itemsView; itemsView = zp.itemsView;
// Sort by title // Sort by title
@ -183,6 +183,15 @@ describe("Zotero.ItemTree", function() {
// fallbackSort pref with title moved to beginning // fallbackSort pref with title moved to beginning
assert.sameMembers(['title', 'firstCreator', 'date', 'dateAdded'], fields); assert.sameMembers(['title', 'firstCreator', 'date', 'dateAdded'], fields);
}); });
it("should sort by item type", async function () {
// Sort by item type
const colIndex = itemsView.tree._getColumns().findIndex(column => column.dataKey == 'itemType');
await itemsView.tree._columns.toggleSort(colIndex);
var fields = itemsView.getSortFields();
assert.sameMembers(['itemType', 'firstCreator', 'date', 'title', 'dateAdded'], fields);
});
}); });
describe("#notify()", function () { describe("#notify()", function () {