Hi Alex,
By Default, In diagram ContextMenu, we can`t able to add items dynamically. Instead, we can able to show or hide the items in contextMenuOpen event. Initially you can add all the menu items in the context menu. Before the context menu gets opened, contextMenuOpen event gets fired. In that event check if any node is selected or not through the diagram selectedItems property. We can able to hide the context menu items through hiddenItems property. In that based on the node’s id you can hide the other menu items. If node-1 gets selected means then push other two items in hiddenItems collection. So that when you select the node-1 open context menu means Item-1 only gets shown, other two items gets hidden. Similary, you can hide the menu items based on your operation. Please refer to the below code snippet . please refer to the below code to show the context menu based on specific condition.
function contextMenuOpen(args) {
for (let item of args.items) {
if (
diagram.selectedItems.nodes.length &&
diagram.selectedItems.nodes[0].id === 'sdlc' &&
item.text !== 'Item-1'
) {
args.hiddenItems.push(item.id);
} else if (
diagram.selectedItems.nodes.length &&
diagram.selectedItems.nodes[0].id === 'support' &&
item.text !== 'Item-2'
) {
args.hiddenItems.push(item.id);
} else if (
diagram.selectedItems.nodes.length &&
diagram.selectedItems.nodes[0].id === 'analysis' &&
item.text !== 'Item-3'
) {
args.hiddenItems.push(item.id);
} if (
diagram.selectedItems.nodes.length &&
diagram.selectedItems.nodes[0].id === 'analysis'
) {
args.hiddenItems.push("item4");
}
}
}
|
Regards,
Aravind Ravi