Skip to content

Commit 990a960

Browse files
authored
Merge pull request #4305 from plotly/updatemenus-toggle-via-args2
Updatemenus toggle buttons via args2
2 parents 5a07b46 + 62cacdb commit 990a960

File tree

6 files changed

+106
-5
lines changed

6 files changed

+106
-5
lines changed

src/components/updatemenus/attributes.js

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ var buttonsAttrs = templatedArray('button', {
4848
'method set in `method` on click.'
4949
].join(' ')
5050
},
51+
args2: {
52+
valType: 'info_array',
53+
role: 'info',
54+
freeLength: true,
55+
items: [
56+
{valType: 'any'},
57+
{valType: 'any'},
58+
{valType: 'any'}
59+
],
60+
description: [
61+
'Sets a 2nd set of `args`,',
62+
'these arguments values are passed to the Plotly',
63+
'method set in `method` when clicking this button while in the active state.',
64+
'Use this to create toggle buttons.'
65+
].join(' ')
66+
},
5167
label: {
5268
valType: 'string',
5369
role: 'info',

src/components/updatemenus/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function buttonDefaults(buttonIn, buttonOut) {
7474
if(visible) {
7575
coerce('method');
7676
coerce('args');
77+
coerce('args2');
7778
coerce('label');
7879
coerce('execute');
7980
}

src/components/updatemenus/draw.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module.exports = function draw(gd) {
119119
var gHeader = d3.select(this);
120120

121121
var _gButton = menuOpts.type === 'dropdown' ? gButton : null;
122+
122123
Plots.manageCommandObserver(gd, menuOpts, menuOpts.buttons, function(data) {
123124
setActive(gd, menuOpts, menuOpts.buttons[data.index], gHeader, _gButton, scrollBox, data.index, true);
124125
});
@@ -306,10 +307,14 @@ function drawButtons(gd, gHeader, gButton, scrollBox, menuOpts) {
306307
// skip `dragend` events
307308
if(d3.event.defaultPrevented) return;
308309

309-
setActive(gd, menuOpts, buttonOpts, gHeader, gButton, scrollBox, buttonIndex);
310-
311310
if(buttonOpts.execute) {
312-
Plots.executeAPICommand(gd, buttonOpts.method, buttonOpts.args);
311+
if(buttonOpts.args2 && menuOpts.active === buttonIndex) {
312+
setActive(gd, menuOpts, buttonOpts, gHeader, gButton, scrollBox, -1);
313+
Plots.executeAPICommand(gd, buttonOpts.method, buttonOpts.args2);
314+
} else {
315+
setActive(gd, menuOpts, buttonOpts, gHeader, gButton, scrollBox, buttonIndex);
316+
Plots.executeAPICommand(gd, buttonOpts.method, buttonOpts.args);
317+
}
313318
}
314319

315320
gd.emit('plotly_buttonclicked', {menu: menuOpts, button: buttonOpts, active: menuOpts.active});
18.2 KB
Loading
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"data": [
3+
{
4+
"mode": "lines",
5+
"y": [1, 1],
6+
"line": {
7+
"width": 5,
8+
"color": "blue"
9+
}
10+
}
11+
],
12+
"layout": {
13+
"updatemenus": [
14+
{
15+
"type": "buttons",
16+
"buttons": [
17+
{
18+
"label": "toggle",
19+
"method": "restyle",
20+
"args": [
21+
"line.color",
22+
"blue"
23+
],
24+
"args2": [
25+
"line.color",
26+
"red"
27+
]
28+
}
29+
]
30+
}
31+
]
32+
}
33+
}

test/jasmine/tests/updatemenus_test.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,50 @@ describe('update menus interactions', function() {
601601
.then(done);
602602
});
603603

604+
it('should apply update on button click (toggle via args2 case)', function(done) {
605+
var menuOpts = {
606+
type: 'buttons',
607+
buttons: [{
608+
label: 'toggle',
609+
method: 'restyle',
610+
args: ['line.color', 'blue'],
611+
args2: ['line.color', 'red']
612+
}]
613+
};
614+
615+
var btn;
616+
617+
function assertLineColor(msg, lineColor) {
618+
expect(gd.data[2].line.color).toBe(lineColor, 'gd.data line.color| ' + msg);
619+
expect(gd._fullData[2].line.color).toBe(lineColor, 'gd._fullData line.color| ' + msg);
620+
}
621+
622+
Plotly.relayout(gd, 'updatemenus', null)
623+
.then(function() { return Plotly.relayout(gd, 'updatemenus[0]', menuOpts); })
624+
.then(function() {
625+
btn = selectButton(0, {type: 'buttons'});
626+
assertItemColor(btn, activeColor);
627+
assertLineColor('base', 'blue');
628+
return click(btn);
629+
})
630+
.then(function() {
631+
assertItemColor(btn, bgColor);
632+
assertLineColor('base', 'red');
633+
return click(btn);
634+
})
635+
.then(function() {
636+
assertItemColor(btn, activeColor);
637+
assertLineColor('base', 'blue');
638+
return click(btn);
639+
})
640+
.then(function() {
641+
assertItemColor(btn, bgColor);
642+
assertLineColor('base', 'red');
643+
})
644+
.catch(failTest)
645+
.then(done);
646+
});
647+
604648
it('should update correctly on failed binding comparisons', function(done) {
605649
// See https://github.com/plotly/plotly.js/issues/1169
606650
// for more info.
@@ -871,8 +915,10 @@ describe('update menus interactions', function() {
871915
return header;
872916
}
873917

874-
function selectButton(buttonIndex) {
875-
var buttons = d3.selectAll('.' + constants.dropdownButtonClassName);
918+
function selectButton(buttonIndex, opts) {
919+
opts = opts || {};
920+
var k = opts.type === 'buttons' ? 'buttonClassName' : 'dropdownButtonClassName';
921+
var buttons = d3.selectAll('.' + constants[k]);
876922
var button = d3.select(buttons[0][buttonIndex]);
877923
return button;
878924
}

0 commit comments

Comments
 (0)