Rename Zotero.Items::keepParents() to keepTopLevel()
So that it works for more than two levels of items Also fix a bug where the parent item could be returned more than once if multiple child items were selected.
This commit is contained in:
parent
497d6376c8
commit
39e45221b1
2 changed files with 28 additions and 10 deletions
|
@ -1746,22 +1746,29 @@ Zotero.Items = function() {
|
|||
|
||||
|
||||
/**
|
||||
* Returns an array of items with children of selected parents removed
|
||||
* Return an array of items with descendants of selected top-level items removed
|
||||
*
|
||||
* Non-top-level items that aren't descendents of selected items are kept.
|
||||
*
|
||||
* @param {Zotero.Item[]}
|
||||
* @return {Zotero.Item[]}
|
||||
*/
|
||||
this.keepParents = function (items) {
|
||||
var parentItems = new Set(
|
||||
items
|
||||
.filter(item => item.isTopLevelItem())
|
||||
.map(item => item.id)
|
||||
this.keepTopLevel = function (items) {
|
||||
var topLevelItems = new Set(
|
||||
items.filter(item => item.isTopLevelItem())
|
||||
);
|
||||
return items.filter(item => {
|
||||
var parentItemID = item.parentItemID;
|
||||
return items.filter((item) => {
|
||||
var topLevelItem = !item.isTopLevelItem() && item.topLevelItem;
|
||||
// Not a child item or not a child of one of the passed items
|
||||
return !parentItemID || !parentItems.has(parentItemID);
|
||||
return !topLevelItem || !topLevelItems.has(topLevelItem);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
this.keepParents = function (items) {
|
||||
Zotero.debug("Zotero.Items.keepParents() is deprecated -- use Zotero.Items.keepTopLevel() instead");
|
||||
return this.keepTopLevel(items);
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
@ -1234,6 +1234,17 @@ describe("Zotero.Items", function () {
|
|||
[item1, item4, item6].map(item => item.id)
|
||||
);
|
||||
});
|
||||
|
||||
it("shouldn't return parent item more than once when two child items are selected", async function () {
|
||||
var item1 = await createDataObject('item');
|
||||
var item2 = await createDataObject('item', { itemType: 'note', parentItemID: item1.id });
|
||||
var item3 = await createDataObject('item', { itemType: 'note', parentItemID: item1.id });
|
||||
var items = Zotero.Items.keepParents([item2, item3]);
|
||||
assert.sameMembers(
|
||||
items.map(item => item.id),
|
||||
[item2.id, item3.id]
|
||||
)
|
||||
});
|
||||
});
|
||||
|
||||
describe("#_loadChildItems()", function () {
|
||||
|
|
Loading…
Reference in a new issue