# GoJS Interactivity

## 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:

- Click a node to select it. Click and drag on a node to move it around, or click and drag on the diagram background to pan the entire Diagram.
- Common keyboard shortcuts such as `CTRL-C`, `CTRL-V`, `CTRL-X` will copy, paste, and cut diagram parts, respectively.
- Drag-and-hold allows you to create a selection box for selecting multiple nodes. CTRL-clicking on nodes allows you to toggle their selection.
- Since the Diagram's undoManager was enabled, `CTRL-Z` and `CTRL-Y` will undo and redo operations. Diagram-panning and selection are not considered undoable operations.

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 }
    ]);

- `clickCreatingTool.archetypeNodeData` allows a double-click in the background to create a new node with the specified data.
- `commandHandler.archetypeGroupData` allows `CTRL-G` to group a selection of nodes, and `CTRL-Shift-G` to ungroup.
- `toolManager.mouseWheelBehavior` allows the mouse wheel to zoom instead of scroll by default. You can toggle this property by clicking on the mouse-wheel. On touch devices, pinch-zooming is enabled by default.
- `editable` to true in the `TextBlock` definition allows the text to be edited in place. Select a node and then click on the text, or press F2, to begin text editing. Click anywhere else on the diagram or press `Tab` to finish editing text.

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:

- The `Shape` has a `pordId` set to make it the port instead of the entire node. Then several `...linkable` properties are set, allowing each node to link to itself and others.
- To create a new link to another node, drag from the port (the edge of the Shape that is not behind the TextBlock) to any node, including itself.
- In the link template, `relinkable...` properties are set, which create handles on the link when selected. You can drag these handles to change a link's from and to node.

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.