@@ -73,7 +73,7 @@ $.widget( "ui.accordion", {
73
73
. addClass ( "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" ) ;
74
74
self . headers . find ( ":first-child" ) . addClass ( "ui-accordion-heading" ) ;
75
75
76
- self . active = self . _findActive ( self . active || options . active )
76
+ self . active = self . _findActive ( options . active )
77
77
. addClass ( "ui-state-default ui-state-active" )
78
78
. toggleClass ( "ui-corner-all" )
79
79
. toggleClass ( "ui-corner-top" ) ;
@@ -120,7 +120,7 @@ $.widget( "ui.accordion", {
120
120
121
121
if ( options . event ) {
122
122
self . headers . bind ( options . event . split ( " " ) . join ( ".accordion " ) + ".accordion" , function ( event ) {
123
- self . _clickHandler . call ( self , event , this ) ;
123
+ self . _eventHandler ( event ) ;
124
124
event . preventDefault ( ) ;
125
125
} ) ;
126
126
}
@@ -173,11 +173,14 @@ $.widget( "ui.accordion", {
173
173
} ,
174
174
175
175
_setOption : function ( key , value ) {
176
- $ . Widget . prototype . _setOption . apply ( this , arguments ) ;
177
-
178
176
if ( key == "active" ) {
179
- this . activate ( value ) ;
177
+ // _activate() will handle invalid values and update this.options
178
+ this . _activate ( value ) ;
179
+ return ;
180
180
}
181
+
182
+ $ . Widget . prototype . _setOption . apply ( this , arguments ) ;
183
+
181
184
if ( key == "icons" ) {
182
185
this . _destroyIcons ( ) ;
183
186
if ( value ) {
@@ -213,7 +216,7 @@ $.widget( "ui.accordion", {
213
216
break ;
214
217
case keyCode . SPACE :
215
218
case keyCode . ENTER :
216
- this . _clickHandler ( { target : event . target } , event . target ) ;
219
+ this . _eventHandler ( event ) ;
217
220
event . preventDefault ( ) ;
218
221
}
219
222
@@ -272,73 +275,64 @@ $.widget( "ui.accordion", {
272
275
return this ;
273
276
} ,
274
277
275
- activate : function ( index ) {
276
- // TODO this gets called on init, changing the option without an explicit call for that
277
- this . options . active = index ;
278
- // call clickHandler with custom event
278
+ _activate : function ( index ) {
279
279
var active = this . _findActive ( index ) [ 0 ] ;
280
- this . _clickHandler ( { target : active } , active ) ;
281
280
282
- return this ;
281
+ // we found a header to activate, just delegate to the event handler
282
+ if ( active ) {
283
+ if ( active !== this . active [ 0 ] ) {
284
+ this . _eventHandler ( { target : active , currentTarget : active } ) ;
285
+ }
286
+ return ;
287
+ }
288
+
289
+ // no header to activate, check if we can collapse
290
+ if ( ! this . options . collapsible ) {
291
+ return ;
292
+ }
293
+
294
+ this . active
295
+ . removeClass ( "ui-state-active ui-corner-top" )
296
+ . addClass ( "ui-state-default ui-corner-all" )
297
+ . children ( ".ui-icon" )
298
+ . removeClass ( this . options . icons . activeHeader )
299
+ . addClass ( this . options . icons . header ) ;
300
+ this . active . next ( ) . addClass ( "ui-accordion-content-active" ) ;
301
+ var toHide = this . active . next ( ) ,
302
+ data = {
303
+ options : this . options ,
304
+ newHeader : $ ( [ ] ) ,
305
+ oldHeader : this . active ,
306
+ newContent : $ ( [ ] ) ,
307
+ oldContent : toHide
308
+ } ,
309
+ toShow = ( this . active = $ ( [ ] ) ) ;
310
+ this . _toggle ( toShow , toHide , data ) ;
283
311
} ,
284
312
313
+ // TODO: add tests/docs for negative values in 2.0 (#6854)
285
314
_findActive : function ( selector ) {
286
- return selector
287
- ? typeof selector === "number"
288
- ? this . headers . filter ( ":eq(" + selector + ")" )
289
- : this . headers . not ( this . headers . not ( selector ) )
290
- : selector === false
291
- ? $ ( [ ] )
292
- : this . headers . filter ( ":eq(0)" ) ;
315
+ return typeof selector === "number" ? this . headers . eq ( selector ) : $ ( [ ] ) ;
293
316
} ,
294
317
295
- // TODO isn't event.target enough? why the separate target argument?
296
- _clickHandler : function ( event , target ) {
297
- var options = this . options ;
318
+ _eventHandler : function ( event ) {
319
+ var options = this . options ,
320
+ clicked = $ ( event . currentTarget ) ,
321
+ clickedIsActive = clicked [ 0 ] === this . active [ 0 ] ;
322
+
298
323
if ( options . disabled ) {
299
324
return ;
300
325
}
301
326
302
- // called only when using activate(false) to close all parts programmatically
303
- if ( ! event . target ) {
304
- if ( ! options . collapsible ) {
305
- return ;
306
- }
307
- this . active
308
- . removeClass ( "ui-state-active ui-corner-top" )
309
- . addClass ( "ui-state-default ui-corner-all" )
310
- . children ( ".ui-icon" )
311
- . removeClass ( options . icons . activeHeader )
312
- . addClass ( options . icons . header ) ;
313
- this . active . next ( ) . addClass ( "ui-accordion-content-active" ) ;
314
- var toHide = this . active . next ( ) ,
315
- data = {
316
- options : options ,
317
- newHeader : $ ( [ ] ) ,
318
- oldHeader : options . active ,
319
- newContent : $ ( [ ] ) ,
320
- oldContent : toHide
321
- } ,
322
- toShow = ( this . active = $ ( [ ] ) ) ;
323
- this . _toggle ( toShow , toHide , data ) ;
327
+ // if animations are still active, or the active header is the target, ignore click
328
+ if ( this . running || ( ! options . collapsible && clickedIsActive ) ) {
324
329
return ;
325
330
}
326
331
327
- // get the click target
328
- var clicked = $ ( event . currentTarget || target ) ,
329
- clickedIsActive = clicked [ 0 ] === this . active [ 0 ] ;
330
-
331
- // TODO the option is changed, is that correct?
332
- // TODO if it is correct, shouldn't that happen after determining that the click is valid?
333
332
options . active = options . collapsible && clickedIsActive ?
334
333
false :
335
334
this . headers . index ( clicked ) ;
336
335
337
- // if animations are still active, or the active header is the target, ignore click
338
- if ( this . running || ( ! options . collapsible && clickedIsActive ) ) {
339
- return ;
340
- }
341
-
342
336
// find elements to show and hide
343
337
var active = this . active ,
344
338
toShow = clicked . next ( ) ,
@@ -375,8 +369,6 @@ $.widget( "ui.accordion", {
375
369
. next ( )
376
370
. addClass ( "ui-accordion-content-active" ) ;
377
371
}
378
-
379
- return ;
380
372
} ,
381
373
382
374
_toggle : function ( toShow , toHide , data , clickedIsActive , down ) {
@@ -685,4 +677,23 @@ $.extend( $.ui.accordion, {
685
677
} ;
686
678
} ( jQuery , jQuery . ui . accordion . prototype ) ) ;
687
679
680
+ // expanded active option, activate method
681
+ ( function ( $ , prototype ) {
682
+ prototype . activate = prototype . _activate ;
683
+
684
+ var _findActive = prototype . _findActive ;
685
+ prototype . _findActive = function ( index ) {
686
+ if ( index === - 1 ) {
687
+ index = false ;
688
+ }
689
+ if ( index && typeof index !== "number" ) {
690
+ index = this . headers . index ( this . headers . filter ( index ) ) ;
691
+ if ( index === - 1 ) {
692
+ index = false ;
693
+ }
694
+ }
695
+ return _findActive . call ( this , index ) ;
696
+ } ;
697
+ } ( jQuery , jQuery . ui . accordion . prototype ) ) ;
698
+
688
699
} ) ( jQuery ) ;
0 commit comments