@@ -56,8 +56,9 @@ module.exports = function draw(gd) {
5656 } ) ;
5757
5858 var clipPath = fullLayout . _topdefs . selectAll ( '#' + clipId )
59- . data ( [ 0 ] )
60- . enter ( ) . append ( 'clipPath' )
59+ . data ( [ 0 ] ) ;
60+
61+ clipPath . enter ( ) . append ( 'clipPath' )
6162 . attr ( 'id' , clipId )
6263 . append ( 'rect' ) ;
6364
@@ -210,7 +211,7 @@ module.exports = function draw(gd) {
210211
211212 legend . attr ( 'transform' , 'translate(' + lx + ',' + ly + ')' ) ;
212213
213- clipPath . attr ( {
214+ clipPath . select ( 'rect' ) . attr ( {
214215 width : opts . width ,
215216 height : scrollheight ,
216217 x : 0 ,
@@ -220,7 +221,8 @@ module.exports = function draw(gd) {
220221 legend . call ( Drawing . setClipUrl , clipId ) ;
221222
222223 // If scrollbar should be shown.
223- if ( gd . firstRender && opts . height - scrollheight > 0 && ! gd . _context . staticPlot ) {
224+ if ( opts . height - scrollheight > 0 && ! gd . _context . staticPlot ) {
225+
224226 bg . attr ( {
225227 width : opts . width - 2 * opts . borderwidth + constants . scrollBarWidth
226228 } ) ;
@@ -229,44 +231,46 @@ module.exports = function draw(gd) {
229231 width : opts . width + constants . scrollBarWidth
230232 } ) ;
231233
232- legend . node ( ) . addEventListener ( 'wheel' , function ( e ) {
233- e . preventDefault ( ) ;
234- scrollHandler ( e . deltaY / 20 ) ;
235- } ) ;
236-
237- scrollBar . node ( ) . addEventListener ( 'mousedown' , function ( e ) {
238- e . preventDefault ( ) ;
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+ }
239245
240- function mMove ( e ) {
241- if ( e . buttons === 1 ) {
242- scrollHandler ( e . movementY ) ;
243- }
244- }
246+ scrollHandler ( 0 , scrollheight ) ;
245247
246- function mUp ( ) {
247- scrollBar . node ( ) . removeEventListener ( 'mousemove' , mMove ) ;
248- window . removeEventListener ( 'mouseup' , mUp ) ;
249- }
248+ legend . on ( 'wheel' , null ) ;
250249
251- window . addEventListener ( 'mousemove' , mMove ) ;
252- window . addEventListener ( 'mouseup' , mUp ) ;
250+ legend . on ( 'wheel' , function ( ) {
251+ var e = d3 . event ;
252+ e . preventDefault ( ) ;
253+ scrollHandler ( e . deltaY / 20 , scrollheight ) ;
253254 } ) ;
254255
255- // Move scrollbar to starting position on the first render
256- scrollBar . call (
257- Drawing . setRect ,
258- opts . width - ( constants . scrollBarWidth + constants . scrollBarMargin ) ,
259- constants . scrollBarMargin ,
260- constants . scrollBarWidth ,
261- constants . scrollBarHeight
262- ) ;
256+ scrollBar . on ( '.drag' , null ) ;
257+ scrollBox . on ( '.drag' , null ) ;
258+ var drag = d3 . behavior . drag ( )
259+ . on ( 'drag' , function ( ) {
260+ scrollHandler ( d3 . event . dy , scrollheight ) ;
261+ } ) ;
262+
263+ scrollBar . call ( drag ) ;
264+ scrollBox . call ( drag ) ;
265+
263266 }
264267
265- function scrollHandler ( delta ) {
268+
269+ function scrollHandler ( delta , scrollheight ) {
266270
267271 var scrollBarTrack = scrollheight - constants . scrollBarHeight - 2 * constants . scrollBarMargin ,
268272 translateY = scrollBox . attr ( 'data-scroll' ) ,
269- scrollBoxY = Lib . constrain ( translateY - delta , Math . min ( scrollheight - opts . height , 0 ) , 0 ) ,
273+ scrollBoxY = Lib . constrain ( translateY - delta , scrollheight - opts . height , 0 ) ,
270274 scrollBarY = - scrollBoxY / ( opts . height - scrollheight ) * scrollBarTrack + constants . scrollBarMargin ;
271275
272276 scrollBox . attr ( 'data-scroll' , scrollBoxY ) ;
0 commit comments