Skip to content

Commit 2cafbeb

Browse files
committed
relayout: merge all array container update logic into 1 block
- by using manageArrayContainers
1 parent 66c3ca0 commit 2cafbeb

File tree

6 files changed

+112
-64
lines changed

6 files changed

+112
-64
lines changed

src/plot_api/plot_api.js

+6-38
Original file line numberDiff line numberDiff line change
@@ -1816,8 +1816,7 @@ function _relayout(gd, aobj) {
18161816
// trunk nodes (everything except the leaf)
18171817
ptrunk = p.parts.slice(0, pend).join('.'),
18181818
parentIn = Lib.nestedProperty(gd.layout, ptrunk).get(),
1819-
parentFull = Lib.nestedProperty(fullLayout, ptrunk).get(),
1820-
diff;
1819+
parentFull = Lib.nestedProperty(fullLayout, ptrunk).get();
18211820

18221821
if(vi === undefined) continue;
18231822

@@ -1951,42 +1950,11 @@ function _relayout(gd, aobj) {
19511950
drawOne(gd, objNum, p.parts.slice(2).join('.'), aobj[ai]);
19521951
delete aobj[ai];
19531952
}
1954-
else if(p.parts[0] === 'images') {
1955-
var update = Lib.objectFromPath(ai, vi);
1956-
Lib.extendDeepAll(gd.layout, update);
1957-
1958-
Registry.getComponentMethod('images', 'supplyLayoutDefaults')(gd.layout, gd._fullLayout);
1959-
Registry.getComponentMethod('images', 'draw')(gd);
1960-
}
1961-
else if(p.parts[0] === 'mapbox' && p.parts[1] === 'layers') {
1962-
Lib.extendDeepAll(gd.layout, Lib.objectFromPath(ai, vi));
1963-
1964-
// append empty container to mapbox.layers
1965-
// so that relinkPrivateKeys does not complain
1966-
1967-
var fullLayers = (gd._fullLayout.mapbox || {}).layers || [];
1968-
diff = (p.parts[2] + 1) - fullLayers.length;
1969-
1970-
for(i = 0; i < diff; i++) fullLayers.push({});
1971-
1972-
flags.doplot = true;
1973-
}
1974-
else if(p.parts[0] === 'updatemenus') {
1975-
Lib.extendDeepAll(gd.layout, Lib.objectFromPath(ai, vi));
1976-
1977-
var menus = gd._fullLayout.updatemenus || [];
1978-
diff = (p.parts[2] + 1) - menus.length;
1979-
1980-
for(i = 0; i < diff; i++) menus.push({});
1981-
flags.doplot = true;
1982-
}
1983-
else if(p.parts[0] === 'sliders') {
1984-
Lib.extendDeepAll(gd.layout, Lib.objectFromPath(ai, vi));
1985-
1986-
var sliders = gd._fullLayout.sliders || [];
1987-
diff = (p.parts[2] + 1) - sliders.length;
1988-
1989-
for(i = 0; i < diff; i++) sliders.push({});
1953+
else if(
1954+
Plots.layoutArrayContainers.indexOf(p.parts[0]) !== -1 ||
1955+
(p.parts[0] === 'mapbox' && p.parts[1] === 'layers')
1956+
) {
1957+
helpers.manageArrayContainers(p, vi, undoit);
19901958
flags.doplot = true;
19911959
}
19921960
// alter gd.layout

test/jasmine/tests/layout_images_test.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,22 @@ describe('Layout images', function() {
329329
return Plotly.relayout(gd, 'images[2]', makeImage(pythonLogo, 0.2, 0.5));
330330
}).then(function() {
331331
assertImages(3);
332+
expect(gd.layout.images.length).toEqual(3);
332333

333-
return Plotly.relayout(gd, 'images[2]', 'remove');
334+
return Plotly.relayout(gd, 'images[2]', null);
334335
}).then(function() {
335336
assertImages(2);
337+
expect(gd.layout.images.length).toEqual(2);
336338

337-
return Plotly.relayout(gd, 'images[1]', 'remove');
339+
return Plotly.relayout(gd, 'images[1]', null);
338340
}).then(function() {
339341
assertImages(1);
342+
expect(gd.layout.images.length).toEqual(1);
340343

341-
return Plotly.relayout(gd, 'images[0]', 'remove');
344+
return Plotly.relayout(gd, 'images[0]', null);
342345
}).then(function() {
343346
assertImages(0);
347+
expect(gd.layout.images).toEqual([]);
344348

345349
done();
346350
});

test/jasmine/tests/lib_test.js

+40-4
Original file line numberDiff line numberDiff line change
@@ -1561,23 +1561,27 @@ describe('Queue', function() {
15611561

15621562
Plotly.plot(gd, [{
15631563
y: [2, 1, 2]
1564-
}]).then(function() {
1564+
}])
1565+
.then(function() {
15651566
expect(gd.undoQueue).toBeUndefined();
15661567

15671568
return Plotly.restyle(gd, 'marker.color', 'red');
1568-
}).then(function() {
1569+
})
1570+
.then(function() {
15691571
expect(gd.undoQueue.index).toEqual(1);
15701572
expect(gd.undoQueue.queue[0].undo.args[0][1]['marker.color']).toEqual([undefined]);
15711573
expect(gd.undoQueue.queue[0].redo.args[0][1]['marker.color']).toEqual('red');
15721574

15731575
return Plotly.relayout(gd, 'title', 'A title');
1574-
}).then(function() {
1576+
})
1577+
.then(function() {
15751578
expect(gd.undoQueue.index).toEqual(2);
15761579
expect(gd.undoQueue.queue[1].undo.args[0][1].title).toEqual(undefined);
15771580
expect(gd.undoQueue.queue[1].redo.args[0][1].title).toEqual('A title');
15781581

15791582
return Plotly.restyle(gd, 'mode', 'markers');
1580-
}).then(function() {
1583+
})
1584+
.then(function() {
15811585
expect(gd.undoQueue.index).toEqual(2);
15821586
expect(gd.undoQueue.queue[2]).toBeUndefined();
15831587

@@ -1587,6 +1591,38 @@ describe('Queue', function() {
15871591
expect(gd.undoQueue.queue[0].undo.args[0][1].title).toEqual(undefined);
15881592
expect(gd.undoQueue.queue[0].redo.args[0][1].title).toEqual('A title');
15891593

1594+
return Plotly.restyle(gd, 'transforms[0]', { type: 'filter' });
1595+
})
1596+
.then(function() {
1597+
expect(gd.undoQueue.queue[1].undo.args[0][1])
1598+
.toEqual({ 'transforms[0]': null });
1599+
expect(gd.undoQueue.queue[1].redo.args[0][1])
1600+
.toEqual({ 'transforms[0]': { type: 'filter' } });
1601+
1602+
return Plotly.relayout(gd, 'updatemenus[0]', { buttons: [] });
1603+
})
1604+
.then(function() {
1605+
expect(gd.undoQueue.queue[1].undo.args[0][1])
1606+
.toEqual({ 'updatemenus[0]': null });
1607+
expect(gd.undoQueue.queue[1].redo.args[0][1])
1608+
.toEqual({ 'updatemenus[0]': { buttons: [] } });
1609+
1610+
return Plotly.relayout(gd, 'updatemenus[0]', null);
1611+
})
1612+
.then(function() {
1613+
expect(gd.undoQueue.queue[1].undo.args[0][1])
1614+
.toEqual({ 'updatemenus[0]': { buttons: []} });
1615+
expect(gd.undoQueue.queue[1].redo.args[0][1])
1616+
.toEqual({ 'updatemenus[0]': null });
1617+
1618+
return Plotly.restyle(gd, 'transforms[0]', null);
1619+
})
1620+
.then(function() {
1621+
expect(gd.undoQueue.queue[1].undo.args[0][1])
1622+
.toEqual({ 'transforms[0]': [ { type: 'filter' } ]});
1623+
expect(gd.undoQueue.queue[1].redo.args[0][1])
1624+
.toEqual({ 'transforms[0]': null });
1625+
15901626
done();
15911627
});
15921628
});

test/jasmine/tests/mapbox_test.js

+33-12
Original file line numberDiff line numberDiff line change
@@ -548,56 +548,77 @@ describe('mapbox plots', function() {
548548
expect(countVisibleLayers(gd)).toEqual(0);
549549

550550
Plotly.relayout(gd, 'mapbox.layers[0]', layer0).then(function() {
551+
expect(gd.layout.mapbox.layers.length).toEqual(1);
551552
expect(countVisibleLayers(gd)).toEqual(1);
552553

553554
return Plotly.relayout(gd, 'mapbox.layers[1]', layer1);
554-
}).then(function() {
555+
})
556+
.then(function() {
557+
expect(gd.layout.mapbox.layers.length).toEqual(2);
555558
expect(countVisibleLayers(gd)).toEqual(2);
556559

557560
return Plotly.relayout(gd, mapUpdate);
558-
}).then(function() {
561+
})
562+
.then(function() {
563+
expect(gd.layout.mapbox.layers.length).toEqual(2);
559564
expect(countVisibleLayers(gd)).toEqual(2);
560565

561566
return Plotly.relayout(gd, styleUpdate0);
562-
}).then(function() {
567+
})
568+
.then(function() {
569+
expect(gd.layout.mapbox.layers.length).toEqual(2);
563570
expect(countVisibleLayers(gd)).toEqual(2);
564571

565572
return assertLayerStyle(gd, {
566573
'fill-color': [1, 0, 0, 1],
567574
'fill-outline-color': [0, 0, 1, 1],
568575
'fill-opacity': 0.3
569576
}, 0);
570-
}).then(function() {
577+
})
578+
.then(function() {
579+
expect(gd.layout.mapbox.layers.length).toEqual(2);
571580
expect(countVisibleLayers(gd)).toEqual(2);
572581

573582
return Plotly.relayout(gd, styleUpdate1);
574-
}).then(function() {
583+
})
584+
.then(function() {
585+
expect(gd.layout.mapbox.layers.length).toEqual(2);
575586
expect(countVisibleLayers(gd)).toEqual(2);
576587

577588
return assertLayerStyle(gd, {
578589
'line-width': 3,
579590
'line-color': [0, 0, 1, 1],
580591
'line-opacity': 0.6
581592
}, 1);
582-
}).then(function() {
593+
})
594+
.then(function() {
595+
expect(gd.layout.mapbox.layers.length).toEqual(2);
583596
expect(countVisibleLayers(gd)).toEqual(2);
584597

585-
return Plotly.relayout(gd, 'mapbox.layers[1]', 'remove');
586-
}).then(function() {
598+
return Plotly.relayout(gd, 'mapbox.layers[1]', null);
599+
})
600+
.then(function() {
601+
expect(gd.layout.mapbox.layers.length).toEqual(1);
587602
expect(countVisibleLayers(gd)).toEqual(1);
588603

589-
return Plotly.relayout(gd, 'mapbox.layers[0]', 'remove');
590-
}).then(function() {
604+
return Plotly.relayout(gd, 'mapbox.layers[0]', null);
605+
})
606+
.then(function() {
607+
expect(gd.layout.mapbox.layers.length).toEqual(0);
591608
expect(countVisibleLayers(gd)).toEqual(0);
592609

593610
return Plotly.relayout(gd, 'mapbox.layers[0]', {});
594-
}).then(function() {
611+
})
612+
.then(function() {
613+
expect(gd.layout.mapbox.layers).toEqual([]);
595614
expect(countVisibleLayers(gd)).toEqual(0);
596615

597616
// layer with no source are not drawn
598617

599618
return Plotly.relayout(gd, 'mapbox.layers[0].source', layer0.source);
600-
}).then(function() {
619+
})
620+
.then(function() {
621+
expect(gd.layout.mapbox.layers.length).toEqual(1);
601622
expect(countVisibleLayers(gd)).toEqual(1);
602623

603624
done();

test/jasmine/tests/sliders_test.js

+2
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,15 @@ describe('update sliders interactions', function() {
211211
assertNodeCount('.' + constants.groupClassName, 1);
212212
expect(gd._fullLayout._pushmargin['slider-0']).toBeUndefined();
213213
expect(gd._fullLayout._pushmargin['slider-1']).toBeDefined();
214+
expect(gd.layout.sliders.length).toEqual(2);
214215

215216
return Plotly.relayout(gd, 'sliders[1]', null);
216217
})
217218
.then(function() {
218219
assertNodeCount('.' + constants.groupClassName, 0);
219220
expect(gd._fullLayout._pushmargin['slider-0']).toBeUndefined();
220221
expect(gd._fullLayout._pushmargin['slider-1']).toBeUndefined();
222+
expect(gd.layout.sliders.length).toEqual(1);
221223

222224
return Plotly.relayout(gd, {
223225
'sliders[0].visible': true,

test/jasmine/tests/updatemenus_test.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ describe('update menus interactions', function() {
260260
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeDefined();
261261

262262
return Plotly.relayout(gd, 'updatemenus[1]', null);
263-
}).then(function() {
263+
})
264+
.then(function() {
264265
assertNodeCount('.' + constants.containerClassName, 0);
265266
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeUndefined();
266267
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
@@ -269,7 +270,8 @@ describe('update menus interactions', function() {
269270
'updatemenus[0].visible': true,
270271
'updatemenus[1].visible': true
271272
});
272-
}).then(function() {
273+
})
274+
.then(function() {
273275
assertMenus([0, 0]);
274276
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeDefined();
275277
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeDefined();
@@ -278,7 +280,8 @@ describe('update menus interactions', function() {
278280
'updatemenus[0].visible': false,
279281
'updatemenus[1].visible': false
280282
});
281-
}).then(function() {
283+
})
284+
.then(function() {
282285
assertNodeCount('.' + constants.containerClassName, 0);
283286
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeUndefined();
284287
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
@@ -291,21 +294,35 @@ describe('update menus interactions', function() {
291294
}]
292295
}
293296
});
294-
}).then(function() {
297+
})
298+
.then(function() {
295299
assertMenus([0]);
296300
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeUndefined();
297301
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
298302
expect(gd._fullLayout._pushmargin['updatemenu-2']).toBeDefined();
299303

300304
return Plotly.relayout(gd, 'updatemenus[0].visible', true);
301-
}).then(function() {
305+
})
306+
.then(function() {
302307
assertMenus([0, 0]);
303308
expect(gd._fullLayout._pushmargin['updatemenu-0']).toBeDefined();
304309
expect(gd._fullLayout._pushmargin['updatemenu-1']).toBeUndefined();
305310
expect(gd._fullLayout._pushmargin['updatemenu-2']).toBeDefined();
311+
expect(gd.layout.updatemenus.length).toEqual(3);
306312

307-
done();
308-
});
313+
return Plotly.relayout(gd, 'updatemenus[0]', null);
314+
})
315+
.then(function() {
316+
assertMenus([0]);
317+
expect(gd.layout.updatemenus.length).toEqual(2);
318+
319+
return Plotly.relayout(gd, 'updatemenus', null);
320+
})
321+
.then(function() {
322+
expect(gd.layout.updatemenus).toBeUndefined();
323+
324+
})
325+
.then(done);
309326
});
310327

311328
it('should drop/fold buttons when clicking on header', function(done) {

0 commit comments

Comments
 (0)