@@ -2,6 +2,7 @@ var Plotly = require('@lib/index');
2
2
var Lib = require ( '@src/lib' ) ;
3
3
var constants = require ( '@src/components/legend/constants' ) ;
4
4
5
+ var d3 = require ( 'd3' ) ;
5
6
var createGraph = require ( '../assets/create_graph_div' ) ;
6
7
var destroyGraph = require ( '../assets/destroy_graph_div' ) ;
7
8
var getBBox = require ( '../assets/get_bbox' ) ;
@@ -11,8 +12,6 @@ var mock = require('../../image/mocks/legend_scroll.json');
11
12
describe ( 'The legend' , function ( ) {
12
13
'use strict' ;
13
14
14
- var gd , legend , bg ;
15
-
16
15
function countLegendGroups ( gd ) {
17
16
return gd . _fullLayout . _toppaper . selectAll ( 'g.legend' ) . size ( ) ;
18
17
}
@@ -27,41 +26,59 @@ describe('The legend', function() {
27
26
return gd . _fullLayout . height - gd . _fullLayout . margin . t - gd . _fullLayout . margin . b ;
28
27
}
29
28
30
- function getLegendHeight ( ) {
29
+ function getLegendHeight ( gd ) {
30
+ var bg = d3 . select ( 'g.legend' ) . select ( '.bg' ) . node ( ) ;
31
31
return gd . _fullLayout . legend . borderwidth + getBBox ( bg ) . height ;
32
32
}
33
33
34
+ function getLegend ( ) {
35
+ return d3 . select ( 'g.legend' ) . node ( ) ;
36
+ }
37
+
38
+ function getScrollBox ( ) {
39
+ return d3 . select ( 'g.legend' ) . select ( '.scrollbox' ) . node ( ) ;
40
+ }
41
+
42
+ function getScrollBar ( ) {
43
+ return d3 . select ( 'g.legend' ) . select ( '.scrollbar' ) . node ( ) ;
44
+ }
45
+
46
+ function getToggle ( ) {
47
+ return d3 . select ( 'g.legend' ) . select ( '.legendtoggle' ) . node ( ) ;
48
+ }
49
+
34
50
describe ( 'when plotted with many traces' , function ( ) {
51
+ var gd ;
52
+
35
53
beforeEach ( function ( done ) {
36
54
gd = createGraph ( ) ;
37
55
38
56
var mockCopy = Lib . extendDeep ( { } , mock ) ;
39
57
40
58
Plotly . plot ( gd , mockCopy . data , mockCopy . layout ) . then ( function ( ) {
41
- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
42
- bg = document . getElementsByClassName ( 'bg' ) [ 0 ] ;
43
59
done ( ) ;
44
60
} ) ;
45
61
} ) ;
46
62
47
63
afterEach ( destroyGraph ) ;
48
64
49
65
it ( 'should not exceed plot height' , function ( ) {
50
- var legendHeight = getLegendHeight ( ) ;
66
+ var legendHeight = getLegendHeight ( gd ) ;
51
67
52
68
expect ( + legendHeight ) . toBe ( getPlotHeight ( gd ) ) ;
53
69
} ) ;
54
70
55
71
it ( 'should insert a scrollbar' , function ( ) {
56
- var scrollBar = legend . getElementsByClassName ( 'scrollbar' ) [ 0 ] ;
72
+ var scrollBar = getScrollBar ( ) ;
57
73
58
74
expect ( scrollBar ) . toBeDefined ( ) ;
59
75
expect ( scrollBar . getAttribute ( 'x' ) ) . not . toBe ( null ) ;
60
76
} ) ;
61
77
62
78
it ( 'should scroll when there\'s a wheel event' , function ( ) {
63
- var scrollBox = legend . getElementsByClassName ( 'scrollbox' ) [ 0 ] ,
64
- legendHeight = getLegendHeight ( ) ,
79
+ var legend = getLegend ( ) ,
80
+ scrollBox = getScrollBox ( ) ,
81
+ legendHeight = getLegendHeight ( gd ) ,
65
82
scrollBoxYMax = gd . _fullLayout . legend . height - legendHeight ,
66
83
scrollBarYMax = legendHeight -
67
84
constants . scrollBarHeight -
@@ -80,8 +97,9 @@ describe('The legend', function() {
80
97
} ) ;
81
98
82
99
it ( 'should keep the scrollbar position after a toggle event' , function ( ) {
83
- var scrollBox = legend . getElementsByClassName ( 'scrollbox' ) [ 0 ] ,
84
- toggle = legend . getElementsByClassName ( 'legendtoggle' ) [ 0 ] ,
100
+ var legend = getLegend ( ) ,
101
+ scrollBox = getScrollBox ( ) ,
102
+ toggle = getToggle ( ) ,
85
103
wheelDeltaY = 100 ;
86
104
87
105
legend . dispatchEvent ( scrollTo ( wheelDeltaY ) ) ;
@@ -96,7 +114,7 @@ describe('The legend', function() {
96
114
97
115
it ( 'should be restored and functional after relayout' , function ( ) {
98
116
var wheelDeltaY = 100 ,
99
- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ,
117
+ legend = getLegend ( ) ,
100
118
scrollBox ,
101
119
scrollBar ,
102
120
scrollBarX ,
@@ -111,10 +129,10 @@ describe('The legend', function() {
111
129
Plotly . relayout ( gd , 'showlegend' , false ) ;
112
130
Plotly . relayout ( gd , 'showlegend' , true ) ;
113
131
114
- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
115
- scrollBox = legend . getElementsByClassName ( 'scrollbox' ) [ 0 ] ;
116
- scrollBar = legend . getElementsByClassName ( 'scrollbar' ) [ 0 ] ;
117
- toggle = legend . getElementsByClassName ( 'legendtoggle' ) [ 0 ] ;
132
+ legend = getLegend ( ) ;
133
+ scrollBox = getScrollBox ( ) ;
134
+ scrollBar = getScrollBar ( ) ;
135
+ toggle = getToggle ( ) ;
118
136
119
137
legend . dispatchEvent ( scrollTo ( wheelDeltaY ) ) ;
120
138
expect ( scrollBar . getAttribute ( 'x' ) ) . toBe ( scrollBarX ) ;
@@ -131,7 +149,8 @@ describe('The legend', function() {
131
149
} ) ;
132
150
133
151
it ( 'should constrain scrolling to the contents' , function ( ) {
134
- var scrollBox = legend . getElementsByClassName ( 'scrollbox' ) [ 0 ] ;
152
+ var legend = getLegend ( ) ,
153
+ scrollBox = getScrollBox ( ) ;
135
154
136
155
legend . dispatchEvent ( scrollTo ( - 100 ) ) ;
137
156
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe ( 'translate(0, 0)' ) ;
@@ -141,8 +160,9 @@ describe('The legend', function() {
141
160
} ) ;
142
161
143
162
it ( 'should scale the scrollbar movement from top to bottom' , function ( ) {
144
- var scrollBar = legend . getElementsByClassName ( 'scrollbar' ) [ 0 ] ,
145
- legendHeight = getLegendHeight ( ) ;
163
+ var legend = getLegend ( ) ,
164
+ scrollBar = getScrollBar ( ) ,
165
+ legendHeight = getLegendHeight ( gd ) ;
146
166
147
167
// The scrollbar is 20px tall and has 4px margins
148
168
@@ -166,10 +186,10 @@ describe('The legend', function() {
166
186
} ) ;
167
187
168
188
it ( 'should resize when relayout\'ed with new height' , function ( done ) {
169
- var origLegendHeight = getLegendHeight ( ) ;
189
+ var origLegendHeight = getLegendHeight ( gd ) ;
170
190
171
191
Plotly . relayout ( gd , 'height' , gd . _fullLayout . height / 2 ) . then ( function ( ) {
172
- var legendHeight = getLegendHeight ( ) ;
192
+ var legendHeight = getLegendHeight ( gd ) ;
173
193
174
194
// legend still exists and not duplicated
175
195
expect ( countLegendGroups ( gd ) ) . toBe ( 1 ) ;
@@ -184,7 +204,6 @@ describe('The legend', function() {
184
204
} ) ;
185
205
} ) ;
186
206
187
-
188
207
describe ( 'when plotted with few traces' , function ( ) {
189
208
var gd ;
190
209
@@ -219,14 +238,10 @@ describe('The legend', function() {
219
238
} ) ;
220
239
221
240
it ( 'should resize when traces added' , function ( done ) {
222
- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
223
- bg = document . getElementsByClassName ( 'bg' ) [ 0 ] ;
224
- var origLegendHeight = getLegendHeight ( ) ;
241
+ var origLegendHeight = getLegendHeight ( gd ) ;
225
242
226
243
Plotly . addTraces ( gd , { x : [ 1 , 2 , 3 ] , y : [ 4 , 3 , 2 ] , name : 'Test2' } ) . then ( function ( ) {
227
- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
228
- bg = document . getElementsByClassName ( 'bg' ) [ 0 ] ;
229
- var legendHeight = getLegendHeight ( ) ;
244
+ var legendHeight = getLegendHeight ( gd ) ;
230
245
231
246
expect ( + legendHeight ) . toBeCloseTo ( + origLegendHeight + 19 , 0 ) ;
232
247
0 commit comments