Skip to content

Commit d770138

Browse files
committed
some more linting
- use fullLayout.(width|height) instead of ly(Min|Max) - use bw instead of opts.borderwidth - 🔪 double-empty-line - 🔪 some useless comments
1 parent f12c62c commit d770138

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

src/components/legend/draw.js

+31-42
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,16 @@ module.exports = function draw(gd) {
9595
function() { return computeLegendDimensions(gd, groups, traces); },
9696
function() { return expandMargin(gd); },
9797
function() {
98-
// Position and size the legend
99-
var lxMin = 0;
100-
var lxMax = fullLayout.width;
101-
var lyMin = 0;
102-
var lyMax = fullLayout.height;
103-
104-
// Scroll section must be executed after repositionLegend.
105-
// It requires the legend width, height, x and y to position the scrollbox
106-
// and these values are mutated in repositionLegend.
10798
var gs = fullLayout._size;
108-
var lx = gs.l + gs.w * opts.x;
109-
var ly = gs.t + gs.h * (1 - opts.y);
99+
var bw = opts.borderwidth;
110100

101+
var lx = gs.l + gs.w * opts.x;
111102
if(Lib.isRightAnchor(opts)) {
112103
lx -= opts._width;
113104
} else if(Lib.isCenterAnchor(opts)) {
114105
lx -= opts._width / 2;
115106
}
116107

117-
if(Lib.isBottomAnchor(opts)) {
118-
ly -= opts._height;
119-
} else if(Lib.isMiddleAnchor(opts)) {
120-
ly -= opts._height / 2;
121-
}
122108

123109
// Make sure the legend left and right sides are visible
124110
var legendWidth = opts._width;
@@ -128,9 +114,16 @@ module.exports = function draw(gd) {
128114
lx = gs.l;
129115
legendWidth = legendWidthMax;
130116
} else {
131-
if(lx + legendWidth > lxMax) lx = lxMax - legendWidth;
132-
if(lx < lxMin) lx = lxMin;
133-
legendWidth = Math.min(lxMax - lx, opts._width);
117+
if(lx + legendWidth > fullLayout.width) lx = fullLayout.width - legendWidth;
118+
if(lx < 0) lx = 0;
119+
legendWidth = Math.min(fullLayout.width - lx, opts._width);
120+
}
121+
122+
var ly = gs.t + gs.h * (1 - opts.y);
123+
if(Lib.isBottomAnchor(opts)) {
124+
ly -= opts._height;
125+
} else if(Lib.isMiddleAnchor(opts)) {
126+
ly -= opts._height / 2;
134127
}
135128

136129
// Make sure the legend top and bottom are visible
@@ -143,9 +136,9 @@ module.exports = function draw(gd) {
143136
ly = gs.t;
144137
legendHeight = legendHeightMax;
145138
} else {
146-
if(ly + legendHeight > lyMax) ly = lyMax - legendHeight;
147-
if(ly < lyMin) ly = lyMin;
148-
legendHeight = Math.min(lyMax - ly, opts._height);
139+
if(ly + legendHeight > fullLayout.height) ly = fullLayout.height - legendHeight;
140+
if(ly < 0) ly = 0;
141+
legendHeight = Math.min(fullLayout.height - ly, opts._height);
149142
}
150143

151144
// Set size and position of all the elements that make up a legend:
@@ -159,19 +152,19 @@ module.exports = function draw(gd) {
159152
if(opts._height <= legendHeight || gd._context.staticPlot) {
160153
// if scrollbar should not be shown.
161154
bg.attr({
162-
width: legendWidth - opts.borderwidth,
163-
height: legendHeight - opts.borderwidth,
164-
x: opts.borderwidth / 2,
165-
y: opts.borderwidth / 2
155+
width: legendWidth - bw,
156+
height: legendHeight - bw,
157+
x: bw / 2,
158+
y: bw / 2
166159
});
167160

168161
Drawing.setTranslate(scrollBox, 0, 0);
169162

170163
clipPath.select('rect').attr({
171-
width: legendWidth - 2 * opts.borderwidth,
172-
height: legendHeight - 2 * opts.borderwidth,
173-
x: opts.borderwidth,
174-
y: opts.borderwidth
164+
width: legendWidth - 2 * bw,
165+
height: legendHeight - 2 * bw,
166+
x: bw,
167+
y: bw
175168
});
176169

177170
Drawing.setClipUrl(scrollBox, clipId, gd);
@@ -193,22 +186,22 @@ module.exports = function draw(gd) {
193186
// by the scrollbar width and margin
194187
bg.attr({
195188
width: legendWidth -
196-
2 * opts.borderwidth +
189+
2 * bw +
197190
constants.scrollBarWidth +
198191
constants.scrollBarMargin,
199-
height: legendHeight - opts.borderwidth,
200-
x: opts.borderwidth / 2,
201-
y: opts.borderwidth / 2
192+
height: legendHeight - bw,
193+
x: bw / 2,
194+
y: bw / 2
202195
});
203196

204197
clipPath.select('rect').attr({
205198
width: legendWidth -
206-
2 * opts.borderwidth +
199+
2 * bw +
207200
constants.scrollBarWidth +
208201
constants.scrollBarMargin,
209-
height: legendHeight - 2 * opts.borderwidth,
210-
x: opts.borderwidth,
211-
y: opts.borderwidth + scrollBoxY
202+
height: legendHeight - 2 * bw,
203+
x: bw,
204+
y: bw + scrollBoxY
212205
});
213206

214207
Drawing.setClipUrl(scrollBox, clipId, gd);
@@ -246,7 +239,6 @@ module.exports = function draw(gd) {
246239
scrollBar.call(drag);
247240
}
248241

249-
250242
function scrollHandler(scrollBoxY, scrollBarHeight, scrollRatio) {
251243
opts._scrollY = gd._fullLayout.legend._scrollY = scrollBoxY;
252244
Drawing.setTranslate(scrollBox, 0, -scrollBoxY);
@@ -271,7 +263,6 @@ module.exports = function draw(gd) {
271263
gd: gd,
272264
prepFn: function() {
273265
var transform = Drawing.getTranslate(legend);
274-
275266
x0 = transform.x;
276267
y0 = transform.y;
277268
},
@@ -634,7 +625,6 @@ function computeLegendDimensions(gd, groups, traces) {
634625
opts._height += endPad;
635626
}
636627

637-
// make sure we're only getting full pixels
638628
opts._width = Math.ceil(opts._width);
639629
opts._height = Math.ceil(opts._height);
640630

@@ -672,7 +662,6 @@ function expandMargin(gd) {
672662
yanchor = 'middle';
673663
}
674664

675-
// lastly check if the margin auto-expand has changed
676665
Plots.autoMargin(gd, 'legend', {
677666
x: opts.x,
678667
y: opts.y,

0 commit comments

Comments
 (0)