Skip to content

Commit 3610253

Browse files
committed
improve object constancy approach:
- the '_index' field now corresponds to the menu index in the user layout update menu container. This is a more 'consistent' field than e.g. the index in the menuData. - the '_index' field is set at the default step -> no need to rely on relinkPrivateKeys.
1 parent b8e80fd commit 3610253

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/components/updatemenus/defaults.js

+6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ module.exports = function updateMenusDefaults(layoutIn, layoutOut) {
2626
menuOut = {};
2727

2828
menuDefaults(menuIn, menuOut, layoutOut);
29+
30+
// used on button click to update the 'active' field
2931
menuOut._input = menuIn;
32+
33+
// used to determine object constancy
34+
menuOut._index = i;
35+
3036
contOut.push(menuOut);
3137
}
3238
};

src/components/updatemenus/draw.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,24 @@ module.exports = function draw(gd) {
125125

126126
function makeMenuData(fullLayout) {
127127
var contOpts = fullLayout[constants.name],
128-
menuData = [],
129-
cnt = 0;
128+
menuData = [];
130129

131130
// Filter visible dropdowns and attach '_index' to each
132131
// fullLayout options object to be used for 'object constancy'
133132
// in the data join key function.
134-
//
135-
// Note that '_index' is relinked from update to update via
136-
// Plots.supplyDefaults.
137133

138134
for(var i = 0; i < contOpts.length; i++) {
139135
var item = contOpts[i];
140136

141-
if(item.visible) {
142-
if(!item._index) item._index = cnt++;
143-
menuData.push(item);
144-
}
137+
if(item.visible) menuData.push(item);
145138
}
146139

147140
return menuData;
148141
}
149142

143+
// Note that '_index' is set at the default step,
144+
// it corresponds to the menu index in the user layout update menu container.
145+
// This is a more 'consistent' field than e.g. the index in the menuData.
150146
function keyFunction(opts) {
151147
return opts._index;
152148
}

test/jasmine/tests/updatemenus_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,27 @@ describe('update menus interactions', function() {
194194
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeUndefined();
195195
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
196196

197+
return Plotly.relayout(gd, {
198+
'updatemenus[2]': {
199+
buttons: [{
200+
method: 'relayout',
201+
args: ['title', 'new title']
202+
}]
203+
}
204+
});
205+
}).then(function() {
206+
assertMenus([0]);
207+
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeUndefined();
208+
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
209+
expect(gd._fullLayout._pushmargin['updatemenu-2']).toBeDefined();
210+
211+
return Plotly.relayout(gd, 'updatemenus[0].visible', true);
212+
}).then(function() {
213+
assertMenus([0, 0]);
214+
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeDefined();
215+
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
216+
expect(gd._fullLayout._pushmargin['updatemenu-2']).toBeDefined();
217+
197218
done();
198219
});
199220
});

0 commit comments

Comments
 (0)