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

Fixed mouse offsets (and small other fixes) #348

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions the-graph/the-graph-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ module.exports.register = function (context) {

// Safari is wheelDeltaY
this.zoomFactor += event.deltaY ? event.deltaY : 0-event.wheelDeltaY;
this.zoomX = event.clientX;
this.zoomY = event.clientY;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
this.zoomX = (event.layerX || event.clientX || 0) - offset.left;
this.zoomY = (event.layerY || event.clientY || 0) - offset.top;

requestAnimationFrame(this.scheduleWheelZoom);
},
scheduleWheelZoom: function () {
Expand Down Expand Up @@ -385,8 +387,9 @@ module.exports.register = function (context) {
if (event.preventTap) { event.preventTap(); }

// Get mouse position
var x = event.x || event.clientX || 0;
var y = event.y || event.clientY || 0;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;

// App.showContext
this.showContext({
Expand Down
5 changes: 3 additions & 2 deletions the-graph/the-graph-edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ module.exports.register = function (context) {
if (event.preventTap) { event.preventTap(); }

// Get mouse position
var x = event.x || event.clientX || 0;
var y = event.y || event.clientY || 0;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;

// App.showContext
this.props.showContext({
Expand Down
9 changes: 5 additions & 4 deletions the-graph/the-graph-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ module.exports.register = function (context) {
}
},
renderPreviewEdge: function (event) {
var x = event.x || event.clientX || 0;
var y = event.y || event.clientY || 0;
x -= this.props.app.state.offsetX || 0;
y -= this.props.app.state.offsetY || 0;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;
// x -= this.props.app.state.offsetX || 0; we already calculated offsets above.
// y -= this.props.app.state.offsetY || 0;
var scale = this.props.app.state.scale;
this.setState({
edgePreviewX: (x - this.props.app.state.x) / scale,
Expand Down
5 changes: 3 additions & 2 deletions the-graph/the-graph-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ module.exports.register = function (context) {
if (event.preventTap) { event.preventTap(); }

// Get mouse position
var x = event.x || event.clientX || 0;
var y = event.y || event.clientY || 0;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;

// App.showContext
this.props.showContext({
Expand Down
5 changes: 3 additions & 2 deletions the-graph/the-graph-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ module.exports.register = function (context) {
if (event.preventTap) { event.preventTap(); }

// Get mouse position
var x = event.x || event.clientX || 0;
var y = event.y || event.clientY || 0;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;

// App.showContext
this.props.showContext({
Expand Down
5 changes: 3 additions & 2 deletions the-graph/the-graph-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ module.exports.register = function (context) {
if (event.preventTap) { event.preventTap(); }

// Get mouse position
var x = event.x || event.clientX || 0;
var y = event.y || event.clientY || 0;
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;

// App.showContext
this.props.showContext({
Expand Down
20 changes: 7 additions & 13 deletions the-graph/the-graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,26 @@
},
onEdgeSelection: function (itemKey, item, toggle) {
if (itemKey === undefined) {
if (this.selectedEdges.length>0) {
if (this.selectedEdges.length > 0) {
this.selectedEdges = [];
}
this.fire('edges', this.selectedEdges);
return;
}
if (toggle) {
} else if (toggle) {
var index = this.selectedEdges.indexOf(item);
var isSelected = (index !== -1);
var shallowClone = this.selectedEdges.slice();
if (isSelected) {
shallowClone.splice(index, 1);
this.selectedEdges = shallowClone;
this.selectedEdges.splice(index, 1);
} else {
shallowClone.push(item);
this.selectedEdges = shallowClone;
this.selectedEdges.push(item);
}
} else {
this.selectedEdges = [item];
}
this.fire('edges', this.selectedEdges);
},
onNodeSelection: function (itemKey, item, toggle) {
if (itemKey === undefined) {
this.selectedNodes = [];
if (this.selectedNodes.length > 0) {
this.selectedNodes = [];
}
} else if (toggle) {
var index = this.selectedNodes.indexOf(item);
var isSelected = (index !== -1);
Expand All @@ -145,7 +140,6 @@
} else {
this.selectedNodes = [item];
}
this.fire('nodes', this.selectedNodes);
},
selectedNodesChanged: function () {
var selectedNodesHash = {};
Expand Down
32 changes: 30 additions & 2 deletions the-graph/the-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ module.exports.register = function (context) {
showTooltip: function (event) {
if ( !this.shouldShowTooltip() ) { return; }

// Get mouse position
var offset = TheGraph.getOffsetUpToElement(event.currentTarget, event.target);
var x = (event.layerX || event.clientX || 0) - offset.left;
var y = (event.layerY || event.clientY || 0) - offset.top;

var tooltipEvent = new CustomEvent('the-graph-tooltip', {
detail: {
tooltip: this.props.label,
x: event.clientX,
y: event.clientY
x: x,
y: y
},
bubbles: true
});
Expand Down Expand Up @@ -431,4 +436,27 @@ module.exports.register = function (context) {
}
};

/**
* Returns offsets for mouse position.
*
* THis function need to calculate correct offset in case the-graph
* used as positioned in not (0, 0) coordinates.
*
* @param initialElement - event target.
* @param upperElement - upper the-graph element (see usages for example).
* @returns {{top: number, left: number}}
*/
TheGraph.getOffsetUpToElement = function(initialElement, upperElement) {
var offset = {top: 0, left: 0};

var offsetParent = initialElement;
while (offsetParent != null && offsetParent != upperElement) {
offset.left += offsetParent.offsetLeft || 0;
offset.top += offsetParent.offsetTop || 0;
offsetParent = offsetParent.parentElement;
}

return offset;
};

};