Skip to content

Commit b5b32ef

Browse files
authored
Merge pull request #6262 from plotly/selection-events-in-draw-modes
Emit selection events in drawmode when an existing selection modified
2 parents 597da12 + 16461df commit b5b32ef

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

draftlogs/6262_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Emit selection event in shape drawing `dragmode`s when an existing selection modified [[#6262](https://github.com/plotly/plotly.js/pull/6262)]

src/components/selections/select.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ function prepSelect(evt, startX, startY, dragOptions, mode) {
451451
dragOptions.doneFnCompleted(selection);
452452
}
453453

454-
emitSelected(gd, eventData);
454+
if(isSelectMode) {
455+
emitSelected(gd, eventData);
456+
}
455457
}).catch(Lib.error);
456458
};
457459
}
@@ -673,15 +675,23 @@ function coerceSelectionsCache(evt, gd, dragOptions) {
673675
}
674676
}
675677

678+
function hasActiveShape(gd) {
679+
return gd._fullLayout._activeShapeIndex >= 0;
680+
}
681+
682+
function hasActiveSelection(gd) {
683+
return gd._fullLayout._activeSelectionIndex >= 0;
684+
}
685+
676686
function clearSelectionsCache(dragOptions, immediateSelect) {
677687
var dragmode = dragOptions.dragmode;
678688
var plotinfo = dragOptions.plotinfo;
679689

680690
var gd = dragOptions.gd;
681-
if(gd._fullLayout._activeShapeIndex >= 0) {
691+
if(hasActiveShape(gd)) {
682692
gd._fullLayout._deactivateShape(gd);
683693
}
684-
if(gd._fullLayout._activeSelectionIndex >= 0) {
694+
if(hasActiveSelection(gd)) {
685695
gd._fullLayout._deactivateSelection(gd);
686696
}
687697

@@ -1503,13 +1513,10 @@ function getFillRangeItems(dragOptions) {
15031513
}
15041514

15051515
function emitSelecting(gd, eventData) {
1506-
if(drawMode(gd._fullLayout.dragmode)) return;
15071516
gd.emit('plotly_selecting', eventData);
15081517
}
15091518

15101519
function emitSelected(gd, eventData) {
1511-
if(drawMode(gd._fullLayout.dragmode)) return;
1512-
15131520
if(eventData) {
15141521
eventData.selections = (gd.layout || {}).selections || [];
15151522
}
@@ -1518,7 +1525,6 @@ function emitSelected(gd, eventData) {
15181525
}
15191526

15201527
function emitDeselect(gd) {
1521-
if(drawMode(gd._fullLayout.dragmode)) return;
15221528
gd.emit('plotly_deselect', null);
15231529
}
15241530

test/jasmine/tests/draw_newselection_test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,80 @@ describe('Activate and edit selections', function() {
651651
.then(done, done.fail);
652652
});
653653
});
654+
655+
656+
describe('emit plotly_selected event on editing selections in various dragmodes', function() {
657+
var gd;
658+
659+
beforeEach(function() {
660+
gd = createGraphDiv();
661+
});
662+
663+
afterEach(destroyGraphDiv);
664+
665+
['zoom', 'pan', 'drawrect', 'drawclosedpath', 'drawcircle'].forEach(function(dragmode) {
666+
it('get eventData for editing selections using ' + dragmode + ' dragmode', function(done) {
667+
var fig = {
668+
data: [
669+
{
670+
x: [0, 1, 2],
671+
y: [1, 2, 3]
672+
}
673+
],
674+
layout: {
675+
width: 800,
676+
height: 600,
677+
margin: {
678+
t: 100,
679+
b: 50,
680+
l: 100,
681+
r: 50
682+
},
683+
selections: [{ x0: 0.5, x1: 1.5, y0: 1.5, y1: 2.5}],
684+
dragmode: dragmode
685+
}
686+
};
687+
688+
var range;
689+
var points;
690+
var lassoPoints;
691+
var selections;
692+
693+
Plotly.newPlot(gd, fig)
694+
695+
.then(function() {
696+
gd.on('plotly_selected', function(d) {
697+
lassoPoints = d.lassoPoints;
698+
range = d.range;
699+
points = d.points;
700+
selections = d.selections;
701+
});
702+
})
703+
704+
.then(function() { click(400, 300); }) // activate selection
705+
.then(function() { drag([[400, 300], [600, 100]]); }) // move selection
706+
.then(function() {
707+
expect(range).not.toBeUndefined();
708+
expect(range.x).toBeCloseToArray([1.1926580086580088, 2.1926580086580088], 3);
709+
expect(range.y).toBeCloseToArray([2.5062641509433967, 3.5062641509433967], 3);
710+
711+
expect(lassoPoints).toBeUndefined();
712+
713+
expect(points).not.toBeUndefined();
714+
expect(points.length).toEqual(1);
715+
expect(points[0].fullData).not.toBeUndefined();
716+
expect(points[0].data).not.toBeUndefined();
717+
expect(points[0].data.selectedpoints).toEqual([2]);
718+
719+
expect(selections).not.toBeUndefined();
720+
expect(selections.length).toEqual(1);
721+
expect(selections[0]).not.toBeUndefined();
722+
expect(selections[0].x0).toBeCloseTo(1.1926580086580088, 3);
723+
expect(selections[0].x1).toBeCloseTo(2.1926580086580088, 3);
724+
expect(selections[0].y0).toBeCloseTo(2.5062641509433967, 3);
725+
expect(selections[0].y1).toBeCloseTo(3.5062641509433967, 3);
726+
})
727+
.then(done, done.fail);
728+
});
729+
});
730+
});

0 commit comments

Comments
 (0)