Event that signifies the tree is expanded/collapsed fully

Hi,

I have a very basic functionality to expand and collapse the tree of a particular node for which I have the following code.

node = diagramInstace.findNodeForKey(nodeId);
    						
if ( node.isTreeExpanded ) 
    diagramInstace.commandHandler.collapseTree(node);
else
    diagramInstace.commandHandler.expandTree(node);

diagramInstace.commandhandler.scrollToPart(node.actualBounds);

But the problems here is the scrollToPart is not working as expected.

I have tree layout applied by default on diagram.

Upon debugging it looked like my scrollToPart statement is executing before the tree is completely expanded or collapsed.

I tried TreeCollapsed and TreeExpanded event on diagram but still no luck.
Following is my code in these event

var it = arguments[0].subject.iterator;
while (it.next()) {
   var item = it.value;
   node = this._diagramInstace.findNodeForKey(item.data.id);
   this._diagramInstace.commandHandler.scrollToPart(node.actualBounds);
}

So is there a event which states the expand/ collapse process is completed and I can scrollToPart now.

I just tried this code in a sample that happens to have a tree in it:

    function test() {
      var nodes = new go.List(myDiagram.nodes).toArray();
      var node = nodes[Math.floor(Math.random() * nodes.length)];
      if (node) {
        myDiagram.select(node);
        if (node.isTreeExpanded) {
          myDiagram.commandHandler.collapseTree(node);
        } else {
          myDiagram.commandHandler.expandTree(node);
        }
        myDiagram.commandHandler.scrollToPart(node);
      }
    }

Calling test() repeatedly seems to work well. Each time the diagram would be scrolled so that the selected and expanded-or-collapsed node was visible in the viewport.

Precisely which version of GoJS are you using? I’m using 2.1.9, but I have also tried 2.0.21 successfully.

To be precise I am running the above code on GoJS v1.8.33

and one small update though if I run the last code line as the following code then it works as expected.

    setTimeout(function() {
       that._diagramInstace.commandhandler.scrollToPart(node);
    },10);

OK

Yes, it was a new feature in v2.0 to have the CommandHandler.scrollToPart command automatically expand trees and groups if needed.

So it should work if I don’t use CommandHandler and directly use diagram.scrollToPart. correct ?