Create an adornment that is added or removed during "runtime"

I have a group which has several node children as a list. Each node has a selection adornment (button) that allows the removal of that node. There must be at least one node, so I need to remove the adornment for that node in the case that there is only one node remaining.

I’ve tried using a click listener on the node, which could allow me to quickly remove the adornment before it is rendered. However, when I access the Part that is passed as an object to the callback, it doesn’t list any adornments.

Any advice would be appreciated.

One idea is to add a Binding on the button’s GraphObject.visible property.

selectionAdornmentTemplate:
  $(go.Adornment, "Spot",
    $(go.Panel, "Auto",
      $(go.Shape, { fill: null, stroke: "dodgerblue", strokeWidth: 4 }),
      $(go.Placeholder, { padding: 3 })
    ),
    $("Button",
      {
        alignment: go.Spot.TopRight,
        click: function(e, button) {
          var node = button.part.adornedPart;
          e.diagram.commit(function(diag) {
            diag.remove(node);
          }, "removed node");
        }
      },
      new go.Binding("visible", "", function(ad) {
        var node = ad.adornedPart;
        var group = node.containingGroup;
        if (!group) return node.diagram.nodes.count > 1;
        return group.memberParts.count > 1;
      }).ofObject(),
      $(go.Shape, "XLine",
        { width: 10, height: 10,
          stroke: "red", strokeWidth: 2, background: "transparent" })
    )
  )

It wasn’t clear what to do if the node were not a member of a group, but you can adapt the code to meet your needs.

Amazing, you guys rock. Thank you!

Interesting!
if an empty string argument for the sourceprop parameter indicates that the whole data object should be passed to the function,

But when Binding SelectionAdornment it will pass the Adornment object instead of nodes whole data object right? i am not found it in document maybe my overlooked.

Note that that Binding was also a Binding.ofObject, i.e. of a GraphObject, not of the data.

1 Like