Skip to content

Commit f75f5ba

Browse files
committed
fix #2387 - prevent legend scrolling from scrollBox drag or right click
1 parent f8e7ee4 commit f75f5ba

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

src/components/dragelement/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ dragElement.init = function init(options) {
135135
startY = offset[1];
136136
initialTarget = e.target;
137137
initialEvent = e;
138-
rightClick = (e.buttons && e.buttons === 2) || e.ctrlKey;
138+
rightClick = e.buttons === 2 || e.ctrlKey;
139139

140140
newMouseDownTime = (new Date()).getTime();
141141
if(newMouseDownTime - gd._mouseDownTime < DBLCLICKDELAY) {

src/components/legend/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
module.exports = {
12-
scrollBarWidth: 4,
12+
scrollBarWidth: 6,
1313
scrollBarHeight: 20,
1414
scrollBarColor: '#808BA4',
1515
scrollBarMargin: 4

src/components/legend/draw.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ module.exports = function draw(gd) {
9494
scrollBar.enter().append('rect')
9595
.attr({
9696
'class': 'scrollbar',
97-
'rx': 20,
98-
'ry': 2,
99-
'width': 0,
100-
'height': 0
97+
rx: 20,
98+
ry: 3,
99+
width: 0,
100+
height: 0
101101
})
102102
.call(Color.fill, '#808BA4');
103103

@@ -283,8 +283,12 @@ module.exports = function draw(gd) {
283283
scrollBox.on('.drag', null);
284284

285285
var drag = d3.behavior.drag().on('drag', function() {
286+
var e3 = d3.event;
287+
var e = e3.sourceEvent;
288+
if(e.buttons === 2 || e.ctrlKey) return;
289+
286290
scrollBarY = Lib.constrain(
287-
d3.event.y - constants.scrollBarHeight / 2,
291+
e3.y - constants.scrollBarHeight / 2,
288292
constants.scrollBarMargin,
289293
constants.scrollBarMargin + scrollBarYMax);
290294
scrollBoxY = - (scrollBarY - constants.scrollBarMargin) /
@@ -293,7 +297,6 @@ module.exports = function draw(gd) {
293297
});
294298

295299
scrollBar.call(drag);
296-
scrollBox.call(drag);
297300
}
298301

299302

test/jasmine/tests/legend_scroll_test.js

+76-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var d3 = require('d3');
77
var createGraph = require('../assets/create_graph_div');
88
var destroyGraph = require('../assets/destroy_graph_div');
99
var getBBox = require('../assets/get_bbox');
10+
var mouseEvent = require('../assets/mouse_event');
1011
var mock = require('../../image/mocks/legend_scroll.json');
1112

1213
describe('The legend', function() {
@@ -76,18 +77,18 @@ describe('The legend', function() {
7677
});
7778

7879
it('should scroll when there\'s a wheel event', function() {
79-
var legend = getLegend(),
80-
scrollBox = getScrollBox(),
81-
legendHeight = getLegendHeight(gd),
82-
scrollBoxYMax = gd._fullLayout.legend._height - legendHeight,
83-
scrollBarYMax = legendHeight -
84-
constants.scrollBarHeight -
85-
2 * constants.scrollBarMargin,
86-
initialDataScroll = scrollBox.getAttribute('data-scroll'),
87-
wheelDeltaY = 100,
88-
finalDataScroll = '' + Lib.constrain(initialDataScroll -
89-
wheelDeltaY / scrollBarYMax * scrollBoxYMax,
90-
-scrollBoxYMax, 0);
80+
var legend = getLegend();
81+
var scrollBox = getScrollBox();
82+
var legendHeight = getLegendHeight(gd);
83+
var scrollBoxYMax = gd._fullLayout.legend._height - legendHeight;
84+
var scrollBarYMax = legendHeight -
85+
constants.scrollBarHeight -
86+
2 * constants.scrollBarMargin;
87+
var initialDataScroll = scrollBox.getAttribute('data-scroll');
88+
var wheelDeltaY = 100;
89+
var finalDataScroll = '' + Lib.constrain(initialDataScroll -
90+
wheelDeltaY / scrollBarYMax * scrollBoxYMax,
91+
-scrollBoxYMax, 0);
9192

9293
legend.dispatchEvent(scrollTo(wheelDeltaY));
9394

@@ -96,6 +97,69 @@ describe('The legend', function() {
9697
'translate(0, ' + finalDataScroll + ')');
9798
});
9899

100+
function dragScroll(element, rightClick) {
101+
var scrollBox = getScrollBox();
102+
var scrollBar = getScrollBar();
103+
var legendHeight = getLegendHeight(gd);
104+
var scrollBoxYMax = gd._fullLayout.legend._height - legendHeight;
105+
var scrollBarYMax = legendHeight -
106+
constants.scrollBarHeight -
107+
2 * constants.scrollBarMargin;
108+
var initialDataScroll = scrollBox.getAttribute('data-scroll');
109+
var dy = 50;
110+
var finalDataScroll = '' + Lib.constrain(initialDataScroll -
111+
dy / scrollBarYMax * scrollBoxYMax,
112+
-scrollBoxYMax, 0);
113+
114+
var scrollBarBB = scrollBar.getBoundingClientRect();
115+
var y0 = scrollBarBB.top + scrollBarBB.height / 2;
116+
var y1 = y0 + dy;
117+
118+
var elBB = element.getBoundingClientRect();
119+
var x = elBB.left + elBB.width / 2;
120+
121+
var opts = {element: element};
122+
if(rightClick) {
123+
opts.button = 2;
124+
opts.buttons = 2;
125+
}
126+
127+
mouseEvent('mousedown', x, y0, opts);
128+
mouseEvent('mousemove', x, y1, opts);
129+
mouseEvent('mouseup', x, y1, opts);
130+
131+
expect(finalDataScroll).not.toBe(initialDataScroll);
132+
133+
return finalDataScroll;
134+
}
135+
136+
it('should scroll on dragging the scrollbar', function() {
137+
var finalDataScroll = dragScroll(getScrollBar());
138+
var scrollBox = getScrollBox();
139+
140+
expect(scrollBox.getAttribute('data-scroll')).toBe(finalDataScroll);
141+
expect(scrollBox.getAttribute('transform')).toBe(
142+
'translate(0, ' + finalDataScroll + ')');
143+
});
144+
145+
it('should not scroll on dragging the scrollbox', function() {
146+
var scrollBox = getScrollBox();
147+
var finalDataScroll = dragScroll(scrollBox);
148+
149+
expect(scrollBox.getAttribute('data-scroll')).not.toBe(finalDataScroll);
150+
expect(scrollBox.getAttribute('transform')).not.toBe(
151+
'translate(0, ' + finalDataScroll + ')');
152+
});
153+
154+
it('should not scroll on dragging the scrollbar with a right click', function() {
155+
var finalDataScroll = dragScroll(getScrollBar(), true);
156+
var scrollBox = getScrollBox();
157+
158+
expect(scrollBox.getAttribute('data-scroll')).not.toBe(finalDataScroll);
159+
expect(scrollBox.getAttribute('transform')).not.toBe(
160+
'translate(0, ' + finalDataScroll + ')');
161+
});
162+
99163
it('should keep the scrollbar position after a toggle event', function(done) {
100164
var legend = getLegend(),
101165
scrollBox = getScrollBox(),

0 commit comments

Comments
 (0)