I have what I suspect is a rather basic question on how d3 works. I've started off with this code that works perfectly and triggers the nodeClick actions...
var nodeEnter = node.enter()
.append("g")
.attr("class", "node")
.on("click", nodeClick)
But then I changed it to use a function on click to determine whether a single or double click was used...
.on("click", function(d) {
if some logic () {
console.log("double click");
} else {
console.log("single click only");
nodeClick;
}
})
This works perfectly in terms of outputting the correct console messages, but it seems like my call to nodeClick isn't working properly while embedded within in a function (i.e. the node click behaviours aren't triggered). I tried changing to nodeClick() and nodeClick(d) but that just results in errors.
Am I missing something that could explain this behaviour? Seems very strange to me that I'm seeing 2 different behaviours to calling "nodeClick" from outside and inside a function.
Thanks for any help!
Here's the complete code in question...
dblclick_timer = false;
//.on("click", nodeClick) //works perfectly
.on("click", function(d) {
if ( dblclick_timer ) {
clearTimeout(dblclick_timer)
dblclick_timer = false
console.log("double click");
d.fixed=false;
}
else dblclick_timer = setTimeout( function(){
dblclick_timer = false
console.log("single click only");
d3.select(this).nodeClick;
}, 250)
})
After all the great feedback, here's the working solution by storing d3.event before it becomes null...
.on("click", function(d,i) {
var cacheEvent = d3.event;
if ( dblclick_timer ) {
clearTimeout(dblclick_timer)
dblclick_timer = false
console.log("double click");
d.fixed=false;
force.start();
}
else dblclick_timer = setTimeout( function(){
dblclick_timer = false
console.log("single click only");
d3.event = cacheEvent;
nodeClick.call(d3.select(this), d, i);
}, 250)
})