Adornment menu only on last selected entity

I created an adornment menu following one of your samples found here and I’d like to know if in case of multiple entity selection is it possible to show the menu only on the last selected entity.

In the example if I select more than one entity the situation is like this one

and I’d like to obtain something like that


OK, starting from the Adornment Buttons sample…

First, since you clearly don’t want the selection Adornment template to include buttons, there’s no reason to specify a custom Node.selectionAdornmentTemplate.

Second, since there will be at most one such set of "Button"s visible at a time, they will be a single Adornment, not a template. So change the code to be like:

    var adornmentButtons =
      $(go.Adornment, "Spot",
        $(go.Panel, "Horizontal",
          . . .

Third, we need to notice as the Diagram.selection collection changes. So we add a “ChangedSelection” DiagramEvent listener:

    $(go.Diagram, . . .,
        "ChangedSelection": function(e) {
          var node = null;  // find the last selected Node, if any
          e.diagram.selection.each(function(p) { if (p instanceof go.Node) node = p; });
          if (node && node !== adornmentButtons.adornedPart) {
            adornmentButtons.adornedObject = node;
            node.addAdornment("Buttons", adornmentButtons);
          } else if (!node) {
            var oldnode = adornmentButtons.adornedPart;
            if (oldnode) {
              adornmentButtons.adornedObject = null;
        . . .

Note how it finds the last selected Node, not just the last selected Part that might be a Link or something else.

Fourth, if we want to select a Node before the Diagram is fully initialized, we should do something like:

    var node = myDiagram.findNodeForKey(4);
    if (node) {
      node.isSelected = true;
      adornmentButtons.adornedObject = node;
      node.addAdornment("Buttons", adornmentButtons);

Thank you Walter!
I would like to meet you for giving you a fruit basket as a sign of gratitude for all the times that you helped me!

Thank you, but I suggest that you donate your time and money to local charities that efficiently help people in ways that you admire.