Built-in GoJS Interactivity

GoJS implements a number of common editing operations, such as manipulating parts (moving, adding, copying, cutting, and deleting). These editing abilities are accessible via mouse (or touch) and keyboard by default, and can also be invoked programatically in JavaScript.

The following Diagram defines a node template and has four nodes in its model:


var $ = go.GraphObject.make;
myDiagram = $(go.Diagram, "myDiagramDiv",
              {
                initialContentAlignment: go.Spot.Center,
                "undoManager.isEnabled": true
              });

// define a simple Node template
myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    $(go.Shape, "Rectangle",
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif" },
      new go.Binding("text", "key"))
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" },
  { key: "Gamma", color: "lightgreen" },
  { key: "Delta", color: "pink" }
]);
    

Out of the box, several interactions are available:


By setting a few properties you can expose more interaction to a user:


var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
              {
                initialContentAlignment: go.Spot.Center,
                // allow double-click in background to create a new node
                "clickCreatingTool.archetypeNodeData": { key: "Node", color: "white" },
                // allow Ctrl-G to group and Ctrl-Shift-G to ungroup
                "commandHandler.archetypeGroupData": { text: "Group", isGroup: true },
                // have mouse wheel events zoom in and out instead of scroll up and down
                "toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
                "undoManager.isEnabled": true // enable undo & redo
              });

myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    $(go.Shape, "Rectangle",
      { stroke: null, name: "SHAPE" },
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif", editable: true },
      new go.Binding("text", "key"))
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" },
  { key: "Gamma", color: "lightgreen", group: "Group1" },
  { key: "Delta", color: "pink", group: "Group1" },
  { key: "Group1", isGroup: true }
]);
    

These interactions are all present in the basic.html sample. Be sure to also see the Intro page on GoJS commands.

You can turn off portions of Diagram interactivity with several properties, depending on what you want to allow users to do. See the Intro page on GoJS permissions for more.

Context Menus

GoJS provides a mechanism for you to define context menus for any object or for the Diagram itself. In the example below, two context menus are defined, one on the Node template (with one button) and one on the Diagram (with two buttons).


var $ = go.GraphObject.make;
var myDiagram = $(go.Diagram, divname,
              {
                initialContentAlignment: go.Spot.Center,
                "undoManager.isEnabled": true // enable undo & redo
              });

// this method is called when the context menu button is clicked
function clickButton(e, obj) {
  alert('node ' + obj.part.data.key + ' was clicked');
}

// defines a context menu to be referenced in the node template
var contextMenuTemplate =
  $(go.Adornment, "Vertical",
    $("ContextMenuButton",
      $(go.TextBlock, "Click me!"),
      { click: clickButton })
    // more ContextMenuButtons would go here
  );

myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    { contextMenu: contextMenuTemplate }, // set the context menu
    $(go.Shape, "Rectangle",
      { stroke: null, name: "SHAPE" },
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif" },
      new go.Binding("text", "key"))
  );

// this method alerts the current number of nodes in the Diagram
function countNodes(e, obj) {
  alert('there are ' + e.diagram.nodes.count + ' nodes');
}

// this method creates a new node and inserts it at the last event's point
function addNode(e, obj) {
  var data = { key: "Node", color: "white" };
  e.diagram.model.addNodeData(data);
  var node = e.diagram.findPartForData(data);
  node.location = e.diagram.lastInput.documentPoint;
}

myDiagram.contextMenu =
  $(go.Adornment, "Vertical",
    $("ContextMenuButton",
      $(go.TextBlock, "Count Nodes"),
      { click: countNodes }),
    $("ContextMenuButton",
      $(go.TextBlock, "Add Node"),
      { click: addNode })
    // more ContextMenuButtons would go here
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" }
]);
return myDiagram;
    

If you right-click (or long-tap on a touch device) on a Node or the Diagram, you will see the GoJS context menu appear with the defined options.

The basic.html sample contains more complex context menu examples. See the Intro page on GoJS context menus for more.

Link interactions

GoJS lets users draw new links and re-link existing ones by dragging out from a port or selecting a link and draggings its handles. To enable these tools, some properties need to be set:


myDiagram.nodeTemplate =
  $(go.Node, "Auto",
    $(go.Shape, "Rectangle",
      {
        stroke: null, name: "SHAPE",
        portId: "", cursor: "pointer",  // make the Shape the port, not the whole Node
        // allow all kinds of links from and to this port:
        fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true,
        toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true
       },
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 6, font: "18px sans-serif" },
      new go.Binding("text", "key"))
  );

myDiagram.linkTemplate =
  $(go.Link,
    {
      toShortLength: 4,
      // allow the user to relink existing links:
      relinkableFrom: true, relinkableTo: true
    },
    $(go.Shape,
      { strokeWidth: 2 }),
    $(go.Shape,
      { toArrow: "Standard", stroke: null })
  );

myDiagram.model = new go.GraphLinksModel(
[
  { key: "Alpha", color: "lightblue" },
  { key: "Beta", color: "orange" },
  { key: "Gamma", color: "lightgreen" },
  { key: "Delta", color: "pink" }
],
[
  { from: 'Alpha', to: 'Beta' },
  { from: 'Alpha', to: 'Delta' },
]
);
    

In the above example:

GoJS allows linking and re-linking to abide by custom criteria in which source and destination nodes are valid. You can read about this in the Intro page on Validation.

GoJS links have many interesting properties that are covered in depth in the Intro page on Links.