Skip to content

Fixing issue #5350 by moving the 'false' check #6727

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 7 commits into from
Sep 22, 2023
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/6727_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Addressing issue [[#5350](https://github.com/plotly/plotly.js/issues/5350)] via pull request [[#6727](https://github.com/plotly/plotly.js/pull/6727)]. This small change allows custom plotly_legenddoubleclick handlers to execute even when the default plotly_legendclick event is cancelled (returns false).
7 changes: 3 additions & 4 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,9 @@ function clickOrDoubleClick(gd, legend, legendItem, numClicks, evt) {
if(Registry.traceIs(trace, 'pie-like')) {
evtData.label = legendItem.datum()[0].label;
}

var clickVal = Events.triggerHandler(gd, 'plotly_legendclick', evtData);
if(clickVal === false) return;

if(numClicks === 1) {
if(clickVal === false) return;
legend._clickTimeout = setTimeout(function() {
if(!gd._fullLayout) return;
handleClick(legendItem, gd, numClicks);
Expand All @@ -478,7 +476,8 @@ function clickOrDoubleClick(gd, legend, legendItem, numClicks, evt) {
gd._legendMouseDownTime = 0;

var dblClickVal = Events.triggerHandler(gd, 'plotly_legenddoubleclick', evtData);
if(dblClickVal !== false) handleClick(legendItem, gd, numClicks);
// Activate default double click behaviour only when both single click and double click values are not false
if(dblClickVal !== false && clickVal !== false) handleClick(legendItem, gd, numClicks);
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,38 @@ describe('legend with custom doubleClickDelay', function() {
.then(_assert('[short] after click + (1.1*t) delay + click', 2, 0))
.then(done, done.fail);
}, 3 * jasmine.DEFAULT_TIMEOUT_INTERVAL);

it('custom plotly_legenddoubleclick handler should fire even when plotly_legendclick has been cancelled', function(done) {
var tShort = 0.75 * DBLCLICKDELAY;
var dblClickCnt = 0;
var newPlot = function(fig) {
return Plotly.newPlot(gd, fig).then(function() {
gd.on('plotly_legendclick', function() { return false; });
gd.on('plotly_legenddoubleclick', function() { dblClickCnt++; });
});
};

function _assert(msg, _dblClickCnt) {
return function() {
expect(dblClickCnt).toBe(_dblClickCnt, msg + '| dblClickCnt');
dblClickCnt = 0;
};
}

newPlot({
data: [
{y: [1, 2, 1]},
{y: [2, 1, 2]}
],
layout: {},
config: {}
})
.then(click(0))
.then(delay(tShort))
.then(click(0))
.then(_assert('Double click increases count', 1))
.then(done);
}, 3 * jasmine.DEFAULT_TIMEOUT_INTERVAL);
});

describe('legend with custom legendwidth', function() {
Expand Down