Search code examples
javascriptgraphgojs

Getting TextBlocks of Panel in GoJS


I need some help with GoJS. I'm working with the sample on this page in SubGraphExpanderButtons section. For example, I want to set color of all TextBlock in a group to red by clicking the SubGraphExpanderButton. I add click event handler

...
$("SubGraphExpanderButton", {
    margin: new go.Margin(0, 3, 5, 0),
    click: function(e, button) {
       ...
    }
}),
...

According to structure of the sample I try to get all the TextBlock something like

button.panel.panel.elt(1)

because the button is in the Horizontal Panel, the Horizontal Panel is in the Vertical Panel and in the Vertical Panel there is the Placeholder which contains all the TextBlocks. But it's wrong. I'm not even sure that this selector get me exactly the Placeholder, and even it this is so, I cann't get nested elements from object I get. Looks like I've misunderstood the very concept of GoJS.

So, my question is, how can I get all The TextBlocks group?


Solution

  • The member Nodes and Links of a Group are not in the visual tree of the containing Group. Every Part (including Nodes and Links and Groups) is a top-level object with GraphObject.panel being null. So you cannot navigate through the visual tree that is a hierarchy of Panels and GraphObjects to get to any member Node. Nor vice-versa, from a member Node through .panel to get to the containing Group.

    Instead, from a Group you can use Group.memberParts to get to its member Nodes and Links, if it has any. And from a Node or a Link you can look at Part.containingGroup to get to its containing Group, if there is any.

    button.part.memberParts.each(function(member) { if (member instanceof go.Node) { . . . } })

    More discussion is at https://forum.nwoods.com/t/getting-textblocks-of-panel-in-gojs/8009.