# Class ContextMenuTool

GoJS® Diagramming Components
version 3.0.7
by Northwoods Software®

#### Hierarchy

- Tool
  - ContextMenuTool

The ContextMenuTool is used to create and show a context menu. It automatically disables any browser context menu.

Define context menus on individual GraphObjects by setting GraphObject.contextMenu. Define a context menu for the diagram background by setting Diagram.contextMenu.

This tool is a standard mouse-down tool, the ToolManager.contextMenuTool.

This tool does not utilize any tool handles. This tool does not modify the model or conduct any transaction, although any code invoked by context menu commands might do so.

There are examples of customizing this tool in the Custom Context Menu and HTML LightBox Context Menu samples.

If you want to programmatically show a context menu for a particular GraphObject or for the whole diagram, call CommandHandler.showContextMenu. That command method is also invoked by the Menu key on the keyboard.

Normally this shows a context menu (if available) on a right-mouse-up event. If you want it to happen on a right-mouse-down event, you'll need to move this tool from the ToolManager.mouseUpTools list to the ToolManager.mouseDownTools list:

    myDiagram.toolManager.mouseDownTools.add(myDiagram.toolManager.replaceTool("ContextMenu", null));

#####
Index

### Constructors

constructor

### Accessors

currentContextMenu
currentObject
defaultTouchContextMenu
mouseDownPoint

### Methods

canStart
doActivate
doMouseDown
doMouseMove
doMouseUp
findObjectWithContextMenu
hideContextMenu
hideDefaultContextMenu
positionContextMenu
showContextMenu
showDefaultContextMenu

#####
Inherited Index

### Accessors

Tool.diagram
Tool.isActive
Tool.isEnabled
Tool.name
Tool.transactionResult

### Methods

Tool.canStartMultiTouch
Tool.cancelWaitAfter
Tool.doCancel
Tool.doDeactivate
Tool.doKeyDown
Tool.doKeyUp
Tool.doMouseWheel
Tool.doWaitAfter
Tool.findToolHandleAt
Tool.isBeyondDragSize
Tool.standardMouseClick
Tool.standardMouseOver
Tool.standardMouseSelect
Tool.standardMouseWheel
Tool.standardPinchZoomMove
Tool.standardPinchZoomStart
Tool.standardWaitAfter
Tool.startTransaction
Tool.stopTool
Tool.stopTransaction
Tool.updateAdornments

## Constructors

### constructor

- new ContextMenuTool(init?: Partial<ContextMenuTool>): ContextMenuTool

- You do not normally need to create an instance of this tool because one already exists as the ToolManager.contextMenuTool, which you can modify.

  The Tool.name of this tool is "ContextMenu".

  #### Parameters

  - ##### `Optional` init: Partial<ContextMenuTool>

  #### Returns ContextMenuTool

## Accessors

### currentContextMenu
: Adornment | HTMLInfo

- Gets or sets the currently showing context menu, or null if there is none. This is typically only set in showContextMenu and not by the user. It is also typically set to null in hideContextMenu.

### currentObject
: GraphObject

- Gets or sets the GraphObject found at the mouse point that has a context menu. This property remembers the result returned by findObjectWithContextMenu if it is a GraphObject. This value is passed to showContextMenu as the second argument. The value will be null if the context menu is for the diagram rather than for a particular GraphObject.

### defaultTouchContextMenu
: Adornment | HTMLInfo

- Gets or sets the HTMLInfo or Adornment that acts as the default touch context menu. On touch devices, this context menu will appear even there is no context menu defined.

  You can disable this functionality by setting this property to null.

  By default shows a set of HTML elements acting as a context menu.

### `Readonly` mouseDownPoint
: Point

- This read-only property returns the original mouse-down point in document coordinates.

## Methods

### `Virtual` `Override` canStart

- canStart(): boolean

- Return true if it's a single mouse right click that hasn't moved Tool.isBeyondDragSize and that is on a GraphObject with a GraphObject.contextMenu. This is also true if the mouse right click is in the diagram background and the diagram's Diagram.contextMenu is non-null.

  On touch devices, a special default context menu will appear even if no object with a context menu is found.

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns boolean

### `Override` doActivate

- doActivate(): void

- Do nothing, activation is special and relies on doMouseUp

  #### Returns void

### `Override` doMouseDown

- doMouseDown(): void

