Skip to content

Commit 0752aa5

Browse files
committed
clean up rangeselector for Plotly.react
1 parent be4932d commit 0752aa5

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/components/rangeselector/draw.js

+33-35
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ var svgTextUtils = require('../../lib/svg_text_utils');
1919
var axisIds = require('../../plots/cartesian/axis_ids');
2020
var anchorUtils = require('../legend/anchor_utils');
2121

22-
var LINE_SPACING = require('../../constants/alignment').LINE_SPACING;
22+
var alignmentConstants = require('../../constants/alignment');
23+
var LINE_SPACING = alignmentConstants.LINE_SPACING;
24+
var FROM_TL = alignmentConstants.FROM_TL;
25+
var FROM_BR = alignmentConstants.FROM_BR;
2326

2427
var constants = require('./constants');
2528
var getUpdateObject = require('./get_update_object');
@@ -58,7 +61,7 @@ module.exports = function draw(gd) {
5861
var button = d3.select(this);
5962
var update = getUpdateObject(axisLayout, d);
6063

61-
d.isActive = isActive(axisLayout, d, update);
64+
d._isActive = isActive(axisLayout, d, update);
6265

6366
button.call(drawButtonRect, selectorLayout, d);
6467
button.call(drawButtonText, selectorLayout, d, gd);
@@ -70,22 +73,17 @@ module.exports = function draw(gd) {
7073
});
7174

7275
button.on('mouseover', function() {
73-
d.isHovered = true;
76+
d._isHovered = true;
7477
button.call(drawButtonRect, selectorLayout, d);
7578
});
7679

7780
button.on('mouseout', function() {
78-
d.isHovered = false;
81+
d._isHovered = false;
7982
button.call(drawButtonRect, selectorLayout, d);
8083
});
8184
});
8285

83-
// N.B. this mutates selectorLayout
84-
reposition(gd, buttons, selectorLayout, axisLayout._name);
85-
86-
selector.attr('transform', 'translate(' +
87-
selectorLayout.lx + ',' + selectorLayout.ly +
88-
')');
86+
reposition(gd, buttons, selectorLayout, axisLayout._name, selector);
8987
});
9088

9189
};
@@ -143,7 +141,7 @@ function drawButtonRect(button, selectorLayout, d) {
143141
}
144142

145143
function getFillColor(selectorLayout, d) {
146-
return (d.isActive || d.isHovered) ?
144+
return (d._isActive || d._isHovered) ?
147145
selectorLayout.activecolor :
148146
selectorLayout.bgcolor;
149147
}
@@ -175,9 +173,9 @@ function getLabel(opts) {
175173
return opts.count + opts.step.charAt(0);
176174
}
177175

178-
function reposition(gd, buttons, opts, axName) {
179-
opts.width = 0;
180-
opts.height = 0;
176+
function reposition(gd, buttons, opts, axName, selector) {
177+
var width = 0;
178+
var height = 0;
181179

182180
var borderWidth = opts.borderwidth;
183181

@@ -188,7 +186,7 @@ function reposition(gd, buttons, opts, axName) {
188186
var tHeight = opts.font.size * LINE_SPACING;
189187
var hEff = Math.max(tHeight * svgTextUtils.lineCount(text), 16) + 3;
190188

191-
opts.height = Math.max(opts.height, hEff);
189+
height = Math.max(height, hEff);
192190
});
193191

194192
buttons.each(function() {
@@ -207,59 +205,59 @@ function reposition(gd, buttons, opts, axName) {
207205
// TODO add buttongap attribute
208206

209207
button.attr('transform', 'translate(' +
210-
(borderWidth + opts.width) + ',' + borderWidth +
208+
(borderWidth + width) + ',' + borderWidth +
211209
')');
212210

213211
rect.attr({
214212
x: 0,
215213
y: 0,
216214
width: wEff,
217-
height: opts.height
215+
height: height
218216
});
219217

220218
svgTextUtils.positionText(text, wEff / 2,
221-
opts.height / 2 - ((tLines - 1) * tHeight / 2) + 3);
219+
height / 2 - ((tLines - 1) * tHeight / 2) + 3);
222220

223-
opts.width += wEff + 5;
221+
width += wEff + 5;
224222
});
225223

226-
buttons.selectAll('rect').attr('height', opts.height);
227-
228224
var graphSize = gd._fullLayout._size;
229-
opts.lx = graphSize.l + graphSize.w * opts.x;
230-
opts.ly = graphSize.t + graphSize.h * (1 - opts.y);
225+
var lx = graphSize.l + graphSize.w * opts.x;
226+
var ly = graphSize.t + graphSize.h * (1 - opts.y);
231227

232228
var xanchor = 'left';
233229
if(anchorUtils.isRightAnchor(opts)) {
234-
opts.lx -= opts.width;
230+
lx -= width;
235231
xanchor = 'right';
236232
}
237233
if(anchorUtils.isCenterAnchor(opts)) {
238-
opts.lx -= opts.width / 2;
234+
lx -= width / 2;
239235
xanchor = 'center';
240236
}
241237

242238
var yanchor = 'top';
243239
if(anchorUtils.isBottomAnchor(opts)) {
244-
opts.ly -= opts.height;
240+
ly -= height;
245241
yanchor = 'bottom';
246242
}
247243
if(anchorUtils.isMiddleAnchor(opts)) {
248-
opts.ly -= opts.height / 2;
244+
ly -= height / 2;
249245
yanchor = 'middle';
250246
}
251247

252-
opts.width = Math.ceil(opts.width);
253-
opts.height = Math.ceil(opts.height);
254-
opts.lx = Math.round(opts.lx);
255-
opts.ly = Math.round(opts.ly);
248+
width = Math.ceil(width);
249+
height = Math.ceil(height);
250+
lx = Math.round(lx);
251+
ly = Math.round(ly);
256252

257253
Plots.autoMargin(gd, axName + '-range-selector', {
258254
x: opts.x,
259255
y: opts.y,
260-
l: opts.width * ({right: 1, center: 0.5}[xanchor] || 0),
261-
r: opts.width * ({left: 1, center: 0.5}[xanchor] || 0),
262-
b: opts.height * ({top: 1, middle: 0.5}[yanchor] || 0),
263-
t: opts.height * ({bottom: 1, middle: 0.5}[yanchor] || 0)
256+
l: width * FROM_TL[xanchor],
257+
r: width * FROM_BR[xanchor],
258+
b: height * FROM_BR[yanchor],
259+
t: height * FROM_TL[yanchor]
264260
});
261+
262+
selector.attr('transform', 'translate(' + lx + ',' + ly + ')');
265263
}

test/jasmine/tests/range_selector_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ describe('range selector interactions:', function() {
472472

473473
function checkActiveButton(activeIndex, msg) {
474474
d3.selectAll('.button').each(function(d, i) {
475-
expect(d.isActive).toBe(activeIndex === i, msg + ': button #' + i);
475+
expect(d._isActive).toBe(activeIndex === i, msg + ': button #' + i);
476476
});
477477
}
478478

@@ -481,7 +481,7 @@ describe('range selector interactions:', function() {
481481
var rect = d3.select(this).select('rect');
482482

483483
expect(rect.node().style.fill).toEqual(
484-
d.isActive ? activeColor : bgColor
484+
d._isActive ? activeColor : bgColor
485485
);
486486
});
487487
}

0 commit comments

Comments
 (0)