I want that when clicking on a button from the context menu, the node and all child nodes change their color to the specified one. I have a function to change color as well as a function to remove all children with a method node.findTreeParts(). But can’t put them together to work. Part of my code:
myDiagram.nodeTemplate.contextMenu =
$("ContextMenu",
$("ContextMenuButton",
$(go.TextBlock, "Удалить ветвь"),
{
click: function(e, obj) {
var node = obj.part.adornedPart;
if (node !== null) {
myDiagram.startTransaction("remove dept");
myDiagram.removeParts(node.findTreeParts());
myDiagram.commitTransaction("remove dept");
}
}
}
),
$("ContextMenuButton",
$(go.TextBlock, "Перекрасить ветвь"),
{
click: function(e, obj) {
var node = obj.part.adornedPart;
if (node !== null) {
myDiagram.startTransaction("remove dept");
var color = graygrad;
var diagram = myDiagram;
var parts = node.findTreeParts();
$.each(parts, function()
{
changeColor(diagram, color);
});
myDiagram.commitTransaction("remove dept");
}
}
}
),
$("ContextMenuButton",
$(go.TextBlock, "Сменить цвет (Фиолетовый)"),
{
click: function(e, obj) {
var node = obj.part.adornedPart;
if (node !== null) {
myDiagram.startTransaction("remove dept");
var color = graygrad;
var diagram = myDiagram;
changeColor(diagram, color);
myDiagram.commitTransaction("remove dept");
}
}
}
),
function changeColor(diagram, color) {
diagram.startTransaction("change color");
diagram.selection.each(function(node) {
if(node instanceof go.Node) {
var data = node.data;
diagram.model.setDataProperty(data, "color", color);
}
});
diagram.commitTransaction("change color");
}