I have a question.
Is there a possibility to bind a double click Mouse Event to a node?
In the documentation, there is only 'click'.
Thanks for your help!
You can add a custom doubleTap
event for Cytoscape like this:
var cy = $('#cy').cytoscape('get');
var tappedBefore;
var tappedTimeout;
cy.on('tap', function(event) {
var tappedNow = event.cyTarget;
if (tappedTimeout && tappedBefore) {
clearTimeout(tappedTimeout);
}
if(tappedBefore === tappedNow) {
tappedNow.trigger('doubleTap');
tappedBefore = null;
} else {
tappedTimeout = setTimeout(function(){ tappedBefore = null; }, 300);
tappedBefore = tappedNow;
}
});
Then, you can subscribe for the new event. For example, if you need to detect double tab on nodes, do:
cy.on('doubleTap', 'node', function(event) { /* ... */ });
Nevertheless, I understant @maxkfranz point of view that this is not mobile-friendly solution.
One of the core requirements of cy.js is that it should work equally well and as-consistently-as-possible across devices. Double click is a carry over from PC days: It often results in poor UI, and it is completely foreign to touch-based devices. Therefore, we do not support it currently. -M
There is also a way to achieve this without setTimeout
. The event has a timeStamp
property with the values of UNIX time stamp when the event got triggered. So, here is the approach:
var doubleClickDelayMs = 350;
var previousTapStamp;
cy.on('tap', function(e) {
var currentTapStamp = e.timeStamp;
var msFromLastTap = currentTapStamp - previousTapStamp;
if (msFromLastTap < doubleClickDelayMs) {
e.target.trigger('doubleTap', e);
}
previousTapStamp = currentTapStamp;
});
cy.on('doubleTap', function(event, originalTapEvent) {
...
});
Here is a working fiddle: https://jsfiddle.net/scebotari66/84x7hxd0/4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With