Skip to content

Commit 614bcf1

Browse files
ghardin137danielcaldas
authored andcommitted
Feature/right clicking (#124)
* Added the ability to right click on nodes and links. * Updating Readme
1 parent 6c1ebce commit 614bcf1

File tree

9 files changed

+115
-0
lines changed

9 files changed

+115
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ const onClickNode = function(nodeId) {
7373
window.alert(`Clicked node ${nodeId}`);
7474
};
7575

76+
const onRightClickNode = function(event, nodeId) {
77+
window.alert(`Right clicked node ${nodeId}`);
78+
};
79+
7680
const onMouseOverNode = function(nodeId) {
7781
window.alert(`Mouse over node ${nodeId}`);
7882
};
@@ -85,6 +89,10 @@ const onClickLink = function(source, target) {
8589
window.alert(`Clicked link between ${source} and ${target}`);
8690
};
8791

92+
const onRightClickLink = function(event, source, target) {
93+
window.alert(`Right clicked link between ${source} and ${target}`);
94+
};
95+
8896
const onMouseOverLink = function(source, target) {
8997
window.alert(`Mouse over in link between ${source} and ${target}`);
9098
};
@@ -98,8 +106,10 @@ const onMouseOutLink = function(source, target) {
98106
data={data}
99107
config={myConfig}
100108
onClickNode={onClickNode}
109+
onRightClickNode={onRightClickNode}
101110
onClickGraph={onClickGraph}
102111
onClickLink={onClickLink}
112+
onRightClickLink={onRightClickLink}
103113
onMouseOverNode={onMouseOverNode}
104114
onMouseOutNode={onMouseOutNode}
105115
onMouseOverLink={onMouseOverLink}

src/components/graph/Graph.jsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ import utils from '../../utils';
5757
* window.alert('Clicked node ${nodeId}');
5858
* };
5959
*
60+
* const onRightClickNode = function(event, nodeId) {
61+
* window.alert('Right clicked node ${nodeId}');
62+
* };
63+
*
6064
* const onMouseOverNode = function(nodeId) {
6165
* window.alert(`Mouse over node ${nodeId}`);
6266
* };
@@ -69,6 +73,10 @@ import utils from '../../utils';
6973
* window.alert(`Clicked link between ${source} and ${target}`);
7074
* };
7175
*
76+
* const onRightClickLink = function(event, source, target) {
77+
* window.alert('Right clicked link between ${source} and ${target}');
78+
* };
79+
*
7280
* const onMouseOverLink = function(source, target) {
7381
* window.alert(`Mouse over in link between ${source} and ${target}`);
7482
* };
@@ -83,7 +91,9 @@ import utils from '../../utils';
8391
* config={myConfig}
8492
* onClickGraph={onClickGraph}
8593
* onClickNode={onClickNode}
94+
* onRightClickNode={onRightClickNode}
8695
* onClickLink={onClickLink}
96+
* onRightClickLink={onRightClickLink}
8797
* onMouseOverNode={onMouseOverNode}
8898
* onMouseOutNode={onMouseOutNode}
8999
* onMouseOverLink={onMouseOverLink}
@@ -383,6 +393,16 @@ export default class Graph extends React.Component {
383393
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
384394
};
385395

