Using custom context menu for cytoscape.js nodes

3.9k views Asked by At

I am trying to use this context menu javascript library but can't seem to get it to trigger when a node is right clicked.

I tried implementing the basic demo and that works, so I have the necessary js on the page.

I added it to the left click of a node and tried to trigger the context menu through the custom way like so:

graph.$('node').on('cxttapstart', function (event) {

            event.cyTarget.contextMenu();
        });

$.contextMenu({
        selector: '.context-menu-one',
        trigger: 'none',
        callback: function (key, options) {
            var m = "clicked: " + key;
            window.console && console.log(m) || alert(m);
        },
        items: {
            "edit": { name: "Edit", icon: "edit" },
            "cut": { name: "Cut", icon: "cut" },
            "copy": { name: "Copy", icon: "copy" },
            "paste": { name: "Paste", icon: "paste" },
            "delete": { name: "Delete", icon: "delete" },
            "sep1": "---------",
            "quit": { name: "Quit", icon: function ($element, key, item) { return 'context-menu-icon context-menu-icon-quit'; } }
        }
    });

but I just don't know what to do on line

selector: '.context-menu-one'

so that the context menu shows up next to the node.

Anybody done this before?

Cheers

1

There are 1 answers

0
maxkfranz On

event.cyTarget (2.x) or event.target (3.x) is not a dom element. It's a cytoscape element. You need to create your own dom element (e.g. div) for the menu and position it relative to node.renderedPosition() on cxttap.

If you want to use a context menu with cytoscape, it's easier to just use an existing extension.

There's one that has a modern circular swipe menu: https://github.com/cytoscape/cytoscape.js-cxtmenu

There's also one that's a traditional list menu: https://github.com/iVis-at-Bilkent/cytoscape.js-context-menus