An implementation of the IInputMode interface that handles the display of a custom context menu when the user right-clicks on the CanvasComponent.
Remarks
This mode can be used with any context menu implementation since it does not impose any specific requirements. Typically, it is used in the following way:
When this mode is informed that the user wants to show the context menu for example because a contextmenu
event was fired, it checks whether its current state allows opening a context menu, and fires the populate-menu and GraphInputMode.populateItemContextMenu
events.
If the context menu was closed because of user interaction with it, for example the user selected a menu entry in a custom context menu, this mode must be informed by calling the method closeMenu.
The context menu HTML element is positioned using "position: absolute". It is therefore positioned with respect to it's offset parent, which is the closest ancestor with "position: relative". In summary, for the context menu to work properly, the element containing the graph component must be styled with "position: relative".
Examples
Typically, the ContextMenuInputMode
is installed as child mode of a GraphEditorInputMode or GraphViewerInputMode and can be retrieved from the contextMenuInputMode property.
const contextMenuInputMode = mode.contextMenuInputMode
It is recommended to configure the context menu handling on the parent GraphEditorInputMode or GraphViewerInputMode instead of the ContextMenuInputMode
. This involves registering for the populate-item-context-menu event instead of the populate-menu event:
// mode is either an instance of GraphEditorInputMode or GraphViewerInputMode
mode.contextMenuItems = GraphItemTypes.NODE
mode.addEventListener('populate-item-context-menu', (evt) => {
// Get the node which is handled or null if the item is not a node
const node = evt.item
// Create the context menu items
if (node !== null) {
// Create a menu item to delete the node
// Show the menu
evt.contextMenu = [
{ label: 'Delete Item', action: () => graph.remove(node) },
]
}
})
Related Programming Samples
- Context Menu
- Shows how to add a context menu to the nodes of a graph and to the canvas background.
Type Details
- yFiles module
- view
context-menu
event will stop other input modes from acting on the subsequent long-press event. Therefore, lowering the longPressTime will extend the time frame between the long-press and the context-menu event.Constructors
Initializes a new instance of the ContextMenuInputMode class.
Parameters
A map of options to pass to the method.
- priority - number
- exclusive - boolean
- A value indicating whether this mode will be the only one running when it has the mutex. This option sets the exclusive property on the created object.
- enabled - boolean
- The enabled state of this input mode. This option sets the enabled property on the created object.
- validMenuLocationHitTestable - IHitTestable
- An IHitTestable that determines whether it is valid to open a context menu at the queried position. This option sets the validMenuLocationHitTestable property on the created object.
- validMenuLocationCursor - Cursor
- The cursor to use when the mouse is at a valid menu location. This option sets the validMenuLocationCursor property on the created object.
- contextMenuParentElement - HTMLElement
- A custom parent element to which the context menu is added as a child in the DOM. This option sets the contextMenuParentElement property on the created object.
- swallowCloseClick - boolean
- A value that determines whether clicks are swallowed if they happen within a short amount of time after a context menu was closed. This option sets the swallowCloseClick property on the created object.
Properties
Gets or sets a custom parent element to which the context menu is added as a child in the DOM.
Remarks
By default this property is null
, which means that the menu is added to the document.body
.
When specifying some other element as parent, it must be assured that the element is currently visible (i.e. has no style with property display: none
).
Gets the installed ConcurrencyController.
Gets or sets the enabled state of this input mode.
Remarks
Examples
mode.contextMenuInputMode.enabled = false
Gets or sets a value indicating whether this mode will be the only one running when it has the mutex.
Remarks
The value of this property will be delegated to the exclusive property of the controller.
If this mode is marked as exclusive and has the mutex, all other modes added to the same MultiplexingInputMode will be deactivated. Otherwise, it will always run concurrently with all other modes.
Retrieves the IInputModeContext this mode has been installed in.
Remarks
null
if this mode is currently not installed. Use createInputModeContext to obtain a context that has this IInputMode as the inputMode.Gets the priority of this input mode.
Remarks
Implements
Gets or sets a value that determines whether clicks are swallowed if they happen within a short amount of time after a context menu was closed.
Remarks
true
.Gets or sets the cursor to use when the mouse is at a valid menu location.
Remarks
null
Gets or sets an IHitTestable that determines whether it is valid to open a context menu at the queried position.
Remarks
false
onMenuOpening will not be queried and the context menu will not be shown. By default, there is a hit testable instance that always yields true
.Methods
Cancels the display of the context menu and requests that the context menu is closed.
Implements
This method must be called by custom event listeners on HTMLElements added in contextMenu to indicate that the menu has been closed.
Remarks
Creates an input mode context that uses this instance as the inputMode and delegates to parentInputModeContext otherwise.
Remarks
null
, i.e. while this mode is installed.Returns
- ↪IInputModeContext
- An instance that has this IInputMode set as the inputMode
Creates an IInputModeContext for use with the populate-menu call in the upcoming query.
Returns
- ↪IInputModeContext
- An instance of IInputModeContext.
Installs this mode in the canvas.
Parameters
A map of options to pass to the method.
- context - IInputModeContext
- The context to install this mode into.
- controller - ConcurrencyController
- The controller for this mode.
See Also
Implements
Called after cancel has been called.
Remarks
Can be overridden in subclasses to perform additional actions after the mode has been canceled.
This implementation does nothing.
Called after the active property of the installed ConcurrencyController has been set to true
.
Remarks
Can be overridden in subclasses to perform additional actions after the mode has been activated.
Overriding implementations should call the base implementation.
Called after the active property of the installed ConcurrencyController has been set to false
.
Remarks
Can be overridden in subclasses to perform additional actions after the mode has been deactivated.
Overriding implementations should call the base implementation.
Called when the menu is closed and triggers the menu-closed event.
Populates the context menu for the given world coordinate.
Remarks
Parameters
A map of options to pass to the method.
- location - Point
- The location in the world coordinate system for which the context menu has been invoked.
Returns
Raises the populate-menu event.
Parameters
A map of options to pass to the method.
- evt - PopulateContextMenuEventArgs
- The PopulateContextMenuEventArgs instance containing the event data.
Called after tryStop has been called.
Remarks
Can be overridden in subclasses to perform additional actions after the mode has been stopped.
This implementation does nothing.
Stops the display of the context menu and requests that the context menu is closed.
Removes the menu from the context and replaces it with the old instance.
Parameters
A map of options to pass to the method.
- context - IInputModeContext
- The context to uninstall this mode from.