Skip to content

Commit

Permalink
DRY up event emission
Browse files Browse the repository at this point in the history
  • Loading branch information
monfera committed Oct 3, 2016
1 parent 2e8db74 commit 9f77c11
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions src/plots/gl2d/scene2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,24 @@ proto.updateTraces = function(fullData, calcData) {
}
};

proto.emitPointAction = function(nextSelection, eventType) {

var curveIndex = this._inputs[nextSelection.trace.uid];

this.graphDiv.emit(eventType, {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
curveNumber: curveIndex,
pointNumber: nextSelection.pointIndex,
data: this.fullData[curveIndex]._input,
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});
};

proto.draw = function() {
if(this.stopped) return;

Expand Down Expand Up @@ -507,22 +525,9 @@ proto.draw = function() {
);

var nextSelection = result && result.object._trace.handlePick(result);
var curveIndex;

if(nextSelection && mouseUp) {
curveIndex = this._inputs[nextSelection.trace.uid];
this.graphDiv.emit('plotly_click', {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
curveNumber: curveIndex,
pointNumber: nextSelection.pointIndex,
data: this.fullData[curveIndex]._input,
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});
this.emitPointAction(nextSelection, 'plotly_click');
}

if(result && result.object._trace.hoverinfo !== 'skip' && fullLayout.hovermode) {
Expand Down Expand Up @@ -554,19 +559,7 @@ proto.draw = function() {

// this needs to happen before the next block that deletes traceCoord data
// also it's important to copy, otherwise data is lost by the time event data is read
curveIndex = this._inputs[nextSelection.trace.uid];
this.graphDiv.emit('plotly_hover', {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
curveNumber: curveIndex,
pointNumber: nextSelection.pointIndex,
data: this.fullData[curveIndex]._input,
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});
this.emitPointAction(nextSelection, 'plotly_hover');

var hoverinfo = selection.hoverinfo;
if(hoverinfo !== 'all') {
Expand Down

0 comments on commit 9f77c11

Please sign in to comment.