@@ -1001,9 +1001,13 @@ describe('legend interaction', function() {
1001
1001
} ;
1002
1002
}
1003
1003
1004
+ function extractVisibilities ( data ) {
1005
+ return data . map ( function ( trace ) { return trace . visible ; } ) ;
1006
+ }
1007
+
1004
1008
function assertVisible ( expectation ) {
1005
1009
return function ( ) {
1006
- var actual = gd . _fullData . map ( function ( trace ) { return trace . visible ; } ) ;
1010
+ var actual = extractVisibilities ( gd . _fullData ) ;
1007
1011
expect ( actual ) . toEqual ( expectation ) ;
1008
1012
} ;
1009
1013
}
@@ -1166,5 +1170,101 @@ describe('legend interaction', function() {
1166
1170
. catch ( failTest ) . then ( done ) ;
1167
1171
} ) ;
1168
1172
} ) ;
1173
+
1174
+ describe ( 'custom legend click/doubleclick handlers' , function ( ) {
1175
+ var fig , to ;
1176
+
1177
+ beforeEach ( function ( ) {
1178
+ fig = Lib . extendDeep ( { } , require ( '@mocks/0.json' ) ) ;
1179
+ } ) ;
1180
+
1181
+ afterEach ( function ( ) {
1182
+ clearTimeout ( to ) ;
1183
+ } ) ;
1184
+
1185
+ function setupFail ( ) {
1186
+ to = setTimeout ( function ( ) {
1187
+ fail ( 'did not trigger plotly_legendclick' ) ;
1188
+ } , 2 * DBLCLICKDELAY ) ;
1189
+ }
1190
+
1191
+ it ( 'should call custom click handler before default handler' , function ( done ) {
1192
+ Plotly . newPlot ( gd , fig ) . then ( function ( ) {
1193
+ var gotCalled = false ;
1194
+
1195
+ gd . on ( 'plotly_legendclick' , function ( d ) {
1196
+ gotCalled = true ;
1197
+ expect ( extractVisibilities ( d . fullData ) ) . toEqual ( [ true , true , true ] ) ;
1198
+ expect ( extractVisibilities ( gd . _fullData ) ) . toEqual ( [ true , true , true ] ) ;
1199
+ } ) ;
1200
+ gd . on ( 'plotly_restyle' , function ( ) {
1201
+ expect ( extractVisibilities ( gd . _fullData ) ) . toEqual ( [ true , 'legendonly' , true ] ) ;
1202
+ if ( gotCalled ) done ( ) ;
1203
+ } ) ;
1204
+ setupFail ( ) ;
1205
+ } )
1206
+ . then ( click ( 1 , 1 ) )
1207
+ . catch ( failTest ) ;
1208
+ } ) ;
1209
+
1210
+ it ( 'should call custom doubleclick handler before default handler' , function ( done ) {
1211
+ Plotly . newPlot ( gd , fig ) . then ( function ( ) {
1212
+ var gotCalled = false ;
1213
+
1214
+ gd . on ( 'plotly_legenddoubleclick' , function ( d ) {
1215
+ gotCalled = true ;
1216
+ expect ( extractVisibilities ( d . fullData ) ) . toEqual ( [ true , true , true ] ) ;
1217
+ expect ( extractVisibilities ( gd . _fullData ) ) . toEqual ( [ true , true , true ] ) ;
1218
+ } ) ;
1219
+ gd . on ( 'plotly_restyle' , function ( ) {
1220
+ expect ( extractVisibilities ( gd . _fullData ) ) . toEqual ( [ 'legendonly' , true , 'legendonly' ] ) ;
1221
+ if ( gotCalled ) done ( ) ;
1222
+ } ) ;
1223
+ setupFail ( ) ;
1224
+ } )
1225
+ . then ( click ( 1 , 2 ) )
1226
+ . catch ( failTest ) ;
1227
+ } ) ;
1228
+
1229
+ it ( 'should not call default click handler if custom handler return *false*' , function ( done ) {
1230
+ Plotly . newPlot ( gd , fig ) . then ( function ( ) {
1231
+ gd . on ( 'plotly_legendclick' , function ( d ) {
1232
+ Plotly . relayout ( gd , 'title' , 'just clicked on trace #' + d . curveNumber ) ;
1233
+ return false ;
1234
+ } ) ;
1235
+ gd . on ( 'plotly_relayout' , function ( d ) {
1236
+ expect ( typeof d ) . toBe ( 'object' ) ;
1237
+ expect ( d . title ) . toBe ( 'just clicked on trace #2' ) ;
1238
+ done ( ) ;
1239
+ } ) ;
1240
+ gd . on ( 'plotly_restyle' , function ( ) {
1241
+ fail ( 'should not have triggered plotly_restyle' ) ;
1242
+ } ) ;
1243
+ setupFail ( ) ;
1244
+ } )
1245
+ . then ( click ( 2 , 1 ) )
1246
+ . catch ( failTest ) ;
1247
+ } ) ;
1248
+
1249
+ it ( 'should not call default doubleclick handle if custom handler return *false*' , function ( done ) {
1250
+ Plotly . newPlot ( gd , fig ) . then ( function ( ) {
1251
+ gd . on ( 'plotly_legenddoubleclick' , function ( d ) {
1252
+ Plotly . relayout ( gd , 'title' , 'just double clicked on trace #' + d . curveNumber ) ;
1253
+ return false ;
1254
+ } ) ;
1255
+ gd . on ( 'plotly_relayout' , function ( d ) {
1256
+ expect ( typeof d ) . toBe ( 'object' ) ;
1257
+ expect ( d . title ) . toBe ( 'just double clicked on trace #0' ) ;
1258
+ done ( ) ;
1259
+ } ) ;
1260
+ gd . on ( 'plotly_restyle' , function ( ) {
1261
+ fail ( 'should not have triggered plotly_restyle' ) ;
1262
+ } ) ;
1263
+ setupFail ( ) ;
1264
+ } )
1265
+ . then ( click ( 0 , 2 ) )
1266
+ . catch ( failTest ) ;
1267
+ } ) ;
1268
+ } ) ;
1169
1269
} ) ;
1170
1270
} ) ;
0 commit comments