Skip to content

Commit f2d8cfd

Browse files
committed
turn scrollBoxY into a positive number
too confusing with it always being negative
1 parent 5660bf5 commit f2d8cfd

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/components/legend/draw.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ module.exports = function draw(gd) {
243243
var scrollBoxYMax = opts._height - legendHeight;
244244
var scrollRatio = scrollBarYMax / scrollBoxYMax;
245245

246-
// scrollBoxY is 0 or a negative number
247-
var scrollBoxY = Math.max(opts._scrollY || 0, -scrollBoxYMax);
246+
var scrollBoxY = Math.min(opts._scrollY || 0, scrollBoxYMax);
248247

249248
// increase the background and clip-path width
250249
// by the scrollbar width and margin
@@ -265,7 +264,7 @@ module.exports = function draw(gd) {
265264
constants.scrollBarMargin,
266265
height: legendHeight - 2 * opts.borderwidth,
267266
x: opts.borderwidth,
268-
y: opts.borderwidth - scrollBoxY
267+
y: opts.borderwidth + scrollBoxY
269268
});
270269

271270
Drawing.setClipUrl(scrollBox, clipId);
@@ -274,11 +273,11 @@ module.exports = function draw(gd) {
274273

275274
legend.on('wheel', function() {
276275
scrollBoxY = Lib.constrain(
277-
opts._scrollY -
276+
opts._scrollY +
278277
d3.event.deltaY / scrollBarYMax * scrollBoxYMax,
279-
-scrollBoxYMax, 0);
278+
0, scrollBoxYMax);
280279
scrollHandler(scrollBoxY, scrollBarHeight, scrollRatio);
281-
if(scrollBoxY !== 0 && scrollBoxY !== -scrollBoxYMax) {
280+
if(scrollBoxY !== 0 && scrollBoxY !== scrollBoxYMax) {
282281
d3.event.preventDefault();
283282
}
284283
});
@@ -295,8 +294,8 @@ module.exports = function draw(gd) {
295294
if(e.buttons === 2 || e.ctrlKey) return;
296295

297296
scrollBoxY = Lib.constrain(
298-
(eventY0 - e.clientY) / scrollRatio + scrollBoxY0,
299-
-scrollBoxYMax, 0);
297+
(e.clientY - eventY0) / scrollRatio + scrollBoxY0,
298+
0, scrollBoxYMax);
300299
scrollHandler(scrollBoxY, scrollBarHeight, scrollRatio);
301300
});
302301

@@ -306,17 +305,17 @@ module.exports = function draw(gd) {
306305

307306
function scrollHandler(scrollBoxY, scrollBarHeight, scrollRatio) {
308307
opts._scrollY = gd._fullLayout.legend._scrollY = scrollBoxY;
309-
Drawing.setTranslate(scrollBox, 0, scrollBoxY);
308+
Drawing.setTranslate(scrollBox, 0, -scrollBoxY);
310309

311310
Drawing.setRect(
312311
scrollBar,
313312
legendWidth,
314-
constants.scrollBarMargin - scrollBoxY * scrollRatio,
313+
constants.scrollBarMargin + scrollBoxY * scrollRatio,
315314
constants.scrollBarWidth,
316315
scrollBarHeight
317316
);
318317
clipPath.select('rect').attr({
319-
y: opts.borderwidth - scrollBoxY
318+
y: opts.borderwidth + scrollBoxY
320319
});
321320
}
322321

test/jasmine/tests/legend_scroll_test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ describe('The legend', function() {
9999
2 * constants.scrollBarMargin;
100100
var initialDataScroll = getScroll(gd);
101101
var wheelDeltaY = 100;
102-
var finalDataScroll = Lib.constrain(initialDataScroll -
102+
var finalDataScroll = Lib.constrain(initialDataScroll +
103103
wheelDeltaY / scrollBarYMax * scrollBoxYMax,
104-
-scrollBoxYMax, 0);
104+
0, scrollBoxYMax);
105105

106106
legend.dispatchEvent(scrollTo(wheelDeltaY));
107107

108108
expect(getScroll(gd)).toBe(finalDataScroll);
109109
expect(scrollBox.getAttribute('transform')).toBe(
110-
'translate(0, ' + finalDataScroll + ')');
110+
'translate(0, ' + -finalDataScroll + ')');
111111
});
112112

