Hi there!
Somehow the PortShiftingTool
and using DynamicPorts
with itemTemplate
seem incompatible to me…
Please correct me if I’m wrong, but here’s how I understood the two:
The PortShiftingTool
requires the ports (no matter if the ports are panels or shapes) to be in a Spot Panel
, but the port must not be the “main element”…
Whereas an itemTemplate
must be a Panel
(at least that’s what my typescript editor tells me).
I started with a nodeTemplate
that could be used in a diagram, links between ports could be drawn, everything worked nice. It’s the template at the end, except for the portId
binding of the itemTemplate
’s Panel
, which I didn’t need yet.
Then I wanted to add the ability to move ports around, and added the PortShiftingTool
, which…just didn’t work.
The tool’s findPort
function returned null - why? because my port (marked by portId
) was the itemTemplate
’s Shape
(Rectangle) inside the itemTemplate
’s Panel
, and findPort
only looks one layer up for a Spot Panel
.
Next I tried to make the itemTemplate
’s Panel
a Spot Panel
, but that didn’t work either, because the port must not be the “main element”, but findMainElement
defaults to the first child.
What worked was moving the portId
binding up a level, then the PortShiftingTool
suddenly was content and findPort
returned a result.
And I was really happy to have found such a simple solution… until I wanted to create a Link
, which - all of a sudden - did not work any more (wtf! ;)). For this to work the portId
had to be bound to the Shape
, moving the binding back and forth between the two illustrates the behaviour.
Now I wonder if it’s acceptable to bind the portId
to both, like so:
$(go.Node, "Vertical",
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
$(go.Panel, "Spot",
$(go.Panel, "Auto", { padding: 4 },
$(go.Picture, new go.Binding("source", "imageUrl"))
),
new go.Binding("itemArray", "contactpoints"),
{
itemTemplate: $(go.Panel, { margin: 0 },
new go.Binding("alignment", "", (obj) => new go.Spot(obj.x, obj.y)),
new go.Binding("portId", "id", (id) => "" + id), // portId necessary for the PortShiftingTool
$(go.Shape, "Rectangle",
{
width: 4, height: 4, fill: "blue", stroke: null, strokeWidth: 0,
fromLinkable: true, toLinkable: true, cursor: "pointer"
},
new go.Binding("portId", "id", (id) => "" + id) // portId necessary for creating links
)
)
}
)
);
…or if I should tweak the PortShiftingTool
to look two layers up (from my Rectangle
to the Spot Panel
).
Or if there’s another solution the gojs-team deems “right” in this case…? :)
Thanks again in advance for your time and effort!
Florian