Skip to content

Commit 468c358

Browse files
committed
Accordion: Moved handling for programmatically collapsing the accordion out of the event handler. Modified event handler to not change the active option until after it determines that the event is valid.
1 parent 3c11cd3 commit 468c358

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

tests/unit/accordion/accordion_events.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test("accordionchange event, open closed and close again", function() {
2424
equals( ui.newHeader.size(), 0 );
2525
equals( ui.newContent.size(), 0 );
2626
})
27-
.accordion("option", "active", 0);
27+
.accordion("option", "active", false);
2828
});
2929

3030
})(jQuery);

ui/jquery.ui.accordion.js

+35-42
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,37 @@ $.widget( "ui.accordion", {
277277

278278
_activate: function( index ) {
279279
var active = this._findActive( index )[ 0 ];
280-
if ( !active ) {
281-
if ( !this.options.collapsible ) {
282-
return;
280+
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 } );
283285
}
284-
index = false;
286+
return;
285287
}
286-
this.options.active = index;
287-
this._eventHandler( { target: active, currentTarget: active } );
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 );
288311
},
289312

290313
// TODO: add tests/docs for negative values in 2.0 (#6854)
@@ -293,51 +316,23 @@ $.widget( "ui.accordion", {
293316
},
294317

295318
_eventHandler: function( event ) {
296-
var options = this.options;
319+
var options = this.options,
320+
clicked = $( event.currentTarget ),
321+
clickedIsActive = clicked[0] === this.active[0];
322+
297323
if ( options.disabled ) {
298324
return;
299325
}
300326

301-
// called only when using activate(false) to close all parts programmatically
302-
if ( !event.target ) {
303-
if ( !options.collapsible ) {
304-
return;
305-
}
306-
this.active
307-
.removeClass( "ui-state-active ui-corner-top" )
308-
.addClass( "ui-state-default ui-corner-all" )
309-
.children( ".ui-icon" )
310-
.removeClass( options.icons.activeHeader )
311-
.addClass( options.icons.header );
312-
this.active.next().addClass( "ui-accordion-content-active" );
313-
var toHide = this.active.next(),
314-
data = {
315-
options: options,
316-
newHeader: $( [] ),
317-
oldHeader: options.active,
318-
newContent: $( [] ),
319-
oldContent: toHide
320-
},
321-
toShow = ( this.active = $( [] ) );
322-
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 ) ) {
323329
return;
324330
}
325331

326-
// get the click target
327-
var clicked = $( event.currentTarget ),
328-
clickedIsActive = clicked[0] === this.active[0];
329-
330-
// TODO the option is changed, is that correct?
331-
// TODO if it is correct, shouldn't that happen after determining that the click is valid?
332332
options.active = options.collapsible && clickedIsActive ?
333333
false :
334334
this.headers.index( clicked );
335335

336-
// if animations are still active, or the active header is the target, ignore click
337-
if ( this.running || ( !options.collapsible && clickedIsActive ) ) {
338-
return;
339-
}
340-
341336
// find elements to show and hide
342337
var active = this.active,
343338
toShow = clicked.next(),
@@ -374,8 +369,6 @@ $.widget( "ui.accordion", {
374369
.next()
375370
.addClass( "ui-accordion-content-active" );
376371
}
377-
378-
return;
379372
},
380373

381374
_toggle: function( toShow, toHide, data, clickedIsActive, down ) {

0 commit comments

Comments
 (0)