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
ca4ced1e9f
commit
b24c7577ec
2 changed files with 28 additions and 10 deletions
|
@ -1747,22 +1747,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[]}
|
* @return {Zotero.Item[]}
|
||||||
*/
|
*/
|
||||||
this.keepParents = function (items) {
|
this.keepTopLevel = function (items) {
|
||||||
var parentItems = new Set(
|
var topLevelItems = new Set(
|
||||||
items
|
items.filter(item => item.isTopLevelItem())
|
||||||
.filter(item => item.isTopLevelItem())
|
|
||||||
.map(item => item.id)
|
|
||||||
);
|
);
|
||||||
return items.filter(item => {
|
return items.filter((item) => {
|
||||||
var parentItemID = item.parentItemID;
|
var topLevelItem = !item.isTopLevelItem() && item.topLevelItem;
|
||||||
// Not a child item or not a child of one of the passed items
|
// 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)
|
[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 () {
|
describe("#_loadChildItems()", function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue