@@ -2,6 +2,7 @@ var Plotly = require('@lib/index');
22var Lib = require ( '@src/lib' ) ;
33var constants = require ( '@src/components/legend/constants' ) ;
44
5+ var d3 = require ( 'd3' ) ;
56var createGraph = require ( '../assets/create_graph_div' ) ;
67var destroyGraph = require ( '../assets/destroy_graph_div' ) ;
78var getBBox = require ( '../assets/get_bbox' ) ;
@@ -11,8 +12,6 @@ var mock = require('../../image/mocks/legend_scroll.json');
1112describe ( 'The legend' , function ( ) {
1213 'use strict' ;
1314
14- var gd , legend , bg ;
15-
1615 function countLegendGroups ( gd ) {
1716 return gd . _fullLayout . _toppaper . selectAll ( 'g.legend' ) . size ( ) ;
1817 }
@@ -27,41 +26,59 @@ describe('The legend', function() {
2726 return gd . _fullLayout . height - gd . _fullLayout . margin . t - gd . _fullLayout . margin . b ;
2827 }
2928
30- function getLegendHeight ( ) {
29+ function getLegendHeight ( gd ) {
30+ var bg = d3 . select ( 'g.legend' ) . select ( '.bg' ) . node ( ) ;
3131 return gd . _fullLayout . legend . borderwidth + getBBox ( bg ) . height ;
3232 }
3333
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+
3450 describe ( 'when plotted with many traces' , function ( ) {
51+ var gd ;
52+
3553 beforeEach ( function ( done ) {
3654 gd = createGraph ( ) ;
3755
3856 var mockCopy = Lib . extendDeep ( { } , mock ) ;
3957
4058 Plotly . plot ( gd , mockCopy . data , mockCopy . layout ) . then ( function ( ) {
41- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
42- bg = document . getElementsByClassName ( 'bg' ) [ 0 ] ;
4359 done ( ) ;
4460 } ) ;
4561 } ) ;
4662
4763 afterEach ( destroyGraph ) ;
4864
4965 it ( 'should not exceed plot height' , function ( ) {
50- var legendHeight = getLegendHeight ( ) ;
66+ var legendHeight = getLegendHeight ( gd ) ;
5167
5268 expect ( + legendHeight ) . toBe ( getPlotHeight ( gd ) ) ;
5369 } ) ;
5470
5571 it ( 'should insert a scrollbar' , function ( ) {
56- var scrollBar = legend . getElementsByClassName ( 'scrollbar' ) [ 0 ] ;
72+ var scrollBar = getScrollBar ( ) ;
5773
5874 expect ( scrollBar ) . toBeDefined ( ) ;
5975 expect ( scrollBar . getAttribute ( 'x' ) ) . not . toBe ( null ) ;
6076 } ) ;
6177
6278 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 ) ,
6582 scrollBoxYMax = gd . _fullLayout . legend . height - legendHeight ,
6683 scrollBarYMax = legendHeight -
6784 constants . scrollBarHeight -
@@ -80,8 +97,9 @@ describe('The legend', function() {
8097 } ) ;
8198
8299 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 ( ) ,
85103 wheelDeltaY = 100 ;
86104
87105 legend . dispatchEvent ( scrollTo ( wheelDeltaY ) ) ;
@@ -96,7 +114,7 @@ describe('The legend', function() {
96114
97115 it ( 'should be restored and functional after relayout' , function ( ) {
98116 var wheelDeltaY = 100 ,
99- legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ,
117+ legend = getLegend ( ) ,
100118 scrollBox ,
101119 scrollBar ,
102120 scrollBarX ,
@@ -111,10 +129,10 @@ describe('The legend', function() {
111129 Plotly . relayout ( gd , 'showlegend' , false ) ;
112130 Plotly . relayout ( gd , 'showlegend' , true ) ;
113131
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 ( ) ;
118136
119137 legend . dispatchEvent ( scrollTo ( wheelDeltaY ) ) ;
120138 expect ( scrollBar . getAttribute ( 'x' ) ) . toBe ( scrollBarX ) ;
@@ -131,7 +149,8 @@ describe('The legend', function() {
131149 } ) ;
132150
133151 it ( 'should constrain scrolling to the contents' , function ( ) {
134- var scrollBox = legend . getElementsByClassName ( 'scrollbox' ) [ 0 ] ;
152+ var legend = getLegend ( ) ,
153+ scrollBox = getScrollBox ( ) ;
135154
136155 legend . dispatchEvent ( scrollTo ( - 100 ) ) ;
137156 expect ( scrollBox . getAttribute ( 'transform' ) ) . toBe ( 'translate(0, 0)' ) ;
@@ -141,8 +160,9 @@ describe('The legend', function() {
141160 } ) ;
142161
143162 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 ) ;
146166
147167 // The scrollbar is 20px tall and has 4px margins
148168
@@ -166,10 +186,10 @@ describe('The legend', function() {
166186 } ) ;
167187
168188 it ( 'should resize when relayout\'ed with new height' , function ( done ) {
169- var origLegendHeight = getLegendHeight ( ) ;
189+ var origLegendHeight = getLegendHeight ( gd ) ;
170190
171191 Plotly . relayout ( gd , 'height' , gd . _fullLayout . height / 2 ) . then ( function ( ) {
172- var legendHeight = getLegendHeight ( ) ;
192+ var legendHeight = getLegendHeight ( gd ) ;
173193
174194 // legend still exists and not duplicated
175195 expect ( countLegendGroups ( gd ) ) . toBe ( 1 ) ;
@@ -184,7 +204,6 @@ describe('The legend', function() {
184204 } ) ;
185205 } ) ;
186206
187-
188207 describe ( 'when plotted with few traces' , function ( ) {
189208 var gd ;
190209
@@ -219,14 +238,10 @@ describe('The legend', function() {
219238 } ) ;
220239
221240 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 ) ;
225242
226243 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 ) ;
230245
231246 expect ( + legendHeight ) . toBeCloseTo ( + origLegendHeight + 19 , 0 ) ;
232247
0 commit comments