396+
/**
397+
* Calls the callback passed to the component.
398+
* @param {Object} event - the event object generated by the event.
399+
* @param {string} clickedNodeId - The id of the node where the click was performed.
400+
* @returns {undefined}
401+
*/
402+
onRightClickNode = (event, clickedNodeId) => {
403+
this.props.onRightClickNode && this.props.onRightClickNode(event, clickedNodeId);
404+
};
405+
386406
/**
387407
* Calls the callback passed to the component.
388408
* @param {Object} e - The event of onClick handler.
@@ -405,13 +425,15 @@ export default class Graph extends React.Component {
405425
this.state.nodes,
406426
{
407427
onClickNode: this.onClickNode,
428+
onRightClickNode: this.onRightClickNode,
408429
onMouseOverNode: this.onMouseOverNode,
409430
onMouseOut: this.onMouseOutNode
410431
},
411432
this.state.d3Links,
412433
this.state.links,
413434
{
414435
onClickLink: this.props.onClickLink,
436+
onRightClickLink: this.onRightClickLink,
415437
onMouseOverLink: this.onMouseOverLink,
416438
onMouseOutLink: this.onMouseOutLink
417439
},

src/components/graph/graph.helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ function buildLinkProps(link, nodes, links, config, linkCallbacks, highlightedNo
288288
className: CONST.LINK_CLASS_NAME,
289289
opacity,
290290
onClickLink: linkCallbacks.onClickLink,
291+
onRightClickLink: linkCallbacks.onRightClickLink,
291292
onMouseOverLink: linkCallbacks.onMouseOverLink,
292293
onMouseOutLink: linkCallbacks.onMouseOutLink
293294
};
@@ -344,6 +345,7 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
344345
id: node.id,
345346
label: node[config.node.labelProperty] || node.id,
346347
onClickNode: nodeCallbacks.onClickNode,
348+
onRightClickNode: nodeCallbacks.onRightClickNode,
347349
onMouseOverNode: nodeCallbacks.onMouseOverNode,
348350
onMouseOut: nodeCallbacks.onMouseOut,
349351
opacity,

src/components/link/Link.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import React from 'react';
77
* window.alert(`Clicked link between ${source} and ${target}`);
88
* };
99
*
10+
* const onRightClickLink = function(event, source, target) {
11+
* window.alert(`Right clicked link between ${source} and ${target}`);
12+
* };
13+
*
1014
* const onMouseOverLink = function(source, target) {
1115
* window.alert(`Mouse over in link between ${source} and ${target}`);
1216
* };
@@ -29,6 +33,7 @@ import React from 'react';
2933
* opacity=1
3034
* mouseCursor='pointer'
3135
* onClickLink={onClickLink}
36+
* onRightClickLink={onRightClickLink}
3237
* onMouseOverLink={onMouseOverLink}
3338
* onMouseOutLink={onMouseOutLink} />
3439
*/
@@ -39,6 +44,15 @@ export default class Link extends React.Component {
3944
*/
4045
handleOnClickLink = () => this.props.onClickLink && this.props.onClickLink(this.props.source, this.props.target);
4146

47+
/**
48+
* Handle link right click event.
49+
* @param {Object} event - the event object
50+
* @returns {undefined}
51+
*/
52+
handleOnRightClickLink = event => {
53+
this.props.onRightClickLink && this.props.onRightClickLink(event, this.props.source, this.props.target);
54+
};
55+
4256
/**
4357
* Handle mouse over link event.
4458
* @returns {undefined}
@@ -66,6 +80,7 @@ export default class Link extends React.Component {
6680
className: this.props.className,
6781
d: this.props.d,
6882
onClick: this.handleOnClickLink,
83+
onContextMenu: this.handleOnRightClickLink,
6984
onMouseOut: this.handleOnMouseOutLink,
7085
onMouseOver: this.handleOnMouseOverLink,
7186
style: lineStyle,

src/components/node/Node.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import nodeHelper from './node.helper';
1111
* window.alert('Clicked node', nodeId);
1212
* };
1313
*
14+
* const onRightClickNode = function(event, nodeId) {
15+
* window.alert('Right clicked node', nodeId);
16+
* }
17+
*
1418
* const onMouseOverNode = function(nodeId) {
1519
* window.alert('Mouse over node', nodeId);
1620
* };
@@ -39,6 +43,7 @@ import nodeHelper from './node.helper';
3943
* viewGenerator=(node) => <CustomComponent node={node} />
4044
* className='node'
4145
* onClickNode={onClickNode}
46+
* onRightClickNode={onRightClickNode}
4247
* onMouseOverNode={onMouseOverNode}
4348
* onMouseOutNode={onMouseOutNode} />
4449
*/
@@ -49,6 +54,13 @@ export default class Node extends React.Component {
4954
*/
5055
handleOnClickNode = () => this.props.onClickNode && this.props.onClickNode(this.props.id);
5156

57+
/**
58+
* Handle right click on the node.
59+
* @param {Object} event - the event object
60+
* @returns {undefined}
61+
*/
62+
handleOnRightClickNode = event => this.props.onRightClickNode && this.props.onClickNode(event, this.props.id);
63+
5264
/**
5365
* Handle mouse over node event.
5466
* @returns {undefined}
@@ -65,6 +77,7 @@ export default class Node extends React.Component {
6577
const nodeProps = {
6678
cursor: this.props.cursor,
6779
onClick: this.handleOnClickNode,
80+
onContextMenu: this.handleOnRightClickNode,
6881
onMouseOut: this.handleOnMouseOutNode,
6982
onMouseOver: this.handleOnMouseOverNode,
7083
opacity: this.props.opacity

test/component/graph/graph.helper.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ describe('Graph Helper', () => {
114114
id: 'id',
115115
label: 'id',
116116
onClickNode: undefined,
117+
onRightClickNode: undefined,
117118
onMouseOut: undefined,
118119
onMouseOverNode: undefined,
119120
opacity: 1,
@@ -159,6 +160,7 @@ describe('Graph Helper', () => {
159160
id: 'id',
160161
label: 'id',
161162
onClickNode: undefined,
163+
onRightClickNode: undefined,
162164
onMouseOut: undefined,
163165
onMouseOverNode: undefined,
164166
opacity: undefined,
@@ -203,6 +205,7 @@ describe('Graph Helper', () => {
203205
id: 'id',
204206
label: 'id',
205207
onClickNode: undefined,
208+
onRightClickNode: undefined,
206209
onMouseOut: undefined,
207210
onMouseOverNode: undefined,
208211
opacity: undefined,

0 commit comments

Comments
 (0)