The myDiagram.toolManager.contextMenuTool.currentObject will be the GraphObject for which the context menu was invoked, or it will be null if the context menu is for the whole Diagram rather than for a particular object. It may also be null if there is no context menu shown – i.e. if ContextMenuTool.currentContextMenu is null.
If the **ContextMenuTool.currentObject` is non-null, then its GraphObject.part will be the Part (presumably a Node in your case) for which the context menu is being shown. You can look at its Part.location or other properties, or you can look at its Part.data and its properties.
You don’t need to find any particular named GraphObject within the context menu unless you really want that particular object. Just getting its GraphObject.part again gets you the context menu again. I.e.
var cm = myDiagram.toolManager.contextMenuTool.currentContextMenu;
if (cm) {
if (cm.findObject("SomeName")) assert(cm === cm.findObject("SomeName").part);
console.log(cm.adornedPart);
console.log(cm.data);
assert(cm.adornedPart.data === cm.data);
}
select a node and click the “Draw Visual Tree” button, so that you can see the overall visual tree for that sample’s example diagram.
Regarding Diagram.findNodesByExample – that returns a (perhaps empty) collection of Nodes. That searches the model’s Model.nodeDataArray looking for data properties that match whatever object you give to the findNodesByExample method.
See the example at Org Chart Static
sorry @walter, think I incorrectly directed question . I am able to get the context menu. But I need to click one of the many text blocks like go.TextBlock … i added named keys there to identify, and its showing in the context menu object with keys …
I am also able to get the context menu sub keys to get the object node using this … but location is showing same … it should be a little different
Like
go.Diagram.fromDiv("id").toolManager.contextMenuTool.currentContextMenu.findObject('Main context menu key name').part.location
gives x and y and
go.Diagram.fromDiv("id").toolManager.contextMenuTool.currentContextMenu.findObject('Sub key 01 of Main context menu').part.location
gives x and y too [ should be maybe x1 and y1]
go.Diagram.fromDiv("id").toolManager.contextMenuTool.currentContextMenu.findObject('Sub key 02 of Main context menu').part.location
The GraphObject.name property is not a “key” because the names need not be unique within the whole Part or Panel.
But yes, if you have given each of your "ContextMenuButton"s a name, then you should be able to evaluate cm.findObject("some name").getDocumentPoint(go.Spot.Center) to get the center of the button in document coordinates. Here I am assuming cm is the context menu – the Adornment.
Call Diagram.transformDocToView to get the Point in viewport coordinates, although almost everything with Parts in GoJS just deals with document coordinates.
hmm … no sorry did not understand you … i was specifically looking to get sub elements/objects locations … are we on same page walter. context menu itself not a issue, sub elements locations are …
Yes, I was talking about the buttons within the context menu.
I just tried modifying the Basic sample, Basic GoJS Sample. All I did was add an assignment of the button’s name:
function makeButton(text, action, visiblePredicate) {
return $("ContextMenuButton",
$(go.TextBlock, text),
{ click: action, name: text },
. . .
Then after showing a context menu for a node, I evaluated the following in the console:
var cm = myDiagram.toolManager.contextMenuTool.currentContextMenu
cm.findObject("Cut").getDocumentPoint(go.Spot.Center).toString()
"Point(171.8333282470703,147.28332824707033)"
cm.findObject("Copy").getDocumentPoint(go.Spot.Center).toString()
"Point(171.8333282470703,166.5833282470703)"
cm.findObject("Group").getDocumentPoint(go.Spot.Center).toString()
"Point(171.8333282470703,205.1833282470703)"
You can see from the resulting Points of the different Buttons that they have the same X value but different Y values.