From c02204f13f6508b26039ea14b54944d4352ff7c8 Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Wed, 7 Jun 2017 00:35:51 -0400 Subject: [PATCH] short-circuit title draw if there's no title --- src/components/titles/index.js | 52 +++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/components/titles/index.js b/src/components/titles/index.js index 6c3a64e87a0..02287decbcc 100644 --- a/src/components/titles/index.js +++ b/src/components/titles/index.js @@ -20,6 +20,7 @@ var Color = require('../color'); var svgTextUtils = require('../../lib/svg_text_utils'); var interactConstants = require('../../constants/interactions'); +var PLACEHOLDER_RE = /Click to enter .+ title/; var Titles = module.exports = {}; @@ -52,29 +53,34 @@ var Titles = module.exports = {}; * title, include here. Otherwise it will go in fullLayout._infolayer */ Titles.draw = function(gd, titleClass, options) { - var cont = options.propContainer, - prop = options.propName, - traceIndex = options.traceIndex, - name = options.dfltName, - avoid = options.avoid || {}, - attributes = options.attributes, - transform = options.transform, - group = options.containerGroup, - - fullLayout = gd._fullLayout, - font = cont.titlefont.family, - fontSize = cont.titlefont.size, - fontColor = cont.titlefont.color, - - opacity = 1, - isplaceholder = false, - txt = cont.title.trim(); + var cont = options.propContainer; + var prop = options.propName; + var traceIndex = options.traceIndex; + var name = options.dfltName; + var avoid = options.avoid || {}; + var attributes = options.attributes; + var transform = options.transform; + var group = options.containerGroup; + + var fullLayout = gd._fullLayout; + var font = cont.titlefont.family; + var fontSize = cont.titlefont.size; + var fontColor = cont.titlefont.color; + + var opacity = 1; + var isplaceholder = false; + var txt = cont.title.trim(); + var editable = gd._context.editable; + if(txt === '') opacity = 0; - if(txt.match(/Click to enter .+ title/)) { + if(txt.match(PLACEHOLDER_RE)) { opacity = 0.2; isplaceholder = true; + if(!editable) txt = ''; } + var elShouldExist = txt || editable; + if(!group) { group = fullLayout._infolayer.selectAll('.g-' + titleClass) .data([0]); @@ -83,7 +89,7 @@ Titles.draw = function(gd, titleClass, options) { } var el = group.selectAll('text') - .data([0]); + .data(elShouldExist ? [0] : []); el.enter().append('text'); el.text(txt) // this is hacky, but convertToTspans uses the class @@ -92,6 +98,9 @@ Titles.draw = function(gd, titleClass, options) { // correct one (only relevant for colorbars, at least // for now) - ie don't use .classed .attr('class', titleClass); + el.exit().remove(); + + if(!elShouldExist) return; function titleLayout(titleEl) { Lib.syncOrAsync([drawTitle, scootTitle], titleEl); @@ -205,7 +214,7 @@ Titles.draw = function(gd, titleClass, options) { }); } - if(gd._context.editable) { + if(editable) { if(!txt) setPlaceholder(); else el.on('.opacity', null); @@ -224,8 +233,5 @@ Titles.draw = function(gd, titleClass, options) { .attr(attributes); }); } - else if(!txt || txt.match(/Click to enter .+ title/)) { - el.remove(); - } el.classed('js-placeholder', isplaceholder); };