Unable to find object by name

@walter mostly its working but … for text block and contextMenu etc … this is not working

go.Diagram.fromDiv("HTML_ID").findNodeForKey(Some_Number).findObject("name Key of the Go Object")

am i doing right ? the gojs code is something of this sort

> $('ContextMenu',
              $(go.TextBlock, 'Text values',
                {name: 'ForFindingintestauotmation'}
                ),
              $('ContextMenuButton',
                $(go.TextBlock, 'Some_Value',
                  {
                    {name: 'ForFindingintestauotmation1',
                    font: 'some font',
                    alignment: left rigfht etc
                  }),
                {
                  click: (e) => {
                    commandhandler methods;
                  }
                }
              ),
              $('ContextMenuButton',
                $(go.TextBlock, 'Some_Other_Value',
                  {
                    {name: 'ForFindingintestauotmation3',
                    font: 'some font',
                    alignment: left rigfht etc
                  }
                )
              )
            )

The doc api says that

But when i use it

Similarly Only below is working, for node
… none of the api as we use it as it says in docs work , am i looking at it correctly ?.

go.Diagram.fromDiv("id").findNodeForKey(1).findObject("Some_Of_The_non_Textblock_Shape_Names").getDocumentPoint(go.Spot.Center)

image

Context menus are implemented as either a GoJS Adornment or an HTML Element. GoJS Context Menus -- Northwoods Software

Once the context menu is showing, get the myDiagram.toolManager.contextMenuTool.currentContextMenu.

If the value is an Adornment, you can call Panel.findObject on it.

If the value is an HTMLInfo, maybe you can examine the HTMLInfo.mainElement – it depends on how the HTML was implemented.

Thanks @walter I got this command and got the context menu itself…

go.Diagram.fromDiv("Id").toolManager.contextMenuTool.currentContextMenu.findObject('Named_Key').part.location

However there are multiple text blocks inside the context menu

I can see them having keys like Name_1 and Name 2…
how do I get the model data for these …
the part looks like a node object itself and not like model

I would like to use findNodesByExample() … but trying to figure how

 go.Diagram.fromDiv("id").findNodesByExample({key: "Name1 Or Name_2"}) does not work either

By using model i am able to get the location co ordinates easily thats why

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);
}

You might want to review:
https://gojs.net/latest/intro/buildingObjects.html#VisualStructureOfNodesAndLinks

And if you look at this sample:

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

gives x and y too [ should be maybe x2 and y2]

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.

Thanks a lot @walter