Skip to content

Commit d9ed496

Browse files
committed
generalize cleanScenes step:
- ensures the deleted geos are only properly removed from the DOM. - ensures the deleted contour, heatmap and colorbar are properly removed
1 parent fd5cab7 commit d9ed496

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/plots/geo/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,16 @@ exports.plot = function plotGeo(gd) {
6565
geo.plot(fullGeoData, fullLayout, gd._promises);
6666
}
6767
};
68+
69+
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
70+
var oldGeoKeys = Plots.getSubplotIds(oldFullLayout, 'geo');
71+
72+
for(var i = 0; i < oldGeoKeys.length; i++) {
73+
var oldGeoKey = oldGeoKeys[i];
74+
var oldGeo = oldFullLayout[oldGeoKey]._geo;
75+
76+
if(!newFullLayout[oldGeoKey] && !!oldGeo) {
77+
oldGeo.geoDiv.remove();
78+
}
79+
}
80+
};

src/plots/gl3d/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ exports.plot = function plotGl3d(gd) {
6767
}
6868
};
6969

70+
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
71+
var oldSceneKeys = Plots.getSubplotIds(oldFullLayout, 'gl3d');
72+
73+
for(var i = 0; i < oldSceneKeys.length; i++) {
74+
var oldSceneKey = oldSceneKeys[i];
75+
76+
if(!newFullLayout[oldSceneKey] && !!oldFullLayout[oldSceneKey]._scene) {
77+
oldFullLayout[oldSceneKey]._scene.destroy();
78+
}
79+
}
80+
};
81+
7082
// clean scene ids, 'scene1' -> 'scene'
7183
exports.cleanId = function cleanId(id) {
7284
if (!id.match(/^scene[0-9]*$/)) return;

src/plots/plots.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ plots.supplyDefaults = function(gd) {
447447
// finally, fill in the pieces of layout that may need to look at data
448448
plots.supplyLayoutModuleDefaults(newLayout, newFullLayout, newFullData);
449449

450-
cleanScenes(newFullLayout, oldFullLayout);
450+
// clean subplots and other artifacts from previous plot calls
451+
cleanPlot(newFullData, newFullLayout, oldFullData, oldFullLayout);
451452

452453
/*
453454
* Relink functions and underscore attributes to promote consistency between
@@ -475,16 +476,46 @@ plots.supplyDefaults = function(gd) {
475476
}
476477
};
477478

478-
function cleanScenes(newFullLayout, oldFullLayout) {
479-
var oldSceneKeys = plots.getSubplotIds(oldFullLayout, 'gl3d');
479+
function cleanPlot(newFullData, newFullLayout, oldFullData, oldFullLayout) {
480+
var i, j;
480481

481-
for(var i = 0; i < oldSceneKeys.length; i++) {
482-
var oldSceneKey = oldSceneKeys[i];
483-
if(!newFullLayout[oldSceneKey] && !!oldFullLayout[oldSceneKey]._scene) {
484-
oldFullLayout[oldSceneKey]._scene.destroy();
482+
var plotTypes = Object.keys(subplotsRegistry);
483+
for(i = 0; i < plotTypes.length; i++) {
484+
var _module = subplotsRegistry[plotTypes[i]];
485+
486+
if(_module.clean) {
487+
_module.clean(newFullData, newFullLayout, oldFullData, oldFullLayout);
485488
}
486489
}
487490

491+
var hasPaper = !!oldFullLayout._paper;
492+
var hasInfoLayer = !!oldFullLayout._infolayer;
493+
494+
oldLoop:
495+
for(i = 0; i < oldFullData.length; i++) {
496+
var oldTrace = oldFullData[i],
497+
oldUid = oldTrace.uid;
498+
499+
for(j = 0; j < newFullData.length; j++) {
500+
var newTrace = newFullData[j];
501+
502+
if(oldUid === newTrace.uid) continue oldLoop;
503+
}
504+
505+
// clean old heatmap and contour traces
506+
if(hasPaper) {
507+
oldFullLayout._paper.selectAll(
508+
'.hm' + oldUid +
509+
',.contour' + oldUid +
510+
',#clip' + oldUid
511+
).remove();
512+
}
513+
514+
// clean old colorbars
515+
if(hasInfoLayer) {
516+
oldFullLayout._infolayer.selectAll('.cb' + oldUid).remove();
517+
}
518+
}
488519
}
489520

490521
/**

0 commit comments

Comments
 (0)