cytoscape / cytoscape.js-qtip

A Cytoscape.js extension that wraps the QTip jQuery library
MIT License
42 stars 35 forks source link

Child node qtips don't get displayed properly #18

Closed somedmguy closed 8 years ago

somedmguy commented 8 years ago

The show event from a child gets propagated up to a parent node, and the parent tries to show its tooltip. The show and hide event handlers need checks that the element is the target of the event.

From adding a parent node to the demo.html and clicking on Jerry: image

maxkfranz commented 8 years ago

Duplicate : qTip is rendered for all parent elements when a child node is clicked. #16

From that issue:

Events bubble up to compound parents, just as events bubble on div parents in the DOM. You'll have to use a custom event for compounds, e.g.

nodes.qtip({ show: { event: 'directtap' } });

nodes.on('tap', function( e ){
  var eventIsDirect = e.cyTarget === this;

  if( eventIsDirect ){
    this.trigger('directtap');
  }
});
0xdabbad00 commented 7 years ago

My event e does not have a cyTarget attribute. Instead I am using e.target and I have to call e.stopPropagation(); so my code is:

cy.elements().on('tap', function( e ){
  var eventIsDirect = e.target === this;
  console.log(e);
  if( eventIsDirect ){
    e.stopPropagation();
    this.trigger('directtap');
  }
});

However, multiple qtips still display for each of the compound parent nodes. Am I doing something wrong such that I don't have a cyTarget? I tested with the demo.html and it does not seem to have a .cyTarget either.

0xdabbad00 commented 7 years ago

I fixed this by modifying https://github.com/cytoscape/cytoscape.js-qtip/blob/486d567eb5456ea4bd77d950186053e4f6269e38/cytoscape-qtip.js#L312 to add a e.stopPropagation(); to this extension.

maxkfranz commented 7 years ago

The extension shouldn't be changed. Just make sure the directtap doesn't bubble:

cy.nodes().qtip({
  show: { event: 'directtap' },
  content: function(){ return 'Example qTip on ele ' + this.id() },
  position: {
    my: 'top center',
    at: 'bottom center'
  },
  style: {
    classes: 'qtip-bootstrap',
    tip: {
      width: 16,
      height: 8
    }
  }
}).on('tap', function( e ){
  var eventIsDirect = e.target.same( this ); // don't use 2.x cyTarget

  if( eventIsDirect ){
    this.emit('directtap');
  }
}).on('directtap', function( e ){
  e.stopPropagation();
});
0xdabbad00 commented 6 years ago

That worked. Thank you @maxkfranz