Skip to content

Commit c02204f

Browse files
committed
short-circuit title draw if there's no title
1 parent 032cf7c commit c02204f

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/components/titles/index.js

+29-23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var Color = require('../color');
2020
var svgTextUtils = require('../../lib/svg_text_utils');
2121
var interactConstants = require('../../constants/interactions');
2222

23+
var PLACEHOLDER_RE = /Click to enter .+ title/;
2324

2425
var Titles = module.exports = {};
2526

@@ -52,29 +53,34 @@ var Titles = module.exports = {};
5253
* title, include here. Otherwise it will go in fullLayout._infolayer
5354
*/
5455
Titles.draw = function(gd, titleClass, options) {
55-
var cont = options.propContainer,
56-
prop = options.propName,
57-
traceIndex = options.traceIndex,
58-
name = options.dfltName,
59-
avoid = options.avoid || {},
60-
attributes = options.attributes,
61-
transform = options.transform,
62-
group = options.containerGroup,
63-
64-
fullLayout = gd._fullLayout,
65-
font = cont.titlefont.family,
66-
fontSize = cont.titlefont.size,
67-
fontColor = cont.titlefont.color,
68-
69-
opacity = 1,
70-
isplaceholder = false,
71-
txt = cont.title.trim();
56+
var cont = options.propContainer;
57+
var prop = options.propName;
58+
var traceIndex = options.traceIndex;
59+
var name = options.dfltName;
60+
var avoid = options.avoid || {};
61+
var attributes = options.attributes;
62+
var transform = options.transform;
63+
var group = options.containerGroup;
64+
65+
var fullLayout = gd._fullLayout;
66+
var font = cont.titlefont.family;
67+
var fontSize = cont.titlefont.size;
68+
var fontColor = cont.titlefont.color;
69+
70+
var opacity = 1;
71+
var isplaceholder = false;
72+
var txt = cont.title.trim();
73+
var editable = gd._context.editable;
74+
7275
if(txt === '') opacity = 0;
73-
if(txt.match(/Click to enter .+ title/)) {
76+
if(txt.match(PLACEHOLDER_RE)) {
7477
opacity = 0.2;
7578
isplaceholder = true;
79+
if(!editable) txt = '';
7680
}
7781

82+
var elShouldExist = txt || editable;
83+
7884
if(!group) {
7985
group = fullLayout._infolayer.selectAll('.g-' + titleClass)
8086
.data([0]);
@@ -83,7 +89,7 @@ Titles.draw = function(gd, titleClass, options) {
8389
}
8490

8591
var el = group.selectAll('text')
86-
.data([0]);
92+
.data(elShouldExist ? [0] : []);
8793
el.enter().append('text');
8894
el.text(txt)
8995
// this is hacky, but convertToTspans uses the class
@@ -92,6 +98,9 @@ Titles.draw = function(gd, titleClass, options) {
9298
// correct one (only relevant for colorbars, at least
9399
// for now) - ie don't use .classed
94100
.attr('class', titleClass);
101+
el.exit().remove();
102+
103+
if(!elShouldExist) return;
95104

96105
function titleLayout(titleEl) {
97106
Lib.syncOrAsync([drawTitle, scootTitle], titleEl);
@@ -205,7 +214,7 @@ Titles.draw = function(gd, titleClass, options) {
205214
});
206215
}
207216

208-
if(gd._context.editable) {
217+
if(editable) {
209218
if(!txt) setPlaceholder();
210219
else el.on('.opacity', null);
211220

@@ -224,8 +233,5 @@ Titles.draw = function(gd, titleClass, options) {
224233
.attr(attributes);
225234
});
226235
}
227-
else if(!txt || txt.match(/Click to enter .+ title/)) {
228-
el.remove();
229-
}
230236
el.classed('js-placeholder', isplaceholder);
231237
};

0 commit comments

Comments
 (0)