(() => {
// Plain userland helpers over the existing API -- nothing new needed
let showIf = predicate => (ev, context) => context.setVisible(!!predicate(context));
// One predicate per entry; adding a 10th submenu is one more key here
let visibility = {
newItemNote: ({ items }) => items?.length && items.every(it => it.isRegularItem()),
showFile: ({ items }) => items?.length && items.every(it => it.isAttachment()),
};
let anyVisible = context => Object.values(visibility).some(p => p(context));
let menuID = Zotero.MenuManager.registerMenu({
menuID: "example-conditional-menu",
pluginID: "exa...@example.example",
target: "main/library/item",
menus: [{
menuType: "submenu",
// "More Options…" (zotero.ftl)
l10nID: "menu-custom-group-submenu",
// Hides itself when no entry would be visible, regardless of entry count,
// without ever invoking the children's onShowing hooks
onShowing: showIf(anyVisible),
menus: [
{
menuType: "menuitem",
// "New Item Note" (zotero.ftl)
l10nID: "menu-new-item-note",
onShowing: showIf(visibility.newItemNote),
onCommand: (ev, context) => {
Zotero.debug("Regular-item action: " + context.items.map(it =>
it.id).join(", "));
},
},
{
menuType: "menuitem",
// "Show File" (zotero.ftl)
l10nID: "menu-file-show-file",
onShowing: showIf(visibility.showFile),
onCommand: (ev, context) => {
Zotero.debug("Attachment action: " + context.items.map(it =>
it.id).join(", "));
},
},
],
}],
});
Zotero.debug("Registered: " + menuID);
return menuID;
})();
</code>