@@ -6,6 +6,7 @@ var DBLCLICKDELAY = require('@src/constants/interactions').DBLCLICKDELAY;
6
6
var d3 = require ( 'd3' ) ;
7
7
var createGraph = require ( '../assets/create_graph_div' ) ;
8
8
var destroyGraph = require ( '../assets/destroy_graph_div' ) ;
9
+ var failTest = require ( '../assets/fail_test' ) ;
9
10
var getBBox = require ( '../assets/get_bbox' ) ;
10
11
var mouseEvent = require ( '../assets/mouse_event' ) ;
11
12
var mock = require ( '../../image/mocks/legend_scroll.json' ) ;
@@ -48,6 +49,17 @@ describe('The legend', function() {
48
49
return d3 . select ( 'g.legend' ) . select ( '.legendtoggle' ) . node ( ) ;
49
50
}
50
51
52
+ function getScroll ( gd ) {
53
+ return gd . _fullLayout . legend . _scrollY ;
54
+ }
55
+
56
+ function hasScrollBar ( ) {
57
+ var scrollBar = getScrollBar ( ) ;
58
+ return scrollBar &&
59
+ + scrollBar . getAttribute ( 'width' ) > 0 &&
60
+ + scrollBar . getAttribute ( 'height' ) > 0 ;
61
+ }
62
+
51
63
describe ( 'when plotted with many traces' , function ( ) {
52
64
var gd ;
53
65
@@ -85,31 +97,30 @@ describe('The legend', function() {
85
97
var scrollBarYMax = legendHeight -
86
98
scrollBar . getBoundingClientRect ( ) . height -
87
99
2 * constants . scrollBarMargin ;
88
- var initialDataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
100
+ var initialDataScroll = getScroll ( gd ) ;
89
101
var wheelDeltaY = 100 ;
90
- var finalDataScroll = '' + Lib . constrain ( initialDataScroll -
102
+ var finalDataScroll = Lib . constrain ( initialDataScroll -
91
103
wheelDeltaY / scrollBarYMax * scrollBoxYMax ,
92
104
- scrollBoxYMax , 0 ) ;
93
105
94
106
legend . dispatchEvent ( scrollTo ( wheelDeltaY ) ) ;
95
107
96
- expect ( scrollBox . getAttribute ( 'data-scroll' ) ) . toBe ( finalDataScroll ) ;
108
+ expect ( getScroll ( gd ) ) . toBe ( finalDataScroll ) ;
97
109
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe (
98
110
'translate(0, ' + finalDataScroll + ')' ) ;
99
111
} ) ;
100
112
101
113
function dragScroll ( element , rightClick ) {
102
- var scrollBox = getScrollBox ( ) ;
103
114
var scrollBar = getScrollBar ( ) ;
104
115
var scrollBarBB = scrollBar . getBoundingClientRect ( ) ;
105
116
var legendHeight = getLegendHeight ( gd ) ;
106
117
var scrollBoxYMax = gd . _fullLayout . legend . _height - legendHeight ;
107
118
var scrollBarYMax = legendHeight -
108
119
scrollBarBB . height -
109
120
2 * constants . scrollBarMargin ;
110
- var initialDataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
121
+ var initialDataScroll = getScroll ( gd ) ;
111
122
var dy = 50 ;
112
- var finalDataScroll = '' + Lib . constrain ( initialDataScroll -
123
+ var finalDataScroll = Lib . constrain ( initialDataScroll -
113
124
dy / scrollBarYMax * scrollBoxYMax ,
114
125
- scrollBoxYMax , 0 ) ;
115
126
@@ -138,7 +149,7 @@ describe('The legend', function() {
138
149
var finalDataScroll = dragScroll ( getScrollBar ( ) ) ;
139
150
var scrollBox = getScrollBox ( ) ;
140
151
141
- var dataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
152
+ var dataScroll = getScroll ( gd ) ;
142
153
expect ( dataScroll ) . toBeCloseTo ( finalDataScroll , 3 ) ;
143
154
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe (
144
155
'translate(0, ' + dataScroll + ')' ) ;
@@ -148,7 +159,7 @@ describe('The legend', function() {
148
159
var scrollBox = getScrollBox ( ) ;
149
160
var finalDataScroll = dragScroll ( scrollBox ) ;
150
161
151
- var dataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
162
+ var dataScroll = getScroll ( gd ) ;
152
163
expect ( dataScroll ) . not . toBeCloseTo ( finalDataScroll , 3 ) ;
153
164
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe (
154
165
'translate(0, ' + dataScroll + ')' ) ;
@@ -158,12 +169,73 @@ describe('The legend', function() {
158
169
var finalDataScroll = dragScroll ( getScrollBar ( ) , true ) ;
159
170
var scrollBox = getScrollBox ( ) ;
160
171
161
- var dataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
172
+ var dataScroll = getScroll ( gd ) ;
162
173
expect ( dataScroll ) . not . toBeCloseTo ( finalDataScroll , 3 ) ;
163
174
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe (
164
175
'translate(0, ' + dataScroll + ')' ) ;
165
176
} ) ;
166
177
178
+ it ( 'removes scroll bar and handlers when switching to horizontal' , function ( done ) {
179
+ expect ( hasScrollBar ( ) ) . toBe ( true ) ;
180
+
181
+ Plotly . relayout ( gd , { 'legend.orientation' : 'h' } )
182
+ . then ( function ( ) {
183
+ expect ( hasScrollBar ( ) ) . toBe ( false ) ;
184
+ expect ( getScroll ( gd ) ) . toBeUndefined ( ) ;
185
+
186
+ getLegend ( ) . dispatchEvent ( scrollTo ( 100 ) ) ;
187
+ expect ( hasScrollBar ( ) ) . toBe ( false ) ;
188
+ expect ( getScroll ( gd ) ) . toBeUndefined ( ) ;
189
+
190
+ return Plotly . relayout ( gd , { 'legend.orientation' : 'v' } ) ;
191
+ } )
192
+ . then ( function ( ) {
193
+ expect ( hasScrollBar ( ) ) . toBe ( true ) ;
194
+ expect ( getScroll ( gd ) ) . toBe ( 0 ) ;
195
+
196
+ getLegend ( ) . dispatchEvent ( scrollTo ( 100 ) ) ;
197
+ expect ( hasScrollBar ( ) ) . toBe ( true ) ;
198
+ expect ( getScroll ( gd ) ) . not . toBe ( 0 ) ;
199
+ } )
200
+ . catch ( failTest )
201
+ . then ( done ) ;
202
+ } ) ;
203
+
204
+ it ( 'updates scrollBar size/existence on deleteTraces' , function ( done ) {
205
+ expect ( hasScrollBar ( ) ) . toBe ( true ) ;
206
+ var dataScroll = dragScroll ( getScrollBar ( ) ) ;
207
+ var scrollBarHeight = getScrollBar ( ) . getBoundingClientRect ( ) . height ;
208
+ var scrollBarHeight1 ;
209
+
210
+ Plotly . deleteTraces ( gd , [ 0 ] )
211
+ . then ( function ( ) {
212
+ expect ( getScroll ( gd ) ) . toBeCloseTo ( dataScroll , 3 ) ;
213
+ scrollBarHeight1 = getScrollBar ( ) . getBoundingClientRect ( ) . height ;
214
+ expect ( scrollBarHeight1 ) . toBeGreaterThan ( scrollBarHeight ) ;
215
+
216
+ // we haven't quite removed the scrollbar, but we should have clipped the scroll value
217
+ return Plotly . deleteTraces ( gd , [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ;
218
+ } )
219
+ . then ( function ( ) {
220
+ expect ( getScroll ( gd ) ) . toBeGreaterThan ( dataScroll + 1 ) ;
221
+ var scrollBarHeight2 = getScrollBar ( ) . getBoundingClientRect ( ) . height ;
222
+ expect ( scrollBarHeight2 ) . toBeGreaterThan ( scrollBarHeight1 ) ;
223
+
224
+ // now no more scrollBar
225
+ return Plotly . deleteTraces ( gd , [ 0 , 1 ] ) ;
226
+ } )
227
+ . then ( function ( ) {
228
+ expect ( hasScrollBar ( ) ) . toBe ( false ) ;
229
+ expect ( getScroll ( gd ) ) . toBeUndefined ( ) ;
230
+
231
+ getLegend ( ) . dispatchEvent ( scrollTo ( 100 ) ) ;
232
+ expect ( hasScrollBar ( ) ) . toBe ( false ) ;
233
+ expect ( getScroll ( gd ) ) . toBeUndefined ( ) ;
234
+ } )
235
+ . catch ( failTest )
236
+ . then ( done ) ;
237
+ } ) ;
238
+
167
239
it ( 'should keep the scrollbar position after a toggle event' , function ( done ) {
168
240
var legend = getLegend ( ) ,
169
241
scrollBox = getScrollBox ( ) ,
@@ -172,12 +244,12 @@ describe('The legend', function() {
172
244
173
245
legend . dispatchEvent ( scrollTo ( wheelDeltaY ) ) ;
174
246
175
- var dataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
247
+ var dataScroll = getScroll ( gd ) ;
176
248
toggle . dispatchEvent ( new MouseEvent ( 'mousedown' ) ) ;
177
249
toggle . dispatchEvent ( new MouseEvent ( 'mouseup' ) ) ;
178
250
setTimeout ( function ( ) {
179
251
expect ( + toggle . parentNode . style . opacity ) . toBeLessThan ( 1 ) ;
180
- expect ( scrollBox . getAttribute ( 'data-scroll' ) ) . toBe ( dataScroll ) ;
252
+ expect ( getScroll ( gd ) ) . toBe ( dataScroll ) ;
181
253
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe (
182
254
'translate(0, ' + dataScroll + ')' ) ;
183
255
done ( ) ;
@@ -210,12 +282,12 @@ describe('The legend', function() {
210
282
expect ( scrollBar . getAttribute ( 'x' ) ) . toBe ( scrollBarX ) ;
211
283
expect ( scrollBar . getAttribute ( 'y' ) ) . toBe ( scrollBarY ) ;
212
284
213
- var dataScroll = scrollBox . getAttribute ( 'data-scroll' ) ;
285
+ var dataScroll = getScroll ( gd ) ;
214
286
toggle . dispatchEvent ( new MouseEvent ( 'mousedown' ) ) ;
215
287
toggle . dispatchEvent ( new MouseEvent ( 'mouseup' ) ) ;
216
288
setTimeout ( function ( ) {
217
289
expect ( + toggle . parentNode . style . opacity ) . toBeLessThan ( 1 ) ;
218
- expect ( scrollBox . getAttribute ( 'data-scroll' ) ) . toBe ( dataScroll ) ;
290
+ expect ( getScroll ( gd ) ) . toBe ( dataScroll ) ;
219
291
expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe (
220
292
'translate(0, ' + dataScroll + ')' ) ;
221
293
expect ( scrollBar . getAttribute ( 'width' ) ) . toBeGreaterThan ( 0 ) ;
0 commit comments