GoJS API
/ to search
    Preparing search index...

    Class 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));
    

    Hierarchy (View Summary)

    Index

    Constructors

    Accessors

    • get currentObject(): GraphObject | null

      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.

      Returns GraphObject | null

    • get defaultTouchContextMenu(): Adornment | HTMLInfo | null

      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.

      Returns Adornment | HTMLInfo | null

    • get diagram(): Diagram

      This read-only property returns the Diagram that owns this tool and for which this tool is handling input events.

      Returns Diagram

    • get isActive(): boolean

      Gets or sets whether this tool is started and is actively doing something.

      You can set this to true after your tool is started (i.e. when it is the Diagram.currentTool and doStart had been called), but when it is not yet in a state that it is actually "doing" something, because it is waiting for the right circumstances. This is typically only important when the tool is used in a modal fashion.

      The default value is false. This is normally set by doActivate and doDeactivate.

      Returns boolean

    • get isEnabled(): boolean

      Gets or sets whether this tool can be started by a mouse event.

      Set this to false to prevent canStart from returning true. Setting this property to false should prevent this tool from being used in a mode-less fashion by the ToolManager with a mouse down/move/up event. However, even when this property is false, this tool can still be used in a modal fashion: it can still be started by explicitly setting the Diagram.currentTool property to this tool.

      The default value is true.

      Returns boolean

    • get mouseDownPoint(): Point

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

      Returns Point

    • get name(): string

      Gets or sets the name of this tool. The default name is an empty string, but the constructor for each instance of a subclass of Tool will initialize it appropriately. For example, the name of the DragSelectingTool is "DragSelecting".

      This name is sometimes used by tools that use Adornments as the Part.category for their Adornments. It is also sometimes used by tools that conduct transactions as the transaction name.

      Returns string

    • get transactionResult(): string | null

      Gets or sets the name of the transaction to be committed by stopTransaction

      If null, the transaction will be rolled back.

      If this is non-null at the time of a call to stopTransaction, it calls Diagram.commitTransaction with this transaction name; if this is null at that time, it calls Diagram.rollbackTransaction.

      The default value is null; startTransaction will also set this to null. Because a value of null when stopTransaction is called will rollback the transaction, it is important that your code sets this property to a non-null value when it thinks it has succeeded.

      This property exists so that no matter what execution path occurs to end the usage of a tool, any ongoing transaction can be properly committed or rolled-back. Many tools call startTransaction and stopTransaction; thus they set this property for their transaction to be committed. doCancel also sets this property to null.

      Returns string | null

    Methods

    • This is called to cancel any running "WaitAfter" timer.

      This is called when a tool is stopped.

      This method is rarely overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

    • 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 Learn page on Extensions for how to override methods and how to call this base method.

      Returns boolean

    • This method controls for some Tools whether canStart may return true depending on the button being used. Normally this returns true if InputEvent.left is true. This method may be overridden to consider other buttons, or to ignore which button by just returning true.

      Returns boolean

      true, if the left mouse button is being held down

      since

      4.0

    • As of version 4, this predicate method is no longer called by the ToolManager. Most of the responsibility for pinch zooming has been moved to the PanningTool.

      Returns boolean

      deprecated
    • Do nothing, activation is special and relies on doMouseUp

      Returns void

    • The diagram will call this method when the user wishes to cancel the current tool's operation. Typically this is called when the user hits the ESCAPE key. This should restore the original state of what was modified by this tool, and then it should call stopTool. This method is not responsible for cleaning up any side-effects that should be performed by doDeactivate and/or doStop, which will always be called whether the tool stops normally or abnormally.

      By default this method just sets transactionResult to null and calls stopTool. You will want to override this method even in tools that call startTransaction and stopTransaction, because the UndoManager might not be enabled. This method may be overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

    • The Diagram calls this method on the old tool when Diagram.currentTool is set to a new tool. This needs to set isActive to false. Overrides of this method might call stopTransaction, if this tool's activity involves modification of the model.

      You should have no reason to call this method, because it is automatically called by the Diagram.currentTool property setter on the old tool.

      By default this only sets isActive to false. This method may be overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      It is commonplace to override this method in order to clean up whatever you have set up in an override of doActivate.

      Returns void

    • The diagram will call this method upon a key down event. By default this just calls doCancel if the key is the ESCAPE key. Implementations of this method can look at Diagram.lastInput to get the key.

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

      Returns void

    • The diagram will call this method upon a key up event. Implementations of this method can look at Diagram.lastInput to get the key.

      By default this method does nothing. This method may be overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

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

      Returns void

    • This is called a certain delay after a call to standardWaitAfter if there has not been any call to cancelWaitAfter. The ToolManager overrides this method in order to implement support for mouse-hover behavior and tooltips.

      By default this does nothing. This method may be overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Parameters

      Returns void

    • 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 Learn page on Extensions for how to override methods and how to call this base method.

      Parameters

      • Optionalobj: GraphObject | Diagram | null

        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 GraphObject | Diagram | null

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

    • This convenience function finds the front-most GraphObject that is at a given point and that is an element of an Adornment that is of a given category. The tool handle must be an immediate element of the Adornment, not a GraphObject that is nested within Panels within the Adornment.

      This method is very infrequently overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Parameters

      Returns GraphObject | null

    • Hide any context menu.

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

      Returns void

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

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

      Returns void

    • Return true when the last mouse point is far enough away from the first mouse down point to constitute a drag operation instead of just a potential click.

      This uses the value of ToolManager.dragSize. On touch devices the value is automatically increased to accommodate the unavoidable movement of fingers.

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

      Parameters

      Returns boolean

    • 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 Learn 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

      Returns 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 Learn page on Extensions for how to override methods and how to call this base method.

      Parameters

      Returns 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 Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

    • Implement the standard behavior for mouse clicks, searching for and calling click handler functions on GraphObjects or on Diagram, and raising the corresponding DiagramEvent.

      A click on a GraphObject of the diagram will raise one of the following DiagramEvents: "ObjectSingleClicked", "ObjectDoubleClicked", or "ObjectContextClicked". This will also look at the corresponding click property: GraphObject.click, GraphObject.doubleClick, or GraphObject.contextClick. If the value is a function, this will call it, passing the current InputEvent and the GraphObject. If the value is null, it tries looking at the parent GraphObject.panel, and so on, walking up the visual tree until it finds the appropriate function to call. After calling the click function, if the value of InputEvent.handled is false, this method will continue walking up the visual tree looking for more click functions to call. Once it has looked at the top-level object (a Part) for a click function, this method stops.

      A click in the background of the diagram will raise one of the following DiagramEvents: "BackgroundSingleClicked", "BackgroundDoubleClicked", or "BackgroundContextClicked". This will also look at the corresponding click property: Diagram.click, Diagram.doubleClick, or Diagram.contextClick. If the value is a function, this will call it, passing the current InputEvent.

      This method is not responsible for selecting or deselecting any parts. Call standardMouseSelect for that functionality.

      Note that this calls GraphObject.isEnabledObject on the target object; if it returns false, no click action will occur.

      The ClickSelectingTool calls this method in its override of doMouseUp in order to raise "click" events. Note that by default GraphObjects in Layers that are Layer.isTemporary will not be "clicked". To change that behavior it is easiest to set GraphObject.isActionable to true on those objects for which you wish to handle "click" events. Then the ActionTool's doMouseUp override will raise the standard "click" events.

      This method may be overridden, but you should consider calling this base method in order to get all of its functionality. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Type Parameters

      Parameters

      • Optionalnavig: ((a: GraphObject) => T | null) | null

        An optional custom navigation function to find target objects. This argument is passed to Diagram.findObjectAt.

      • Optionalpred: ((a: T) => boolean) | null

        An optional custom predicate function to find target objects. A null value is effectively the same as a function that always returns true. This argument is passed to Diagram.findObjectAt, unless the value is undefined, which means to use a predicate to consider only objects in layers holding permanent objects.

      Returns boolean

      true if InputEvent.handled had been set to true on the Diagram.lastInput.

    • Implement the standard behavior for mouse enter, over, and leave events, where the mouse is moving but no button is pressed. This should be called by mouse move event handlers when wanting to detect and invoke mouse enter/over/leave event handlers.

      The GraphObject.mouseEnter property provides a function to call when the mouse first enters an object or any of its contained objects (if the object is actually a Panel).

      The GraphObject.mouseLeave property provides a function to call when the mouse leaves an object and all of its contained objects (if the object is actually a Panel).

      The GraphObject.mouseOver property and Diagram.mouseOver properties provide functions to call when the mouse moves but stays within the same GraphObject or when the mouse moves in the background of the Diagram.

      This method is also responsible for updating the Diagram.currentCursor according to the value of GraphObject.cursor and Diagram.defaultCursor.

      This method may be overridden, but you should consider calling this base method in order to get all of its functionality. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

    • Implement the standard behavior for selecting parts with the mouse, depending on the control and shift modifier keys.

      Control-clicking on a part will select it if it wasn't already, and will deselect if it had been selected. Shift-clicking on a part will add it to the selection (if it wasn't already). Otherwise, clicking on a part will select it (if it wasn't already).

      Note that there are restrictions on selection. For example, a part cannot be selected in this manner if Part.selectable is false, or if Diagram.maxSelectionCount would be exceeded.

      A left click in the background of the diagram with no modifier keys clears the selection.

      This method does not implement any click event behavior -- that is implemented by standardMouseClick.

      The ClickSelectingTool calls this method in its override of doMouseUp in order to change the selection.

      This method may be overridden, but you should consider calling this base method in order to get all of its functionality. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

    • As of version 4, this method is no longer called by the ToolManager. Most of the responsibility for pinch zooming has been moved to the PanningTool.

      Returns void

      deprecated
    • As of version 4, this method is no longer called by the ToolManager. Most of the responsibility for pinch zooming has been moved to the PanningTool.

      Returns void

      deprecated
    • This is called to start a new timer to call doWaitAfter after a given delay. It first cancels any previously running "WaitAfter" timer, by calling cancelWaitAfter.

      This is normally used to implement mouse hover and mouse hold events. If the mouse has moved, it must not have moved beyond the distance as determined by Tool.isBeyondDragSize for it be considered "stationary". So the regular ToolManager.doMouseMove implementation only calls this method when the mouse has moved beyond the drag size.

      This method is rarely overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Parameters

      Returns void

    • If the Diagram.currentTool is this tool, stop this tool and start the Diagram.defaultTool by making it be the new current tool. The implementation of various tool methods can call this method to stop the current tool. This will call doStop -- you should not call that method directly.

      If you want to stop the current tool and have it restore the original state, call doCancel. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Returns void

    • The diagram asks each tool to update any adornments the tool might use for a given part. If the tool uses its own tool handles, this should display them or hide them as appropriate. Typically this should only show them if the part is selected.

      By default this method does nothing. This method may be overridden. Please read the Learn page on Extensions for how to override methods and how to call this base method.

      Parameters

      Returns void