- Activate this but also execute the normal behavior that would happen on a mouse-up if this tool is in the ToolManager.mouseDownTools list. Perform ActionTool.doMouseDown if a ContextMenuButton was hit.

  #### Returns void

### `Override` doMouseMove

- doMouseMove(): void

- Handle mouse-enter, mouse-over, and mouse-leave events, as well as tooltips.

  #### Returns void

### `Override` doMouseUp

- doMouseUp(): void

- If there is something found by findObjectWithContextMenu, call showContextMenu with that object's GraphObject.contextMenu or the Diagram.contextMenu.

  Once a context menu is being shown, if a click occurs on a part of the context menu, call Tool.standardMouseClick. Otherwise if the click occurs elsewhere, just stop this tool. Unlike most tools, the first mouse-up should not stop this tool.

  #### Returns void

### `Virtual` findObjectWithContextMenu

- findObjectWithContextMenu(obj?: Diagram | GraphObject): Diagram | GraphObject

- Find a GraphObject at the current mouse point with a GraphObject.contextMenu, or return the Diagram if there is a Diagram.contextMenu.

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Parameters

  - ##### `Optional` obj: Diagram | GraphObject

    Optional GraphObject with which to start searching for a context menu. If null, the Diagram will be used. If no argument is specified, this method will look for an object at the current mouse point.

  #### Returns Diagram | GraphObject

  something with a `contextMenu`, or null if nothing can be found with a context menu at the current mouse point.

### `Virtual` hideContextMenu

- hideContextMenu(): void

- Hide any context menu.

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns void

### `Virtual` hideDefaultContextMenu

- hideDefaultContextMenu(): void

- This is the HTMLInfo.hide method for the defaultTouchContextMenu.

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns void

### `Virtual` positionContextMenu

- positionContextMenu(contextmenu: Adornment, obj: GraphObject): void

- This is called by showContextMenu to position the context menu within the viewport. It normally goes just below the cursor. But if the mouse is too close to the right edge or the bottom edge of the viewport, it is positioned left and/or above the cursor.

  This method only operates if the context menu, an Adornment, does not have a Adornment.placeholder. When there is a Placeholder in the context menu, that Adornment is automatically positioned so that the Placeholder is positioned at the adorned object, the second argument to this method.

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  If you override this method to position the context menu, the context menu has already been measured but not arranged, so you can use its GraphObject.measuredBounds width and height but not its GraphObject.actualBounds.

  #### Parameters

  - ##### contextmenu: Adornment

  - ##### obj: GraphObject

    The GraphObject getting the context menu, or null if the context menu is for the diagram background.

  #### Returns void

### `Virtual` showContextMenu

- showContextMenu(contextmenu: Adornment | HTMLInfo, obj: GraphObject): void

- Show an Adornment or HTMLInfo as a context menu.

  This method is called by the context click (Tool.doMouseDown) and CommandHandler.showContextMenu. If you want to programmatically show a context menu for a particular GraphObject or for the whole diagram, do not call this method, which only does one small piece of the process of bringing up a context menu. Instead call CommandHandler.showContextMenu, which will start this tool and eventually call this method and handle additional input events.

  For Adornment context menus: If the object's containing Part is data-bound, this sets the contextmenu's `Part.data` to the same value. The Adornment.adornedObject property is set to the GraphObject for which the menu is being shown.

  This method sets the currentContextMenu.

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Parameters

  - ##### contextmenu: Adornment | HTMLInfo

  - ##### obj: GraphObject

    the GraphObject for which the context menu is being shown; this is null if the contextmenu is being shown for the diagram background.

  #### Returns void

### `Virtual` showDefaultContextMenu

- showDefaultContextMenu(): void

- This is the HTMLInfo.show method for the defaultTouchContextMenu.

  If the object's containing Part is data-bound, set the contextmenu's `Part.data` to the same value. The Adornment.adornedObject property is set to the GraphObject for which the menu is being shown.

  The menu carries some default CSS styling and uses the following CSS classes:

  - `goCXforeground` for the DIV containing the buttons
  - `goCXbackground` for the darker DIV behind the context menu
  - `goCXul` for the HTML `ul` items
  - `goCXli` for the HTML `li` tag items
  - `goCXa` for the HTML `a` tag items

  This method may be overridden. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns void

Copyright © 1998-2024 by Northwoods Software Corporation.