Restoring group location when collapsed

My group template is binded to “position” as follows :
new go.Binding(“position” “loc”, go.Parse.point).makeTwoWay(go.point.stringify);

When a group is expanded i save the group current location : =;
When the group is collapsed, i try to restore its original location by :

But, the group location moves to a new arbitrary position.
How can i restore its original location ?

If your Group has a Placeholder, then the Group needs to be located where its Placeholder is. And the Placeholder will be wherever its member nodes are.

But you can move a group, including all of its member nodes and links, by calling Part.move or Part.moveTo. Perhaps in an event handler for Group | GoJS API ?

I tried the moveTo, it behaves exactly as my code.
I noticed that when the group is collapsed, its position.x value is negative (-357) ,
although the group is located in the center of the diagram.
So i believe when when i collapse the group, and restore the negative value, the group is moved to some other location, -357 relative to its expanded position.
Beside, why is the ‘X’ value negative, if the group is positioned in the center of the diagram ?

Part.position and Part.location are always in document coordinates.

If the point (-357, 0) is in the center of your viewport, it is because you diagram has been scrolled that way, causing Diagram.position to be even more negative than -357. GoJS Coordinate Systems-- Northwoods Software

So, what is the best way to restore the group position to its original position ?
which values to use ?

Which coordinate system are you asking about? Normally collapsing a group and then expanding it will keep it at its original location.

I’m not clear with your question.
What does it mean “coordinate system” ?

Please read GoJS Coordinate Systems-- Northwoods Software

I see,
I 'm using the Part.position, document coordinates.

So what are the values of Part.position and Part.location for that Group, before collapsing, after collapsing, and after expanding? Look in the debug console. If the group is selected: myDiagram.selection.first().location.toString()

The diagram starts with a collapsed group : (53,188)
After it is expanded its new location is set to (-450,1080)
After it is collapsed, its location is set to back to (53,188) but the Group moves “north”.
These are my handlers :

For the Group | GoJS API event handler, the Group is the argument, so you shouldn’t be looking at the selection. Use the selection when debugging interactively.

Maybe you want to override the CommandHandler commands for collapsing and expanding groups, to remember the Diagram.position beforehand, and to restore it afterwards in an “AnimationFinished” DiagramEvent Listener.

Actually the functions in the picture are “SubGraphCollapsed” and “SubGraphExpnded” diagram listeners respectively.
What’s wrong with implementing such listeners ? The timing of them ?
The Animation timing ?

There’s nothing wrong with implementing such event handlers – in fact that can often be useful. And then looking at the Diagram.selection might make sense.

However in your case getting called after a collapse or expand has happened doesn’t help you remember the value of Diagram.position before it happened.

So in which handler i should “catch” the group position “before” it is collapsed ?
And in which handler i should restore the group position “after” it is collapsed ?

Please read what I wrote more carefully. I said in my reply a few posts ago that you should override CommandHandler commands (CommandHandler.collapseSubGraph and expandSubGraph) so that you can remember the Diagram.position.

I did not say that you should implement a DiagramEvent listener and remember the Group.position.

Yes, you are right, this is exactly what i’m trying to do write now,
Still, the group moves “north” when collapsed.This is the code :

Your earlier debugging showed that expanding and then collapsing a group does not change its Part.location. So I do not understand what you are trying to do with the location of the group.

I’m trying to follow your advise and save and restore group.location during the command handler.
All the other code i wrote and showed you is not relevant any more.
I understood from your suggestions that i should override the command handler and not implement diagram listeners.

I’m finding this frustrating. Please read again what I have written twice. You have overrides of CommandHandler commands – that’s good. But you are trying to save and restore the Group.location when I keep telling you to save and restore the Diagram.position. Furthermore, as I said originally, if there is any animation, you might need to restore the Diagram.position after an “AnimationFinished” DiagramEvent listener