@@ -43,9 +43,6 @@ module.exports = function draw(gd) {
43
43
return ;
44
44
}
45
45
46
- if ( typeof gd . firstRender === 'undefined' ) gd . firstRender = true ;
47
- else if ( gd . firstRender ) gd . firstRender = false ;
48
-
49
46
var legend = fullLayout . _infolayer . selectAll ( 'g.legend' )
50
47
. data ( [ 0 ] ) ;
51
48
@@ -122,7 +119,8 @@ module.exports = function draw(gd) {
122
119
. call ( setupTraceToggle , gd ) ;
123
120
} ) ;
124
121
125
- if ( gd . firstRender ) {
122
+ var firstRender = legend . enter ( ) . size ( ) !== 0 ;
123
+ if ( firstRender ) {
126
124
computeLegendDimensions ( gd , groups , traces ) ;
127
125
expandMargin ( gd ) ;
128
126
}
@@ -198,60 +196,64 @@ module.exports = function draw(gd) {
198
196
// legend, background and border, scroll box and scroll bar
199
197
Lib . setTranslate ( legend , lx , ly ) ;
200
198
201
- bg . attr ( {
202
- width : legendWidth - opts . borderwidth ,
203
- height : legendHeight - opts . borderwidth ,
204
- x : opts . borderwidth / 2 ,
205
- y : opts . borderwidth / 2
206
- } ) ;
199
+ var scrollBarYMax = legendHeight -
200
+ constants . scrollBarHeight -
201
+ 2 * constants . scrollBarMargin ,
202
+ scrollBoxYMax = opts . height - legendHeight ,
203
+ scrollBarY ,
204
+ scrollBoxY ;
207
205
208
- var scrollPosition = scrollBox . attr ( 'data-scroll' ) || 0 ;
206
+ if ( opts . height <= legendHeight || gd . _context . staticPlot ) {
207
+ // if scrollbar should not be shown.
208
+ bg . attr ( {
209
+ width : legendWidth - opts . borderwidth ,
210
+ height : legendHeight - opts . borderwidth ,
211
+ x : opts . borderwidth / 2 ,
212
+ y : opts . borderwidth / 2
213
+ } ) ;
209
214
210
- Lib . setTranslate ( scrollBox , 0 , scrollPosition ) ;
215
+ Lib . setTranslate ( scrollBox , 0 , 0 ) ;
211
216
212
- clipPath . select ( 'rect' ) . attr ( {
213
- width : legendWidth - 2 * opts . borderwidth ,
214
- height : legendHeight - 2 * opts . borderwidth ,
215
- x : opts . borderwidth - scrollPosition ,
216
- y : opts . borderwidth
217
- } ) ;
218
-
219
- scrollBox . call ( Drawing . setClipUrl , clipId ) ;
217
+ clipPath . select ( 'rect' ) . attr ( {
218
+ width : legendWidth - 2 * opts . borderwidth ,
219
+ height : legendHeight - 2 * opts . borderwidth ,
220
+ x : opts . borderwidth ,
221
+ y : opts . borderwidth
222
+ } ) ;
220
223
221
- // If scrollbar should be shown.
222
- if ( opts . height - legendHeight > 0 && ! gd . _context . staticPlot ) {
224
+ scrollBox . call ( Drawing . setClipUrl , clipId ) ;
225
+ }
226
+ else {
227
+ scrollBarY = constants . scrollBarMargin ,
228
+ scrollBoxY = scrollBox . attr ( 'data-scroll' ) || 0 ;
223
229
224
230
// increase the background and clip-path width
225
231
// by the scrollbar width and margin
226
232
bg . attr ( {
227
233
width : legendWidth -
228
234
2 * opts . borderwidth +
229
235
constants . scrollBarWidth +
230
- constants . scrollBarMargin
236
+ constants . scrollBarMargin ,
237
+ height : legendHeight - opts . borderwidth ,
238
+ x : opts . borderwidth / 2 ,
239
+ y : opts . borderwidth / 2
231
240
} ) ;
232
241
233
242
clipPath . select ( 'rect' ) . attr ( {
234
243
width : legendWidth -
235
244
2 * opts . borderwidth +
236
245
constants . scrollBarWidth +
237
- constants . scrollBarMargin
246
+ constants . scrollBarMargin ,
247
+ height : legendHeight - 2 * opts . borderwidth ,
248
+ x : opts . borderwidth ,
249
+ y : opts . borderwidth - scrollBoxY
238
250
} ) ;
239
251
240
- if ( gd . firstRender ) {
241
- // Move scrollbar to starting position
242
- scrollHandler ( constants . scrollBarMargin , 0 ) ;
243
- }
244
-
245
- var scrollBarYMax = legendHeight -
246
- constants . scrollBarHeight -
247
- 2 * constants . scrollBarMargin ,
248
- scrollBoxYMax = opts . height - legendHeight ,
249
- scrollBarY = constants . scrollBarMargin ,
250
- scrollBoxY = 0 ;
252
+ scrollBox . call ( Drawing . setClipUrl , clipId ) ;
251
253
252
- scrollHandler ( scrollBarY , scrollBoxY ) ;
254
+ if ( firstRender ) scrollHandler ( scrollBarY , scrollBoxY ) ;
253
255
254
- legend . on ( 'wheel' , null ) ;
256
+ legend . on ( 'wheel' , null ) ; // to be safe, remove previous listeners
255
257
legend . on ( 'wheel' , function ( ) {
256
258
scrollBoxY = Lib . constrain (
257
259
scrollBox . attr ( 'data-scroll' ) -
@@ -263,8 +265,10 @@ module.exports = function draw(gd) {
263
265
d3 . event . preventDefault ( ) ;
264
266
} ) ;
265
267
268
+ // to be safe, remove previous listeners
266
269
scrollBar . on ( '.drag' , null ) ;
267
270
scrollBox . on ( '.drag' , null ) ;
271
+
268
272
var drag = d3 . behavior . drag ( ) . on ( 'drag' , function ( ) {
269
273
scrollBarY = Lib . constrain (
270
274
d3 . event . y - constants . scrollBarHeight / 2 ,
@@ -277,7 +281,6 @@ module.exports = function draw(gd) {
277
281
278
282
scrollBar . call ( drag ) ;
279
283
scrollBox . call ( drag ) ;
280
-
281
284
}
282
285
283
286
0 commit comments