cytoscape.js-context-menus icon indicating copy to clipboard operation
cytoscape.js-context-menus copied to clipboard

Pop up menu appears outside cytoscape network, even when nothing is clicked

Open jonng1000 opened this issue 2 years ago • 7 comments

Hi, I noticed that my pop up menu appears outside cytoscape network, even when nothing is clicked. May I know how to solve it please?

image

My code is

<CytoscapeComponent 
              elements={props.elements} 
              style={ {  
                height: sizeHeight + 'px',
                cursor: my_cursor
              } } 
              cy={cy => {
                cy.on('add', 'node', _evt => {
                    cy.layout(layout).run()
                    cy.fit()
                    setCy(cy)
                })
                cy.on('mouseover', 'node', (evt) => {
                  changeCursor('pointer')
                  //makePopper(evt.target)
                });
                cy.on('mouseout', 'node', (evt) => {
                  changeCursor('default')
                });

                var options = {
                  // Customize event to bring up the context menu
                  // Possible options https://js.cytoscape.org/#events/user-input-device-events
                  evtType: 'cxttap',
                  // List of initial menu items
                  // A menu item must have either onClickFunction or submenu or both
                  menuItems: [
                    {
                      content: 'test',
                      selector: 'node',
                      onClickFunction: function (evt) { 
                        console.log(evt.target.data('123'))// The function to be executed on click
                        //console.log('remove element');
                      },
                      show: false
                    },
                    {
                      id: 'test',
                      content: 'test1',
                      selector: 'node',
                      coreAsWell: false,
                      show: true,
                      onClickFunction: function (evt) { 
                        console.log('test')// The function to be executed on click
                        //console.log('remove element');
                      },
                    }
                  ],
                  // css classes that menu items will have
                  menuItemClasses: [
                    // add class names to this list
                  ],
                  // css classes that context menu will have
                  contextMenuClasses: [
                    // add class names to this list
                  ]
                };
                cy.contextMenus(options);
              }}

Thank you

jonng1000 avatar Feb 03 '22 13:02 jonng1000

I observed similar things with an angular app before. It goes away after you click or pan, zoom to the canvas. It might hide the context menu items later with an event. For now, you might try emitting the event manually.

canbax avatar Feb 03 '22 13:02 canbax

Thanks, how exactly do I do that? May I get a code sample please? Sorry, am very very new to javascript :/

jonng1000 avatar Feb 03 '22 13:02 jonng1000

Try cy.zoom(cy.zoom()) it will set the zoom level

canbax avatar Feb 03 '22 13:02 canbax

Hi I did that but it didnt work. Any other suggestions?

<CytoscapeComponent 
              elements={props.elements} 
              style={ {  
                height: sizeHeight + 'px',
                cursor: my_cursor
              } } 
              cy={cy => {
                cy.on('add', 'node', _evt => {
                    cy.layout(layout).run()
                    cy.fit()
                    setCy(cy)
                })
                cy.on('mouseover', 'node', (evt) => {
                  changeCursor('pointer')
                  //makePopper(evt.target)
                });
                cy.on('mouseout', 'node', (evt) => {
                  changeCursor('default')
                });

                var options = {
                  // Customize event to bring up the context menu
                  // Possible options https://js.cytoscape.org/#events/user-input-device-events
                  evtType: 'cxttap',
                  // List of initial menu items
                  // A menu item must have either onClickFunction or submenu or both
                  menuItems: [
                    {
                      content: 'test',
                      selector: 'node',
                      onClickFunction: function (evt) { 
                        console.log(evt.target.data('123'))// The function to be executed on click
                        //console.log('remove element');
                      },
                      show: false
                    },
                    {
                      id: 'test',
                      content: 'test1',
                      selector: 'node',
                      coreAsWell: false,
                      show: true,
                      onClickFunction: function (evt) { 
                        console.log('test')// The function to be executed on click
                        //console.log('remove element');
                      },
                    }
                  ],
                  // css classes that menu items will have
                  menuItemClasses: [
                    // add class names to this list
                  ],
                  // css classes that context menu will have
                  contextMenuClasses: [
                    // add class names to this list
                  ]
                };
                cy.contextMenus(options);
               cy.zoom(cy.zoom);
              }}

I included your suggestion at the bottom

jonng1000 avatar Feb 03 '22 13:02 jonng1000

@jonng1000 You should not write your application logic code in there. Follow some react.js tutorials, read docs about it. I'm not sure maybe it is because of this. Normal I would have put my JS logic inside componentDidMount

canbax avatar Feb 04 '22 05:02 canbax

@jonng1000 I tried this.cy.pan(this.cy.pan()); it was working

canbax avatar Feb 06 '22 09:02 canbax

Ok thanks so much for your help! =)

jonng1000 avatar Feb 07 '22 15:02 jonng1000