Skip to content

Commit ef9e078

Browse files
committed
Refactor code to clean up deprecated title attr structure [882]
1 parent e4dbc5d commit ef9e078

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

src/plot_api/helpers.js

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ function cleanAxRef(container, attr) {
229229
}
230230
}
231231

232+
/**
233+
* Cleans up old title attribute structure (flat) in favor of the new one (nested).
234+
*
235+
* @param {Object} titleContainer - an object potentially including deprecated title attributes
236+
*/
232237
function cleanTitle(titleContainer) {
233238
if(titleContainer) {
234239

@@ -241,45 +246,26 @@ function cleanTitle(titleContainer) {
241246
};
242247
}
243248

244-
// TODO 882 DRY UP?
245-
// titlefont -> title.font
246-
var oldFontAttrSet = Lib.isPlainObject(titleContainer.titlefont);
247-
var newFontAttrSet = titleContainer.title && Lib.isPlainObject(titleContainer.title.font);
248-
if(oldFontAttrSet && !newFontAttrSet) {
249-
nestTitleAttr('titlefont', 'font');
250-
}
251-
252-
// titleposition -> title.position
253-
var oldPositionAttrSet = titleContainer.titleposition;
254-
var newPositionAttrSet = titleContainer.title && titleContainer.title.position;
255-
if(oldPositionAttrSet && !newPositionAttrSet) {
256-
nestTitleAttr('titleposition', 'position');
257-
}
249+
rewireAttr('titlefont', 'font');
250+
rewireAttr('titleposition', 'position');
251+
rewireAttr('titleside', 'side');
252+
rewireAttr('titleoffset', 'offset');
253+
}
258254

259-
// titleside -> title.side
260-
var oldSideAttrSet = titleContainer.titleside;
261-
var newSideAttrSet = titleContainer.title && titleContainer.title.side;
262-
if(oldSideAttrSet && !newSideAttrSet) {
263-
nestTitleAttr('titleside', 'side');
264-
}
255+
function rewireAttr(oldAttrName, newAttrName) {
256+
var oldAttrSet = titleContainer[oldAttrName];
257+
var newAttrSet = titleContainer.title && titleContainer.title[newAttrName];
265258

266-
// titleoffset -> title.offset
267-
var oldOffsetAttrSet = titleContainer.titleoffset;
268-
var newOffsetAttrSet = titleContainer.title && titleContainer.title.offset;
269-
if(oldOffsetAttrSet && !newOffsetAttrSet) {
270-
nestTitleAttr('titleoffset', 'offset');
271-
}
272-
}
259+
if(oldAttrSet && !newAttrSet) {
273260

274-
function nestTitleAttr(oldAttrName, newAttrName) {
261+
// Ensure title object exists
262+
if(!titleContainer.title) {
263+
titleContainer.title = {};
264+
}
275265

276-
// Ensure title object exists
277-
if(!titleContainer.title) {
278-
titleContainer.title = {};
266+
titleContainer.title[newAttrName] = titleContainer[oldAttrName];
267+
delete titleContainer[oldAttrName];
279268
}
280-
281-
titleContainer.title[newAttrName] = titleContainer[oldAttrName];
282-
delete titleContainer[oldAttrName];
283269
}
284270
}
285271

0 commit comments

Comments
 (0)