Skip to content

Commit 47fcc1e

Browse files
committed
lint int titles/index.js
- use `OPPOSITE_SIDE` constants - use `shiftSign` result in `maxshift` computation
1 parent 25fe1c9 commit 47fcc1e

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/components/titles/index.js

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

23-
module.exports = {
24-
draw: draw
25-
};
26-
23+
var OPPOSITE_SIDE = require('../../constants/alignment').OPPOSITE_SIDE;
2724
var numStripRE = / [XY][0-9]* /;
2825

2926
/**
@@ -167,29 +164,26 @@ function draw(gd, titleClass, options) {
167164

168165
// move toward avoid.side (= left, right, top, bottom) if needed
169166
// can include pad (pixels, default 2)
170-
var shift = 0;
171-
var backside = {
172-
left: 'right',
173-
right: 'left',
174-
top: 'bottom',
175-
bottom: 'top'
176-
}[avoid.side];
177-
var shiftSign = (['left', 'top'].indexOf(avoid.side) !== -1) ?
178-
-1 : 1;
167+
var backside = OPPOSITE_SIDE[avoid.side];
168+
var shiftSign = (avoid.side === 'left' || avoid.side === 'top') ? -1 : 1;
179169
var pad = isNumeric(avoid.pad) ? avoid.pad : 2;
170+
180171
var titlebb = Drawing.bBox(titleGroup.node());
181172
var paperbb = {
182173
left: 0,
183174
top: 0,
184175
right: fullLayout.width,
185176
bottom: fullLayout.height
186177
};
187-
var maxshift = avoid.maxShift || (
188-
(paperbb[avoid.side] - titlebb[avoid.side]) *
189-
((avoid.side === 'left' || avoid.side === 'top') ? -1 : 1));
178+
179+
var maxshift = avoid.maxShift ||
180+
shiftSign * (paperbb[avoid.side] - titlebb[avoid.side]);
181+
var shift = 0;
182+
190183
// Prevent the title going off the paper
191-
if(maxshift < 0) shift = maxshift;
192-
else {
184+
if(maxshift < 0) {
185+
shift = maxshift;
186+
} else {
193187
// so we don't have to offset each avoided element,
194188
// give the title the opposite offset
195189
var offsetLeft = avoid.offsetLeft || 0;
@@ -218,8 +212,7 @@ function draw(gd, titleClass, options) {
218212
top: [0, -shift],
219213
bottom: [0, shift]
220214
}[avoid.side];
221-
titleGroup.attr('transform',
222-
'translate(' + shiftTemplate + ')');
215+
titleGroup.attr('transform', 'translate(' + shiftTemplate + ')');
223216
}
224217
}
225218
}
@@ -265,3 +258,7 @@ function draw(gd, titleClass, options) {
265258

266259
return group;
267260
}
261+
262+
module.exports = {
263+
draw: draw
264+
};

0 commit comments

Comments
 (0)