@@ -20,33 +20,37 @@ var SHOWISOLATETIP = true;
2020exports . handleItemClick = function handleItemClick ( g , gd , legendObj , mode ) {
2121 var fullLayout = gd . _fullLayout ;
2222
23- if ( gd . _dragged || gd . _editing ) return ;
23+ if ( gd . _dragged || gd . _editing ) return ;
2424
2525 var legendItem = g . data ( ) [ 0 ] [ 0 ] ;
26- if ( legendItem . groupTitle && legendItem . noClick ) return ;
26+ if ( legendItem . groupTitle && legendItem . noClick ) return ;
2727
2828 var groupClick = legendObj . groupclick ;
2929
3030 // Show isolate tip on first single click when default behavior is active
31- if ( mode === 'toggle' && legendObj . itemdoubleclick === 'toggleothers' &&
32- SHOWISOLATETIP && gd . data && gd . _context . showTips
31+ if (
32+ mode === 'toggle' &&
33+ legendObj . itemdoubleclick === 'toggleothers' &&
34+ SHOWISOLATETIP &&
35+ gd . data &&
36+ gd . _context . showTips
3337 ) {
3438 Lib . notifier ( Lib . _ ( gd , 'Double-click on legend to isolate one trace' ) , 'long' , gd ) ;
3539 SHOWISOLATETIP = false ;
3640 }
3741
3842 var toggleGroup = groupClick === 'togglegroup' ;
3943
40- var hiddenSlices = fullLayout . hiddenlabels ?
41- fullLayout . hiddenlabels . slice ( ) :
42- [ ] ;
44+ var hiddenSlices = fullLayout . hiddenlabels ? fullLayout . hiddenlabels . slice ( ) : [ ] ;
4345
4446 var fullData = gd . _fullData ;
45- var shapesWithLegend = ( fullLayout . shapes || [ ] ) . filter ( function ( d ) { return d . showlegend ; } ) ;
47+ var shapesWithLegend = ( fullLayout . shapes || [ ] ) . filter ( function ( d ) {
48+ return d . showlegend ;
49+ } ) ;
4650 var allLegendItems = fullData . concat ( shapesWithLegend ) ;
4751
4852 var fullTrace = legendItem . trace ;
49- if ( fullTrace . _isShape ) {
53+ if ( fullTrace . _isShape ) {
5054 fullTrace = fullTrace . _fullInput ;
5155 }
5256
@@ -61,11 +65,11 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
6165 function insertDataUpdate ( traceIndex , value ) {
6266 var attrIndex = dataIndices . indexOf ( traceIndex ) ;
6367 var valueArray = dataUpdate . visible ;
64- if ( ! valueArray ) {
68+ if ( ! valueArray ) {
6569 valueArray = dataUpdate . visible = [ ] ;
6670 }
6771
68- if ( dataIndices . indexOf ( traceIndex ) === - 1 ) {
72+ if ( dataIndices . indexOf ( traceIndex ) === - 1 ) {
6973 dataIndices . push ( traceIndex ) ;
7074 attrIndex = dataIndices . length - 1 ;
7175 }
@@ -75,7 +79,7 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
7579 return attrIndex ;
7680 }
7781
78- var updatedShapes = ( fullLayout . shapes || [ ] ) . map ( function ( d ) {
82+ var updatedShapes = ( fullLayout . shapes || [ ] ) . map ( function ( d ) {
7983 return d . _input ;
8084 } ) ;
8185
@@ -87,19 +91,19 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
8791 }
8892
8993 function setVisibility ( fullTrace , visibility ) {
90- if ( legendItem . groupTitle && ! toggleGroup ) return ;
94+ if ( legendItem . groupTitle && ! toggleGroup ) return ;
9195
9296 var fullInput = fullTrace . _fullInput || fullTrace ;
9397 var isShape = fullInput . _isShape ;
9498 var index = fullInput . index ;
95- if ( index === undefined ) index = fullInput . _index ;
99+ if ( index === undefined ) index = fullInput . _index ;
96100
97101 // false -> false (not possible since will not be visible in legend)
98102 // true -> legendonly
99103 // legendonly -> true
100104 var nextVisibility = fullInput . visible === false ? false : visibility ;
101105
102- if ( isShape ) {
106+ if ( isShape ) {
103107 insertShapesUpdate ( index , nextVisibility ) ;
104108 } else {
105109 insertDataUpdate ( index , nextVisibility ) ;
@@ -111,37 +115,37 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
111115 var fullInput = fullTrace . _fullInput ;
112116 var isShape = fullInput && fullInput . _isShape ;
113117
114- if ( ! isShape && Registry . traceIs ( fullTrace , 'pie-like' ) ) {
118+ if ( ! isShape && Registry . traceIs ( fullTrace , 'pie-like' ) ) {
115119 var thisLabel = legendItem . label ;
116120 var thisLabelIndex = hiddenSlices . indexOf ( thisLabel ) ;
117121
118- if ( mode === 'toggle' ) {
119- if ( thisLabelIndex === - 1 ) hiddenSlices . push ( thisLabel ) ;
122+ if ( mode === 'toggle' ) {
123+ if ( thisLabelIndex === - 1 ) hiddenSlices . push ( thisLabel ) ;
120124 else hiddenSlices . splice ( thisLabelIndex , 1 ) ;
121- } else if ( mode === 'toggleothers' ) {
125+ } else if ( mode === 'toggleothers' ) {
122126 var changed = thisLabelIndex !== - 1 ;
123127 var unhideList = [ ] ;
124- for ( i = 0 ; i < gd . calcdata . length ; i ++ ) {
128+ for ( i = 0 ; i < gd . calcdata . length ; i ++ ) {
125129 var cdi = gd . calcdata [ i ] ;
126- for ( j = 0 ; j < cdi . length ; j ++ ) {
130+ for ( j = 0 ; j < cdi . length ; j ++ ) {
127131 var d = cdi [ j ] ;
128132 var dLabel = d . label ;
129133
130134 // ensure we toggle slices that are in this legend)
131- if ( thisLegend === cdi [ 0 ] . trace . legend ) {
132- if ( thisLabel !== dLabel ) {
133- if ( hiddenSlices . indexOf ( dLabel ) === - 1 ) changed = true ;
135+ if ( thisLegend === cdi [ 0 ] . trace . legend ) {
136+ if ( thisLabel !== dLabel ) {
137+ if ( hiddenSlices . indexOf ( dLabel ) === - 1 ) changed = true ;
134138 pushUnique ( hiddenSlices , dLabel ) ;
135139 unhideList . push ( dLabel ) ;
136140 }
137141 }
138142 }
139143 }
140144
141- if ( ! changed ) {
142- for ( var q = 0 ; q < unhideList . length ; q ++ ) {
145+ if ( ! changed ) {
146+ for ( var q = 0 ; q < unhideList . length ; q ++ ) {
143147 var pos = hiddenSlices . indexOf ( unhideList [ q ] ) ;
144- if ( pos !== - 1 ) {
148+ if ( pos !== - 1 ) {
145149 hiddenSlices . splice ( pos , 1 ) ;
146150 }
147151 }
@@ -153,20 +157,20 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
153157 var hasLegendgroup = legendgroup && legendgroup . length ;
154158 var traceIndicesInGroup = [ ] ;
155159 var tracei ;
156- if ( hasLegendgroup ) {
157- for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
160+ if ( hasLegendgroup ) {
161+ for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
158162 tracei = allLegendItems [ i ] ;
159- if ( ! tracei . visible ) continue ;
160- if ( tracei . legendgroup === legendgroup ) {
163+ if ( ! tracei . visible ) continue ;
164+ if ( tracei . legendgroup === legendgroup ) {
161165 traceIndicesInGroup . push ( i ) ;
162166 }
163167 }
164168 }
165169
166- if ( mode === 'toggle' ) {
170+ if ( mode === 'toggle' ) {
167171 var nextVisibility ;
168172
169- switch ( fullTrace . visible ) {
173+ switch ( fullTrace . visible ) {
170174 case true :
171175 nextVisibility = 'legendonly' ;
172176 break ;
@@ -178,11 +182,11 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
178182 break ;
179183 }
180184
181- if ( hasLegendgroup ) {
182- if ( toggleGroup ) {
183- for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
185+ if ( hasLegendgroup ) {
186+ if ( toggleGroup ) {
187+ for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
184188 var item = allLegendItems [ i ] ;
185- if ( item . visible !== false && item . legendgroup === legendgroup ) {
189+ if ( item . visible !== false && item . legendgroup === legendgroup ) {
186190 setVisibility ( item , nextVisibility ) ;
187191 }
188192 }
@@ -192,58 +196,63 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
192196 } else {
193197 setVisibility ( fullTrace , nextVisibility ) ;
194198 }
195- } else if ( mode === 'toggleothers' ) {
199+ } else if ( mode === 'toggleothers' ) {
196200 // Compute the clicked index. expandedIndex does what we want for expanded traces
197201 // but also culls hidden traces. That means we have some work to do.
198202 var isClicked , isInGroup , notInLegend , otherState , _item ;
199203 var isIsolated = true ;
200- for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
204+ for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
201205 _item = allLegendItems [ i ] ;
202206 isClicked = _item === fullTrace ;
203207 notInLegend = _item . showlegend !== true ;
204- if ( isClicked || notInLegend ) continue ;
208+ if ( isClicked || notInLegend ) continue ;
205209
206- isInGroup = ( hasLegendgroup && _item . legendgroup === legendgroup ) ;
210+ isInGroup = hasLegendgroup && _item . legendgroup === legendgroup ;
207211
208- if ( ! isInGroup && _item . legend === thisLegend && _item . visible === true && ! Registry . traceIs ( _item , 'notLegendIsolatable' ) ) {
212+ if (
213+ ! isInGroup &&
214+ _item . legend === thisLegend &&
215+ _item . visible === true &&
216+ ! Registry . traceIs ( _item , 'notLegendIsolatable' )
217+ ) {
209218 isIsolated = false ;
210219 break ;
211220 }
212221 }
213222
214- for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
223+ for ( i = 0 ; i < allLegendItems . length ; i ++ ) {
215224 _item = allLegendItems [ i ] ;
216225
217226 // False is sticky; we don't change it. Also ensure we don't change states of itmes in other legend
218- if ( _item . visible === false || _item . legend !== thisLegend ) continue ;
227+ if ( _item . visible === false || _item . legend !== thisLegend ) continue ;
219228
220- if ( Registry . traceIs ( _item , 'notLegendIsolatable' ) ) {
229+ if ( Registry . traceIs ( _item , 'notLegendIsolatable' ) ) {
221230 continue ;
222231 }
223232
224- switch ( fullTrace . visible ) {
233+ switch ( fullTrace . visible ) {
225234 case 'legendonly' :
226235 setVisibility ( _item , true ) ;
227236 break ;
228237 case true :
229238 otherState = isIsolated ? true : 'legendonly' ;
230239 isClicked = _item === fullTrace ;
231240 // N.B. consider traces that have a set legendgroup as toggleable
232- notInLegend = ( _item . showlegend !== true && ! _item . legendgroup ) ;
241+ notInLegend = _item . showlegend !== true && ! _item . legendgroup ;
233242 isInGroup = isClicked || ( hasLegendgroup && _item . legendgroup === legendgroup ) ;
234- setVisibility ( _item , ( isInGroup || notInLegend ) ? true : otherState ) ;
243+ setVisibility ( _item , isInGroup || notInLegend ? true : otherState ) ;
235244 break ;
236245 }
237246 }
238247 }
239248
240- for ( i = 0 ; i < carrs . length ; i ++ ) {
249+ for ( i = 0 ; i < carrs . length ; i ++ ) {
241250 kcont = carrs [ i ] ;
242- if ( ! kcont ) continue ;
251+ if ( ! kcont ) continue ;
243252 var update = kcont . constructUpdate ( ) ;
244253
245254 var updateKeys = Object . keys ( update ) ;
246- for ( j = 0 ; j < updateKeys . length ; j ++ ) {
255+ for ( j = 0 ; j < updateKeys . length ; j ++ ) {
247256 key = updateKeys [ j ] ;
248257 val = dataUpdate [ key ] = dataUpdate [ key ] || [ ] ;
249258 val [ carrIdx [ i ] ] = update [ key ] ;
@@ -255,18 +264,18 @@ exports.handleItemClick = function handleItemClick(g, gd, legendObj, mode) {
255264 // as updates and not accidentally reset to the default value. This fills
256265 // out sparse arrays with the required number of undefined values:
257266 keys = Object . keys ( dataUpdate ) ;
258- for ( i = 0 ; i < keys . length ; i ++ ) {
267+ for ( i = 0 ; i < keys . length ; i ++ ) {
259268 key = keys [ i ] ;
260- for ( j = 0 ; j < dataIndices . length ; j ++ ) {
269+ for ( j = 0 ; j < dataIndices . length ; j ++ ) {
261270 // Use hasOwnProperty to protect against falsy values:
262- if ( ! dataUpdate [ key ] . hasOwnProperty ( j ) ) {
271+ if ( ! dataUpdate [ key ] . hasOwnProperty ( j ) ) {
263272 dataUpdate [ key ] [ j ] = undefined ;
264273 }
265274 }
266275 }
267276
268- if ( shapesUpdated ) {
269- Registry . call ( '_guiUpdate' , gd , dataUpdate , { shapes : updatedShapes } , dataIndices ) ;
277+ if ( shapesUpdated ) {
278+ Registry . call ( '_guiUpdate' , gd , dataUpdate , { shapes : updatedShapes } , dataIndices ) ;
270279 } else {
271280 Registry . call ( '_guiRestyle' , gd , dataUpdate , dataIndices ) ;
272281 }
@@ -286,7 +295,9 @@ exports.handleTitleClick = function handleTitleClick(gd, legendObj, mode) {
286295 const fullLayout = gd . _fullLayout ;
287296 const fullData = gd . _fullData ;
288297 const legendId = helpers . getId ( legendObj ) ;
289- const shapesWithLegend = ( fullLayout . shapes || [ ] ) . filter ( function ( d ) { return d . showlegend ; } ) ;
298+ const shapesWithLegend = ( fullLayout . shapes || [ ] ) . filter ( function ( d ) {
299+ return d . showlegend ;
300+ } ) ;
290301 const allLegendItems = fullData . concat ( shapesWithLegend ) ;
291302
292303 function isInLegend ( item ) {
@@ -296,17 +307,17 @@ exports.handleTitleClick = function handleTitleClick(gd, legendObj, mode) {
296307 var toggleThisLegend ;
297308 var toggleOtherLegends ;
298309
299- if ( mode === 'toggle' ) {
310+ if ( mode === 'toggle' ) {
300311 // If any item is visible in this legend, hide all. If all are hidden, show all
301- const anyVisibleHere = allLegendItems . some ( function ( item ) {
312+ const anyVisibleHere = allLegendItems . some ( function ( item ) {
302313 return isInLegend ( item ) && item . visible === true ;
303314 } ) ;
304315
305316 toggleThisLegend = ! anyVisibleHere ;
306317 toggleOtherLegends = false ;
307318 } else {
308319 // isolate this legend or set all legends to visible
309- const anyVisibleElsewhere = allLegendItems . some ( function ( item ) {
320+ const anyVisibleElsewhere = allLegendItems . some ( function ( item ) {
310321 return ! isInLegend ( item ) && item . visible === true && item . showlegend !== false ;
311322 } ) ;
312323
@@ -316,26 +327,28 @@ exports.handleTitleClick = function handleTitleClick(gd, legendObj, mode) {
316327
317328 const dataUpdate = { visible : [ ] } ;
318329 const dataIndices = [ ] ;
319- const updatedShapes = ( fullLayout . shapes || [ ] ) . map ( function ( d ) { return d . _input ; } ) ;
330+ const updatedShapes = ( fullLayout . shapes || [ ] ) . map ( function ( d ) {
331+ return d . _input ;
332+ } ) ;
320333 var shapesUpdated = false ;
321334
322- for ( var i = 0 ; i < allLegendItems . length ; i ++ ) {
335+ for ( var i = 0 ; i < allLegendItems . length ; i ++ ) {
323336 const item = allLegendItems [ i ] ;
324337 const inThisLegend = isInLegend ( item ) ;
325338
326339 // If item is not in this legend, skip if in toggle mode
327340 // or if item is not displayed in the legend
328- if ( ! inThisLegend ) {
329- const notDisplayed = ( item . showlegend !== true && ! item . legendgroup ) ;
330- if ( mode === 'toggle' || notDisplayed ) continue ;
341+ if ( ! inThisLegend ) {
342+ const notDisplayed = item . showlegend !== true && ! item . legendgroup ;
343+ if ( mode === 'toggle' || notDisplayed ) continue ;
331344 }
332345
333346 const shouldShow = inThisLegend ? toggleThisLegend : toggleOtherLegends ;
334347 const newVis = shouldShow ? true : 'legendonly' ;
335348
336349 // Only update if visibility would actually change
337- if ( ( item . visible !== false ) && ( item . visible !== newVis ) ) {
338- if ( item . _isShape ) {
350+ if ( item . visible !== false && item . visible !== newVis ) {
351+ if ( item . _isShape ) {
339352 updatedShapes [ item . _index ] . visible = newVis ;
340353 shapesUpdated = true ;
341354 } else {
@@ -345,9 +358,9 @@ exports.handleTitleClick = function handleTitleClick(gd, legendObj, mode) {
345358 }
346359 }
347360
348- if ( shapesUpdated ) {
349- Registry . call ( '_guiUpdate' , gd , dataUpdate , { shapes : updatedShapes } , dataIndices ) ;
350- } else if ( dataIndices . length ) {
361+ if ( shapesUpdated ) {
362+ Registry . call ( '_guiUpdate' , gd , dataUpdate , { shapes : updatedShapes } , dataIndices ) ;
363+ } else if ( dataIndices . length ) {
351364 Registry . call ( '_guiRestyle' , gd , dataUpdate , dataIndices ) ;
352365 }
353366} ;
0 commit comments