Skip to content

Commit 66a4b90

Browse files
committed
[poc] sliders split pushMargin from draw
1 parent 313ef25 commit 66a4b90

File tree

2 files changed

+59
-75
lines changed

2 files changed

+59
-75
lines changed

src/components/sliders/draw.js

+57-74
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ var LINE_SPACING = alignmentConstants.LINE_SPACING;
2323
var FROM_TL = alignmentConstants.FROM_TL;
2424
var FROM_BR = alignmentConstants.FROM_BR;
2525

26-
module.exports = function draw(gd) {
26+
function draw(gd) {
2727
var fullLayout = gd._fullLayout;
28-
var sliderData = makeSliderData(fullLayout, gd);
28+
var sliderData = makeSliderData(gd);
29+
var gs = fullLayout._size;
2930

3031
// draw a container for *all* sliders:
3132
var sliders = fullLayout._infolayer
@@ -41,11 +42,6 @@ module.exports = function draw(gd) {
4142
sliderOpts._commandObserver.remove();
4243
delete sliderOpts._commandObserver;
4344
}
44-
45-
// Most components don't need to explicitly remove autoMargin, because
46-
// marginPushers does this - but slider updates don't go through
47-
// a full replot so we need to explicitly remove it.
48-
Plots.autoMargin(gd, autoMarginId(sliderOpts));
4945
}
5046

5147
sliders.exit().each(function() {
@@ -67,14 +63,18 @@ module.exports = function draw(gd) {
6763
.each(clearSlider)
6864
.remove();
6965

70-
// Find the dimensions of the sliders:
71-
for(var i = 0; i < sliderData.length; i++) {
72-
var sliderOpts = sliderData[i];
73-
findDimensions(gd, sliderOpts);
74-
}
75-
7666
sliderGroups.each(function(sliderOpts) {
7767
var gSlider = d3.select(this);
68+
var dims = sliderOpts._dims;
69+
70+
dims.lx = Math.round(
71+
gs.l + gs.w * sliderOpts.x -
72+
dims.outerLength * FROM_TL[Lib.getXanchor(sliderOpts)]
73+
);
74+
dims.ly = Math.round(
75+
gs.t + gs.h * (1 - sliderOpts.y) -
76+
dims.height * FROM_TL[Lib.getYanchor(sliderOpts)]
77+
);
7878

7979
computeLabelSteps(sliderOpts);
8080

@@ -95,14 +95,11 @@ module.exports = function draw(gd) {
9595

9696
drawSlider(gd, d3.select(this), sliderOpts);
9797
});
98-
};
99-
100-
function autoMarginId(sliderOpts) {
101-
return constants.autoMarginIdRoot + sliderOpts._index;
10298
}
10399

104100
// This really only just filters by visibility:
105-
function makeSliderData(fullLayout, gd) {
101+
function makeSliderData(gd) {
102+
var fullLayout = gd._fullLayout;
106103
var contOpts = fullLayout[constants.name];
107104
var sliderData = [];
108105

@@ -121,8 +118,11 @@ function keyFunction(opts) {
121118
return opts._index;
122119
}
123120

124-
// Compute the dimensions (mutates sliderOpts):
125121
function findDimensions(gd, sliderOpts) {
122+
var fullLayout = gd._fullLayout;
123+
var gs = fullLayout._size;
124+
var dims = sliderOpts._dims = {};
125+
126126
var sliderLabels = Drawing.tester.selectAll('g.' + constants.labelGroupClass)
127127
.data(sliderOpts._visibleSteps);
128128

@@ -144,27 +144,16 @@ function findDimensions(gd, sliderOpts) {
144144
maxLabelWidth = Math.max(maxLabelWidth, bBox.width);
145145
}
146146
});
147-
148147
sliderLabels.remove();
149148

150-
var dims = sliderOpts._dims = {};
151-
152149
dims.inputAreaWidth = Math.max(
153150
constants.railWidth,
154151
constants.gripHeight
155152
);
156153

157-
// calculate some overall dimensions - some of these are needed for
158-
// calculating the currentValue dimensions
159-
var graphSize = gd._fullLayout._size;
160-
dims.lx = graphSize.l + graphSize.w * sliderOpts.x;
161-
dims.ly = graphSize.t + graphSize.h * (1 - sliderOpts.y);
162-
163154
if(sliderOpts.lenmode === 'fraction') {
164-
// fraction:
165-
dims.outerLength = Math.round(graphSize.w * sliderOpts.len);
155+
dims.outerLength = Math.round(gs.w * sliderOpts.len);
166156
} else {
167-
// pixels:
168157
dims.outerLength = sliderOpts.len;
169158
}
170159

@@ -203,51 +192,10 @@ function findDimensions(gd, sliderOpts) {
203192
dummyGroup.remove();
204193
}
205194

206-
dims.height = dims.currentValueTotalHeight + constants.tickOffset + sliderOpts.ticklen + constants.labelOffset + dims.labelHeight + sliderOpts.pad.t + sliderOpts.pad.b;
207-
208-
var xanchor = 'left';
209-
if(Lib.isRightAnchor(sliderOpts)) {
210-
dims.lx -= dims.outerLength;
211-
xanchor = 'right';
212-
}
213-
if(Lib.isCenterAnchor(sliderOpts)) {
214-
dims.lx -= dims.outerLength / 2;
215-
xanchor = 'center';
216-
}
217-
218-
var yanchor = 'top';
219-
if(Lib.isBottomAnchor(sliderOpts)) {
220-
dims.ly -= dims.height;
221-
yanchor = 'bottom';
222-
}
223-
if(Lib.isMiddleAnchor(sliderOpts)) {
224-
dims.ly -= dims.height / 2;
225-
yanchor = 'middle';
226-
}
227-
195+
dims.height = Math.ceil(dims.currentValueTotalHeight + constants.tickOffset + sliderOpts.ticklen + constants.labelOffset + dims.labelHeight + sliderOpts.pad.t + sliderOpts.pad.b);
228196
dims.outerLength = Math.ceil(dims.outerLength);
229-
dims.height = Math.ceil(dims.height);
230-
dims.lx = Math.round(dims.lx);
231-
dims.ly = Math.round(dims.ly);
232-
233-
var marginOpts = {
234-
y: sliderOpts.y,
235-
b: dims.height * FROM_BR[yanchor],
236-
t: dims.height * FROM_TL[yanchor]
237-
};
238197

239-
if(sliderOpts.lenmode === 'fraction') {
240-
marginOpts.l = 0;
241-
marginOpts.xl = sliderOpts.x - sliderOpts.len * FROM_TL[xanchor];
242-
marginOpts.r = 0;
243-
marginOpts.xr = sliderOpts.x + sliderOpts.len * FROM_BR[xanchor];
244-
} else {
245-
marginOpts.x = sliderOpts.x;
246-
marginOpts.l = dims.outerLength * FROM_TL[xanchor];
247-
marginOpts.r = dims.outerLength * FROM_BR[xanchor];
248-
}
249-
250-
Plots.autoMargin(gd, autoMarginId(sliderOpts), marginOpts);
198+
return dims;
251199
}
252200

253201
function drawSlider(gd, sliderGroup, sliderOpts) {
@@ -628,3 +576,38 @@ function drawRail(sliderGroup, sliderOpts) {
628576
(dims.inputAreaWidth - constants.railWidth) * 0.5 + dims.currentValueTotalHeight
629577
);
630578
}
579+
580+
function pushMargin(gd) {
581+
var sliderData = makeSliderData(gd);
582+
583+
for(var i = 0; i < sliderData.length; i++) {
584+
var sliderOpts = sliderData[i];
585+
var xanchor = Lib.getXanchor(sliderOpts);
586+
var yanchor = Lib.getYanchor(sliderOpts);
587+
var dims = findDimensions(gd, sliderOpts);
588+
589+
var marginOpts = {
590+
y: sliderOpts.y,
591+
b: dims.height * FROM_BR[yanchor],
592+
t: dims.height * FROM_TL[yanchor]
593+
};
594+
595+
if(sliderOpts.lenmode === 'fraction') {
596+
marginOpts.l = 0;
597+
marginOpts.xl = sliderOpts.x - sliderOpts.len * FROM_TL[xanchor];
598+
marginOpts.r = 0;
599+
marginOpts.xr = sliderOpts.x + sliderOpts.len * FROM_BR[xanchor];
600+
} else {
601+
marginOpts.x = sliderOpts.x;
602+
marginOpts.l = dims.outerLength * FROM_TL[xanchor];
603+
marginOpts.r = dims.outerLength * FROM_BR[xanchor];
604+
}
605+
606+
Plots.autoMargin(gd, constants.autoMarginIdRoot + sliderOpts._index, marginOpts);
607+
}
608+
}
609+
610+
module.exports = {
611+
pushMargin: pushMargin,
612+
draw: draw
613+
};

src/components/sliders/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ module.exports = {
1717
layoutAttributes: require('./attributes'),
1818
supplyLayoutDefaults: require('./defaults'),
1919

20-
draw: require('./draw')
20+
pushMargin: require('./draw').pushMargin,
21+
draw: require('./draw').draw
2122
};

0 commit comments

Comments
 (0)