How to build palette programatically?


Is there any example how to add new nodes to the palette programatically (let’s say in a loop)? Each node’s properties have to be set programatically (colours, number of ports, etc).

I’ve been playing with diagram.nodeTemplate and it looks like $=go.GraphObject.make takes varargs parameters so the numbers of parameters depends on number of ports. I tried to use
go.GraphObject.make.apply(null, array);
where array describes all parameters but I’m getting
"Cannot add a GraphObject that already belongs to another Panel to this Panel: Panel(Panel.Auto)#1110, already contained by Node#1128, cannot be shared by this Panel: Node#11311 "

Is there example to follow?

Thank you

There are lots of examples of initializing Palettes throughout the samples, and at

Basically, you just want to add data to the palette’s model. You can share the same templates that your regular diagram uses. Until you want to do something different and better for your palettes.

Hi Walter,

Yes I’ve seen those examples and it looks like the palette node template is hardcoded regarding number of input and output ports.

So the specific question is:
How do I build a palette containing N elements where each element will have a different number of input and output ports? It will be driven by json file so number of palette elements nor number of ports if not known in advance.

I guess I need a node template map to define different types of palette elements, but how do I define programatically dynamic number of ports?

Thank you

It’s exactly the same process as creating N nodes in a Diagram, each with different number of ports (or anything else). The Palette class inherits from the Diagram class.

You can see an example of a diagram holding nodes with different numbers of ports but all using the same template in the Dynamic Ports sample. That also demonstrates changing the number of ports on a side dynamically.