Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ondoubleclicknode #202

Merged
merged 13 commits into from
Jun 20, 2019
Merged

Feature/ondoubleclicknode #202

merged 13 commits into from
Jun 20, 2019

Conversation

IgorMacGregor
Copy link
Contributor

Hi ! I have added the onDoubleClick feature as requested on issue #194. There is still a small bug that I couldn't fix: when double-clicking on a node, a zoom is triggered on the graph and I don't know how we could cancel it but it should be related to the d3-zoom library.
I've done my best to update the documentation, tell me if something is missing ! And it seems that some prettier script makes me change some of the README signs, I don't know how I could cancel it :'(
Have a good day !

@@ -64,6 +64,10 @@ export default class Sandbox extends React.Component {
// });
};

onDoubleClickNode = id => {
!this.state.config.collapsible && window.alert(`Double clicked node ${id}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give it a try and write before the call add:

event.stopPropagation()

Note that first you need to grab the event in the parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done many attempts but it didn't work ... I've also tried preventDefault() but it doesnt do anything ...
I was thinking that maybe there would be a way to say to d3-zoom to zoom only if the double click happens on the graph ? As the onClickGraph function isn't triggered when clicking the node, it consider both clicks differently and we could leverage that ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the only way I found to solve this event was disabling double-click zoom on the whole graph, by adding .on("dblclick.zoom", null) to the _zoomConfig function. I am trying to implement it only on the nodes but that doesn't work yet

Copy link
Owner

@danielcaldas danielcaldas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, please consider the following comments.

README.md Outdated
@@ -20,9 +20,9 @@

You can also load different datasets and configurations via URL query parameter, here are the links:

* [small dataset](https://goodguydaniel.com/react-d3-graph/sandbox/index.html?data=small) - small example.
* [custom node dataset](https://goodguydaniel.com/react-d3-graph/sandbox/index.html?data=custom-node) - sample config with custom views.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did prettier ran against README.md 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found a way to correct that :)

@@ -2,94 +2,94 @@

### Table of Contents
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please discard the changes on this file 🙂

@@ -20,10 +20,10 @@
"dev": true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please discard changes to this file 🙂

@@ -58,6 +58,10 @@ import utils from "../../utils";
* window.alert('Clicked node ${nodeId}');
* };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also discard changes for src/components/graph/Graph.jsx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this one, you mean I should delete the onDoubleClickNode function ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore this one 😄

this.props.onClickNode && this.props.onClickNode(clickedNodeId);
if (!this.timeoutID) {
this.timeoutID = setTimeout(() => {
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason for this call to be wrapped inside a setTimeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it represents the time when the Node is waiting to see if another click is coming, and then decides if it should apply the onClickNode or onDoubleClickNode function

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you tried it out, but there's also a native dblclick event in JavaScript. Maybe you could try to use that instead of the setTimeout hack and see what happens.

If it doesn't work, this solution you came up with looks good to me. Still, since this is meant to be a handler for nodes only, I think it would be better to do this at node level instead of in the Graph component. Consider moving this logic to the handleOnClickNode method in Node component instead. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the dblclick event, it didn't work, JS and React as a whole have huge problems managing click and doubleclick on the same element and StackOverflow is full of questions regarding this issue.
I have run some tests to try to move the logic to the Node component but it doesn't work anymore, nothing is triggered when I double click, I don't understand why ...

@@ -293,7 +298,16 @@ export default class Graph extends React.Component {
() => this.props.onClickNode && this.props.onClickNode(clickedNodeId)
);
} else {
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
if (!this.timeoutID) {
this.timeoutID = setTimeout(() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the naming this.nodeClickTimer. Also, do you need to assign the timer to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, from what I've tried we can't initiate a variable (here, a nodeClickTimer not linked to this) in a component, or I would need to add a state but I found that too heavy for just one functionality

@IgorMacGregor
Copy link
Contributor Author

IgorMacGregor commented May 27, 2019

I have disabled the double click zoom on the whole graph, I think it's the better (and only possible from what I know) way !
Sorry for the multiple commits, I had some difficulties to revert my change and I am rather new to Git ...
I have corrected the changes by adding docs/DOCUMENTATION.md and README.md on the .prettierignore file, so I suggest adding them on the master branch !
Thanks, have a good day :)

this.props.onClickNode && this.props.onClickNode(clickedNodeId);
if (!this.timeoutID) {
this.timeoutID = setTimeout(() => {
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you tried it out, but there's also a native dblclick event in JavaScript. Maybe you could try to use that instead of the setTimeout hack and see what happens.

If it doesn't work, this solution you came up with looks good to me. Still, since this is meant to be a handler for nodes only, I think it would be better to do this at node level instead of in the Graph component. Consider moving this logic to the handleOnClickNode method in Node component instead. 😉

@@ -64,6 +64,11 @@ export default class Sandbox extends React.Component {
// });
};

onDoubleClickNode = (event, id) => {
event.stopPropagation(); //??? Marche pas
Copy link
Collaborator

@LonelyPrincess LonelyPrincess May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried running the sandbox in your branch, but I'm getting the following error when I double click on a node:

Uncaught TypeError: event.stopPropagation is not a function

I debugged it a little and I found out that the first parameter of the method contains the node id instead of an event. The id parameter is always undefined, and no event is ever received by Sandbox's onDoubleClickNode function.

Although you added the event parameter in this function, you didn't add it in the Node and Graph components. The handleOnClickNode function in the Node component is the one that actually catches the event, and it must pass it to the parent components so it reaches the Sandbox callback. To begin with, we must pass the event to the Graph component like this:

handleOnClickNode = event => this.props.onClickNode && this.props.onClickNode(event, this.props.id);

After doing this, you have to also update the onClickNode function inside of the Graph component to receive the new event parameter and then update the call to this.props.onDoubleClickNode by passing it the event as the first parameter.

this.props.onDoubleClickNode(event, clickedNodeId)

Only when this is done you could actually try to call event methods from the Sandbox component. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was supposed to be deleted sorry, it is when I tried to cancel the zoom due to d3-zoom but it also led me to a dead-end, I couldn't cancel it

@IgorMacGregor
Copy link
Contributor Author

Hello,
I have received a message saying that this PR is becoming stale, do you want any more changes ? Everything works now and I have followed your advices :)
Have a good day !

@danielcaldas
Copy link
Owner

Hey @IgorMacGregor thanks for noticing I will proceed with this soon 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants