Skip to content

Commit 59b0c52

Browse files
committed
robustify top-paper <defs>:
- append <defs> to toppaper in make-framework step - use fullLayout._uid in legend <clipPath> id to avoid conflict on DOM queries.
1 parent a7829cf commit 59b0c52

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

src/components/legend/constants.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ module.exports = {
1212
scrollBarWidth: 4,
1313
scrollBarHeight: 20,
1414
scrollBarColor: '#808BA4',
15-
scrollBarMargin: 4,
16-
17-
clipId: 'legend'
15+
scrollBarMargin: 4
1816
};

src/components/legend/draw.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var anchorUtils = require('./anchor_utils');
2727

2828
module.exports = function draw(gd) {
2929
var fullLayout = gd._fullLayout;
30+
var clipId = 'legend' + fullLayout._uid;
3031

3132
if(!fullLayout._infolayer || !gd.calcdata) return;
3233

@@ -36,6 +37,8 @@ module.exports = function draw(gd) {
3637

3738
if(!fullLayout.showlegend || !legendData.length) {
3839
fullLayout._infolayer.selectAll('.legend').remove();
40+
fullLayout._topdefs.select('#' + clipId).remove();
41+
3942
Plots.autoMargin(gd, 'legend');
4043
return;
4144
}
@@ -52,6 +55,12 @@ module.exports = function draw(gd) {
5255
'pointer-events': 'all'
5356
});
5457

58+
var clipPath = fullLayout._topdefs.selectAll('#' + clipId)
59+
.data([0])
60+
.enter().append('clipPath')
61+
.attr('id', clipId)
62+
.append('rect');
63+
5564
var bg = legend.selectAll('rect.bg')
5665
.data([0]);
5766

@@ -201,16 +210,14 @@ module.exports = function draw(gd) {
201210

202211
legend.attr('transform', 'translate(' + lx + ',' + ly + ')');
203212

204-
var clipPath = selectClipPath(gd);
205-
206213
clipPath.attr({
207214
width: opts.width,
208215
height: scrollheight,
209216
x: 0,
210217
y: 0
211218
});
212219

213-
legend.call(Drawing.setClipUrl, constants.clipId);
220+
legend.call(Drawing.setClipUrl, clipId);
214221

215222
// If scrollbar should be shown.
216223
if(gd.firstRender && opts.height - scrollheight > 0 && !gd._context.staticPlot) {
@@ -470,21 +477,3 @@ function repositionLegend(gd, traces) {
470477
t: opts.height * ({bottom: 1, middle: 0.5}[yanchor] || 0)
471478
});
472479
}
473-
474-
function selectClipPath(gd) {
475-
var container = gd._fullLayout._infolayer.node().parentNode;
476-
477-
var defs = d3.select(container).selectAll('defs')
478-
.data([0]);
479-
480-
defs.enter().append('defs');
481-
482-
var clipPath = defs.selectAll('#' + constants.clipId)
483-
.data([0]);
484-
485-
var path = clipPath.enter().append('clipPath')
486-
.attr('id', constants.clipId)
487-
.append('rect');
488-
489-
return path;
490-
}

src/plot_api/plot_api.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,9 @@ function makePlotFramework(gd) {
25852585
fullLayout._defs = fullLayout._paper.append('defs')
25862586
.attr('id', 'defs-' + fullLayout._uid);
25872587

2588+
fullLayout._topdefs = fullLayout._toppaper.append('defs')
2589+
.attr('id', 'defs-' + fullLayout._uid);
2590+
25882591
fullLayout._draggers = fullLayout._paper.append('g')
25892592
.classed('draglayer', true);
25902593

0 commit comments

Comments
 (0)