Skip to content

fix to allow you to add fake selections #7164

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

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
1 change: 1 addition & 0 deletions draftlogs/7164_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Allow null or broken selection objects without throwing an error [[#7164](https://github.com/plotly/plotly.js/pull/7164)]
1 change: 1 addition & 0 deletions src/components/selections/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function prepSelect(evt, startX, startY, dragOptions, mode) {
for(var q = 0; q < selections.length; q++) {
var s = fullLayout.selections[q];
if(
!s ||
s.xref !== xRef ||
s.yref !== yRef
) {
Expand Down
102 changes: 101 additions & 1 deletion test/jasmine/tests/select_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function assertSelectionNodes(cornerCnt, outlineCnt, _msg) {
}

var selectingCnt, selectingData, selectedCnt, selectedData, deselectCnt, doubleClickData;
var selectedPromise, deselectPromise, clickedPromise;
var selectedPromise, deselectPromise, clickedPromise, relayoutPromise;

function resetEvents(gd) {
selectingCnt = 0;
Expand Down Expand Up @@ -125,6 +125,12 @@ function resetEvents(gd) {
resolve();
});
});

relayoutPromise = new Promise(function(resolve) {
gd.on('plotly_relayout', function() {
resolve();
});
});
}

function assertEventCounts(selecting, selected, deselect, msg) {
Expand Down Expand Up @@ -1035,6 +1041,100 @@ describe('Test select box and lasso in general:', function() {
});
});

describe('select / deselect with fake selections', function() {
var gd;
beforeEach(function(done) {
gd = createGraphDiv();

var mockCopy = Lib.extendDeep({}, mock);
mockCopy.layout.dragmode = 'select';
mockCopy.layout.hovermode = 'closest';
mockCopy.layout.selections = [null];
addInvisible(mockCopy);

_newPlot(gd, mockCopy.data, mockCopy.layout)
.then(done);
});

it('should trigger selecting/selected/deselect events', function(done) {
resetEvents(gd);

drag(selectPath);

selectedPromise.then(function() {
expect(selectedCnt).toBe(1, 'with the correct selected count');
assertEventData(selectedData.points, [{
curveNumber: 0,
pointNumber: 0,
pointIndex: 0,
x: 0.002,
y: 16.25
}, {
curveNumber: 0,
pointNumber: 1,
pointIndex: 1,
x: 0.004,
y: 12.5
}], 'with the correct selected points (2)');
assertRange(selectedData.range, {
x: [0.002000, 0.0046236],
y: [0.10209191961595454, 24.512223978291406]
}, 'with the correct selected range');

return doubleClick(250, 200);
})
.then(deselectPromise)
.then(function() {
expect(doubleClickData).toBe(null, 'with the correct deselect data');
})
.then(done, done.fail);
});

it('should handle add/sub selection', function(done) {
resetEvents(gd);
expect(gd.layout.selections.length).toBe(1);

drag([[193, 193], [213, 193]], {shiftKey: true})

selectedPromise.then(function() {
expect(selectedCnt).toBe(1, 'with the correct selected count');
assertEventData(selectedData.points, [{
curveNumber: 0,
pointNumber: 4,
pointIndex: 4,
x: 0.013,
y: 6.875
}], 'with the correct selected points (1)');
})
.then(function() {
// this is not working here, but it works in the test dashboard, not sure why
// but at least this test shows us that no errors are thrown.
// expect(gd.layout.selections.length).toBe(2, 'fake selection is still there');

resetEvents(gd);

return doubleClick(250, 200);
})
.then(relayoutPromise)
.then(function() {
expect(gd.layout.selections.length).toBe(0, 'fake selection is cleared');
expect(doubleClickData).toBe(null, 'with the correct deselect data');
})
.then(done, done.fail);
});

it('should clear fake selections on doubleclick', function(done) {
resetEvents(gd);

doubleClick(250, 200);

relayoutPromise.then(function() {
expect(gd.layout.selections.length).toBe(0, 'fake selections are cleared');
})
.then(done, done.fail);
});
});

describe('lasso events', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.layout.dragmode = 'lasso';
Expand Down