Skip to content

Commit 197f7fa

Browse files
change legend scrollbar drag to d3 drag behavior
1 parent 52f1a3c commit 197f7fa

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

src/components/legend/draw.js

+24-29
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,19 @@ module.exports = function draw(gd) {
231231
width: opts.width + constants.scrollBarWidth
232232
});
233233

234+
if(gd.firstRender){
235+
// Move scrollbar to starting position
236+
scrollBar.call(
237+
Drawing.setRect,
238+
opts.width - (constants.scrollBarWidth + constants.scrollBarMargin),
239+
constants.scrollBarMargin,
240+
constants.scrollBarWidth,
241+
constants.scrollBarHeight
242+
);
243+
scrollBox.attr('data-scroll',0)
244+
}
234245

235-
// Move scrollbar to starting position
236-
scrollBar.call(
237-
Drawing.setRect,
238-
opts.width - (constants.scrollBarWidth + constants.scrollBarMargin),
239-
constants.scrollBarMargin,
240-
constants.scrollBarWidth,
241-
constants.scrollBarHeight
242-
);
246+
scrollHandler(0,scrollheight);
243247

244248
legend.on('wheel',null);
245249

@@ -249,33 +253,24 @@ module.exports = function draw(gd) {
249253
scrollHandler(e.deltaY / 20, scrollheight);
250254
})
251255

252-
scrollBar.on('mousedown',null);
253-
254-
scrollBar.on('mousedown', function(){
255-
var e = d3.event;
256-
e.preventDefault();
257-
258-
function mMove(e) {
259-
if(e.buttons === 1) {
260-
scrollHandler(e.movementY, scrollheight);
261-
}
262-
}
263-
264-
function mUp() {
265-
scrollBar.node().removeEventListener('mousemove', mMove);
266-
window.removeEventListener('mouseup', mUp);
267-
}
268-
269-
window.addEventListener('mousemove', mMove);
270-
window.addEventListener('mouseup', mUp);
271-
});
256+
scrollBar.on(".drag",null);
257+
scrollBox.on(".drag",null);
258+
var drag = d3.behavior.drag()
259+
.on('drag', function(d){
260+
scrollHandler( d3.event.dy, scrollheight);
261+
});
262+
263+
scrollBar.call(drag);
264+
scrollBox.call(drag);
265+
272266
}
267+
273268

274269
function scrollHandler(delta, scrollheight) {
275270

276271
var scrollBarTrack = scrollheight - constants.scrollBarHeight - 2 * constants.scrollBarMargin,
277272
translateY = scrollBox.attr('data-scroll'),
278-
scrollBoxY = Lib.constrain(translateY - delta, Math.min(scrollheight - opts.height, 0), 0),
273+
scrollBoxY = Lib.constrain(translateY - delta, scrollheight-opts.height, 0),
279274
scrollBarY = -scrollBoxY / (opts.height - scrollheight) * scrollBarTrack + constants.scrollBarMargin;
280275

281276
scrollBox.attr('data-scroll', scrollBoxY);

0 commit comments

Comments
 (0)