Why the difference in whether LayoutCompleted is called?

I can’t seem to duplicate this in a simple fiddle yet, so I hope it’s mostly my misunderstanding of the framework.

In my real app, I want to listen to the diagram’s LayoutCompleted event so I can take a thumbnail image and store it away. In general, this event seems to fire when I expect, such as when I add a new node to the diagram with myDiagram.model.addNodeData(mynode). But in my app, the LayoutCompleted event fires only when there’s a link added and not simply when I add a node on its own. I want to catch an event like this whenever a node is added, regardless of its links. If I do something silly like add a bogus empty link with “myDiagram.model.addLinkData({})” at the same time I add the node, this does seem to elicit the LayoutCompleted, which is nice, though it also leaves this empty link in my linkDataArray which is not nice.

I created this fiddle (Edit fiddle - JSFiddle - Code Playground) with the goal of trying to replicate this effect, but here the event fires every time, regardless of whether there’s also a link added. So, I’m not sure how to handle this.

Is there an obvious reason why adding nodes with no links would not cause a layout followed by a LayoutComplete event?


It sounds as if the layout is not being invalidated when you add a node. But the default behavior is to invalidate the layout when a node has been added, so perhaps your Node template has Part.layoutConditions set to some flag combination that does not include Part.LayoutAdded.

Oh, thanks so much for the pointer.

I am not ever modifying layoutConditions for my parts, but I am setting isLayoutPositioned to false, because I wanted to manually control aspects of the document’s positioning and that seemed to work well. I see that the docs point out that layouts aren’t invalidated when non-positioned parts are added.

It looks like I can execute a diagram.layoutDiagram(true) after I add nodes without links to the diagram and it triggers the LayoutCompleted event. Is this reasonable?

That would also explain the behavior. Yes, I think you could do what you suggest.

Another possibility is to call Layout.invalidateLayout on the Layout that is responsible for positioning those nodes. That might be more efficient depending on how your diagram is configured.