Skip to content

Commit 4d21886

Browse files
committed
Deprecate *.titlefont attributes in favor of *.title.font [882]
1 parent 50a1e9b commit 4d21886

File tree

10 files changed

+358
-35
lines changed

10 files changed

+358
-35
lines changed

src/components/colorbar/draw.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ module.exports = function draw(gd, id) {
184184
showticksuffix: opts.showticksuffix,
185185
ticksuffix: opts.ticksuffix,
186186
title: opts.title,
187-
titlefont: opts.titlefont,
188187
showline: true,
189188
anchor: 'free',
190189
side: 'right',
@@ -290,7 +289,7 @@ module.exports = function draw(gd, id) {
290289
// when we squish the axis. This one only applies to
291290
// top or bottom titles, not right side.
292291
var x = gs.l + (opts.x + xpadFrac) * gs.w,
293-
fontSize = cbAxisOut.titlefont.size,
292+
fontSize = cbAxisOut.title.font.size,
294293
y;
295294

296295
if(opts.titleside === 'top') {
@@ -460,7 +459,7 @@ module.exports = function draw(gd, id) {
460459
},
461460
function() {
462461
if(['top', 'bottom'].indexOf(opts.titleside) === -1) {
463-
var fontSize = cbAxisOut.titlefont.size,
462+
var fontSize = cbAxisOut.title.font.size,
464463
y = cbAxisOut._offset + cbAxisOut._length / 2,
465464
x = gs.l + (cbAxisOut.position || 0) * gs.w + ((cbAxisOut.side === 'right') ?
466465
10 + fontSize * ((cbAxisOut.showticklabels ? 1 : 0.5)) :

src/components/titles/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,21 @@ function draw(gd, titleClass, options) {
6767
var group = options.containerGroup;
6868

6969
var fullLayout = gd._fullLayout;
70-
var titlefont = cont.titlefont || {};
71-
var font = titlefont.family;
72-
var fontSize = titlefont.size;
73-
var fontColor = titlefont.color;
7470

7571
var opacity = 1;
7672
var isplaceholder = false;
7773
var title = cont.title;
7874
var txt = (title && title.text ? title.text : '').trim();
7975

76+
var font = title && title.font ? title.font : {};
77+
var fontFamily = font.family;
78+
var fontSize = font.size;
79+
var fontColor = font.color;
80+
8081
// only make this title editable if we positively identify its property
8182
// as one that has editing enabled.
8283
var editAttr;
84+
// TODO 882 Probably compare against 'title.text'
8385
if(prop === 'title') editAttr = 'titleText';
8486
else if(prop.indexOf('axis') !== -1) editAttr = 'axisTitleText';
8587
else if(prop.indexOf('colorbar' !== -1)) editAttr = 'colorbarTitleText';
@@ -138,7 +140,7 @@ function draw(gd, titleClass, options) {
138140
titleEl.attr('transform', transformVal);
139141

140142
titleEl.style({
141-
'font-family': font,
143+
'font-family': fontFamily,
142144
'font-size': d3.round(fontSize, 2) + 'px',
143145
fill: Color.rgb(fontColor),
144146
opacity: opacity * Color.opacity(fontColor),

src/plot_api/helpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ exports.cleanLayout = function(layout) {
193193
};
194194
}
195195

196+
// Check for old-style title font definitions
197+
if(Lib.isPlainObject(layout.titlefont) && !Lib.isPlainObject(layout.title.font)) {
198+
layout.title.font = layout.titlefont;
199+
delete layout.titlefont;
200+
}
201+
if(layout.xaxis &&
202+
Lib.isPlainObject(layout.xaxis.titlefont) && !Lib.isPlainObject(layout.xaxis.title.font)) {
203+
layout.xaxis.title.font = layout.xaxis.titlefont;
204+
delete layout.xaxis.titlefont;
205+
}
206+
if(layout.yaxis &&
207+
Lib.isPlainObject(layout.yaxis.titlefont) && !Lib.isPlainObject(layout.yaxis.title.font)) {
208+
layout.yaxis.title.font = layout.yaxis.titlefont;
209+
delete layout.yaxis.titlefont;
210+
}
211+
196212
/*
197213
* Moved from rotate -> orbit for dragmode
198214
*/

src/plot_api/plot_api.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,34 @@ function _restyle(gd, aobj, traces) {
16861686
};
16871687
}
16881688

1689+
/**
1690+
* Maps deprecated layout attributes that need to be converted
1691+
* to the current API at this stage to ensure backwards compatibility.
1692+
*
1693+
* @param layoutObj
1694+
*/
1695+
function mapDeprecatedLayoutAttributes(layoutObj) {
1696+
1697+
// Support old-style update of the title's font
1698+
var isTitlefontSet = layoutObj.titlefont;
1699+
var isFontInTitleSet = layoutObj.title && layoutObj.title.font;
1700+
var isFontInTitleNotSet = !isFontInTitleSet;
1701+
if(isTitlefontSet && isFontInTitleNotSet) {
1702+
1703+
// Use string attribute because initiating a new title object
1704+
// to be able to specify a font property on it would require to
1705+
// know the potentially existing `title.text` property.
1706+
layoutObj['title.font'] = layoutObj.titlefont;
1707+
delete layoutObj.titlefont;
1708+
}
1709+
1710+
// Note, that updating x-axis and y-axis title fonts
1711+
// was never supported unless (i) using string
1712+
// attributes or (ii) passing `xaxis.title` / `yaxis.title`
1713+
// again. And these cases are covered by
1714+
// helpers.cleanLayout anyways.
1715+
}
1716+
16891717
/**
16901718
* Maps deprecated layout "string attributes" to
16911719
* "string attributes" of the current API to ensure backwards compatibility.
@@ -1698,13 +1726,32 @@ function mapDeprecatedLayoutAttributeStrings(layoutObj) {
16981726
if(layoutObj.title && !Lib.isPlainObject(layoutObj.title)) {
16991727
layoutObj.title = {text: layoutObj.title};
17001728
}
1701-
if(layoutObj['xaxis.title'] !== undefined) {
1702-
layoutObj['xaxis.title.text'] = layoutObj['xaxis.title'];
1703-
delete layoutObj['xaxis.title'];
1704-
}
1705-
if(layoutObj['yaxis.title'] !== undefined) {
1706-
layoutObj['yaxis.title.text'] = layoutObj['yaxis.title'];
1707-
delete layoutObj['yaxis.title'];
1729+
replace('xaxis.title', 'xaxis.title.text');
1730+
replace('yaxis.title', 'yaxis.title.text');
1731+
1732+
// Note: Only "nested" (dot notation) attributes
1733+
// need to be converted. For example 'titlefont'
1734+
// was a top-level attribute and thus is covered by
1735+
// the general compatibility layer.
1736+
replace('titlefont.color', 'title.font.color');
1737+
replace('titlefont.family', 'title.font.family');
1738+
replace('titlefont.size', 'title.font.size');
1739+
1740+
replace('xaxis.titlefont', 'xaxis.title.font');
1741+
replace('xaxis.titlefont.color', 'xaxis.title.font.color');
1742+
replace('xaxis.titlefont.family', 'xaxis.title.font.family');
1743+
replace('xaxis.titlefont.size', 'xaxis.title.font.size');
1744+
1745+
replace('yaxis.titlefont', 'yaxis.title.font');
1746+
replace('yaxis.titlefont.color', 'yaxis.title.font.color');
1747+
replace('yaxis.titlefont.family', 'yaxis.title.font.family');
1748+
replace('yaxis.titlefont.size', 'yaxis.title.font.size');
1749+
1750+
function replace(oldKey, newKey) {
1751+
if(layoutObj[oldKey] !== undefined) {
1752+
layoutObj[newKey] = layoutObj[oldKey];
1753+
delete layoutObj[oldKey];
1754+
}
17081755
}
17091756
}
17101757

@@ -1748,6 +1795,7 @@ exports.relayout = function relayout(gd, astr, val) {
17481795

17491796
if(Object.keys(aobj).length) gd.changed = true;
17501797

1798+
mapDeprecatedLayoutAttributes(aobj);
17511799
mapDeprecatedLayoutAttributeStrings(aobj);
17521800

17531801
var specs = _relayout(gd, aobj);
@@ -2227,6 +2275,7 @@ exports.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
22272275
var restyleSpecs = _restyle(gd, Lib.extendFlat({}, traceUpdate), traces);
22282276
var restyleFlags = restyleSpecs.flags;
22292277

2278+
mapDeprecatedLayoutAttributes(layoutUpdate);
22302279
mapDeprecatedLayoutAttributeStrings(layoutUpdate);
22312280

22322281
var relayoutSpecs = _relayout(gd, Lib.extendFlat({}, layoutUpdate));

src/plots/cartesian/axes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,7 +2337,7 @@ axes.drawTitle = function(gd, ax) {
23372337
var axLetter = axId.charAt(0);
23382338
var offsetBase = 1.5;
23392339
var gs = fullLayout._size;
2340-
var fontSize = ax.titlefont.size;
2340+
var fontSize = ax.title.font.size;
23412341

23422342
var transform, counterAxis, x, y;
23432343

src/plots/cartesian/axis_defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
6969
var dfltTitle = splomStash.label || layoutOut._dfltTitle[letter];
7070

7171
coerce('title.text', dfltTitle);
72-
Lib.coerceFont(coerce, 'titlefont', {
72+
Lib.coerceFont(coerce, 'title.font', {
7373
family: font.family,
7474
size: Math.round(font.size * 1.2),
7575
color: dfltFontColor

src/plots/cartesian/layout_attributes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ module.exports = {
4747
editType: 'ticks',
4848
description: 'Sets the title of this axis.'
4949
},
50+
font: fontAttrs({
51+
editType: 'ticks',
52+
description: [
53+
'Sets this axis\' title font.'
54+
].join(' ')
55+
}),
5056
editType: 'ticks'
5157
},
52-
titlefont: fontAttrs({
53-
editType: 'ticks',
54-
description: [
55-
'Sets this axis\' title font.'
56-
].join(' ')
57-
}),
5858
type: {
5959
valType: 'enumerated',
6060
// '-' means we haven't yet run autotype or couldn't find any data

src/plots/layout_attributes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ module.exports = {
3434
'Sets the plot\'s title.'
3535
].join(' ')
3636
},
37+
font: fontAttrs({
38+
editType: 'layoutstyle',
39+
description: 'Sets the title font.'
40+
}),
3741
editType: 'layoutstyle'
3842
},
39-
titlefont: fontAttrs({
40-
editType: 'layoutstyle',
41-
description: 'Sets the title font.'
42-
}),
4343
autosize: {
4444
valType: 'boolean',
4545
role: 'info',

src/plots/plots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut, formatObj) {
13261326

13271327
coerce('title.text', layoutOut._dfltTitle.plot);
13281328

1329-
Lib.coerceFont(coerce, 'titlefont', {
1329+
Lib.coerceFont(coerce, 'title.font', {
13301330
family: globalFont.family,
13311331
size: Math.round(globalFont.size * 1.4),
13321332
color: globalFont.color

0 commit comments

Comments
 (0)