Skip to content

Commit

Permalink
Merge pull request #6262 from plotly/selection-events-in-draw-modes
Browse files Browse the repository at this point in the history
Emit selection events in drawmode when an existing selection modified
  • Loading branch information
archmoj authored Jul 15, 2022
2 parents 597da12 + 16461df commit b5b32ef
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 7 deletions.
1 change: 1 addition & 0 deletions draftlogs/6262_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Emit selection event in shape drawing `dragmode`s when an existing selection modified [[#6262](https://github.com/plotly/plotly.js/pull/6262)]
20 changes: 13 additions & 7 deletions src/components/selections/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ function prepSelect(evt, startX, startY, dragOptions, mode) {
dragOptions.doneFnCompleted(selection);
}

emitSelected(gd, eventData);
if(isSelectMode) {
emitSelected(gd, eventData);
}
}).catch(Lib.error);
};
}
Expand Down Expand Up @@ -673,15 +675,23 @@ function coerceSelectionsCache(evt, gd, dragOptions) {
}
}

function hasActiveShape(gd) {
return gd._fullLayout._activeShapeIndex >= 0;
}

function hasActiveSelection(gd) {
return gd._fullLayout._activeSelectionIndex >= 0;
}

function clearSelectionsCache(dragOptions, immediateSelect) {
var dragmode = dragOptions.dragmode;
var plotinfo = dragOptions.plotinfo;

var gd = dragOptions.gd;
if(gd._fullLayout._activeShapeIndex >= 0) {
if(hasActiveShape(gd)) {
gd._fullLayout._deactivateShape(gd);
}
if(gd._fullLayout._activeSelectionIndex >= 0) {
if(hasActiveSelection(gd)) {
gd._fullLayout._deactivateSelection(gd);
}

Expand Down Expand Up @@ -1503,13 +1513,10 @@ function getFillRangeItems(dragOptions) {
}

function emitSelecting(gd, eventData) {
if(drawMode(gd._fullLayout.dragmode)) return;
gd.emit('plotly_selecting', eventData);
}

function emitSelected(gd, eventData) {
if(drawMode(gd._fullLayout.dragmode)) return;

if(eventData) {
eventData.selections = (gd.layout || {}).selections || [];
}
Expand All @@ -1518,7 +1525,6 @@ function emitSelected(gd, eventData) {
}

function emitDeselect(gd) {
if(drawMode(gd._fullLayout.dragmode)) return;
gd.emit('plotly_deselect', null);
}

Expand Down
77 changes: 77 additions & 0 deletions test/jasmine/tests/draw_newselection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,80 @@ describe('Activate and edit selections', function() {
.then(done, done.fail);
});
});


describe('emit plotly_selected event on editing selections in various dragmodes', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

['zoom', 'pan', 'drawrect', 'drawclosedpath', 'drawcircle'].forEach(function(dragmode) {
it('get eventData for editing selections using ' + dragmode + ' dragmode', function(done) {
var fig = {
data: [
{
x: [0, 1, 2],
y: [1, 2, 3]
}
],
layout: {
width: 800,
height: 600,
margin: {
t: 100,
b: 50,
l: 100,
r: 50
},
selections: [{ x0: 0.5, x1: 1.5, y0: 1.5, y1: 2.5}],
dragmode: dragmode
}
};

var range;
var points;
var lassoPoints;
var selections;

Plotly.newPlot(gd, fig)

.then(function() {
gd.on('plotly_selected', function(d) {
lassoPoints = d.lassoPoints;
range = d.range;
points = d.points;
selections = d.selections;
});
})

.then(function() { click(400, 300); }) // activate selection
.then(function() { drag([[400, 300], [600, 100]]); }) // move selection
.then(function() {
expect(range).not.toBeUndefined();
expect(range.x).toBeCloseToArray([1.1926580086580088, 2.1926580086580088], 3);
expect(range.y).toBeCloseToArray([2.5062641509433967, 3.5062641509433967], 3);

expect(lassoPoints).toBeUndefined();

expect(points).not.toBeUndefined();
expect(points.length).toEqual(1);
expect(points[0].fullData).not.toBeUndefined();
expect(points[0].data).not.toBeUndefined();
expect(points[0].data.selectedpoints).toEqual([2]);

expect(selections).not.toBeUndefined();
expect(selections.length).toEqual(1);
expect(selections[0]).not.toBeUndefined();
expect(selections[0].x0).toBeCloseTo(1.1926580086580088, 3);
expect(selections[0].x1).toBeCloseTo(2.1926580086580088, 3);
expect(selections[0].y0).toBeCloseTo(2.5062641509433967, 3);
expect(selections[0].y1).toBeCloseTo(3.5062641509433967, 3);
})
.then(done, done.fail);
});
});
});

0 comments on commit b5b32ef

Please sign in to comment.