Skip to content

Commit ed57047

Browse files
committed
Accordion: First pass at cleaning up activation/event handling code.
1 parent ee88a56 commit ed57047

File tree

1 file changed

+34
-70
lines changed

1 file changed

+34
-70
lines changed

ui/jquery.ui.accordion.js

Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,13 @@ $.widget( "ui.accordion", {
262262
},
263263

264264
_activate: function( index ) {
265-
var active = this._findActive( index )[ 0 ];
265+
var active = this._findActive( index )[ 0 ],
266+
eventData = {
267+
oldHeader: this.active,
268+
oldContent: this.active.next(),
269+
newHeader: $(),
270+
newContent: $()
271+
};
266272

267273
// we found a header to activate, just delegate to the event handler
268274
if ( active ) {
@@ -282,34 +288,20 @@ $.widget( "ui.accordion", {
282288
}
283289

284290
// allow the activation to be canceled
285-
var eventData = {
286-
oldHeader: this.active,
287-
oldContent: this.active.next(),
288-
newHeader: $(),
289-
newContent: $()
290-
};
291291
if ( this._trigger( "beforeActivate", null, eventData ) === false ) {
292292
return;
293293
}
294294

295-
this.options.active = false;
296295
this.active
297296
.removeClass( "ui-state-active ui-corner-top" )
298297
.addClass( "ui-state-default ui-corner-all" )
299298
.children( ".ui-accordion-header-icon" )
300299
.removeClass( this.options.icons.activeHeader )
301300
.addClass( this.options.icons.header );
302301
this.active.next().addClass( "ui-accordion-content-active" );
303-
var toHide = this.active.next(),
304-
data = {
305-
options: this.options,
306-
newHeader: $(),
307-
oldHeader: this.active,
308-
newContent: $(),
309-
oldContent: toHide
310-
},
311-
toShow = ( this.active = $() );
312-
this._toggle( toShow, toHide, data );
302+
this.options.active = false;
303+
this.active = $();
304+
this._toggle( eventData );
313305
},
314306

315307
_findActive: function( selector ) {
@@ -322,47 +314,33 @@ $.widget( "ui.accordion", {
322314
clicked = $( event.currentTarget ),
323315
clickedIsActive = clicked[ 0 ] === active[ 0 ],
324316
collapsing = clickedIsActive && options.collapsible,
325-
toShow = clicked.next(),
317+
toShow = collapsing ? $() : clicked.next(),
326318
toHide = active.next(),
327319
eventData = {
328320
oldHeader: active,
329321
oldContent: toHide,
330322
newHeader: collapsing ? $() : clicked,
331-
newContent: collapsing ? $() : toShow
323+
newContent: toShow
332324
};
333325

334326
event.preventDefault();
335327

336-
if ( options.disabled ) {
337-
return;
338-
}
339-
340-
// if animations are still active, or the active header is the target, ignore click
341-
if ( this.running || ( !options.collapsible && clickedIsActive ) ) {
342-
return;
343-
}
344-
345-
// allow the activation to be canceled
346-
if ( this._trigger( "beforeActivate", null, eventData ) === false ) {
328+
if ( options.disabled ||
329+
// can't switch during an animation
330+
this.running ||
331+
// click on active header, but not collapsible
332+
( clickedIsActive && !options.collapsible ) ||
333+
// allow canceling activation
334+
( this._trigger( "beforeActivate", null, eventData ) === false ) ) {
347335
return;
348336
}
349337

350338
options.active = collapsing ? false : this.headers.index( clicked );
351339

352-
// find elements to show and hide
353-
var data = {
354-
options: options,
355-
newHeader: collapsing ? $() : clicked,
356-
oldHeader: active,
357-
newContent: collapsing ? $() : toShow,
358-
oldContent: toHide
359-
},
360-
down = this.headers.index( active[0] ) > this.headers.index( clicked[0] );
361-
362340
// when the call to ._toggle() comes after the class changes
363341
// it causes a very odd bug in IE 8 (see #6720)
364342
this.active = clickedIsActive ? $() : clicked;
365-
this._toggle( toShow, toHide, data, clickedIsActive, down );
343+
this._toggle( eventData );
366344

367345
// switch classes
368346
active
@@ -384,9 +362,12 @@ $.widget( "ui.accordion", {
384362
}
385363
},
386364

387-
_toggle: function( toShow, toHide, data, clickedIsActive, down ) {
365+
_toggle: function( data ) {
388366
var self = this,
389-
options = self.options;
367+
options = self.options,
368+
toShow = data.newContent,
369+
toHide = data.oldContent,
370+
down = toShow.length && ( !toHide.length || ( toShow.index() < toHide.index() ) );
390371

391372
self.toShow = toShow;
392373
self.toHide = toHide;
@@ -403,25 +384,13 @@ $.widget( "ui.accordion", {
403384
self.running = toHide.size() === 0 ? toShow.size() : toHide.size();
404385

405386
if ( options.animated ) {
406-
var animOptions = {};
407-
408-
if ( options.collapsible && clickedIsActive ) {
409-
animOptions = {
410-
toShow: $(),
411-
toHide: toHide,
412-
complete: complete,
413-
down: down,
414-
autoHeight: options.heightStyle !== "content"
415-
};
416-
} else {
417-
animOptions = {
418-
toShow: toShow,
419-
toHide: toHide,
420-
complete: complete,
421-
down: down,
422-
autoHeight: options.heightStyle !== "content"
423-
};
424-
}
387+
var animOptions = {
388+
toShow: toShow,
389+
toHide: toHide,
390+
complete: complete,
391+
down: down,
392+
autoHeight: options.heightStyle !== "content"
393+
};
425394

426395
if ( !options.proxied ) {
427396
options.proxied = options.animated;
@@ -457,13 +426,8 @@ $.widget( "ui.accordion", {
457426

458427
animations[ easing ]( animOptions );
459428
} else {
460-
if ( options.collapsible && clickedIsActive ) {
461-
toShow.toggle();
462-
} else {
463-
toHide.hide();
464-
toShow.show();
465-
}
466-
429+
toHide.hide();
430+
toShow.show();
467431
complete( true );
468432
}
469433

0 commit comments

Comments
 (0)