113113
function dragScroll(element, rightClick) {
@@ -120,9 +120,9 @@ describe('The legend', function() {
120120
2 * constants.scrollBarMargin;
121121
var initialDataScroll = getScroll(gd);
122122
var dy = 50;
123-
var finalDataScroll = Lib.constrain(initialDataScroll -
123+
var finalDataScroll = Lib.constrain(initialDataScroll +
124124
dy / scrollBarYMax * scrollBoxYMax,
125-
-scrollBoxYMax, 0);
125+
0, scrollBoxYMax);
126126

127127
var y0 = scrollBarBB.top + scrollBarBB.height / 5;
128128
var y1 = y0 + dy;
@@ -152,7 +152,7 @@ describe('The legend', function() {
152152
var dataScroll = getScroll(gd);
153153
expect(dataScroll).toBeCloseTo(finalDataScroll, 3);
154154
expect(scrollBox.getAttribute('transform')).toBe(
155-
'translate(0, ' + dataScroll + ')');
155+
'translate(0, ' + -dataScroll + ')');
156156
});
157157

158158
it('should not scroll on dragging the scrollbox', function() {
@@ -162,7 +162,7 @@ describe('The legend', function() {
162162
var dataScroll = getScroll(gd);
163163
expect(dataScroll).not.toBeCloseTo(finalDataScroll, 3);
164164
expect(scrollBox.getAttribute('transform')).toBe(
165-
'translate(0, ' + dataScroll + ')');
165+
'translate(0, ' + -dataScroll + ')');
166166
});
167167

168168
it('should not scroll on dragging the scrollbar with a right click', function() {
@@ -172,7 +172,7 @@ describe('The legend', function() {
172172
var dataScroll = getScroll(gd);
173173
expect(dataScroll).not.toBeCloseTo(finalDataScroll, 3);
174174
expect(scrollBox.getAttribute('transform')).toBe(
175-
'translate(0, ' + dataScroll + ')');
175+
'translate(0, ' + -dataScroll + ')');
176176
});
177177

178178
it('removes scroll bar and handlers when switching to horizontal', function(done) {
@@ -214,15 +214,15 @@ describe('The legend', function() {
214214
expect(scrollBarHeight1).toBeGreaterThan(scrollBarHeight);
215215

216216
// we haven't quite removed the scrollbar, but we should have clipped the scroll value
217-
return Plotly.deleteTraces(gd, [0, 1, 2, 3, 4, 5, 6, 7]);
217+
return Plotly.deleteTraces(gd, [0, 1, 2, 3, 4, 5, 6]);
218218
})
219219
.then(function() {
220-
expect(getScroll(gd)).toBeGreaterThan(dataScroll + 1);
220+
expect(getScroll(gd)).toBeLessThan(dataScroll - 1);
221221
var scrollBarHeight2 = getScrollBar().getBoundingClientRect().height;
222222
expect(scrollBarHeight2).toBeGreaterThan(scrollBarHeight1);
223223

224224
// now no more scrollBar
225-
return Plotly.deleteTraces(gd, [0, 1]);
225+
return Plotly.deleteTraces(gd, [0, 1, 2]);
226226
})
227227
.then(function() {
228228
expect(hasScrollBar()).toBe(false);
@@ -251,7 +251,7 @@ describe('The legend', function() {
251251
expect(+toggle.parentNode.style.opacity).toBeLessThan(1);
252252
expect(getScroll(gd)).toBe(dataScroll);
253253
expect(scrollBox.getAttribute('transform')).toBe(
254-
'translate(0, ' + dataScroll + ')');
254+
'translate(0, ' + -dataScroll + ')');
255255
done();
256256
}, DBLCLICKDELAY * 2);
257257
});
@@ -289,7 +289,7 @@ describe('The legend', function() {
289289
expect(+toggle.parentNode.style.opacity).toBeLessThan(1);
290290
expect(getScroll(gd)).toBe(dataScroll);
291291
expect(scrollBox.getAttribute('transform')).toBe(
292-
'translate(0, ' + dataScroll + ')');
292+
'translate(0, ' + -dataScroll + ')');
293293
expect(scrollBar.getAttribute('width')).toBeGreaterThan(0);
294294
expect(scrollBar.getAttribute('height')).toBeGreaterThan(0);
295295
done();

0 commit comments

Comments
 (0)