cytoscape-cxtmenu

repository·master·Indexed 41 days ago

https://github.com/cytoscape/cytoscape.js-cxtmenu

A circular, swipeable context menu extension for Cytoscape.js version 3.5.0. It provides a radial UI for performing actions on nodes, edges, or the graph background, supporting customizable commands, dynamic menu generation, and configurable appearance options such as menuRadius and activeFillColor.

Tokens
3.3K
Snippets
8
Records
10
Agent score
48%

What's inside cytoscape-cxtmenu

  1. Register cytoscape-cxtmenu in your project

    master

    Depending on your module system, use the following patterns to register the extension with Cytoscape.js.

    ES Import:

    import cytoscape from 'cytoscape';
    import cxtmenu from 'cytoscape-cxtmenu';
    
    cytoscape.use( cxtmenu );

    CommonJS (require):

    let cytoscape = require('cytoscape');
    let cxtmenu = require('cytoscape-cxtmenu');
    
    cytoscape.use( cxtmenu );

    AMD:

    require(['cytoscape', 'cytoscape-cxtmenu'], function( cytoscape, cxtmenu ){ 
      cxtmenu( cytoscape ); 
    });

    Plain HTML/JS: If you are using plain HTML/JS without a module loader, the extension is registered automatically.

  2. Register the cytoscape-cxtmenu extension

    master

    To use the context menu extension, you must register it with your Cytoscape.js instance. The extension provides a register function that attaches the cxtmenu capability to the Cytoscape core.

    const cytoscape = require('cytoscape');
    const registerCxtmenu = require('cytoscape-cxtmenu');
    
    // Register the extension
    registerCxtmenu(cytoscape);
    
    // Now you can initialize cytoscape with cxtmenu options
    const cy = cytoscape({
      container: document.getElementById('cy'),
      elements: [ /* ... */ ],
      cxtmenu: { /* configuration options */ }
    });
    const cytoscape = require('cytoscape');
    const registerCxtmenu = require('cytoscape-cxtmenu');
    
    registerCxtmenu(cytoscape);
  3. Initialize and configure the context menu

    master

    Initialize the plugin by calling .cxtmenu(options) on your Cytoscape instance. The menu is initialized on the same HTML DOM element container used for Cytoscape.js.

    Configuration Options

    OptionTypeDefaultDescription
    menuRadiusnumber or function(ele)function(ele){ return 100; }The outer radius (node center to the end of the menu) in pixels. Added to the rendered size of the node.
    selectorstring'node'Cytoscape.js selector for elements that trigger the menu.
    commandsArray<Object> or function(ele)[]An array of command objects or a function returning them (or a promise of them).
    fillColorstring'rgba(0, 0, 0, 0.75)'Background color of the menu.
    activeFillColorstring'rgba(1, 105, 217, 0.75)'Color used to indicate the selected command.
    activePaddingnumber20Additional size in pixels for the active command.
    indicatorSizenumber24Size of the pointer to the active command.
    separatorWidthnumber3Empty spacing in pixels between successive commands.
    spotlightPaddingnumber4Extra spacing between the element and the spotlight.
    adaptativeNodeSpotlightRadiusbooleanfalseWhether the spotlight radius should adapt to the node size.
    minSpotlightRadiusnumber24Minimum radius of the spotlight (used for edge/background if adaptative is enabled).
    maxSpotlightRadiusnumber38Maximum radius of the spotlight (used for edge/background if adaptative is enabled).
    openMenuEventsstring'cxttapstart taphold'Space-separated Cytoscape events to open the menu. Only cxttapstart and taphold work.
    itemColorstring'white'Color of text in the command's content.
    itemTextShadowColorstring'transparent'Text shadow color of the command's content.
    zIndexnumber9999Z-index of the UI div.
    atMousebooleanfalseIf true, draws menu at mouse position.
    outsideMenuCancelnumber or booleanfalseIf a number, cancels the command if the pointer is released outside the spotlight + this padding.

    Command Object Structure

    Each object in the commands array can contain:

    • content: (string/HTML) Text/content displayed in the menu.
    • fillColor: (string) Custom background color for the item.
    • contentStyle: (Object) CSS key:value pairs for the command's CSS.
    • select: (function(ele)) Executed when the command is selected. ele is the active element.
    • hover: (function(ele)) Executed when the command is hovered. ele is the active element.
    • enabled: (boolean) Whether the command is selectable.
    let cy = cytoscape({
    	container: document.getElementById('cy'),
    	/* ... */
    });
    
    let menu = cy.cxtmenu({
      selector: 'node',
      commands: [
        {
          content: 'Delete Node',
          select: function(ele){ 
            ele.remove();
          }
        }
      ]
    });
  4. Configure cytoscape-cxtmenu options

    master

    When initializing the cytoscape-cxtmenu extension, you can provide a configuration object to customize the menu's appearance, behavior, and commands.

    • fillColor: The background color of the menu (e.g., 'rgba(0, 0, 0, 0.75)').
    • activeFillColor: The color used to indicate the currently selected command.
    • activePadding: Additional size in pixels for the active command.
    • itemColor: The color of the text in the command's content.
    • itemTextShadowColor: The text shadow color of the command's content.
    • zIndex: The z-index of the UI div.
    • menuRadius: A function function(ele){ ... } that returns the radius of the circular menu in pixels.
    • separatorWidth: The empty spacing in pixels between successive commands.

    Spotlight and Interaction

    • indicatorSize: The size in pixels of the pointer to the active command. Defaults to the node size if the node size is smaller than this value.
    • spotlightPadding: Extra spacing in pixels between the element and the spotlight.
    • adaptativeNodeSpotlightRadius: Boolean. If true, the spotlight radius adapts to the node size.
    • minSpotlightRadius: The minimum radius in pixels of the spotlight (used for edges/background or when adaptativeNodeSpotlightRadius is enabled).
    • maxSpotlightRadius: The maximum radius in pixels of the spotlight (used for edges/background or when adaptativeNodeSpotlightRadius is enabled).
    • atMouse: Boolean. If true, the menu is drawn at the mouse position.
    • outsideMenuCancel: If set to a number, the command is cancelled if the pointer is released outside of the spotlight, padded by this number.

    Triggering and Selection

    • selector: A Cytoscape.js selector string. Elements matching this selector will trigger the context menu (default: 'node').
    • openMenuEvents: A space-separated string of Cytoscape.js events that will open the menu. Only cxttapstart and/or taphold are supported.
    • commands: An array of command objects or a function function(ele){ ... } that returns an array of command objects.
  5. Configure the cytoscape-cxtmenu extension

    master

    The cxtmenu configuration object allows you to customize the appearance, behavior, and commands of the radial context menu. You can provide a static array of commands or a function that dynamically generates commands based on the target element.

    Command Object Structure

    Each command in the commands array can contain:

    • content: HTML string or HTMLElement to display in the menu.
    • fillColor: (Optional) Custom background color for the specific menu item.
    • contentStyle: (Optional) CSS key-value pairs to apply to the command's content.
    • hover: A function called when the command is hovered. Receives (ele, gestureStartEvent).
    • select: A function called when the command is selected. Receives (ele, gestureStartEvent).
    • enabled: Boolean indicating if the command is selectable (defaults to true).

    Configuration Options

    OptionDefaultDescription
    selector'node'Elements matching this selector will trigger the menu.
    commands[]Array of command objects or a function (ele) => commands[]
    menuRadius(ele) => 100Function or number defining the menu radius in pixels.
    fillColor'rgba(0, 0, 0, 0.75)'Background color of the menu.
    activeFillColor'rgba(1, 105, 217, 0.75)'Color used to indicate the currently hovered/active command.
    activePadding20Additional size in pixels for the active command.
    indicatorSize24Size of the pointer to the active command.
    separatorWidth3Spacing in pixels between successive commands.
    spotlightPadding4Extra spacing between the element and the spotlight.
    adaptativeNodeSpotlightRadiusfalseIf true, spotlight radius adapts to node size.
    minSpotlightRadius24Minimum radius for the spotlight.
    maxSpotlightRadius38Maximum radius for the spotlight.
    openMenuEvents'cxttapstart taphold'Cytoscape events that trigger the menu.
    itemColor'white'Color of the text in the command's content.
    itemTextShadowColor'transparent'Text shadow color for command content.
    zIndex9999Z-index of the menu UI container.
    atMousefalseIf true, draws menu at mouse position instead of element position.
    outsideMenuCancelfalseIf a number, cancels command if pointer is released outside spotlight + this value.
    const cy = cytoscape({
      container: document.getElementById('cy'),
      elements: [ /* ... */ ],
      cxtmenu: {
        selector: 'node',
        commands: (ele) => [
          {
            content: 'Delete',
            select: (e) => e.remove(),
            fillColor: 'rgba(255, 0, 0, 0.75)'
          },
          {
            content: '<b>Bold</b>',
            contentStyle: { fontWeight: 'bold' }
          }
        ]
      }
    });
  6. Destroy a cytoscape-cxtmenu instance

    master

    If you need to manually clean up the context menu (e.g., when removing the Cytoscape instance or switching views), the extension returns a destroy method. This method removes all event listeners and the menu's DOM elements from the container.

    // Note: The destroy method is part of the object returned by the cxtmenu implementation
    // when called internally. In standard usage, you manage the lifecycle via the cy instance.
    // If you have a reference to the instance:
    instance.destroy();
  7. Define context menu commands

    master

    The commands option defines the items available in the context menu. You can provide a static array of command objects or a function that receives the active element (ele) and returns an array of commands, allowing for dynamic menus based on the selected element.

    Each command object can contain:

    • content: The HTML or text content to be displayed in the menu.
    • fillColor: (Optional) Custom background color for the specific menu item.
    • contentStyle: (Optional) CSS key:value pairs to set the command's CSS via JavaScript.
    • hover: A function function(ele){ ... } executed when the command is hovered. ele is the reference to the active element.
    • select: A function function(ele){ ... } executed when the command is selected. ele is the reference to the active element.
    • enabled: Boolean. Whether the command is selectable (default: true).
    commands: [
      {
        content: 'a command name',
        fillColor: 'rgba(200, 200, 200, 0.75)',
        contentStyle: { fontWeight: 'bold' },
        hover: function(ele) {
          console.log('Hovering:', ele.id());
        },
        select: function(ele) {
          console.log('Selected:', ele.id());
        },
        enabled: true
      }
    ]